summaryrefslogtreecommitdiff
path: root/src/dictionary/DictionaryWriter.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/dictionary/DictionaryWriter.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/dictionary/DictionaryWriter.cpp')
-rw-r--r--src/dictionary/DictionaryWriter.cpp79
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();
+ }