6 vector< string >
readCSV(
string s,
string delim )
10 unsigned int epos = 0;
11 unsigned int sl = s.length();
15 while ( (epos < sl) && !( (s[epos] ==
'\r') || (s[epos] ==
'\n') ) ) {
18 if (s[epos] ==
'\"') {
19 if ( (epos+1 < sl) && (s[epos+1] ==
'\"') )
23 quote = insert =
false;
28 size_t dp = delim.find( s[epos] );
29 if (dp != string::npos) ddelim = delim[dp];
30 ep = (dp != string::npos);
32 ep = s[epos] == ddelim;
39 }
else if (s[epos] ==
'\"') {
40 if ( (epos+1 < sl) && (s[epos+1] ==
'\"') )
43 insert = !(quote =
true);
46 if ( insert && (epos < sl) ) as += s[epos];
49 if ( !as.empty() ) l.push_back( as );
53 static string addQuote(
string s,
string delim =
",;" )
55 if (s.find_first_of( delim+
"\"" ) != string::npos) {
56 if (s.find(
'\"' ) != string::npos) {
61 for (
int i = 0;
i < sl;
i++)
62 if (s[
i] !=
'\"') as += s[
i];
68 s.insert( s.begin(), 1,
'\"' );
69 s.insert( s.end(), 1,
'\"' );
74 string writeCSV(
const vector< string > &key,
const vector< string > &table,
string delim )
77 unsigned int wid = key.size();
79 for (i = 0; i < wid; ++i) {
85 for (i = 0; i < table.size(); ++i) {
95 void CSVTable::Init(
string data )
102 const string delim(
",;" );
103 const char *cdata = data.c_str();
104 const char *csep = strchr( cdata,
'\n' );
105 if (csep == NULL)
return;
106 string buffer( cdata, csep-cdata );
109 for (
unsigned int i = 0;
i <
key.size();
i++)
111 while (cdata && *cdata) {
112 csep = strchr( cdata,
'\n' );
117 buffer.assign( cdata, csep-cdata );
124 unsigned int row =
table.size()/
key.size();
125 while ( strs.size() >
key.size() ) {
126 fprintf( stderr,
"error in csv, line %d: %s has no key", row+1, strs.back().c_str() );
129 while ( strs.size() <
key.size() )
130 strs.push_back(
"" );
131 assert( strs.size() ==
key.size() );
132 table.insert(
table.end(), strs.begin(), strs.end() );
155 this->parent = parent;
156 iter = parent->
rows[key]*parent->
key.size();
161 this->parent = parent;
162 iter = i*parent->
key.size();
167 static string empty_string;
168 vsUMap< string, int >::iterator
i = parent->
columns.find( col );
169 if ( i == parent->
columns.end() )
173 return parent->
table[iter+(*i).second];
178 return parent->
table[iter+col];
183 return parent->
key[which];
188 vsUMap< string, int >::iterator i =
rows.find( name );
189 if ( i ==
rows.end() )
197 vsUMap< string, int >::iterator i =
columns.find( name );
210 fprintf( stderr,
"Error getting root for unit\n" );
220 for (
unsigned int i = 0; i < keys.size(); ++i)