--- parser3/src/classes/table.C 2016/07/29 22:40:58 1.317 +++ parser3/src/classes/table.C 2016/09/07 11:56:20 1.321 @@ -22,7 +22,7 @@ #define USE_STRINGSTREAM #endif -volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.317 2016/07/29 22:40:58 moko Exp $"; +volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.321 2016/09/07 11:56:20 moko Exp $"; // class @@ -51,8 +51,7 @@ String table_reverse_name(TABLE_REVERSE_ // methods -static Table::Action_options get_action_options(Request& r, MethodParams& params, - size_t options_index, const Table& source) { +static Table::Action_options get_action_options(Request& r, MethodParams& params, size_t options_index, const Table& source) { Table::Action_options result; if(params.count() <= options_index) return result; @@ -73,9 +72,7 @@ static Table::Action_options get_action_ if(soffset == "cur") result.offset=source.current(); else - throw Exception(PARSER_RUNTIME, - &soffset, - "must be 'cur' string or expression"); + throw Exception(PARSER_RUNTIME, &soffset, "must be 'cur' string or expression"); } else result.offset=r.process_to_value(*voffset).as_int(); } @@ -96,23 +93,27 @@ static Table::Action_options get_action_ return result; } -struct TableSeparators { - char column; const String* scolumn; +struct TableControlChars { + char separator; const String* sseparator; char encloser; const String* sencloser; - TableSeparators(): - column('\t'), scolumn(new String("\t")), + char separators[3]; + + TableControlChars(): + separator('\t'), sseparator(new String("\t")), encloser(0), sencloser(0) - {} + { + strcpy(separators,"\t\n"); + } + int load( HashStringValue& options ) { int result=0; if(Value* vseparator=options.get(PA_COLUMN_SEPARATOR_NAME)) { - scolumn=&vseparator->as_string(); - if(scolumn->length()!=1) - throw Exception(PARSER_RUNTIME, - scolumn, - "separator must be one character long"); - column=scolumn->first_char(); + sseparator=&vseparator->as_string(); + if(sseparator->length()!=1) + throw Exception(PARSER_RUNTIME, sseparator, "separator must be one character long"); + separator=sseparator->first_char(); + separators[0]=separator; result++; } if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) { @@ -121,9 +122,7 @@ struct TableSeparators { encloser=0; } else { if(sencloser->length()!=1) - throw Exception(PARSER_RUNTIME, - sencloser, - "encloser must be one character long"); + throw Exception(PARSER_RUNTIME, sencloser, "encloser must be one character long"); encloser=sencloser->first_char(); } result++; @@ -132,6 +131,7 @@ struct TableSeparators { } }; + static void _create(Request& r, MethodParams& params) { // clone/copy part? if(Table *source=params[0].get_table()) { @@ -149,16 +149,14 @@ static void _create(Request& r, MethodPa if(params[0].is_string()){ // can be nameless only const String& snameless=params.as_string(0, "called with more then 1 param, first param may be only string 'nameless' or junction"); if(snameless!="nameless") - throw Exception(PARSER_RUNTIME, - &snameless, - "table::create called with more then 1 param, first param may be only 'nameless'"); + throw Exception(PARSER_RUNTIME, &snameless, "table::create called with more then 1 param, first param may be only 'nameless'"); nameless=true; data_param_index++; } } HashStringValue *options=0; - TableSeparators separators; + TableControlChars control_chars; size_t options_param_index=data_param_index+1; if( @@ -167,11 +165,9 @@ static void _create(Request& r, MethodPa ) { // cloning, so that we could change options=new HashStringValue(*options); - separators.load(*options); - if(separators.encloser){ - throw Exception(PARSER_RUNTIME, - 0, - "encloser not supported for table::create yet"); + control_chars.load(*options); + if(control_chars.encloser){ + throw Exception(PARSER_RUNTIME, 0, "encloser not supported for table::create yet"); } } @@ -196,7 +192,7 @@ static void _create(Request& r, MethodPa if(head[0]->is_empty()) *columns += new String(); else - head[0]->split(*columns, col_pos_after, *separators.scolumn, String::L_AS_IS); + head[0]->split(*columns, col_pos_after, *control_chars.sseparator, String::L_AS_IS); } } @@ -214,7 +210,7 @@ static void _create(Request& r, MethodPa continue; size_t col_pos_after=0; - string.split(*row, col_pos_after, *separators.scolumn, String::L_AS_IS); + string.split(*row, col_pos_after, *control_chars.sseparator, String::L_AS_IS); table+=row; } @@ -226,45 +222,37 @@ struct lsplit_result { char* piece; char delim; + lsplit_result(char *apiece=0) : piece(apiece), delim(0){} operator bool() { return piece!=0; } }; -inline lsplit_result lsplit(char* string, char delim1, char delim2) { - lsplit_result result; - if(string) { - char delims[]={delim1, delim2, 0}; - if(char* v=strpbrk(string, delims)) { +inline lsplit_result lsplit(char* *string_ref, const char* delims) { + lsplit_result result(*string_ref); + if(result.piece) { + if(char* v=strpbrk(result.piece, delims)) { result.delim=*v; *v=0; - result.piece=v+1; + *string_ref=v+1; return result; + } else { + *string_ref=0; } } - result.piece=0; - result.delim=0; - return result; -} - -inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) { - lsplit_result result; - result.piece=*string_ref; - lsplit_result next=lsplit(*string_ref, delim1, delim2); - result.delim=next.delim; - *string_ref=next.piece; return result; } -static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) { - lsplit_result result; +static lsplit_result lsplit(char** string_ref, const char* delims, char encloser) { + lsplit_result result(*string_ref); - if(char* string=*string_ref) { - if(encloser && *string==encloser) { - string++; + if(result.piece) { + if(encloser && *result.piece==encloser) { + result.piece++; + char c; char *read; char *write; - write=read=string; - char c; + write=read=result.piece; + // we are enclosed, searching for second encloser while(c=*read++) { if(c==encloser) { @@ -275,22 +263,22 @@ static lsplit_result lsplit(char** strin } *write++=c; } + // we are no longer enclosed, searching for delimiter, skipping extra enclosers while(c=*read++) { - if(c==delim1 || c==delim2) { + if(c==delims[0] || c==delims[1]) { result.delim=c; break; } else if(c!=encloser) *write++=c; } + *write=0; // terminate - *string_ref=c? read: 0; - result.piece=string; + *string_ref=c ? read : 0; return result; } else - return lsplit(string_ref, delim1, delim2); + return lsplit(string_ref, delims); } - result.piece=0; return result; } @@ -333,22 +321,18 @@ static void _load(Request& r, MethodPara size_t options_param_index=filename_param_index+1; HashStringValue *options=0; - TableSeparators separators; + TableControlChars control_chars; if(options_param_indexcount()) // append last empty column [if without \n] break; *row+=new String(sr.piece, String::L_TAINTED); @@ -415,32 +399,32 @@ static void enclose( pa_stringstream& to } } -static void table_to_csv(pa_stringstream& result, Table& table, TableSeparators& separators, bool output_column_names) { +static void table_to_csv(pa_stringstream& result, Table& table, TableControlChars& control_chars, bool output_column_names) { if(output_column_names) { if(table.columns()) { // named table - if(separators.encloser){ + if(control_chars.encloser){ for(Array_iterator i(*table.columns()); i.has_next(); ) { - enclose( result, i.next(), separators.encloser ); + enclose( result, i.next(), control_chars.encloser ); if(i.has_next()) - result< i(*table.columns()); i.has_next(); ) { result<cstr(); if(i.has_next()) - result<count():0) for(int column=0; column i(table); - if(separators.encloser){ + if(control_chars.encloser){ while(i.has_next()) { for(Array_iterator c(*i.next()); c.has_next(); ) { - enclose( result, c.next(), separators.encloser ); + enclose( result, c.next(), control_chars.encloser ); if(c.has_next()) - result< c(*i.next()); c.has_next(); ) { result<cstr(); if(c.has_next()) - result< i(*table.columns()); i.has_next(); ) { - enclose( result, i.next(), separators.encloser, separators.sencloser ); + enclose( result, i.next(), control_chars.encloser, control_chars.sencloser ); if(i.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } } else { for(Array_iterator i(*table.columns()); i.has_next(); ) { result<<*i.next(); if(i.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } } } else { // nameless table [we were asked to output column names] if(int lsize=table.count()?table[0]->count():0) for(int column=0; column i(table); - if(separators.encloser){ + if(control_chars.encloser){ while(i.has_next()) { for(Array_iterator c(*i.next()); c.has_next(); ) { - enclose( result, c.next(), separators.encloser, separators.sencloser ); + enclose( result, c.next(), control_chars.encloser, control_chars.sencloser ); if(c.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } result.append_know_length("\n", 1, String::L_CLEAN); } @@ -544,7 +528,7 @@ static void table_to_csv(String& result, for(Array_iterator c(*i.next()); c.has_next(); ) { result<<*c.next(); if(c.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } result.append_know_length("\n", 1, String::L_CLEAN); } @@ -574,25 +558,23 @@ static void _save(Request& r, MethodPara if(do_append && file_exist(file_spec)) output_column_names=false; - TableSeparators separators; + TableControlChars control_chars; if(param_indexcount()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } if(param_indexcount()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } @@ -638,13 +618,13 @@ static void _csv_string(Request& r, Meth #ifdef USE_STRINGSTREAM pa_stringstream ost(std::stringstream::out); - table_to_csv(ost, table, separators, output_column_names); + table_to_csv(ost, table, control_chars, output_column_names); r.write_no_lang(*new VString(*new String(pa_strdup(ost.str().c_str()), String::L_CLEAN))); #else String sdata; - table_to_csv(sdata, table, separators, output_column_names); + table_to_csv(sdata, table, control_chars, output_column_names); r.write_no_lang(*new VString(*new String(sdata.cstr(), String::L_CLEAN))); #endif @@ -685,9 +665,7 @@ static void _offset(Request& r, MethodPa else if(whence=="set") absolute=true; else - throw Exception(PARSER_RUNTIME, - &whence, - "is invalid whence, valid are 'cur' or 'set'"); + throw Exception(PARSER_RUNTIME, &whence, "is invalid whence, valid are 'cur' or 'set'"); } int offset=params.as_int(params.count()-1, "offset must be expression", r); @@ -1211,9 +1189,7 @@ static void _join(Request& r, MethodPara Table& dest=GET_SELF(r, VTable).table(); if(&src == &dest) - throw Exception(PARSER_RUNTIME, - 0, - "source and destination are same table"); + throw Exception(PARSER_RUNTIME, 0, "source and destination are same table"); if(dest.columns()) // dest is named src.table_for_each(join_named_row, &dest, o);