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

samptable.cpp

Example file for table data read/write features.

// dataio 0.5 (beta) - Copyright (C) 2001, by Sadao Massago              //
// file: samptable.cpp (table 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>
#include <vector>

// #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 stock {
public:
  string product;
  int unit;
  double weight;
  double price;
};

class stockio : public dataio {
  // vector <datarec> &data;
  public:
  vector <stock> *data;
  // datareclistio(vector <datarec> *datalist)
  // {&data = datalist;}
  void startinblock(unsigned i)
  {       
    #ifdef _DEBUG_
      cout << "startinblock: i = " << i  << endl;
    #endif
    if(!data)
      return;
    clear();
    data->push_back(stock());
    add("product", &(*data)[i].product );
    add("unit", &(*data)[i].unit);
    add("weight of unit", &(*data)[i].weight);
    add("price", &(*data)[i].price);
  }     
  void validate(unsigned i)
  {
    if(!setted()) {
      cout << "warn: stockio::validate:column " << i << 
              " some value is missing or invalid\n";
    }
  }    
  bool startoutblock(unsigned i)
  {
    #ifdef _DEBUG_
      cout << "startoutblock: i = " << i  << endl;
    #endif
         if(!data || (i >= data->size()) )
           return false;
         clear();

    ostrstream ostr;
    ostr << i << "-th item" << ends;
    addcomment(ostr.str());
         
    add("product", &(*data)[i].product );
    add("unit", &(*data)[i].unit);
    add("weight of unit", &(*data)[i].weight);
    add("price", &(*data)[i].price);
         return true;
  }     
  stockio():dataio() {data=0;}
  stockio(vector <stock> &stocklist) : dataio()
  {data = &stocklist; }
  void set(vector <stock> &stocklist)
  {data = &stocklist;}
};

//    void main(int argc, char **argv)
//    {
int main()
{
  vector <stock> food;
  vector <stock> bazar;

  stockio io(food);

  ifstream f("samptable.txt"); 

  if(!f) {
     cout << "main:error: can't open input files"<< endl;
     return 1;
  }

  // file open succeeds
  // io settings
  io.ignorecase(true);
  io.columnoriented = true;
  io.istable = true;
  io.decimal(',');
  
  cout << "main: readding data" << endl;
  f >> io;
  cout << "main: data loaded. now, writing...\n";
          
  // io.columnoriented = false;
  // io.istable = false;
  cout << "food.size() = " << food.size() << endl;
  io.decimal('.');
  cout << io;    

  io.set(bazar);
  // io.ignorecase(true);
  // io.columnoriented = true;
  // io.istable = true;
  io.decimal(',');
  f >> io;
  cout << "main: new data loaded...\n";
  cout << "bazar.size() = " << bazar.size() << endl;
  io.decimal('.');
  cout << io;    
  

  cout << "now, outputting bazar as record list...\n";
  io.columnoriented = false;
  io.istable = false;
  dataio recio;
  recio.add("item", (dataio *)&io);
  cout << recio;
}       

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