Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

sampattrib.cpp

Example file for value atribution format data (like config file) read/write features.

// dataio 0.5.4 (beta) - Copyright (C) 2001, by Sadao Massago              //
// file: sampattrib.cpp (atrib indicator data examples)                           //
// requires: dataio.h, dataio.cpp, stringutil.h, stringutil.cpp              //
// ----------------------------------------------------------------------//
// The dataio library and related files is licenced under the term of    //
// GNU Lesser General Public License version 2.1 or latter               //
// (see lesser.txt for detail).                                          //
// For information over GNU or GNU compatible license, visit the site    //
// http://www.gnu.org.                                                   //

//#include <string>
#include <iostream>
#include <strstream>

// #define _DEBUG_

#include "dataio.h"
// it want to copile for single linkable file (without using makefile)
// comment above include and active follow two includes:
// #include "stringutil.cpp"
// #include "dataio.cpp"

// for main()
#include <fstream>


class cfg {
  public:
    vector <string> path;
    unsigned maxiter;
    bool printlog;
    string defaultlogfile;

    bool read(istream &f) {
      dataio io;
      // configure as unix config file like
      io.attribseparator='=';
      io.columnseparator=' ';
      // cfg variable settings.      
      io.add("path", &path);
      io.add("log-file", &defaultlogfile);
      io.add("print-log-on", &printlog); // prices with name fields
      io.add("iter-limit", &maxiter); // prices with name fields
      f >> io;
      return true;
    }
    bool read(const char *filename) {
      ifstream f(filename);
      if(!f)
        return false;
      // cfg variable settings.
      return read(f);
    }
    bool write(ostream &f)
    {
      dataio io;
      // configure as unix config file like
      io.attribseparator='=';
      io.columnseparator=';';
      // cfg variable settings.
      io.add("path", &path); 
      io.add("log-file", &defaultlogfile);
      io.add("print-log-on", &printlog); // prices with name fields
      io.add("iter-limit", &maxiter); // prices with name fields
      // some comments
      // item comment need to add after items
      io.addcomment("This is config file like data");  // global comment
      io.addcomment("path", "the path list"); 
      io.addcomment("log-file", "log file name");
      io.addcomment("iter-limit", "maximum number of iteration");
      f << io;
      return true;
    }
    bool write(const char *filename)
    {
      ofstream f(filename);
      if(!f)
        return false;
      return write(f);
    }
    bool writeCSV(ostream &f)
    // normal output
    {
      dataio io;
      // // configure as unix config file like
      // io.columnattribseparator='=';
      // io.columnseparator=' ';
      // cfg variable settings.
      io.add("path", &path);
      io.add("log-file", &defaultlogfile);
      io.add("print-log-on", &printlog); // prices with name fields
      io.add("iter-limit", &maxiter); // prices with name fields
      f << io;
      return true;
    }
    bool writeCSV(const char *filename)
    {
      ofstream f(filename);
      if(!f)
        return false;
      return writeCSV(f);
    }
}; // cfg

//    void main(int argc, char **argv)
//    {
int main()
{

  cfg progcfg;

  if(!progcfg.read("sampattrib.txt")){
         cout << "main:error: can't read input files"<< endl;
         return 1;
  }
      
      // config file reading succeeds
  cout << "configuration loaded\n";
  cout << "progcfg.path.size(): " << progcfg.path.size() << endl;
  cout << "progcfg.maxiter = " << progcfg.maxiter << endl;
  progcfg.write(cout);
  cout << "now, config values as CSV data (with separator \'\\t\' ):" << endl;
  progcfg.writeCSV(cout); 
}       

Generated at Thu Sep 6 13:45:40 2001 for dataio by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001