|
|
| version 1.124, 2002/12/09 12:19:16 | version 1.125.2.4, 2003/02/04 14:12:42 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b string parser class. | Parser: @b string parser class. |
| Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_STRING_C="$Date$"; | static const char* IDENT_STRING_C="$Date$"; |
| #include "classes.h" | #include "classes.h" |
| #include "pa_vmethod_frame.h" | |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vdouble.h" | #include "pa_vdouble.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| Line 29 public: // Methoded | Line 31 public: // Methoded |
| // methods | // methods |
| static void _length(Request& r, const String& method_name, MethodParams *) { | static void _length(Request& r, StringPtr method_name, MethodParams& ) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| double result=r.get_self()->get_string()->size(); | double result=r.get_self()->get_string()->size(); |
| r.write_no_lang(*new(pool) VDouble(pool, result)); | r.write_no_lang(*new(pool) VDouble(pool, result)); |
| } | } |
| static void _int(Request& r, const String& method_name, MethodParams *params) { | static void _int(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| const String *self_string=r.get_self()->get_string(); | const String *self_string=r.get_self()->get_string(); |
| int converted; | int converted; |
| Value *default_code=params->size()>0?¶ms->as_junction(0, "default must be int"):0; // (default) | Value *default_code=params.count()>0?¶ms.as_junction(0, "default must be int"):0; // (default) |
| try { | try { |
| if(!self_string || self_string->is_empty()) | if(!self_string || self_string->is_empty()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 48 static void _int(Request& r, const Strin | Line 50 static void _int(Request& r, const Strin |
| converted=self_string->as_int(); | converted=self_string->as_int(); |
| } catch(...) { // convert problem | } catch(...) { // convert problem |
| if(!default_code) // we have a problem when no default | if(!default_code) // we have a problem when no default |
| /*re*/throw; | rethrow; |
| else | else |
| converted=r.process_to_value(*default_code).as_int(); | converted=r.process_to_value(*default_code).as_int(); |
| } | } |
| r.write_no_lang(*new(pool) VInt(pool, converted)); | r.write_no_lang(*new(pool) VInt(pool, converted)); |
| } | } |
| static void _double(Request& r, const String& method_name, MethodParams *params) { | static void _double(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| const String *self_string=r.get_self()->get_string(); | const String *self_string=r.get_self()->get_string(); |
| double converted; | double converted; |
| Value *default_code=params->size()>0?¶ms->as_junction(0, "default must be double"):0; // (default) | Value *default_code=params.count()>0?¶ms.as_junction(0, "default must be double"):0; // (default) |
| try { | try { |
| if(!self_string || self_string->is_empty()) | if(!self_string || self_string->is_empty()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 68 static void _double(Request& r, const St | Line 70 static void _double(Request& r, const St |
| converted=self_string->as_double(); | converted=self_string->as_double(); |
| } catch(...) { // convert problem | } catch(...) { // convert problem |
| if(!default_code) // we have a problem when no default | if(!default_code) // we have a problem when no default |
| /*re*/throw; | rethrow; |
| else | else |
| converted=r.process_to_value(*default_code).as_double(); | converted=r.process_to_value(*default_code).as_double(); |
| } | } |
| Line 76 static void _double(Request& r, const St | Line 78 static void _double(Request& r, const St |
| r.write_no_lang(*new(pool) VDouble(pool, converted)); | r.write_no_lang(*new(pool) VDouble(pool, converted)); |
| } | } |
| /*not static*/void _string_format(Request& r, const String& method_name, MethodParams *params) { | /*not static*/void _string_format(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& fmt_maybe_code=params->get(0); | Value& fmt_maybe_code=params.get(0); |
| // for some time due to stupid {} in original design | // for some time due to stupid {} in original design |
| const String& fmt=r.process_to_string(fmt_maybe_code); | const String& fmt=r.process_to_string(fmt_maybe_code); |
| Line 92 static void _double(Request& r, const St | Line 94 static void _double(Request& r, const St |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| static void _left(Request& r, const String&, MethodParams *params) { | static void _left(Request& r, StringPtr /*method_name*/, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| size_t n=(size_t)params->as_int(0, "n must be int", r); | size_t n=(size_t)params.as_int(0, "n must be int", r); |
| const String& string=static_cast<VString *>(r.get_self())->string(); | const String& string=static_cast<VString *>(r.get_self())->string(); |
| r.write_assign_lang(string.mid(0, n)); | r.write_assign_lang(string.mid(0, n)); |
| } | } |
| static void _right(Request& r, const String&, MethodParams *params) { | static void _right(Request& r, StringPtr /*method_name*/, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| size_t n=(size_t)params->as_int(0, "n must be int", r); | size_t n=(size_t)params.as_int(0, "n must be int", r); |
| const String& string=static_cast<VString *>(r.get_self())->string(); | const String& string=static_cast<VString *>(r.get_self())->string(); |
| r.write_assign_lang(string.mid(string.size()-n, string.size())); | r.write_assign_lang(string.mid(string.size()-n, string.size())); |
| } | } |
| static void _mid(Request& r, const String&, MethodParams *params) { | static void _mid(Request& r, StringPtr /*method_name*/, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| const String& string=*r.get_self()->get_string(); | const String& string=*r.get_self()->get_string(); |
| size_t p=(size_t)max(0, params->as_int(0, "p must be int", r)); | size_t p=(size_t)max(0, params.as_int(0, "p must be int", r)); |
| size_t n=params->size()>1? | size_t n=params.count()>1? |
| (size_t)max(0, params->as_int(1, "n must be int", r)):string.size(); | (size_t)max(0, params.as_int(1, "n must be int", r)):string.size(); |
| r.write_assign_lang(string.mid(p, p+n)); | r.write_assign_lang(string.mid(p, p+n)); |
| } | } |
| static void _pos(Request& r, const String& method_name, MethodParams *params) { | static void _pos(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& substr=params->as_no_junction(0, "substr must not be code"); | Value& substr=params.as_no_junction(0, "substr must not be code"); |
| const String& string=static_cast<VString *>(r.get_self())->string(); | const String& string=static_cast<VString *>(r.get_self())->string(); |
| r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string()))); | r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string()))); |
| Line 134 static void split_list(Request& r, const | Line 136 static void split_list(Request& r, const |
| MethodParams *params, int paramIndex, | MethodParams *params, int paramIndex, |
| const String& string, | const String& string, |
| Array& result) { | Array& result) { |
| Value& delim_value=params->as_no_junction(paramIndex, "delimiter must not be code"); | Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code"); |
| string.split(result, 0, delim_value.as_string()); | string.split(result, 0, delim_value.as_string()); |
| } | } |
| Line 146 static void split_list(Request& r, const | Line 148 static void split_list(Request& r, const |
| static int split_options(const String *options) { | static int split_options(const String *options) { |
| struct Split_option { | struct Split_option { |
| const char *keyL; | const char* keyL; |
| const char *keyU; | const char* keyU; |
| int setBit; | int setBit; |
| int checkBit; | int checkBit; |
| } split_option[]={ | } split_option[]={ |
| Line 220 static Table *split_horizontal(Request& | Line 222 static Table *split_horizontal(Request& |
| return &table; | return &table; |
| } | } |
| static void split_with_options(Request& r, const String& method_name, MethodParams *params, | static void split_with_options(Request& r, StringPtr method_name, MethodParams& params, |
| int bits) { | int bits) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| const String& string=*r.get_self()->get_string(); | const String& string=*r.get_self()->get_string(); |
| Line 231 static void split_with_options(Request& | Line 233 static void split_with_options(Request& |
| if(!bits) { | if(!bits) { |
| const String *options=0; | const String *options=0; |
| if(params->size()>1) { | if(params.count()>1) { |
| options=¶ms->as_string(1, "options must not be code"); | options=¶ms.as_string(1, "options must not be code"); |
| } | } |
| bits=split_options(options); | bits=split_options(options); |
| } | } |
| Line 247 static void split_with_options(Request& | Line 249 static void split_with_options(Request& |
| r.write_no_lang(*new(pool) VTable(pool, table)); | r.write_no_lang(*new(pool) VTable(pool, table)); |
| } | } |
| static void _split(Request& r, const String& method_name, MethodParams *params) { | static void _split(Request& r, StringPtr method_name, MethodParams& params) { |
| split_with_options(r, method_name, params, 0 /* maybe-determine from param #2 */); | split_with_options(r, method_name, params, 0 /* maybe-determine from param #2 */); |
| } | } |
| static void _lsplit(Request& r, const String& method_name, MethodParams *params) { | static void _lsplit(Request& r, StringPtr method_name, MethodParams& params) { |
| split_with_options(r, method_name, params, SPLIT_LEFT); | split_with_options(r, method_name, params, SPLIT_LEFT); |
| } | } |
| static void _rsplit(Request& r, const String& method_name, MethodParams *params) { | static void _rsplit(Request& r, StringPtr method_name, MethodParams& params) { |
| split_with_options(r, method_name, params, SPLIT_RIGHT); | split_with_options(r, method_name, params, SPLIT_RIGHT); |
| } | } |
| Line 295 static void replace_action(Table& table, | Line 297 static void replace_action(Table& table, |
| } | } |
| /// @todo use pcre:study somehow | /// @todo use pcre:study somehow |
| static void _match(Request& r, const String& method_name, MethodParams *params) { | static void _match(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& regexp=params->as_no_junction(0, "regexp must not be code"); | Value& regexp=params.as_no_junction(0, "regexp must not be code"); |
| const String *options= | const String *options= |
| params->size()>1? | params.count()>1? |
| ¶ms->as_no_junction(1, "options must not be code").as_string():0; | ¶ms.as_no_junction(1, "options must not be code").as_string():0; |
| Temp_lang temp_lang(r, String::UL_PASS_APPENDED); | Temp_lang temp_lang(r, String::UL_PASS_APPENDED); |
| Table *table; | Table *table; |
| if(params->size()<3) { // search | if(params.count()<3) { // search |
| const String& src=static_cast<VString *>(r.get_self())->string(); | const String& src=static_cast<VString *>(r.get_self())->string(); |
| bool was_global; | bool was_global; |
| Line 326 static void _match(Request& r, const Str | Line 328 static void _match(Request& r, const Str |
| } else { // replace | } else { // replace |
| const String& src=*r.get_self()->get_string(); | const String& src=*r.get_self()->get_string(); |
| Value& replacement_code=params->as_junction(2, "replacement param must be code"); | Value& replacement_code=params.as_junction(2, "replacement param must be code"); |
| String& result=*new(pool) String(pool); | String& result=*new(pool) String(pool); |
| VTable vtable(pool); | VTable vtable(pool); |
| Line 348 static void _match(Request& r, const Str | Line 350 static void _match(Request& r, const Str |
| } | } |
| } | } |
| static void change_case(Request& r, const String& method_name, MethodParams *params, | static void change_case(Request& r, StringPtr method_name, MethodParams& params, |
| String::Change_case_kind kind) { | String::Change_case_kind kind) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| const String& src=static_cast<VString *>(r.get_self())->string(); | const String& src=static_cast<VString *>(r.get_self())->string(); |
| r.write_assign_lang(src.change_case(pool, kind)); | r.write_assign_lang(src.change_case(pool, kind)); |
| } | } |
| static void _upper(Request& r, const String& method_name, MethodParams *params) { | static void _upper(Request& r, StringPtr method_name, MethodParams& params) { |
| change_case(r, method_name, params, String::CC_UPPER); | change_case(r, method_name, params, String::CC_UPPER); |
| } | } |
| static void _lower(Request& r, const String& method_name, MethodParams *params) { | static void _lower(Request& r, StringPtr method_name, MethodParams& params) { |
| change_case(r, method_name, params, String::CC_LOWER); | change_case(r, method_name, params, String::CC_LOWER); |
| } | } |
| Line 366 static void _lower(Request& r, const Str | Line 368 static void _lower(Request& r, const Str |
| class String_sql_event_handlers : public SQL_Driver_query_event_handlers { | class String_sql_event_handlers : public SQL_Driver_query_event_handlers { |
| public: | public: |
| String_sql_event_handlers(Pool& apool, | String_sql_event_handlers(Pool& apool, |
| const String& astatement_string, const char *astatement_cstr): | const String& astatement_string, const char* astatement_cstr): |
| pool(apool), | pool(apool), |
| statement_string(astatement_string), | statement_string(astatement_string), |
| statement_cstr(astatement_cstr), | statement_cstr(astatement_cstr), |
| Line 396 public: | Line 398 public: |
| try { | try { |
| got_cell=true; | got_cell=true; |
| result->APPEND_TAINTED((const char *)ptr, size, statement_cstr, 0); | result->APPEND_TAINTED((const char* )ptr, size, statement_cstr, 0); |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell"); | error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell"); |
| Line 406 public: | Line 408 public: |
| private: | private: |
| Pool& pool; | Pool& pool; |
| const String& statement_string; const char *statement_cstr; | const String& statement_string; const char* statement_cstr; |
| bool got_column; | bool got_column; |
| public: | public: |
| bool got_cell; | bool got_cell; |
| String *result; | String *result; |
| }; | }; |
| #endif | #endif |
| const String* sql_result_string(Request& r, const String& method_name, MethodParams *params, | const String* sql_result_string(Request& r, StringPtr method_name, MethodParams& params, |
| Hash *& options, Value *& default_code) { | Hash *& options, Value *& default_code) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& statement=params->as_junction(0, "statement must be code"); | Value& statement=params.as_junction(0, "statement must be code"); |
| ulong limit=0; | ulong limit=0; |
| ulong offset=0; | ulong offset=0; |
| default_code=0; | default_code=0; |
| if(params->size()>1) { | if(params.count()>1) { |
| Value& voptions=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(!voptions.is_string()) | if(!voptions.is_string()) |
| if(options=voptions.get_hash(&method_name)) { | if(options=voptions.get_hash(&method_name)) { |
| if(Value *vlimit=(Value *)options->get(*sql_limit_name)) | if(Value *vlimit=(Value *)options->get(*sql_limit_name)) |
| Line 447 const String* sql_result_string(Request& | Line 449 const String* sql_result_string(Request& |
| Temp_lang temp_lang(r, String::UL_SQL); | Temp_lang temp_lang(r, String::UL_SQL); |
| const String& statement_string=r.process_to_string(statement); | const String& statement_string=r.process_to_string(statement); |
| const char *statement_cstr= | const char* statement_cstr= |
| statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); | statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); |
| String_sql_event_handlers handlers(pool, statement_string, statement_cstr); | String_sql_event_handlers handlers(pool, statement_string, statement_cstr); |
| r.connection(&method_name)->query( | r.connection(&method_name)->query( |
| Line 461 const String* sql_result_string(Request& | Line 463 const String* sql_result_string(Request& |
| return handlers.result; | return handlers.result; |
| } | } |
| static void _sql(Request& r, const String& method_name, MethodParams *params) { | static void _sql(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Hash *options; | Hash *options; |
| Line 481 static void _sql(Request& r, const Strin | Line 483 static void _sql(Request& r, const Strin |
| r.write_assign_lang(*string); | r.write_assign_lang(*string); |
| } | } |
| static void _replace(Request& r, const String& method_name, MethodParams *params) { | static void _replace(Request& r, StringPtr method_name, MethodParams& params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| const String& src=*r.get_self()->get_string(); | const String& src=*r.get_self()->get_string(); |
| Table *table=params->as_no_junction(0, "parameter must not be code").get_table(); | Table *table=params.as_no_junction(0, "parameter must not be code").get_table(); |
| if(!table) | if(!table) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &method_name, | &method_name, |
| Line 495 static void _replace(Request& r, const S | Line 497 static void _replace(Request& r, const S |
| r.write_assign_lang(src.replace(pool, dict)); | r.write_assign_lang(src.replace(pool, dict)); |
| } | } |
| static void _save(Request& r, const String& method_name, MethodParams *params) { | static void _save(Request& r, StringPtr method_name, MethodParams& params) { |
| const String& file_name=params->as_string(params->size()-1, | const String& file_name=params.as_string(params.count()-1, |
| "file name must be string"); | "file name must be string"); |
| const String& src=static_cast<VString *>(r.get_self())->string(); | const String& src=static_cast<VString *>(r.get_self())->string(); |
| bool do_append=false; | bool do_append=false; |
| if(params->size()>1) { | if(params.count()>1) { |
| const String& mode=params->as_string(0, "mode must be string"); | const String& mode=params.as_string(0, "mode must be string"); |
| if(mode=="append") | if(mode=="append") |
| do_append=true; | do_append=true; |
| else | else |
| Line 513 static void _save(Request& r, const Stri | Line 515 static void _save(Request& r, const Stri |
| } | } |
| // write | // write |
| const char *buf=src.cstr(String::UL_UNSPECIFIED, r.connection(0/*no error if none*/)); | const char* buf=src.cstr(String::UL_UNSPECIFIED, r.connection(0/*no error if none*/)); |
| file_write(r.absolute(file_name), | file_write(r.absolute(file_name), |
| buf, strlen(buf), true, do_append); | buf, strlen(buf), true, do_append); |
| } | } |
| static void _normalize(Request& r, const String& method_name, MethodParams * /*params*/) { | static void _normalize(Request& r, StringPtr method_name, MethodParams& /*params*/) { |
| r.write_assign_lang(r.get_self()->get_string()->join_chains(r.pool(), 0/*cstr*/)); | r.write_assign_lang(r.get_self()->get_string()->join_chains(r.pool(), 0/*cstr*/)); |
| } | } |