/**
 * Write the current object 
 */
void FrequencyTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Frequency* pfreq = dynamic_cast< const Frequency*>(pObject);
	if( pfreq == NULL) 
		throw TranslationException("FrequencyTranslator cannot cast frequency object");

	XMLElement* pelemf = elem.GetDocument()->NewElement( pszName);
	pelemf->SetAttribute("format", _szfmts[ pfreq->Format()]);
	char buff[64];
	const Frequency::ValueType& val = pfreq->Value();
	
	switch( pfreq->Format() )
	{
	case Frequency::Ratio:
		{
			sprintf(buff, "%ld,%ld", val.ratioVal.numerator,
				val.ratioVal.denominator);
		}
		break;
	default:
		{
			sprintf(buff, "%0.16le", val.doubleVal);
		}
		break;
	}
	pelemf->SetText( buff);
	elem.InsertEndChild( pelemf);

}
/**
 * Write the current object 
 */
void ChannelTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Channel* pchannel = dynamic_cast< const Channel*>(pObject);
	if( pchannel == NULL) 
		throw TranslationException("ChannelTranslator cannot cast Channel object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	if( !pchannel->IsReference())
	{
		XMLElement* pelem;

		//Write CenterFrequency
		WriteElement( &pchannel->CenterFrequency(), "centerfreq", ctxt, *pelemc);
	
		//Write Translated Frequency
		WriteElement( &pchannel->TranslatedFrequency(), "translatedfreq", ctxt, *pelemc);

		//Inverted Element
		pelem = elem.GetDocument()->NewElement( "inverted");
		pelem->SetText( (pchannel->Inverted())? "true":"false");
		pelemc->InsertEndChild( pelem);

		//delaybias
		WriteElement( &pchannel->DelayBias(), "delaybias", ctxt, *pelemc);

		//System
		WriteElement( &pchannel->System(), "system", ctxt, *pelemc);
	}
	
	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *pchannel, ctxt, *pelemc);
	elem.InsertEndChild( pelemc);
}
/**
 * Write the current object 
 */
void SystemTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const System* psystem = dynamic_cast< const System*>(pObject);
	if( psystem == NULL) 
		throw TranslationException("SystemTranslator cannot cast System object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *psystem, ctxt, *pelemc);

	if( !psystem->IsReference())
	{
		//Write Frequency Base.
		WriteElement( &psystem->BaseFrequency(), "freqbase", ctxt, *pelemc);
	
		//Write Equipment [0..1]
		WriteElement("equipment", psystem->Equipment().c_str(),  pelemc, false, "");

		//Write Type Attribute [0..1]
		WriteElement( "types", _szTypes[psystem->Type()], pelemc, false, "");

		//Write source elements [1..*]
		WriteList<Source>( psystem->Sources(), "source", ctxt,*pelemc);

		//Write cluster elements [0..*]
		WriteList<Cluster>( psystem->Clusters(), "cluster", ctxt,*pelemc);
	}
	
	elem.InsertEndChild( pelemc);
}
Exemple #4
0
inline void CameraSet<T>::appendTextElement( tinyxml2::XMLDocument& doc,
                                             tinyxml2::XMLNode& node,
                                             std::string name,
                                             std::string val )
{
    tinyxml2::XMLNode* tmp = node.InsertEndChild( doc.NewElement( name.c_str() ) );
    tmp->InsertEndChild( doc.NewText( val.c_str() ));
}
/**
 * Write the current object 
 */
void StreamTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Stream* pstream = dynamic_cast< const Stream*>(pObject);
	if( pstream == NULL) 
		throw TranslationException("StreamTranslator cannot cast Stream object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *pstream, ctxt, *pelemc);

	if( !pstream->IsReference())
	{
		XMLElement* pelem;
		char buff[64];

		//Write ratefactor
		pelem = elem.GetDocument()->NewElement( "ratefactor");
		sprintf( buff, "%ld", pstream->RateFactor() );
		pelem->SetText( buff );
		pelemc->InsertEndChild( pelem);

		//Write quantization
		pelem = elem.GetDocument()->NewElement( "quantization");
		sprintf( buff, "%ld", pstream->Quantization() );
		pelem->SetText( buff );
		pelemc->InsertEndChild( pelem);

		//Write packedbits
		pelem = elem.GetDocument()->NewElement( "packedbits");
		sprintf( buff, "%ld", pstream->Packedbits() );
		pelem->SetText( buff );
		pelemc->InsertEndChild( pelem);

		//Write alignment
		pelem = elem.GetDocument()->NewElement( "alignment");
		pelem->SetText( _szAlignFmts[ pstream->Alignment()] );
		pelemc->InsertEndChild( pelem);

		//Write format
		pelem = elem.GetDocument()->NewElement( "format");
		pelem->SetText( _szSampleFmts[pstream->Format()] );
		pelemc->InsertEndChild( pelem);

		//Write encoding
		pelem = elem.GetDocument()->NewElement( "encoding");
		pelem->SetText( pstream->Encoding().c_str() );
		pelemc->InsertEndChild( pelem);

		//Write band
		WriteList<Band>( pstream->Bands(), "band", ctxt,*pelemc);

	}
	
	elem.InsertEndChild( pelemc);
}
/**
 * Write the current object 
 */
void RfConfigTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const RfConfiguration* pconfig = dynamic_cast< const RfConfiguration*>(pObject);
	if( pconfig == NULL) 
		throw TranslationException("RfConfigTranslator cannot cast to RfConfiguration object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *pconfig, ctxt, *pelemc);
	elem.InsertEndChild( pelemc);
}
/**
 * Write the current object 
 */
void SessionTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Session* psession = dynamic_cast< const Session*>(pObject);
	if( psession == NULL) 
		throw TranslationException("SessionTranslator cannot cast Session object");
	else if( psession->Id().length() == 0)
		return;
	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	if( !psession->IsReference())
	{
		XMLElement* pelem;

		//Write scenario
		pelem = elem.GetDocument()->NewElement( "scenario");
		pelem->SetText( psession->Scenario().c_str());
		pelemc->InsertEndChild(pelem);
		
		//Write campaign
		pelem = elem.GetDocument()->NewElement( "campaign");
		pelem->SetText( psession->Campaign().c_str());
		pelemc->InsertEndChild(pelem);

		//Write location
		pelem = elem.GetDocument()->NewElement( "location");
		char buff[128];
		const Location& llh = psession->Location();
		sprintf(buff,"%0.8lf", llh.Latitude());
		pelem->SetAttribute("lat", buff);
		sprintf(buff,"%0.8lf", llh.Longitude());
		pelem->SetAttribute("lon", buff);
		sprintf(buff,"%0.3lf", llh.Height());
		pelem->SetAttribute("height", buff);
		pelemc->InsertEndChild(pelem);

		//Write conact
		pelem = elem.GetDocument()->NewElement( "contact");
		pelem->SetText( psession->Contact().c_str());
		pelemc->InsertEndChild(pelem);

		//Write poc
		pelem = elem.GetDocument()->NewElement( "poc");
		pelem->SetText( psession->Poc().c_str());
		pelemc->InsertEndChild(pelem);
	}
	
	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *psession, ctxt, *pelemc);
	elem.InsertEndChild( pelemc);
}
	/**
	*	@brief This function calls WriteSaveSlot using each of the three save slots loaded 
	*	from earlier and saves the saveData.xml file.
	*/
	void save()	{
		xmlDoc.SaveFile("Backup_SavedData.xml");
		xmlDoc.Clear();
		initFile();

		WriteSaveSlot(*saved_data[0]);
		WriteSaveSlot(*saved_data[1]);
		WriteSaveSlot(*saved_data[2]);

		pRoot->InsertEndChild(saveGame);

		tinyxml2::XMLError eResult = xmlDoc.SaveFile("Assets/save_data.xml");
		if (eResult != NULL)
			perror("Error saving file a backup will be loaded on restart");
		if (remove("Backup_SavedData.xml") != 0)
			perror("Error deleting backup file");
	}
/**
 * Write the current object 
 */
void SessionTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Session* psession = dynamic_cast< const Session*>(pObject);
	if( psession == NULL) 
		throw TranslationException("SessionTranslator cannot cast Session object");
	else if( psession->Id().length() == 0)
		return;
	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *psession, ctxt, *pelemc);

	if( !psession->IsReference())
	{
		XMLElement* pelem;
		
		//Write toa
		pelem = elem.GetDocument()->NewElement( "toa");
		pelem->SetText( psession->Toa().toString().c_str());
		pelemc->InsertEndChild( pelem);

		//Write Position
		WriteElement( &psession->Position(), "position", ctxt, *pelemc);

		//TODO Write Attitude 

		//Write poc
		WriteElement("poc",psession->Poc().c_str(), pelemc, false, "");

		//Write conact
		WriteElement("contact",psession->Contact().c_str(), pelemc, false, "");

		//Write campaign
		WriteElement("campaign",psession->Campaign().c_str(), pelemc, false, "");

		//Write scenario
		WriteElement("scenario",psession->Scenario().c_str(), pelemc, false, "");

		//Write system
		WriteList<System>( psession->Systems(), "system", ctxt,*pelemc);		//Write location
	}
	

	elem.InsertEndChild( pelemc);
}
/**
 * Write the current object 
 */
void SourceTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Source* psource = dynamic_cast< const Source*>(pObject);
	if( psource == NULL) 
		throw TranslationException("SourceTranslator cannot cast Source object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	//Source object must have ID defined in order to support association
	//with bandsrc.
	WriteAttributedObject( *psource, ctxt, *pelemc, true);

	if( !psource->IsReference())
	{
		XMLElement* pelem;

		//Write type [0..1]
		pelem = elem.GetDocument()->NewElement( "type");
		pelem->SetText( _szSourceType[ psource->Type()] );
		pelemc->InsertEndChild( pelem);

		//Write polarization [0..1]
		pelem = elem.GetDocument()->NewElement( "polarization");
		pelem->SetText( _szSourcePolarization[ psource->Polarization()] );
		pelemc->InsertEndChild( pelem);

		//Write origin [0..1]
		WriteElement( &psource->Origin(), "origin", ctxt, *pelemc);

		//TODO Write rotation 

		//Write idcluster [0..1]
		WriteElement("idcluster",psource->IdCluster().c_str(), pelemc, false, "");
	}
	
	elem.InsertEndChild( pelemc);
}
/**
 * Write the current object 
 */
void LumpTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Lump* plump = dynamic_cast< const Lump*>(pObject);
	if( plump == NULL) 
		throw TranslationException("LumpTranslator cannot cast Lump object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *plump, ctxt, *pelemc);

	if( !plump->IsReference())
	{

      //Write band
		WriteList<IonStream>( plump->Streams(), "stream", ctxt,*pelemc);

	}
	

	elem.InsertEndChild( pelemc);
}
/**
 * Write the current object 
 */
void LaneTranslator::OnWrite( const Object * pObject, pcstr pszName, Context & ctxt, tinyxml2::XMLNode & elem )
{
	const Lane* plane = dynamic_cast< const Lane*>(pObject);
	if( plane == NULL) 
		throw TranslationException("LaneTranslator cannot cast Lane object");

	XMLElement* pelemc = elem.GetDocument()->NewElement( pszName);

	//Fill out id, artifacts, and comments last in accordance
	//with schema.
	WriteAttributedObject( *plane, ctxt, *pelemc);

	if( !plane->IsReference())
	{
		//Write bandsrc [1..*]
		Lane::BandSourceList::const_iterator iter = plane->BandSources().begin();
        for(; iter != plane->BandSources().end(); iter++)
		{
			XMLElement* pelem = elem.GetDocument()->NewElement("bandsrc");
			pelem->SetAttribute("idband", iter->idBand.c_str());
			pelem->SetAttribute("idsrc", iter->idSource.c_str());
			pelemc->InsertEndChild(pelem);
		}

		//Write session [0..*]
		WriteList<Session>( plane->Sessions(), "session", ctxt,*pelemc);

		//Write system [1..*]
		WriteList<System>( plane->Systems(), "system", ctxt,*pelemc);

		//Write block [1..*]
		WriteList<Block>( plane->Blocks(), "block", ctxt,*pelemc);
	}
	
	elem.InsertEndChild( pelemc);
}
	/**
	*	@brief This generates a new saveData.xml for the game insertting all the approprate
	*	formatting for reading later.
	*	@see SaveSlot
	*/
	void GenerateSaveFile(){
		cLog::inst()->print(3, "XML Loader", "Generating new save_data.xml");

		//create and insert the root node of the .xml document
		tinyxml2::XMLNode *pRoot = xmlDoc.NewElement("Root");
		xmlDoc.InsertFirstChild(pRoot);

		tinyxml2::XMLElement *saveGame = xmlDoc.NewElement("SaveGame");
		for (int i = 0; i < 3; i++)
		{
			string d = "SaveSlot" + std::to_string(i + 1);
			const char * c = d.c_str();
			tinyxml2::XMLElement * saveSlot = xmlDoc.NewElement(c);

			//Inserts IntValue to the .xml file
			tinyxml2::XMLElement * pElement = xmlDoc.NewElement("TimePlayed");
			pElement->SetText(0);
			saveSlot->InsertEndChild(pElement);

			pElement = xmlDoc.NewElement("CurrentGold");
			pElement->SetText(0);
			saveSlot->InsertEndChild(pElement);

			pElement = xmlDoc.NewElement("Levels");
			for (int i = 0; i < 7; i++)
			{
				string d = "LVL" + std::to_string(i + 1);
				const char * c = d.c_str();
				tinyxml2::XMLElement * pListElement = xmlDoc.NewElement(c);
				pListElement->SetText(1);

				pElement->InsertEndChild(pListElement);
			}

			saveSlot->InsertEndChild(pElement);

			//Insert Achievement Tracking into the saveData.sav
			pElement = xmlDoc.NewElement("Achievements");
			for (int i = 0; i < 21; i++)
			{
				string d = "ACH" + std::to_string(i + 1);
				const char * c = d.c_str();
				tinyxml2::XMLElement * pListElement = xmlDoc.NewElement(c);
				pListElement->SetText(0);

				pElement->InsertEndChild(pListElement);
			}

			saveSlot->InsertEndChild(pElement);

			//Insert Stat Tracking into the saveData.sav
			pElement = xmlDoc.NewElement("Statistics");
			for (int i = 0; i < 10; i++)
			{
				string d = "STAT" + std::to_string(i + 1);
				const char * c = d.c_str();
				tinyxml2::XMLElement * pListElement = xmlDoc.NewElement(c);
				pListElement->SetText(0);

				pElement->InsertEndChild(pListElement);
			}

			saveSlot->InsertEndChild(pElement);

			saveGame->InsertEndChild(saveSlot);
		}
		pRoot->InsertEndChild(saveGame);

		tinyxml2::XMLError eResult = xmlDoc.SaveFile("Assets/save_data.xml");

	}