|
|
| version 1.378, 2024/12/11 15:46:38 | version 1.386, 2026/11/29 13:29:18 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b table parser class. | Parser: @b table parser class. |
| Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| Line 417 static void _load(Request& r, MethodPara | Line 417 static void _load(Request& r, MethodPara |
| control_chars.load(*options); | control_chars.load(*options); |
| // loading text | // loading text |
| char *data=file_load_text(r, r.full_disk_path(params.as_file_name(filename_param_index)), true, options); | char *data=file_load_text(r, r.full_disk_path(params.as_file_name(filename_param_index), PA_ALLOW_HTTP), true, options); |
| Skip_lines_action skip_lines_action = (control_chars.separator=='#' || control_chars.encloser=='#') ? skip_empty_lines : skip_empty_and_comment_lines; | Skip_lines_action skip_lines_action = (control_chars.separator=='#' || control_chars.encloser=='#') ? skip_empty_lines : skip_empty_and_comment_lines; |
| Line 849 static void table_row_to_hash(Table::ele | Line 849 static void table_row_to_hash(Table::ele |
| for(Array_iterator<int> i(*info->value_fields); i; ) { | for(Array_iterator<int> i(*info->value_fields); i; ) { |
| size_t value_field=i.next(); | size_t value_field=i.next(); |
| if(value_field<row->count()) | if(value_field<row->count()) |
| hash.put(columns ? *columns->get(value_field) : String(pa_uitoa(value_field)), new VString(*row->get(value_field))); | hash.put(columns ? *columns->get(value_field) : String::Body::uitoa(value_field), new VString(*row->get(value_field))); |
| } | } |
| } else { // all fields | } else { // all fields |
| for(size_t index=0; index<row->count(); index++) { | for(size_t index=0; index<row->count(); index++) { |
| hash.put(columns && index < columns->count() ? *columns->get(index) : String(pa_uitoa(index)), new VString(*row->get(index))); | hash.put(columns && index < columns->count() ? *columns->get(index) : String::Body::uitoa(index), new VString(*row->get(index))); |
| } | } |
| } | } |
| exist=info->hash->put_dont_replace(*key, vhash); | exist=info->hash->put_dont_replace(*key, vhash); |
| Line 1071 static void _cells(Request& r, MethodPar | Line 1071 static void _cells(Request& r, MethodPar |
| size_t row_size=self_table[self_table.current()]->count(); // number of columns in current row | size_t row_size=self_table[self_table.current()]->count(); // number of columns in current row |
| if(params.count()){ | if(params.count()){ |
| int limit=params.as_int(params.count()-1, "offset must be expression", r); | int limit=params.as_int(params.count()-1, "limit must be expression", r); |
| if(limit<0) | if(limit<0) |
| limit=0; | limit=0; |
| if((size_t)limit<row_size) | if((size_t)limit<row_size) |
| Line 1100 struct Table_seq_item : public PA_Alloca | Line 1100 struct Table_seq_item : public PA_Alloca |
| }; | }; |
| #endif | #endif |
| static int sort_cmp_string(const void *a, const void *b) { | static int sort_cmp_string(const void *a, const void *b) { |
| return strcmp( | return Charset::strcmp_utf8( |
| static_cast<const Table_seq_item *>(a)->value.c_str, | static_cast<const Table_seq_item *>(a)->value.c_str, |
| static_cast<const Table_seq_item *>(b)->value.c_str | static_cast<const Table_seq_item *>(b)->value.c_str |
| ); | ); |
| Line 1143 static void _sort(Request& r, MethodPara | Line 1143 static void _sort(Request& r, MethodPara |
| seq[i].value.d=value.as_expr_result().as_double(); | seq[i].value.d=value.as_expr_result().as_double(); |
| } | } |
| // @todo: handle this elsewhere | if(!r.charsets.source().isUTF8() && key_values_are_strings) { // Charset::strcmp_utf8 requires UTF-8 |
| if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings) { | |
| for(i=0; i<old_count; i++) | for(i=0; i<old_count; i++) |
| if(*seq[i].value.c_str) | if(*seq[i].value.c_str) |
| seq[i].value.c_str=Charset::transcode(seq[i].value.c_str, r.charsets.source(), pa_UTF8_charset).cstr(); | seq[i].value.c_str=Charset::transcode(seq[i].value.c_str, r.charsets.source(), pa_UTF8_charset).cstr(); |
| Line 1282 static void update_cell(HashStringValue: | Line 1281 static void update_cell(HashStringValue: |
| dest->put_element(String(aname, String::L_CLEAN), avalue); // new not required | dest->put_element(String(aname, String::L_CLEAN), avalue); // new not required |
| } | } |
| inline Table::element_type row_from_string(Request& r, Value ¶m){ | inline Table::element_type row_from_string(Request& r, MethodParams& params){ |
| Value& param=params[0]; | |
| if(!param.is_string() && !param.get_junction()) | if(!param.is_string() && !param.get_junction()) |
| throw Exception(PARSER_RUNTIME, 0, "row must be string, code or hash"); | throw Exception(PARSER_RUNTIME, 0, "row must be string, code or hash"); |
| char separator[2]="\t"; | |
| char encloser=0; | |
| if(params.count()>1) | |
| if(HashStringValue* options=params.as_hash(1)) { // as in ^table::create, but '\n' is not a row delimiter | |
| TableControlChars control_chars; | |
| if(control_chars.load(*options)!=options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| separator[0]=control_chars.separator; | |
| encloser=control_chars.encloser; | |
| } | |
| const String& string=r.process_to_string(param); | const String& string=r.process_to_string(param); |
| // parse cells | // parse cells |
| Table::element_type row=new ArrayString; | Table::element_type row=new ArrayString; |
| string.split(*row, 0, "\t", String::L_AS_IS); | if(encloser) { |
| StringSplitHelper sdata(string); | |
| char *data=sdata.base; | |
| while( lsplit_sresult sr=lsplit(&data, separator, encloser, sdata) ) | |
| *row+=sr.piece; | |
| } else | |
| string.split(*row, 0, separator, String::L_AS_IS); | |
| return row; | return row; |
| } | } |
| Line 1306 static void _append(Request& r, MethodPa | Line 1323 static void _append(Request& r, MethodPa |
| table.set_current(table.count()-1); | table.set_current(table.count()-1); |
| hash->for_each<VTable*>(update_cell, &vtable); | hash->for_each<VTable*>(update_cell, &vtable); |
| } else { | } else { |
| table+=row_from_string(r, params[0]); | table+=row_from_string(r, params); |
| } | } |
| if(table.count() > pa_array_limit) | |
| throw Exception(PARSER_RUNTIME, 0, "table size (%d) exceeds limit $MAIN:LIMITS.max_array_size (%d)", table.count(), pa_array_limit); | |
| } | } |
| static void _insert(Request& r, MethodParams& params) { | static void _insert(Request& r, MethodParams& params) { |
| Line 1318 static void _insert(Request& r, MethodPa | Line 1338 static void _insert(Request& r, MethodPa |
| table.insert(table.current(), new ArrayString()); | table.insert(table.current(), new ArrayString()); |
| hash->for_each<VTable*>(update_cell, &vtable); | hash->for_each<VTable*>(update_cell, &vtable); |
| } else { | } else { |
| table.insert(table.current(), row_from_string(r, params[0])); | table.insert(table.current(), row_from_string(r, params)); |
| } | } |
| if(table.count() > pa_array_limit) | |
| throw Exception(PARSER_RUNTIME, 0, "table size (%d) exceeds limit $MAIN:LIMITS.max_array_size (%d)", table.count(), pa_array_limit); | |
| } | } |
| static void _delete(Request& r, MethodParams&) { | static void _delete(Request& r, MethodParams&) { |
| Line 1439 void unmarshal_bind_updates(HashStringVa | Line 1462 void unmarshal_bind_updates(HashStringVa |
| if(ph->is_null) | if(ph->is_null) |
| value=VVoid::get(); | value=VVoid::get(); |
| else | else |
| value=new VString(*new String(ph->value, String::L_TAINTED)); | value=new VString(ph->value); |
| hash.put(ph->name, value); | hash.put(ph->name, value); |
| } | } |
| } | } |
| Line 1695 MTable::MTable(): Methoded("table") { | Line 1718 MTable::MTable(): Methoded("table") { |
| // ^table.foreach[row-num;value]{code}[delim] | // ^table.foreach[row-num;value]{code}[delim] |
| add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 3, 4); | add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 3, 4); |
| // ^table.append{row{tab}data} | // ^table.append{row{tab}data}[options] |
| add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); | add_native_method("append", Method::CT_DYNAMIC, _append, 1, 2); |
| // ^table.insert{row{tab}data} before current row | // ^table.insert{row{tab}data}[options] before current row |
| add_native_method("insert", Method::CT_DYNAMIC, _insert, 1, 1); | add_native_method("insert", Method::CT_DYNAMIC, _insert, 1, 2); |
| // ^table.delete[] current row | // ^table.delete[] current row |
| add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 0); | add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 0); |