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

samptablewithoutdataio.cpp

Example file for simple table data read/write as string tables, (using only the stringutil)

// dataio 0.5 (beta) - Copyright (C) 2001, by Sadao Massago              //
// file: samptablewithoutdataio.cpp (table data examples)                //
// requires: 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 <fstream>
#include <vector>
#include <stdlib.h>

// This sample use only the stringutil (dataio does not used)

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

void stringlisttodoublelist(vector <string> const &l, vector <double> &x)
{
  if(!l.empty()) {
     x.clear();
     for(unsigned i=0; i<l.size(); i++)
        x.push_back(atof(l[i].c_str()));
  }
}

void stringlisttointlist(vector <string> const &l, vector <int> &x)
{
  if(!l.empty()) {
     x.clear();
     for(unsigned i=0; i<l.size(); i++)
        x.push_back(atoi(l[i].c_str()));
  }
}

//    void main(int argc, char **argv)
//    {
int main()
{
  vector <vector<string> > table;
  // vector <string> line;
  unsigned i;

  vector <string> product;
  vector <double> price;
  vector <int> unit;
  vector <double> weight;

  ifstream f("samptablewithoutdataio.txt"); 
         
  cout << "this sample read table without dataio (using only the stringutil)" << endl;                        // succeeds
  if(!f) {
     cout << "main:error: can't open input files"<< endl;
     return 1;
  }

  // file open succeeds
  cout << "main: readding data" << endl;
  getlines(f, table, '\t', '#', '\0');
  // clear empty lines
  i=0;
  while(i<table.size()) {
     if(table[i].empty())
          table.erase(&table[i], &table[i+1]);
     else i++; 
  }
  transpose(table); // the data is in column oriented mode

  cout << "main: data loaded. now, converting...\n";
          
  cout << "table.size() = " << table.size() << endl;
  for(unsigned i=0; i<table.size(); i++) {
       // line = table[i];
       if(!table[i].empty()) {
          string name = table[i][0]; // first is name
          table[i].erase(&table[i][0], &table[i][1]); // delete name from this
          if(name == "product")
              product.swap(table[i]); // get name list
          else if (name == "price")
              stringlisttodoublelist(table[i], price); // convert to double list               
          else if(name == "unit")
              stringlisttointlist(table[i], unit); // convert to int list               
          else if(name == "weight of unit")
              stringlisttodoublelist(table[i], weight); // convert to double list               
          else {
              cout << "unknow name: " << name << endl;
              cout << "associated data is : ";
              putline(cout, table[i]);
          }
       }
       // cout << endl;
  } // for

  // now, printing data.
  cout << "loaded data: " << endl;
  cout << "product:";
  for(i=0; i<product.size(); i++)
      cout << " " << product[i];
  cout << endl; 
  cout << "price:";
  for(i=0; i<price.size(); i++)
      cout << " " << price[i];
  cout << endl; 
  cout << "unit:";
  for(i=0; i<unit.size(); i++)
      cout << " " << unit[i];
  cout << endl; 
  cout << "weight of unit:";
  for(i=0; i<weight.size(); i++)
      cout << " " << weight[i];
  cout << endl; 

} // main()     

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