Diff for /parser3/src/classes/table.C between versions 1.103 and 1.112

version 1.103, 2001/08/10 07:13:07 version 1.112, 2001/09/18 16:05:42
Line 7 Line 7
 */  */
 static const char *RCSId="$Id$";   static const char *RCSId="$Id$"; 
   
 #include "pa_config_includes.h"  
   
 #include "pcre.h"  
   
 #include "classes.h"  #include "classes.h"
   #include "pa_config_includes.h"
 #include "pa_common.h"  #include "pa_common.h"
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_vtable.h"  #include "pa_vtable.h"
 #include "pa_vint.h"  #include "pa_vint.h"
 #include "pa_sql_connection.h"  #include "pa_sql_connection.h"
 #include "pa_dir.h"  
 #include "pa_vbool.h"  #include "pa_vbool.h"
   
 // defines  // defines
Line 84  static void _set(Request& r, const Strin Line 80  static void _set(Request& r, const Strin
 static void _load(Request& r, const String& method_name, MethodParams *params) {  static void _load(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         // filename is last parameter          // filename is last parameter
         Value& vfilename=params->as_no_junction(params->size()-1,           const String& filename=params->as_string(params->size()-1, 
                 "file name must not be code");                  "file name must not be code");
   
         // loading text          // loading text
         char *data=file_read_text(pool, r.absolute(vfilename.as_string()));          char *data=file_read_text(pool, r.absolute(filename));
   
         // parse columns          // parse columns
         Array *columns;          Array *columns;
Line 122  static void _load(Request& r, const Stri Line 118  static void _load(Request& r, const Stri
                         cell->APPEND_TAINTED(cell_chars, 0, file, line);                          cell->APPEND_TAINTED(cell_chars, 0, file, line);
                         *row+=cell;                          *row+=cell;
                 }                  }
   #ifndef NO_STRING_ORIGIN
                 line++;                  line++;
   #endif
                 table+=row;                  table+=row;
         };          };
   
Line 156  static Table *fill_month_days(Request& r Line 154  static Table *fill_month_days(Request& r
         for(int wday=0; wday<7; wday++, _day++) {          for(int wday=0; wday<7; wday++, _day++) {
                 String *cell=new(pool) String(pool);                  String *cell=new(pool) String(pool);
                         if(_day>=1 && _day<=monthDays) {                          if(_day>=1 && _day<=monthDays) {
                                 char *buf=(char *)malloc(2+1);                                   char *buf=(char *)pool.malloc(2+1); 
                                 cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day),                                   cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), 
                                         method_name.origin().file, method_name.origin().line);                                          method_name.origin().file, method_name.origin().line);
             }              }
Line 200  static Table *fill_week_days(Request& r, Line 198  static Table *fill_week_days(Request& r,
         tm *tmOut=localtime(&t);          tm *tmOut=localtime(&t);
                 Array& row=*new(pool) Array(pool, 4);                  Array& row=*new(pool) Array(pool, 4);
 #define WDFILL(size, value) { \  #define WDFILL(size, value) { \
                 char *buf=(char *)malloc(size+1); \                  char *buf=(char *)pool.malloc(size+1); \
                 String *cell=new(pool) String(pool); \                  String *cell=new(pool) String(pool); \
                 cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \                  cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
                         method_name.origin().file, \                          method_name.origin().file, \
Line 265  static void _save(Request& r, const Stri Line 263  static void _save(Request& r, const Stri
                 } else { // nameless table                  } else { // nameless table
                         if(int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0)                          if(int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0)
                                 for(int column=0; column<lsize; column++) {                                  for(int column=0; column<lsize; column++) {
                                         char *cindex_tab=(char *)malloc(MAX_NUMBER);                                          char *cindex_tab=(char *)pool.malloc(MAX_NUMBER);
                                         snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);                                          snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
                                         sdata.APPEND_CONST(cindex_tab);                                          sdata.APPEND_CONST(cindex_tab);
                                 }                                  }
Line 344  static void _menu(Request& r, const Stri Line 342  static void _menu(Request& r, const Stri
         table.set_current(saved_current);          table.set_current(saved_current);
 }  }
   
 static void _empty(Request& r, const String& method_name, MethodParams *params) {  #ifndef DOXYGEN
         Pool& pool=r.pool();  
         Table& table=static_cast<VTable *>(r.self)->table();  
           
         r.write_no_lang(*new(pool) VBool(pool, table.size()==0));  
 }  
   
 /// used by table: _hash / table_row_to_hash  
 struct Row_info {  struct Row_info {
         Table *table;          Table *table;
         int key_field;          int key_field;
         Array *value_fields;          Array *value_fields;
         Hash *hash;          Hash *hash;
 };  };
   #endif
 static void table_row_to_hash(Array::Item *value, void *info) {  static void table_row_to_hash(Array::Item *value, void *info) {
         Array& row=*static_cast<Array *>(value);          Array& row=*static_cast<Array *>(value);
         Row_info& ri=*static_cast<Row_info *>(info);          Row_info& ri=*static_cast<Row_info *>(info);
Line 412  static void _hash(Request& r, const Stri Line 404  static void _hash(Request& r, const Stri
         r.write_no_lang(result);          r.write_no_lang(result);
 }  }
   
 /// used by table: _sort / sort_cmp_string|sort_cmp_double  #ifndef DOXYGEN
 struct Table_seq_item {  struct Table_seq_item {
         Array *row;          Array *row;
         union {          union {
Line 420  struct Table_seq_item { Line 412  struct Table_seq_item {
                 double d;                  double d;
         } value;          } value;
 };  };
   #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 strcmp(
                 static_cast<const Table_seq_item *>(a)->value.c_str,                   static_cast<const Table_seq_item *>(a)->value.c_str, 
Line 442  static void _sort(Request& r, const Stri Line 435  static void _sort(Request& r, const Stri
   
         bool reverse=params->size()==2/*..[desc|asc|]*/?          bool reverse=params->size()==2/*..[desc|asc|]*/?
                 reverse=params->as_no_junction(1, "order must not be code").as_string()=="desc":                  reverse=params->as_no_junction(1, "order must not be code").as_string()=="desc":
                 true;                  false; // default=asc
   
         Table& old_table=static_cast<VTable *>(r.self)->table();          Table& old_table=static_cast<VTable *>(r.self)->table();
         Table& new_table=*new(pool) Table(pool, &method_name, old_table.columns());          Table& new_table=*new(pool) Table(pool, &method_name, old_table.columns());
Line 619  static void _sql(Request& r, const Strin Line 612  static void _sql(Request& r, const Strin
         ulong limit=0;          ulong limit=0;
         ulong offset=0;          ulong offset=0;
         if(params->size()>1) {          if(params->size()>1) {
                 Value& options_param=params->as_no_junction(1, "options must be hash, not code");                  Value& voptions=params->as_no_junction(1, "options must be hash, not code");
                 if(Hash *options=options_param.get_hash()) {                  if(voptions.is_defined())
                         if(Value *vlimit=(Value *)options->get(*sql_limit_name))                          if(Hash *options=voptions.get_hash()) {
                                 limit=(ulong)r.process(*vlimit).as_double();                                  if(Value *vlimit=(Value *)options->get(*sql_limit_name))
                         if(Value *voffset=(Value *)options->get(*sql_offset_name))                                          limit=(ulong)r.process(*vlimit).as_double();
                                 offset=(ulong)r.process(*voffset).as_double();                                  if(Value *voffset=(Value *)options->get(*sql_offset_name))
                 } else                                          offset=(ulong)r.process(*voffset).as_double();
                         PTHROW(0, 0,                          } else
                                 &method_name,                                  PTHROW(0, 0,
                                 "options must be hash");                                          &method_name,
                                           "options must be hash");
         }          }
   
         Temp_lang temp_lang(r, String::UL_SQL);          Temp_lang temp_lang(r, String::UL_SQL);
Line 638  static void _sql(Request& r, const Strin Line 632  static void _sql(Request& r, const Strin
         Table_sql_event_handlers handlers(pool, method_name,          Table_sql_event_handlers handlers(pool, method_name,
                 statement_string, statement_cstr);                  statement_string, statement_cstr);
         bool need_rethrow=false; Exception rethrow_me;          bool need_rethrow=false; Exception rethrow_me;
         PTRY {                  PTRY {
                 r.connection->query(                  r.connection->query(
                         statement_cstr, offset, limit,                           statement_cstr, offset, limit, 
                         handlers);                          handlers);
Line 660  static void _sql(Request& r, const Strin Line 654  static void _sql(Request& r, const Strin
         static_cast<VTable *>(r.self)->set_table(*result);          static_cast<VTable *>(r.self)->set_table(*result);
 }  }
   
 static void _dir(Request& r, const String& method_name, MethodParams *params) {  
         Pool& pool=r.pool();  
   
         Value& relative_path=params->as_no_junction(0, "path must not be code");  
   
         const String *regexp;  
         pcre *regexp_code;  
         int ovecsize;  
         int *ovector;  
         if(params->size()>1) {  
                 regexp=&params->as_no_junction(1, "regexp must not be code").as_string();  
   
                 const char *pattern=regexp->cstr(String::UL_AS_IS);  
                 const char *errptr;  
                 int erroffset;  
                 regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL,   
                         &errptr, &erroffset,   
                         r.pcre_tables);  
   
                 if(!regexp_code)  
                         PTHROW(0, 0,   
                                 &regexp->mid(erroffset, regexp->size()),   
                                 "regular expression syntax error - %s", errptr);  
   
                 ovector=(int *)malloc(sizeof(int)*(ovecsize=(1/*match*/)*3));  
         } else   
                 regexp_code=0;  
   
   
         const char* absolute_path_cstr=r.absolute(relative_path.as_string())  
                 .cstr(String::UL_FILE_NAME);  
   
         Array& columns=*new(pool) Array(pool);  
         columns+=new(pool) String(pool, "name");          
         Table& table=*new(pool) Table(pool, &method_name, &columns);  
   
         LOAD_DIR(absolute_path_cstr,   
                 size_t file_name_size=strlen(ffblk.ff_name);  
                 bool suits=true;  
                 if(regexp_code) {  
                         int exec_result=pcre_exec(regexp_code, 0,   
                                 ffblk.ff_name, file_name_size, 0,   
                                 0, ovector, ovecsize);  
                           
                         if(exec_result==PCRE_ERROR_NOMATCH)  
                                 suits=false;  
                         else if(exec_result<0) {  
                                 (*pcre_free)(regexp_code);  
                                 PTHROW(0, 0,   
                                         regexp,   
                                         "regular expression execute (%d)",   
                                                 exec_result);  
                         }  
                 }  
   
                 if(suits) {  
                         char *file_name_cstr=(char *)r.malloc(file_name_size);  
                         memcpy(file_name_cstr, ffblk.ff_name, file_name_size);  
                         String &file_name=*new(pool) String(pool);  
                         file_name.APPEND(file_name_cstr, file_name_size, String::UL_FILE_NAME,   
                                 method_name.origin().file, method_name.origin().line);  
                   
                         Array& row=*new(pool) Array(pool);  
                         row+=&file_name;  
                         table+=&row;  
                 }  
         );  
   
         if(regexp_code)  
                 (*pcre_free)(regexp_code);  
   
         // replace any previous table value  
         static_cast<VTable *>(r.self)->set_table(table);  
 }  
   
 static void _columns(Request& r, const String& method_name, MethodParams *) {  static void _columns(Request& r, const String& method_name, MethodParams *) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
   
Line 792  MTable::MTable(Pool& apool) : Methoded(a Line 711  MTable::MTable(Pool& apool) : Methoded(a
         // ^table.menu{code}[delim]          // ^table.menu{code}[delim]
         add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);          add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
   
         // ^table.empty[]  
         add_native_method("empty", Method::CT_DYNAMIC, _empty, 0, 0);  
   
         // ^table:hash[key field name]          // ^table:hash[key field name]
         // ^table:hash[key field name][value field name;...]          // ^table:hash[key field name][value field name;...]
         add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000);          add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000);
Line 820  MTable::MTable(Pool& apool) : Methoded(a Line 736  MTable::MTable(Pool& apool) : Methoded(a
         // ^table:sql[query][$.limit(1) $.offset(2)]          // ^table:sql[query][$.limit(1) $.offset(2)]
         add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);          add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
   
         // ^table:dir[path]  
         // ^table:dir[path][regexp]  
         add_native_method("dir", Method::CT_DYNAMIC, _dir, 1, 2);  
   
         // ^table:columns[]          // ^table:columns[]
         add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);          add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
 }  }

Removed from v.1.103  
changed lines
  Added in v.1.112


E-mail: