<commit_before>/**
 * Clever programming language
 * Copyright (c) Clever Team
 *
 * This file is distributed under the MIT license. See LICENSE for details.
 */

#include <fstream>
#include "core/value.h"
#include "core/clever.h"
#include "modules/std/file/cfile.h"
#include "types/type.h"
#include "modules/std/core/function.h"

// @TODO(muriloadriano): make this available and use in all Clever API
#define CLEVER_CAST(Type, obj) (static_cast<Type*>(obj))

using ::std::fstream;

namespace clever { namespace packages { namespace std {

namespace detail {

// Given a mode string returns the according ios flag
::std::ios::openmode convert_open_mode(const CString& mode)
{
	// @TODO(muriloadriano): support binary modes.
	if (mode == "r") {
		return ::std::ios_base::in;
	} else if (mode == "w") {
		return ::std::ios_base::out | ::std::ios_base::trunc;
	} else if (mode == "a") {
		return ::std::ios_base::out | ::std::ios_base::app;
	} else if (mode == "r+") {
		return ::std::ios_base::in | ::std::ios_base::out;
	} else if (mode == "w+") {
		return ::std::ios_base::in | ::std::ios_base::out |
				::std::ios_base::trunc;
	} else if (mode == "a+") {
		return ::std::ios_base::in | ::std::ios_base::out |
				::std::ios_base::app;
	} else {
		// @TODO(muriloadriano): return some kind of error here.
		return ::std::ios_base::in;
	}
}

}

void CFile::dump(const void* data) const
{
	dump(data, ::std::cout);
}

void CFile::dump(const void* data, ::std::ostream& out) const
{
	out << "File class (" << data << ")";
}

// File.new(string fileName, string openMode)
void* CFile::allocData(CLEVER_TYPE_CTOR_ARGS) const
{
	// @TODO(muriloadriano): allow object construction with a single argument
	// and check the argument's type.
	return new fstream(args->at(0)->getStr()->c_str(),
		static_cast< ::std::ios_base::openmode>(args->at(1)->getInt()));
}

void CFile::deallocData(CLEVER_TYPE_DTOR_ARGS)
{
	delete CLEVER_CAST(fstream*, data);
}

CLEVER_METHOD(CFile::ctor)
{
	if (!clever_check_args("ss")) {
		return;
	}

	result->setObj(this, allocData(&args));
}

// string File.read()
// Return the next token from the current stream
CLEVER_METHOD(CFile::read)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	CString* token = new CString;
	*file >> *token;

	result->setStr(token);
}

// string File.readLine()
// Return the contents from the current positon until the next line
CLEVER_METHOD(CFile::readLine)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	CString* token = new CString;
	::std::getline(*file, *token);

	result->setStr(token);
}

// void File.write(String)
// Return the contents from the current positon until the next line
// TODO(muriloadriano): allow use other primitive types as argument
CLEVER_METHOD(CFile::write)
{
	if (!clever_check_args("s")) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	*file << *(args[0]->getStr());
}

// void File.open(string fileName, Int openMode)
CLEVER_METHOD(CFile::open)
{
	if (!clever_check_args("si")) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	file->open(args[0]->getStr()->c_str(),
		static_cast< ::std::ios_base::openmode>(args[1]->getInt()));
}

// void File.close()
CLEVER_METHOD(CFile::close)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());
	file->close();
}

// bool File.isOpen()
CLEVER_METHOD(CFile::isOpen)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());
	result->setBool(file->is_open());
}

CLEVER_TYPE_INIT(CFile::init)
{
	Function* ctor = new Function("File", (MethodPtr)&CFile::ctor);

	setConstructor(ctor);
	addMethod(ctor);
	addMethod(new Function("read",		(MethodPtr)&CFile::read));
	addMethod(new Function("readLine",	(MethodPtr)&CFile::readLine));
	addMethod(new Function("write",		(MethodPtr)&CFile::write));
	addMethod(new Function("open",		(MethodPtr)&CFile::open));
	addMethod(new Function("close",		(MethodPtr)&CFile::close));
	addMethod(new Function("isOpen",	(MethodPtr)&CFile::isOpen));

	addProperty(CSTRING("IN"), new Value(long(::std::ios_base::in), true));
	addProperty(CSTRING("OUT"), new Value(long(::std::ios_base::out), true));
	addProperty(CSTRING("TRUNC"),
		new Value(long(::std::ios_base::trunc), true));
	addProperty(CSTRING("APP"), new Value(long(::std::ios_base::app), true));
	addProperty(CSTRING("ATE"), new Value(long(::std::ios_base::ate), true));
	addProperty(CSTRING("BIN"), new Value(long(::std::ios_base::binary), true));
}

}}} // clever::packages::std
<commit_msg>Changed File ctor to use flags<commit_after>/**
 * Clever programming language
 * Copyright (c) Clever Team
 *
 * This file is distributed under the MIT license. See LICENSE for details.
 */

#include <fstream>
#include "core/value.h"
#include "core/clever.h"
#include "modules/std/file/cfile.h"
#include "types/type.h"
#include "modules/std/core/function.h"

// @TODO(muriloadriano): make this available and use in all Clever API
#define CLEVER_CAST(Type, obj) (static_cast<Type*>(obj))

using ::std::fstream;

namespace clever { namespace packages { namespace std {

namespace detail {

// Given a mode string returns the according ios flag
::std::ios::openmode convert_open_mode(const CString& mode)
{
	// @TODO(muriloadriano): support binary modes.
	if (mode == "r") {
		return ::std::ios_base::in;
	} else if (mode == "w") {
		return ::std::ios_base::out | ::std::ios_base::trunc;
	} else if (mode == "a") {
		return ::std::ios_base::out | ::std::ios_base::app;
	} else if (mode == "r+") {
		return ::std::ios_base::in | ::std::ios_base::out;
	} else if (mode == "w+") {
		return ::std::ios_base::in | ::std::ios_base::out |
				::std::ios_base::trunc;
	} else if (mode == "a+") {
		return ::std::ios_base::in | ::std::ios_base::out |
				::std::ios_base::app;
	} else {
		// @TODO(muriloadriano): return some kind of error here.
		return ::std::ios_base::in;
	}
}

}

void CFile::dump(const void* data) const
{
	dump(data, ::std::cout);
}

void CFile::dump(const void* data, ::std::ostream& out) const
{
	out << "File class (" << data << ")";
}

// File.new(string fileName, string openMode)
void* CFile::allocData(CLEVER_TYPE_CTOR_ARGS) const
{
	// @TODO(muriloadriano): allow object construction with a single argument
	// and check the argument's type.
	return new fstream(args->at(0)->getStr()->c_str(),
		static_cast< ::std::ios_base::openmode>(args->at(1)->getInt()));
}

void CFile::deallocData(CLEVER_TYPE_DTOR_ARGS)
{
	delete CLEVER_CAST(fstream*, data);
}

CLEVER_METHOD(CFile::ctor)
{
	if (!clever_check_args("si")) {
		return;
	}

	result->setObj(this, allocData(&args));
}

// string File.read()
// Return the next token from the current stream
CLEVER_METHOD(CFile::read)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	CString* token = new CString;
	*file >> *token;

	result->setStr(token);
}

// string File.readLine()
// Return the contents from the current positon until the next line
CLEVER_METHOD(CFile::readLine)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	CString* token = new CString;
	::std::getline(*file, *token);

	result->setStr(token);
}

// void File.write(String)
// Return the contents from the current positon until the next line
// TODO(muriloadriano): allow use other primitive types as argument
CLEVER_METHOD(CFile::write)
{
	if (!clever_check_args("s")) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	*file << *(args[0]->getStr());
}

// void File.open(string fileName, Int openMode)
CLEVER_METHOD(CFile::open)
{
	if (!clever_check_args("si")) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());

	file->open(args[0]->getStr()->c_str(),
		static_cast< ::std::ios_base::openmode>(args[1]->getInt()));
}

// void File.close()
CLEVER_METHOD(CFile::close)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());
	file->close();
}

// bool File.isOpen()
CLEVER_METHOD(CFile::isOpen)
{
	if (!clever_check_no_args()) {
		return;
	}

	fstream* file = CLEVER_GET_OBJECT(fstream*, CLEVER_THIS());
	result->setBool(file->is_open());
}

CLEVER_TYPE_INIT(CFile::init)
{
	Function* ctor = new Function("File", (MethodPtr)&CFile::ctor);

	setConstructor(ctor);
	addMethod(ctor);
	addMethod(new Function("read",		(MethodPtr)&CFile::read));
	addMethod(new Function("readLine",	(MethodPtr)&CFile::readLine));
	addMethod(new Function("write",		(MethodPtr)&CFile::write));
	addMethod(new Function("open",		(MethodPtr)&CFile::open));
	addMethod(new Function("close",		(MethodPtr)&CFile::close));
	addMethod(new Function("isOpen",	(MethodPtr)&CFile::isOpen));

	addProperty(CSTRING("IN"), new Value(long(::std::ios_base::in), true));
	addProperty(CSTRING("OUT"), new Value(long(::std::ios_base::out), true));
	addProperty(CSTRING("TRUNC"),
		new Value(long(::std::ios_base::trunc), true));
	addProperty(CSTRING("APP"), new Value(long(::std::ios_base::app), true));
	addProperty(CSTRING("ATE"), new Value(long(::std::ios_base::ate), true));
	addProperty(CSTRING("BIN"), new Value(long(::std::ios_base::binary), true));
}

}}} // clever::packages::std

{'fraction_non_alphanumeric': 0.18862307283359914, 'fraction_numerical': 0.001063264221158958, 'mean_word_length': 5.757183908045977, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6580479', 'n_tokens_mistral': 3549, 'n_tokens_neox': 3418, 'n_words': 709}