コード例 #1
0
void TextFileStorage::saveToIndividualFile()
{

	string str;
	ofstream file;

  // Declare actual field data object.
  FieldData* data; 

  // Get the initial position of the internal iterator of the Datahandler.
  int pos = this->getInput()->getPosition();

	ObjectData* objectData = NULL;
	if( onlyCurrent )
	{
		// Get the current data object.
		objectData = getInput()->getCurrentObject();
	}
	else
	{
		// Get the first data object.
		objectData = getInput()->getFirstObjectCompleted();  
	}

	// Loop over all elements in the datahandler
	while( objectData != NULL)
	{

		// Create the file name.
		stringstream integerConvertor;
		integerConvertor << fileNumber++;
		str = this->getLocation() + this->getFileName() + integerConvertor.str();

		// Open the file;
		file.open( str.c_str() );
		
		if( file.is_open() )
		{
			
			// Loop through all fieldData elements in an objectData object.
			for( int j = 0; j < objectData->getSize(); j++ )
			{
				data = objectData->getDataAt(j);
				
				switch( data->getType() )
				{
					case typeFieldDataInt:
						file << data->getInt() << endl;
					break;
					
					case typeFieldDataFloat:
						file << data->getFloat() << endl;
					break;
					
					case typeFieldDataDouble:
						file << data->getDouble() << endl;
					break;
					
					case typeFieldDataString:
						file << data->getString() << endl;
					break;
					
					case typeFieldDataBool:
						if( data->getBool() )
						{
							file << "true" << endl;
						}
						else
						{
							file << "false" << endl;
						}
					break;				
				}		
			}			
			
			file.close();
		}
		else
		{
			//TODO throw
		}		
		
	
		if( onlyCurrent )
		{
			// Stop the loop if only the current object is to be executed.
			objectData = NULL;
		}
		else
		{		
			// Get the next data object.
			objectData = getInput()->getNextObjectCompleted(); 
		} 		
		
		
	}

  // Resore the initial position of the internal Datahandler iterator. This is not as it is seposed to be. TODO alter the datahandler internal iterator.
  (this->getInput())->getObjectAt( pos );


	// Set the output to the location of the files.
	this->setOutput( this->getLocation() );

};
コード例 #2
0
void TextFileStorage::saveToSingleFile()
{

	string str = this->getLocation() + this->getFileName();

	ofstream file( str.c_str(), ios::app);
  
  // Get the initial position of the internal iterator of the Datahandler.
  int pos = this->getInput()->getPosition();
  

  
  if( file.is_open() )
  {
    
  	ObjectData* objectData = NULL;
  	if( onlyCurrent )
  	{
  		// Get the current data object.
  		objectData = getInput()->getCurrentObject();
  	}
  	else
  	{
  		// Get the first data object.
  		objectData = getInput()->getFirstObjectCompleted();  
  	}
    
    // Declare actual field data object.
    FieldData* data; 

		// Loop through all objectData elements in the Datahandler which are finched.
    while( objectData != NULL )
    {

			// Start with a new object.
			file << "\n# BEGIN OF OBJECT\n";
						
			// Loop through all fieldData elements in an objectData object.
			for( int j = 0; j < objectData->getSize(); j++ )
			{
				data = objectData->getDataAt(j);
				

				switch( data->getType() )
				{
					case typeFieldDataInt:
						file << data->getInt() << "\n";
					break;
					
					case typeFieldDataFloat:
						file << data->getFloat() << "\n";
					break;
					
					case typeFieldDataDouble:  
						file << data->getDouble() << "\n";
					break;
					
					case typeFieldDataString:
						file << data->getString() << "\n";
					break;
					
					case typeFieldDataBool:
						if( data->getBool() )
						{
							file << "true\n";
						}
						else
						{
							file << "false\n";
						}
					break;				
				}		
			}
			
			// The end of the object
			file << "# END OF OBJECT\n";
			
			if( onlyCurrent )
			{
				// Stop the loop if only the current object is to be executed.
				objectData = NULL;
			}
			else
			{		
				// Get the next data object.
				objectData = getInput()->getNextObjectCompleted(); 
			} 
			  	
    }
    

	  // Resore the initial position of the internal Datahandler iterator. This is not as it is seposed to be. TODO alter the datahandler internal iterator.
	  getInput()->getObjectAt( pos );
    
    // Close the file.
    file.close();

  }
  else
	{
		//TODO throw
	}


  // Resore the initial position of the internal Datahandler iterator. This is not as it is seposed to be. TODO alter the datahandler internal iterator.
  (this->getInput())->getObjectAt( pos );


	// Set the output to the full file name.
	this->setOutput( this->getLocation() + this->getFileName() );


};