<commit_before>/** @file
 *
 * @ingroup modularLibrary
 *
 * @brief Handles text file reading and writing to be able to store / recall state of an object into/from text files.
 *
 * @authors <NAME>
 *
 * @copyright © 2010, <NAME> @n
 * This code is licensed under the terms of the "New BSD License" @n
 * http://creativecommons.org/licenses/BSD/
 */


#include "TTTextHandler.h"
#include <fstream>

#define thisTTClass			TTTextHandler
#define thisTTClassName		"TextHandler"
#define thisTTClassTags		"text, handler"

TT_MODULAR_CONSTRUCTOR,
mFilePath(kTTSymEmpty),
mWriter(NULL),
mReader(NULL),
mLine(NULL),
mTabCount(0),
mSpaceNumberForTab(4),
mFirstLine(NO),
mLastLine(NO),
mIsWriting(NO),
mIsReading(NO)
{
	addAttribute(Object, kTypeObject);
	addAttribute(SpaceNumberForTab, kTypeInt8);
	
	addMessageWithArguments(Write);
	addMessageWithArguments(Read);
    
    addMessage(WriteAgain);
	addMessage(ReadAgain);
}

TTTextHandler::~TTTextHandler()
{
	;
}

TTErr TTTextHandler::Write(const TTValue& args, TTValue& outputValue)
{
    TTValue		v, none;
	TTObject	aTTObject;
	
	// an object have to be selected
	if (!mObject.valid())
		return kTTErrGeneric;
	
	// memorize this object because it could change if the handler is used recursively
	aTTObject = mObject;
	
	if (args.size() == 1) {
		
		mIsWriting = true;
		
		// if the first argument is kTypeSymbol : get the path of the file to write
		if (args[0].type() == kTypeSymbol) {
			
			mFilePath = args[0];
			
			// Create a new text file
			std::ofstream file(mFilePath.string(), std::ofstream::out);
			
			// Create a new string text
			this->mWriter = new TTString();
			
			if (!file.is_open()) {
				TT_ASSERT("TTTextHandler : Error creating the text file\n", true);
				return kTTErrGeneric;
			}
			
			// Call the WriteAsText method of the handled object
			aTTObject.send("WriteAsText", TTObject(this), none);
			
			// Write the writer string into the file
            file << this->mWriter->data();
			
			// Close the text file
			file.close();
			
			// Clear the text
			delete this->mWriter;
		}
		
		// if the first argument is kTypePointer : get the text where to write
		else if (args[0].type() == kTypePointer) {
			
			mWriter = (TTString*)((TTPtr)args[0]);
			
			// Call the WriteAsText method of the handled object
			aTTObject.send("WriteAsText", TTObject(this), none);
			
		}
		else
			return kTTErrGeneric;
		
		mIsWriting = false;
		
		// Memorize the TTObject as the last handled object
		mObject = aTTObject;
		
		return kTTErrNone;
	}
	
	// else
	return aTTObject.send("WriteAsText", TTObject(this), none);
}

TTErr TTTextHandler::WriteAgain()
{
	TTValue args;
	TTValue dummy;
	
	args.append(mFilePath);
	return Write(args, dummy);
}

TTErr TTTextHandler::Read(const TTValue& args, TTValue& outputValue)
{
	TTObject	aTTObject;
	size_t		found, last, size;
	TTUInt8		i;
	TTString	line;
	TTValue		none;
	
	// an object have to be selected
	if (!mObject.valid())
		return kTTErrGeneric;
	
	// memorize this object because it could change if the handler is used recursively
	aTTObject = mObject;
	
	if (args.size() == 1) {
		
		mIsReading = true;
		
		// if the first argument is kTypeSymbol : get the path of the file to read
		if (args[0].type() == kTypeSymbol) {
			
			mFilePath = args[0];
			
			std::ifstream file(mFilePath.c_str());
			
			if (!file.is_open()) {
				TT_ASSERT("TTTextHandler: Error opening the text file\n", true);
				return kTTErrGeneric;
			}
			
			// Start the document
			mFirstLine = YES;
			mLastLine = NO;
			
			while (!file.eof()) {
				
				// parse line
                char c_line[1024];
				file.getline(c_line, 1024);
                
                TTString s_line = TTString(c_line);
				mLine = new TTValue(s_line);
				mLine->fromString();
				
				aTTObject.send("ReadFromText", TTObject(this), none);
				
				if (mFirstLine)
                    mFirstLine = NO;
                
                delete mLine;
			}
            
            // Close the reading
            mLine = new TTValue();
            mLastLine = YES;
            aTTObject.send("ReadFromText", TTObject(this), none);
            delete mLine;
		}
		<commit_msg>Fixing the parsing of line with more than 1024 character. now there is no size limit for a line<commit_after>/** @file
 *
 * @ingroup modularLibrary
 *
 * @brief Handles text file reading and writing to be able to store / recall state of an object into/from text files.
 *
 * @authors <NAME>
 *
 * @copyright © 2010, <NAME> @n
 * This code is licensed under the terms of the "New BSD License" @n
 * http://creativecommons.org/licenses/BSD/
 */


#include "TTTextHandler.h"
#include <fstream>

#define thisTTClass			TTTextHandler
#define thisTTClassName		"TextHandler"
#define thisTTClassTags		"text, handler"

TT_MODULAR_CONSTRUCTOR,
mFilePath(kTTSymEmpty),
mWriter(NULL),
mReader(NULL),
mLine(NULL),
mTabCount(0),
mSpaceNumberForTab(4),
mFirstLine(NO),
mLastLine(NO),
mIsWriting(NO),
mIsReading(NO)
{
	addAttribute(Object, kTypeObject);
	addAttribute(SpaceNumberForTab, kTypeInt8);
	
	addMessageWithArguments(Write);
	addMessageWithArguments(Read);
    
    addMessage(WriteAgain);
	addMessage(ReadAgain);
}

TTTextHandler::~TTTextHandler()
{
	;
}

TTErr TTTextHandler::Write(const TTValue& args, TTValue& outputValue)
{
    TTValue		v, none;
	TTObject	aTTObject;
	
	// an object have to be selected
	if (!mObject.valid())
		return kTTErrGeneric;
	
	// memorize this object because it could change if the handler is used recursively
	aTTObject = mObject;
	
	if (args.size() == 1) {
		
		mIsWriting = true;
		
		// if the first argument is kTypeSymbol : get the path of the file to write
		if (args[0].type() == kTypeSymbol) {
			
			mFilePath = args[0];
			
			// Create a new text file
			std::ofstream file(mFilePath.string(), std::ofstream::out);
			
			// Create a new string text
			this->mWriter = new TTString();
			
			if (!file.is_open()) {
				TT_ASSERT("TTTextHandler : Error creating the text file\n", true);
				return kTTErrGeneric;
			}
			
			// Call the WriteAsText method of the handled object
			aTTObject.send("WriteAsText", TTObject(this), none);
			
			// Write the writer string into the file
            file << this->mWriter->data();
			
			// Close the text file
			file.close();
			
			// Clear the text
			delete this->mWriter;
		}
		
		// if the first argument is kTypePointer : get the text where to write
		else if (args[0].type() == kTypePointer) {
			
			mWriter = (TTString*)((TTPtr)args[0]);
			
			// Call the WriteAsText method of the handled object
			aTTObject.send("WriteAsText", TTObject(this), none);
			
		}
		else
			return kTTErrGeneric;
		
		mIsWriting = false;
		
		// Memorize the TTObject as the last handled object
		mObject = aTTObject;
		
		return kTTErrNone;
	}
	
	// else
	return aTTObject.send("WriteAsText", TTObject(this), none);
}

TTErr TTTextHandler::WriteAgain()
{
	TTValue args;
	TTValue dummy;
	
	args.append(mFilePath);
	return Write(args, dummy);
}

TTErr TTTextHandler::Read(const TTValue& args, TTValue& outputValue)
{
	TTObject	aTTObject;
	size_t		found, last, size;
	TTUInt8		i;
	TTString	line;
	TTValue		none;
	
	// an object have to be selected
	if (!mObject.valid())
		return kTTErrGeneric;
	
	// memorize this object because it could change if the handler is used recursively
	aTTObject = mObject;
	
	if (args.size() == 1)
    {	
		mIsReading = true;
		
		// if the first argument is kTypeSymbol : get the path of the file to read
		if (args[0].type() == kTypeSymbol)
        {
			mFilePath = args[0];
			
			std::ifstream file(mFilePath.c_str());
			
			if (!file.is_open())
            {
				TT_ASSERT("TTTextHandler::Read : Error opening the text file\n", true);
				return kTTErrGeneric;
			}
			
			// Start the document
			mFirstLine = YES;
			mLastLine = NO;
			
			while (!file.eof())
            {
				// parse line                
                std::string s_line;
                std::getline(file, s_line);

				mLine = new TTValue(TTString(s_line));
				mLine->fromString();
				
				aTTObject.send("ReadFromText", TTObject(this), none);
				
				if (mFirstLine)
                    mFirstLine = NO;
                
                delete mLine;
			}
            
            // Close the reading
            mLine = new TTValue();
            mLastLine = YES;
            aTTObject.send("ReadFromText", TTObject(this), none);
            delete mLine;
		}
		
{'fraction_non_alphanumeric': 0.11220729822893141, 'fraction_numerical': 0.005230001188636634, 'mean_word_length': 2.840255591054313, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '1550993', 'n_tokens_mistral': 3185, 'n_tokens_neox': 2687, 'n_words': 777}