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

sampreclist.cpp

Example file for multi-block record list read/write features.

// dataio 0.5 (beta) - Copyright (C) 2001, by Sadao Massago              //
// file: recordlist.cpp (record list (multi block) 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_
// #define _DEBUG2_

#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;
      unsigned unit;
      double price;
   };

   class stockio : public dataio {
   // vector <datarec> &data;
      vector <stock> *data;
   // datareclistio(vector <datarec> *datalist)
   // {&data = datalist;}
      bool status;
   public:
      void startinblock(unsigned i)
      {   
      #ifdef _DEBUG2_
      cout << "startinblock: i = " << i  << endl;
      #endif
         if(!data)
            return;
         if(i==0) // first call
           status = true;
         clear();
         data->push_back(stock());
         add("product", &(*data)[i].product);
         add("unit", &(*data)[i].unit);
         add("price", &(*data)[i].price);
      } 
      bool startoutblock(unsigned i)
      {
         #ifdef _DEBUG2_
            cout << "startoutblock: i = " << i  << endl;
         #endif
         if(!data || (i >= data->size()) )
            return false;
         clear();        
         add("product", &(*data)[i].product );
         add("unit", &(*data)[i].unit);
         add("price", &(*data)[i].price);
         ostrstream ostr; 
         ostr << i << "-th item" << ends;
         addcomment(ostr.str());
         return true;
      } 
      void validate(unsigned i)
      {
      #ifdef _DEBUG2_
      cout << "validate: i = " << i  << endl;
      putline(cout, this->referednames());
      #endif
         if(!data || (i >= data->size()) ) { // if will not occu
            cout << "error: stockio::validate: dataio internal problem\n"; 
            return;
         }
         // if not correct setted, set as defaul values
         if(!refered("product")) {
           cout << "error: stockio::validate: " << i << "-th the product name missing...\n"; 
           status = false;
         } else if(!setted("product") || (*data)[i].product.empty()) {
           cout << "error: stockio::validate: " << i << "-th the product name is invalid...\n"; 
           status = false;
         }
         // if not correct setted, set as defaul values
         if(!refered("unit")) { // not refered. Assume as none
           (*data)[i].unit = 0;
         } else if(!setted("unit")) { // invalid value
           cout << "error: stockio::validate: " << i << "-th the unit value is invalid...\n"; 
           status = false;
         }
         if(!refered("price")) {
           cout << "warn: stockio: " << i << "-th price missing...\n"; 
           (*data)[i].price = 0; // o is unkonow price
           status = false;
         } else if(!setted("price")) {
           cout << "error: stockio: " << i << "-th the price is invalid...\n"; 
           status = false;
         }
      } // validate     

      stockio():dataio() {
         data=0; status = false;}
      stockio(vector <stock> &stocklist) : dataio()
      {data = &stocklist; }
      void set(vector <stock> &stocklist)
      {data = &stocklist;}
   };


   //    void main(int argc, char **argv)
   //    {
   int main()
   {
      vector <stock> stocklist;
   
      stockio stockbase(stocklist);
      dataio io;
   
      ifstream f("sampreclist.txt"); 
      if(!f) { 
         cout << "main:error: can't open input files"<< endl;
         return 1;
      }
      
      // file open succeeds
      io.add("item", (dataio *)&stockbase);
      io.ignorecase(true);
      // io.colunoriented = true;
      // io.tabledata = true;
      io.decimal(',');
   
   #ifdef _DEBUG_
     cout << "main: readding data" << endl;
   #endif
   f >> io;
   cout << "main: data loaded...\n";
   
   #ifdef _DEBUG_
     cout << "main: writting data" << endl;
   #endif
   // data.colunoriented = false;
   // data.tabledata = false;
      cout << "loaded data...\n";
      cout << "stocklist.size() = " << stocklist.size() << endl;
      io.decimal('.');
      cout << io;    

      cout << "now, stock as column oriented table\n";
      // note the diference of recordlist output and table output
      stockbase.clearcomment(); // clear comment setted by stockbase in above output
      stockbase.addcomment("table of product stock");
      stockbase.istable = true;
      stockbase.columnoriented = true;
      stockbase.decimal('.');
      cout << stockbase;    
   }

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