Previous: Writing a parser, Up: Programming


6.8 Adding a generator

Steps neccessary to add a generator. XXX refers to the output format you want to implement.

src/generator/XXX.h:

     #include "../gnuclad.h"
     derive class from generator:
       class GeneratorXXX: public Generator
     declare constructor and destructor
     declare public method:
       void writeData(Cladogram * clad, OutputFile & out);

src/generator/XXX.cpp:

     #include "XXX.h"
     implement constructor and destructor
     implement writeData(Cladogram * clad, OutputFile & out) { ... }

src/gnuclad.cpp:

     #include "generator/XXX.h"
     update outFormats
     add choser:
       else if(outputExt == "XXX") generator = new GeneratorXXX;

src/Makefile.am:

     add to SOURCES: generator/XXX.h generator/XXX.cpp

6.9 How to write a generator

Your generator is called only by this function:

       generator->writeData(Cladogram * clad, OutputFile & out);

Therefore it should implement the following one:

       void generatorXXX::writeData(Cladogram * clad, OutputFile & out) { ... }

The OutputFile object holds a correctly opened output file. It also holds the file name in case your generator needs it. You can use the object like this:

       ofstream * fp = out.p;
       // or
       ofstream & f = *(out.p);
       // or
       ofstream myfp(out.name);

You should try to utilise as much information from the Cladogram as possible. This can be done by iterating over its data vectors:

     vector<Node *> nodes
     vector<Connector *> connectors
     vector<Domain *> domains
     vector<Image *> includeSVG
     vector<Image *> includePNG

Additionally, you should strive to make use of all configuration options the Cladogram contains.

If a variable is empty, try to handle it as gracefully as possible. All measures (height, thickness, ...) are "generic units".

Example:

     #include "../gnuclad.h"
     #include "generatorXXX.h"
     #include <iostream>
     void generatorXXX::writeData(Cladogram * clad, OutputFile & out) {
     
       Node * n;
       for(int i = 0; i < (int)clad->nodes.size(); ++i) {
     
         n = clad->nodes[i];
         std::cout << "\nWe have " << n->name
                   << " at offset " << n->offset;
     
       }
     
       std::cout << "\n\nOur end of time is "
                 << Date2str(clad->endOfTime) << "\n";
     
       std::cout << "\n\nOur background color (in hex) is "
                 << clad->mainBackground.hex << "\n";
     
     }