Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

StringTable.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // Copyright (c) 2002 by Julien Keable 00004 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are met: 00007 // 00008 // 1. Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // 00011 // 2. Redistributions in binary form must reproduce the above copyright 00012 // notice, this list of conditions and the following disclaimer in the 00013 // documentation and/or other materials provided with the distribution. 00014 // 00015 // 3. The name of the authors may not be used to endorse or promote 00016 // products derived from this software without specific prior written 00017 // permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00022 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00024 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00026 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // This file is part of the PLearn library. For more information on the PLearn 00031 // library, go to the PLearn Web site at www.plearn.org 00032 00033 #include <plearn/io/fileutils.h> 00034 #include "StringTable.h" 00035 #include "stringutils.h" 00036 00037 00038 namespace PLearn { 00039 using namespace std; 00040 00041 00042 ostream& operator<<(ostream& out,const StringTable& st) 00043 { 00044 // find out width of each columns 00045 TVec<size_t> colsiz(st.width(),(size_t)0); 00046 for(int j=0;j<st.length();j++) 00047 { 00048 TVec<string> row=st.data(j); 00049 for(int i=0;i<st.width();i++) 00050 if((size_t)row[i].length() > colsiz[i]) 00051 colsiz[i]=(size_t)row[i].length(); 00052 } 00053 for(int i=0;i<st.width();i++) 00054 if(st.fieldnames[i].length() > colsiz[i]) 00055 colsiz[i]=(size_t)st.fieldnames[i].length(); 00056 00057 out<<"#: "; 00058 for(int i=0;i<st.width();i++) 00059 out<<left(st.fieldnames[i],colsiz[i]+3); 00060 out<<"\n"; 00061 00062 for(int j=0;j<st.length();j++) 00063 { 00064 TVec<string> row=st.data(j); 00065 out<<" "; 00066 for(int i=0;i<st.width();i++) 00067 out<<left(row[i],colsiz[i])<<";"; 00068 out<<"\n"; 00069 } 00070 00071 return out; 00072 00073 } 00074 00075 void StringTable::appendRow(const list<pair<string,string> > &row) 00076 { 00077 vector<string> vec; 00078 data.resize(data.length()+1,data.width()); 00079 int rownum=data.length()-1; 00080 for(list<pair<string,string> >::const_iterator it=row.begin();it!=row.end();++it) 00081 { 00082 int colnum; 00083 map<string,int>::iterator revit=rev_fn.find(it->first); 00084 if(revit==rev_fn.end()) 00085 { 00086 colnum=data.width(); 00087 TMat<string> tmp(data.length(),colnum+1); 00088 tmp.subMatColumns(0,colnum)<<data; 00089 data=tmp; 00090 rev_fn[it->first]=colnum; 00091 fieldnames.push_back(it->first);; 00092 } 00093 else colnum=rev_fn[it->first]; 00094 data(rownum,colnum)=it->second; 00095 } 00096 } 00097 00098 // This function will go through the list and will add columns as new fields are encountered. 00099 // It will do nothing on existing fields. Its use is mainly to force column order. 00100 // The second string in the pair is ignored.. the type of row is like this for convenience 00101 // ** Note : Do not use if the Table has more than 1 row (because resize won't work) 00102 void StringTable::declareFields(const list<pair<string,string> > & row) 00103 { 00104 for(list<pair<string,string> >::const_iterator it=row.begin();it!=row.end();++it) 00105 { 00106 map<string,int>::iterator revit=rev_fn.find(it->first); 00107 if(revit==rev_fn.end()) 00108 { 00109 data.resize(data.length(),data.width()+1); 00110 rev_fn[it->first]=data.width()-1; 00111 fieldnames.push_back(it->first);; 00112 } 00113 } 00114 } 00115 00116 StringTable::StringTable(){} 00117 00118 StringTable::StringTable(const string & filename) 00119 { 00120 int nrows= countNonBlankLinesOfFile(filename); 00121 string str; 00122 ifstream in(filename.c_str()); 00123 in.ignore(2); 00124 if(in.peek()==' ') 00125 in.ignore(1); 00126 getline(in,str); 00127 fieldnames=split(str); 00128 data.resize(nrows, (int)fieldnames.size()); 00129 int rnum=0; 00130 getline(in,str); 00131 while(removeblanks(str)!="") 00132 { 00133 /* 00134 vector<string> line; 00135 size_t pos,lpos=0; 00136 while((pos=str.find(";",lpos))!=string::npos) 00137 { 00138 line.push_back(str.substr(lpos,pos-lpos)); 00139 lpos=pos+1; 00140 } 00141 line.push_back(str.substr(lpos,str.size()-lpos)); 00142 if(line.size()!=fieldnames.size()) 00143 PLERROR("in row %i : elements (%i) mismatch number of fields (%i)",rnum,line.size(),fieldnames.size()); 00144 */ 00145 vector<string> line=split(str,";"); 00146 //line.pop_back(); // last string found is garbage *** NO, not true...! 00147 if(line.size()!=fieldnames.size()) 00148 PLERROR("in row %i : elements (%i) mismatch number of fields (%i)",rnum,line.size(),fieldnames.size()); 00149 00150 for(unsigned int i= 0; i < line.size(); ++i) 00151 data(rnum,i)= line[i]; 00152 ++rnum; 00153 getline(in,str); 00154 } 00155 data.resize(rnum, (int)fieldnames.size()); 00156 } 00157 00158 00159 } // end of namespace PLearn

Generated on Tue Aug 17 16:07:31 2004 for PLearn by doxygen 1.3.7