|
|
| version 1.142, 2005/06/06 08:45:11 | version 1.151, 2007/02/03 18:08:38 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b string parser class. | Parser: @b string parser class. |
| Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 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) |
| */ | */ |
| Line 54 static void _length(Request& r, MethodPa | Line 54 static void _length(Request& r, MethodPa |
| static void _int(Request& r, MethodParams& params) { | static void _int(Request& r, MethodParams& params) { |
| const String& self_string=GET_SELF(r, VString).string(); | const String& self_string=GET_SELF(r, VString).string(); |
| int converted; | int converted; |
| Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be int") | |
| :0; // (default) | |
| try { | try { |
| if(self_string.is_empty()) | if(self_string.is_empty()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 63 static void _int(Request& r, MethodParam | Line 61 static void _int(Request& r, MethodParam |
| "parameter is empty string, error converting"); | "parameter is empty string, error converting"); |
| converted=self_string.as_int(); | converted=self_string.as_int(); |
| } catch(...) { // convert problem | } catch(...) { // convert problem |
| if(default_code) | if(params.count()>0) |
| converted=r.process_to_value(*default_code).as_int(); | converted=params.as_int(0, "default must be int", r); // (default) |
| else | else |
| rethrow; // we have a problem when no default | rethrow; // we have a problem when no default |
| } | } |
| Line 74 static void _int(Request& r, MethodParam | Line 72 static void _int(Request& r, MethodParam |
| static void _double(Request& r, MethodParams& params) { | static void _double(Request& r, MethodParams& params) { |
| const String& self_string=GET_SELF(r, VString).string(); | const String& self_string=GET_SELF(r, VString).string(); |
| double converted; | double converted; |
| Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be double") | |
| :0; // (default) | |
| try { | try { |
| if(self_string.is_empty()) | if(self_string.is_empty()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 83 static void _double(Request& r, MethodPa | Line 79 static void _double(Request& r, MethodPa |
| "parameter is empty string, error converting"); | "parameter is empty string, error converting"); |
| converted=self_string.as_double(); | converted=self_string.as_double(); |
| } catch(...) { // convert problem | } catch(...) { // convert problem |
| if(default_code) | if(params.count()>0) |
| converted=r.process_to_value(*default_code).as_double(); | converted=params.as_double(0, "default must be double", r); // (default) |
| else | else |
| rethrow; // we have a problem when no default | rethrow; // we have a problem when no default |
| } | } |
| Line 92 static void _double(Request& r, MethodPa | Line 88 static void _double(Request& r, MethodPa |
| r.write_no_lang(*new VDouble(converted)); | r.write_no_lang(*new VDouble(converted)); |
| } | } |
| static void _bool(Request& r, MethodParams& params) { | |
| const String& self_string=GET_SELF(r, VString).string(); | |
| bool converted; | |
| try { | |
| if(self_string.is_empty()) | |
| throw Exception("parser.runtime", | |
| 0, | |
| "parameter is empty string, error converting"); | |
| converted=self_string.as_bool(); | |
| } catch(...) { // convert problem | |
| if(params.count()>0) | |
| converted=params.as_bool(0, "default must be bool", r); // (default) | |
| else | |
| rethrow; // we have a problem when no default | |
| } | |
| r.write_no_lang(*new VBool(converted)); | |
| } | |
| /*not static*/void _string_format(Request& r, MethodParams& params) { | /*not static*/void _string_format(Request& r, MethodParams& params) { |
| Value& fmt_maybe_code=params[0]; | Value& fmt_maybe_code=params[0]; |
| Line 327 static void _match(Request& r, MethodPar | Line 342 static void _match(Request& r, MethodPar |
| regexp.as_string(), options, | regexp.as_string(), options, |
| search_action, 0, | search_action, 0, |
| just_matched); | just_matched); |
| Value* result; | // Value* result; |
| if(table) | // if(table) |
| result=new VTable(table); // table of pre/match/post+substrings | // result=new VTable(table); // table of pre/match/post+substrings |
| else | // else |
| result=new VBool(just_matched); | // result=new VBool(just_matched); |
| r.write_assign_lang(*result); | // r.write_assign_lang(*result); |
| r.write_assign_lang(*new VTable(table)); | |
| } else { // replace | } else { // replace |
| Value& replacement_code=params.as_junction(2, "replacement param must be code"); | Value& replacement_code=params.as_junction(2, "replacement param must be code"); |
| Line 433 const String* sql_result_string(Request& | Line 449 const String* sql_result_string(Request& |
| default_code=0; | default_code=0; |
| if(params.count()>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_defined() && !voptions.is_string()) |
| if((options=voptions.get_hash())) { | if((options=voptions.get_hash())) { |
| int valid_options=0; | int valid_options=0; |
| if(Value* vbind=options->get(sql_bind_name)) { | if(Value* vbind=options->get(sql_bind_name)) { |
| Line 450 const String* sql_result_string(Request& | Line 466 const String* sql_result_string(Request& |
| } | } |
| if((default_code=options->get(sql_default_name))) { | if((default_code=options->get(sql_default_name))) { |
| valid_options++; | valid_options++; |
| if(!default_code->get_junction()) | |
| throw Exception("parser.runtime", | |
| 0, | |
| "default option must be code"); | |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 591 static void _append(Request& r, MethodPa | Line 603 static void _append(Request& r, MethodPa |
| va.set_string(c); | va.set_string(c); |
| } | } |
| static void _base64(Request& r, MethodParams& params) { | |
| if(params.count()) { | |
| // decode | |
| const char* cstr=params.as_string(0, "parameter must be string").cstr(); | |
| char* decoded_cstr=0; | |
| size_t decoded_size=0; | |
| pa_base64_decode(cstr, strlen(cstr), decoded_cstr, decoded_size); | |
| if(decoded_cstr && decoded_size) | |
| r.write_assign_lang(*new String(decoded_cstr, decoded_size, true)); | |
| } else { | |
| // encode | |
| VString& self=GET_SELF(r, VString); | |
| const char* cstr=self.string().cstr(); | |
| const char* encoded=pa_base64_encode(cstr, strlen(cstr)); | |
| r.write_assign_lang(*new String(encoded, 0, true/*once ?param=base64(something) was needed*/)); | |
| } | |
| } | |
| // constructor | // constructor |
| MString::MString(): Methoded("string") { | MString::MString(): Methoded("string") { |
| Line 603 MString::MString(): Methoded("string") { | Line 633 MString::MString(): Methoded("string") { |
| // ^string.double[] | // ^string.double[] |
| // ^string.double(default) | // ^string.double(default) |
| add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1); | add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1); |
| // ^void.bool[] | |
| // ^void.bool(default) | |
| add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1); | |
| // ^string.format{format} | // ^string.format{format} |
| add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1); | add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1); |
| Line 653 MString::MString(): Methoded("string") { | Line 686 MString::MString(): Methoded("string") { |
| // ^string.append[string] | // ^string.append[string] |
| add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); | add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); |
| // ^string.base64[] << encode | |
| // ^string:base64[encoded string] << decode | |
| add_native_method("base64", Method::CT_ANY, _base64, 0, 1); | |
| } | } |