Diff for /parser3/src/classes/table.C between versions 1.375 and 1.385

version 1.375, 2024/12/06 22:03:58 version 1.385, 2026/11/28 22:13:43
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 404  static void skip_empty_lines( char** dat Line 404  static void skip_empty_lines( char** dat
 typedef void (*Skip_lines_action)(char** data_ref);  typedef void (*Skip_lines_action)(char** data_ref);
   
 static void _load(Request& r, MethodParams& params) {  static void _load(Request& r, MethodParams& params) {
         const String&  first_param=params.as_string(0, FILE_NAME_MUST_BE_STRING);          const String&  first_param=params.as_file_name(0);
         int filename_param_index=0;          int filename_param_index=0;
         bool nameless=first_param=="nameless";          bool nameless=first_param=="nameless";
         if(nameless)          if(nameless)
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_string(filename_param_index, FILE_NAME_MUST_BE_STRING)), 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 624  static void table_to_csv(String& result, Line 624  static void table_to_csv(String& result,
   
   
 static void _save(Request& r, MethodParams& params) {  static void _save(Request& r, MethodParams& params) {
         const String& first_arg=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);          const String& first_arg=params.as_string(0, PARAMETER_MUST_BE_STRING);
         size_t param_index=1;          size_t param_index=1;
   
         bool do_append=false;          bool do_append=false;
Line 638  static void _save(Request& r, MethodPara Line 638  static void _save(Request& r, MethodPara
         else          else
                 --param_index;                  --param_index;
   
         const String& file_name=params.as_string(param_index++, FILE_NAME_MUST_BE_STRING);          const String& file_name=params.as_file_name(param_index++);
         String file_spec=r.full_disk_path(file_name);          String file_spec=r.full_disk_path(file_name);
   
         if(do_append && file_exist(file_spec))          if(do_append && file_exist(file_spec))
Line 683  static void _csv_string(Request& r, Meth Line 683  static void _csv_string(Request& r, Meth
         bool output_column_names=true;          bool output_column_names=true;
         size_t param_index=0;          size_t param_index=0;
         if(params.count()>0 && params[0].is_string()) {          if(params.count()>0 && params[0].is_string()) {
                 if(params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE)=="nameless") {                  if(params.as_string(0, PARAM_MUST_NOT_BE_CODE)=="nameless") {
                         output_column_names=false;                          output_column_names=false;
                         param_index++;                          param_index++;
                 } else {                  } else {
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 1308  static void _append(Request& r, MethodPa Line 1307  static void _append(Request& r, MethodPa
         } else {          } else {
                 table+=row_from_string(r, params[0]);                  table+=row_from_string(r, params[0]);
         }          }
   
           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 1320  static void _insert(Request& r, MethodPa Line 1322  static void _insert(Request& r, MethodPa
         } else {          } else {
                 table.insert(table.current(), row_from_string(r, params[0]));                  table.insert(table.current(), row_from_string(r, params[0]));
         }          }
   
           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 1444  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 1567  static void _select(Request& r, MethodPa Line 1572  static void _select(Request& r, MethodPa
                                 if(r.check_skip_break())                                  if(r.check_skip_break())
                                         break;                                          break;
   
                                 if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result                                  if(condition && ++appended > (size_t)offset && source_table.valid(row)) // ...condition is true, adding to the result
                                         result_table+=source_table[row];                                          result_table+=source_table[row];
                                 if(row==0) break;                                  if(row==0) break;
                         }                          }
Line 1580  static void _select(Request& r, MethodPa Line 1585  static void _select(Request& r, MethodPa
                                 if(r.check_skip_break())                                  if(r.check_skip_break())
                                         break;                                          break;
   
                                 if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result                                  if(condition && ++appended > (size_t)offset && source_table.valid(row)) // ...condition is true, adding to the result
                                         result_table+=source_table[row];                                          result_table+=source_table[row];
                         }                          }
                 }                  }

Removed from v.1.375  
changed lines
  Added in v.1.385


E-mail: