diff options
Diffstat (limited to 'src/dictionary/DictionaryWriter.cpp')
-rw-r--r-- | src/dictionary/DictionaryWriter.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/dictionary/DictionaryWriter.cpp b/src/dictionary/DictionaryWriter.cpp new file mode 100644 index 0000000..14cd087 --- /dev/null +++ b/src/dictionary/DictionaryWriter.cpp @@ -0,0 +1,79 @@ +#include "DictionaryWriter.h" +#include "Dictionary.h" +#include "../version.h" +#include "CardPack.h" +#include "DicRecord.h" + +DictionaryWriter::DictionaryWriter( const Dictionary* aDict ): + m_dict( aDict ) +{ + setAutoFormatting( true ); +} + +bool DictionaryWriter::write( QIODevice* aDevice ) +{ + setDevice( aDevice ); + writeStartDocument(); + writeDTD( "<!DOCTYPE freshmemory-dict>" ); + writeStartElement("dict"); + writeAttribute( "version", DIC_VERSION ); + + writeStartElement( "fields" ); + foreach( Field *field, m_dict->fields() ) + writeField( field ); + writeEndElement(); + + writeStartElement( "packs" ); + foreach( const CardPack* pack, m_dict->cardPacks() ) + writePack( pack ); + writeEndElement(); + + writeStartElement( "entries" ); + foreach( const DicRecord* entry, m_dict->getRecords() ) + writeEntry( entry, m_dict->fields() ); + writeEndElement(); + + writeEndDocument(); + return true; +} + +void DictionaryWriter::writeField( Field* aField ) + { + writeStartElement( "field" ); + QString style = aField->style(); + if( !style.isEmpty() && style != FieldStyleFactory::DefaultStyle ) + writeAttribute( "style", style ); + writeCharacters( aField->name() ); + writeEndElement(); + } + +void DictionaryWriter::writePack( const CardPack* aPack ) + { + writeStartElement( "pack" ); + if(aPack->getUsesExactAnswer()) + writeAttribute( "exact-ans", "true" ); + writeTextElement( "qst", aPack->getQuestionField()->name() ); + foreach( const Field* field, aPack->getAnswerFields() ) + writeTextElement( "ans", field->name() ); + writeEndElement(); + } + +void DictionaryWriter::writeEntry( const DicRecord* aEntry, QList<Field*> aFields ) + { + writeStartElement( "e" ); + int emptyFieldsNum = 0; + foreach( Field* field, aFields ) + { + QString fieldValue = aEntry->getFieldValue( field->name() ); + if( fieldValue.isEmpty() ) + emptyFieldsNum++; + else + { + for( int k = 0; k < emptyFieldsNum; k++ ) + writeEmptyElement( "f" ); + emptyFieldsNum = 0; + writeTextElement( "f", fieldValue ); + } + } + writeEndElement(); + } |