#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include "stringutil.h"
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()));
}
}
int main()
{
vector <vector<string> > table;
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;
if(!f) {
cout << "main:error: can't open input files"<< endl;
return 1;
}
cout << "main: readding data" << endl;
getlines(f, table, '\t', '#', '\0');
i=0;
while(i<table.size()) {
if(table[i].empty())
table.erase(&table[i], &table[i+1]);
else i++;
}
transpose(table);
cout << "main: data loaded. now, converting...\n";
cout << "table.size() = " << table.size() << endl;
for(unsigned i=0; i<table.size(); i++) {
if(!table[i].empty()) {
string name = table[i][0];
table[i].erase(&table[i][0], &table[i][1]);
if(name == "product")
product.swap(table[i]);
else if (name == "price")
stringlisttodoublelist(table[i], price);
else if(name == "unit")
stringlisttointlist(table[i], unit);
else if(name == "weight of unit")
stringlisttodoublelist(table[i], weight);
else {
cout << "unknow name: " << name << endl;
cout << "associated data is : ";
putline(cout, table[i]);
}
}
}
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;
}