|
|
| version 1.381, 2025/05/27 15:10:24 | 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 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 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); |