--- parser3/src/classes/table.C 2003/04/11 16:05:43 1.172.2.14.2.30 +++ parser3/src/classes/table.C 2003/11/03 13:20:30 1.186 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_TABLE_C="$Date: 2003/04/11 16:05:43 $"; +static const char* IDENT_TABLE_C="$Date: 2003/11/03 13:20:30 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -50,6 +50,12 @@ String sql_default_name(SQL_DEFAULT_NAME String sql_distinct_name(SQL_DISTINCT_NAME); String table_reverse_name(TABLE_REVERSE_NAME); +// local defines + +#define COLUMN_SEPARATOR_NAME "column-separator" +#define COLUMN_ENCLOSER_NAME "column-encloser" + + // methods static Table::Action_options get_action_options(Request& r, MethodParams& params, @@ -58,12 +64,22 @@ static Table::Action_options get_action_ if(!params.count()) return result; + Value& maybe_options=params.last(); +/* can not do it: + want to enable ^table::create[$source; +# $.option[] + ] + but there is ^table.locate[name;value] - HashStringValue* options=params.last().get_hash(); + if(maybe_options.is_string()) { // allow empty options + result.defined=true; + return result; + } +*/ + HashStringValue* options=maybe_options.get_hash(); if(!options) return result; - result.defined=true; bool defined_offset=false; @@ -100,7 +116,7 @@ static Table::Action_options get_action_ return result; } -static check_option_param(bool options_defined, +static void check_option_param(bool options_defined, MethodParams& params, size_t next_param_index, const char *msg) { if(next_param_index+(options_defined?1:0) != params.count()) @@ -162,6 +178,34 @@ static void _create(Request& r, MethodPa GET_SELF(r, VTable).set_table(table); } +inline char* remove_encloser(char* cstr, char encloser) { + if(cstr[0]!=encloser) + return cstr; + + size_t length=strlen(cstr); + if(length<2 || cstr[length-1]!=encloser) + return cstr; + + // 'string' + + cstr[length-1]=0; + cstr++; + + // double-encloser stands for encloser + char *read; + char *write; + write=read=cstr; + while(char c=*read++) { + if(c==encloser && *read==encloser) + read++; + + *write++=c; + } + *write=0; // terminate + + return cstr; +} + static void _load(Request& r, MethodParams& params) { const String& first_param=params.as_string(0, "file name must be string"); int filename_param_index=0; @@ -169,12 +213,39 @@ static void _load(Request& r, MethodPara if(nameless) filename_param_index++; size_t options_param_index=filename_param_index+1; - + + HashStringValue *options=0; + char column_separator='\t'; + char column_encloser=0; + if(options_param_indexget(COLUMN_SEPARATOR_NAME)) { + options->remove(COLUMN_SEPARATOR_NAME); + const String& sseparator=vseparator->as_string(); + if(sseparator.length()!=1) + throw Exception("parser.runtime", + &sseparator, + "separator must be one character long"); + column_separator=sseparator.first_char(); + } + if(Value* vencloser=options->get(COLUMN_ENCLOSER_NAME)) { + options->remove(COLUMN_ENCLOSER_NAME); + const String& sencloser=vencloser->as_string(); + if(sencloser.length()!=1) + throw Exception("parser.runtime", + &sencloser, + "encloser must be one character long"); + column_encloser=sencloser.first_char(); + } + } + // loading text char *data=file_read_text(r.charsets, r.absolute(params.as_string(filename_param_index, "file name must be string")), true, - options_param_indexget_table(); else { // no? creating table of same structure as source - Table::Action_options table_options; + Table::Action_options table_options(0, 0); table=new Table(*info->table, table_options/*no rows, just structure*/); info->hash->put(*key, new VTable(table)); } @@ -460,7 +539,7 @@ static void _hash(Request& r, MethodPara { - Row_info info; + Row_info info={0}; info.r=&r; info.table=&self_table; Value* key_param=¶ms[0]; @@ -549,13 +628,13 @@ static void _sort(Request& r, MethodPara } #ifndef DOXYGEN -struct Locate_expression_func_info { +struct Expression_is_true_info { Request* r; Value* expression_code; }; #endif -bool locate_expression_func(Table& self, void* ainfo) { - Locate_expression_func_info& info=*static_cast(ainfo); +static bool expression_is_true(Table& self, void* ainfo) { + Expression_is_true_info& info=*static_cast(ainfo); return info.r->process_to_value(*info.expression_code).as_bool(); } static bool _locate_expression(Table& table, Table::Action_options o, @@ -564,8 +643,8 @@ static bool _locate_expression(Table& ta "locate by expression only has parameters: expression and, maybe, options"); Value& expression_code=params.as_junction(0, "must be expression"); - Locate_expression_func_info info={&r, &expression_code}; - return table.locate(locate_expression_func, &info, o); + Expression_is_true_info info={&r, &expression_code}; + return table.table_first_that(expression_is_true, &info, o); } static bool _locate_name_value(Table& table, Table::Action_options o, Request& r, MethodParams& params) { @@ -589,7 +668,7 @@ static void _locate(Request& r, MethodPa static void _flip(Request& r, MethodParams& params) { Table& old_table=GET_SELF(r, VTable).table(); - Table& new_table=*new Table(old_table.columns()); + Table& new_table=*new Table(0); if(size_t old_count=old_table.count()) if(size_t old_cols=old_table[0]->count()) for(size_t column=0; columncolumns(); + size_t dest_columns_count=dest_columns->count(); + Table::element_type dest_row(new ArrayString(dest_columns_count)); + for(size_t dest_column=0; dest_columnget(dest_column)); + *dest_row+=src_item?src_item:new String; + } + *dest+=dest_row; +} +static void join_nameless_row(Table& src, Table* dest) { + *dest+=src[src.current()]; +} static void _join(Request& r, MethodParams& params) { Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table(); if(!maybe_src) @@ -635,27 +727,10 @@ static void _join(Request& r, MethodPara 0, "source and destination are same table"); - if(Table::columns_type dest_columns=dest.columns()) { // dest is named - size_t saved_src_current=src.current(); - size_t m=src.count()-o.offset; - if(!o.limit || o.limit>m) - o.limit=m; - int end=o.offset+o.limit; - for(int src_row=o.offset; src_rowcount(); dest_column++) { - const String* src_item=src.item(*dest_columns->get(dest_column)); - *dest_row+=src_item?src_item:new String; - } - dest+=dest_row; - } - src.set_current(saved_src_current); - } else { // dest is nameless - size_t src_count=src.count(); - for(size_t src_row=0; src_row