Annotation of parser3/src/classes/string.C, revision 1.169
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.143 paf 4: Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.97 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.113 paf 6: */
1.24 paf 7:
1.169 ! misha 8: static const char * const IDENT_STRING_C="$Date: 2008-07-22 13:11:38 $";
1.1 paf 9:
1.43 paf 10: #include "classes.h"
1.126 paf 11: #include "pa_vmethod_frame.h"
12:
1.1 paf 13: #include "pa_request.h"
14: #include "pa_vdouble.h"
15: #include "pa_vint.h"
1.17 paf 16: #include "pa_vtable.h"
1.25 paf 17: #include "pa_vbool.h"
1.27 paf 18: #include "pa_string.h"
1.53 parser 19: #include "pa_sql_connection.h"
1.71 parser 20: #include "pa_dictionary.h"
1.119 paf 21: #include "pa_vmethod_frame.h"
1.1 paf 22:
1.41 paf 23: // class
24:
1.126 paf 25: class MString: public Methoded {
1.41 paf 26: public:
1.126 paf 27: MString();
1.44 paf 28: public: // Methoded
1.53 parser 29: bool used_directly() { return true; }
1.41 paf 30: };
1.1 paf 31:
1.126 paf 32: // global variable
33:
34: DECLARE_CLASS_VAR(string, new MString, 0);
35:
36: // defines for statics
37:
38: #define MATCH_VAR_NAME "match"
1.162 misha 39: #define TRIM_START_OPTION "left"
40: #define TRIM_END_OPTION "right"
1.133 paf 41: #define TRIM_BOTH_OPTION "both"
1.126 paf 42:
43: // statics
44:
45: static const String match_var_name(MATCH_VAR_NAME);
46:
1.1 paf 47: // methods
48:
1.126 paf 49: static void _length(Request& r, MethodParams&) {
1.163 misha 50: double result=GET_SELF(r, VString).string().length(r.charsets.source());
1.126 paf 51: r.write_no_lang(*new VDouble(result));
1.1 paf 52: }
53:
1.126 paf 54: static void _int(Request& r, MethodParams& params) {
55: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 56: int converted;
1.84 parser 57: try {
1.126 paf 58: if(self_string.is_empty())
1.153 misha 59: throw Exception(PARSER_RUNTIME,
1.126 paf 60: 0,
1.121 paf 61: "parameter is empty string, error converting");
1.126 paf 62: converted=self_string.as_int();
1.84 parser 63: } catch(...) { // convert problem
1.144 paf 64: if(params.count()>0)
65: converted=params.as_int(0, "default must be int", r); // (default)
1.84 parser 66: else
1.126 paf 67: rethrow; // we have a problem when no default
1.72 parser 68: }
1.126 paf 69: r.write_no_lang(*new VInt(converted));
1.1 paf 70: }
71:
1.126 paf 72: static void _double(Request& r, MethodParams& params) {
73: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 74: double converted;
1.84 parser 75: try {
1.126 paf 76: if(self_string.is_empty())
1.153 misha 77: throw Exception(PARSER_RUNTIME,
1.126 paf 78: 0,
1.121 paf 79: "parameter is empty string, error converting");
1.126 paf 80: converted=self_string.as_double();
1.84 parser 81: } catch(...) { // convert problem
1.144 paf 82: if(params.count()>0)
83: converted=params.as_double(0, "default must be double", r); // (default)
1.84 parser 84: else
1.126 paf 85: rethrow; // we have a problem when no default
1.72 parser 86: }
87:
1.126 paf 88: r.write_no_lang(*new VDouble(converted));
1.1 paf 89: }
90:
1.151 misha 91: static void _bool(Request& r, MethodParams& params) {
92: const String& self_string=GET_SELF(r, VString).string();
93: bool converted;
94: try {
95: if(self_string.is_empty())
1.153 misha 96: throw Exception(PARSER_RUNTIME,
1.151 misha 97: 0,
98: "parameter is empty string, error converting");
1.158 misha 99:
100: try {
101: converted=self_string.as_bool();
102: } catch(...) {
103: const String& lower_string=self_string.change_case(r.charsets.source(), String::CC_LOWER);
104: if(lower_string == "true"){
105: converted=true;
106: } else if (lower_string == "false"){
107: converted=false;
108: } else {
109: rethrow;
110: }
111: }
1.151 misha 112: } catch(...) { // convert problem
113: if(params.count()>0)
114: converted=params.as_bool(0, "default must be bool", r); // (default)
115: else
116: rethrow; // we have a problem when no default
117: }
118:
119: r.write_no_lang(*new VBool(converted));
120: }
121:
1.126 paf 122: /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9 paf 123:
1.126 paf 124: Value& fmt_maybe_code=params[0];
1.95 paf 125: // for some time due to stupid {} in original design
1.99 paf 126: const String& fmt=r.process_to_string(fmt_maybe_code);
1.9 paf 127:
1.159 misha 128: const char* buf=format(r.get_self().as_double(), fmt.trim().cstrm());
1.63 parser 129:
1.126 paf 130: r.write_no_lang(String(buf));
1.9 paf 131: }
1.11 paf 132:
1.126 paf 133: static void _left(Request& r, MethodParams& params) {
1.138 paf 134: ssize_t sn=params.as_int(0, "n must be int", r);
135: if(sn<0)
1.153 misha 136: throw Exception(PARSER_RUNTIME,
1.138 paf 137: 0,
138: "n(%d) must be >=0", sn);
139: size_t n=(size_t)sn;
140:
1.126 paf 141: const String& string=GET_SELF(r, VString).string();
1.163 misha 142: r.write_assign_lang(string.mid(r.charsets.source(), 0, n));
1.15 paf 143: }
144:
1.126 paf 145: static void _right(Request& r, MethodParams& params) {
1.138 paf 146: ssize_t sn=(size_t)params.as_int(0, "n must be int", r);
147: if(sn<0)
1.153 misha 148: throw Exception(PARSER_RUNTIME,
1.138 paf 149: 0,
150: "n(%d) must be >=0", sn);
151: size_t n=(size_t)sn;
1.159 misha 152:
1.126 paf 153: const String& string=GET_SELF(r, VString).string();
1.163 misha 154: size_t length=string.length(r.charsets.source());
1.164 misha 155: r.write_assign_lang(n<length?string.mid(r.charsets.source(), length-n, length, length):string);
1.15 paf 156: }
157:
1.126 paf 158: static void _mid(Request& r, MethodParams& params) {
159: const String& string=GET_SELF(r, VString).string();
1.83 parser 160:
1.138 paf 161: ssize_t sbegin=params.as_int(0, "p must be int", r);
162: if(sbegin<0)
1.153 misha 163: throw Exception(PARSER_RUNTIME,
1.138 paf 164: 0,
165: "p(%d) must be >=0", sbegin);
166: size_t begin=(size_t)sbegin;
167:
168: size_t end;
1.164 misha 169: size_t length=0;
1.138 paf 170: if(params.count()>1) {
171: ssize_t sn=params.as_int(1, "n must be int", r);
172: if(sn<0)
1.153 misha 173: throw Exception(PARSER_RUNTIME,
1.138 paf 174: 0,
175: "n(%d) must be >=0", sn);
176: end=begin+(size_t)sn;
1.164 misha 177: } else {
178: length=string.length(r.charsets.source());
179: end=length;
180: }
1.163 misha 181:
1.164 misha 182: r.write_assign_lang(string.mid(r.charsets.source(), begin, end, length));
1.15 paf 183: }
184:
1.126 paf 185: static void _pos(Request& r, MethodParams& params) {
186: Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 187:
1.126 paf 188: const String& string=GET_SELF(r, VString).string();
1.165 misha 189: ssize_t offset=0;
190: if(params.count()>1){
191: offset=params.as_int(1, "n must be int", r);
192: if(offset<0)
193: throw Exception(PARSER_RUNTIME,
194: 0,
195: "n(%d) must be >=0", offset);
196: }
197:
1.166 misha 198: r.write_no_lang(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), (size_t)offset)));
1.16 paf 199: }
200:
1.128 paf 201: static void split_list(MethodParams& params, int paramIndex,
1.126 paf 202: const String& string,
203: ArrayString& result) {
204: Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
1.23 paf 205:
1.126 paf 206: size_t pos_after=0;
207: string.split(result, pos_after, delim_value.as_string());
1.19 paf 208: }
209:
1.118 paf 210: #define SPLIT_LEFT 0x0001
211: #define SPLIT_RIGHT 0x0010
212: #define SPLIT_HORIZONTAL 0x0100
213: #define SPLIT_VERTICAL 0x1000
214:
1.126 paf 215: static int split_options(const String* options) {
1.118 paf 216: struct Split_option {
1.126 paf 217: const char* keyL;
218: const char* keyU;
1.118 paf 219: int setBit;
220: int checkBit;
221: } split_option[]={
222: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
223: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
224: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
225: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
1.130 paf 226: {0, 0, 0, 0}
1.118 paf 227: };
228:
229: int result=0;
1.126 paf 230: if(options) {
1.118 paf 231: for(Split_option *o=split_option; o->keyL; o++)
1.126 paf 232: if(options->pos(o->keyL)!=STRING_NOT_FOUND
233: || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.118 paf 234: if(result & o->checkBit)
1.153 misha 235: throw Exception(PARSER_RUNTIME,
1.118 paf 236: options,
237: "conflicting split options");
238: result |= o->setBit;
239: }
240: }
241:
242: return result;
243: }
244:
1.156 misha 245: static Table& split_vertical(ArrayString& pieces, bool right, const String* column_name) {
1.126 paf 246: Table::columns_type columns(new ArrayString);
1.156 misha 247: *columns+=column_name;
1.19 paf 248:
1.126 paf 249: Table& table=*new Table(columns, pieces.count());
1.118 paf 250: if(right) { // right
1.126 paf 251: for(int i=pieces.count(); --i>=0; ) {
252: Table::element_type row(new ArrayString);
253: *row+=pieces[i];
254: table+=row;
1.118 paf 255: }
256: } else { // left
1.126 paf 257: Array_iterator<const String*> i(pieces);
1.118 paf 258: while(i.has_next()) {
1.126 paf 259: Table::element_type row(new ArrayString);
260: *row+=i.next();
261: table+=row;
1.118 paf 262: }
1.61 parser 263: }
1.118 paf 264:
1.126 paf 265: return table;
1.19 paf 266: }
267:
1.128 paf 268: static Table& split_horizontal(ArrayString& pieces, bool right) {
1.126 paf 269: Table& table=*new Table(Table::columns_type(0) /* nameless */);
270: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 271: if(right) { // right
1.131 paf 272: for(int i=pieces.count(); --i>=0; )
1.126 paf 273: *row+=pieces[i];
1.118 paf 274: } else { // left
1.126 paf 275: for(Array_iterator<const String*> i(pieces); i.has_next(); )
276: *row+=i.next();
1.118 paf 277: }
1.126 paf 278: table+=row;
1.118 paf 279:
1.126 paf 280: return table;
1.118 paf 281: }
282:
1.126 paf 283: static void split_with_options(Request& r, MethodParams& params,
1.118 paf 284: int bits) {
1.126 paf 285: const String& string=GET_SELF(r, VString).string();
1.19 paf 286:
1.126 paf 287: ArrayString pieces;
1.128 paf 288: split_list(params, 0, string, pieces);
1.19 paf 289:
1.118 paf 290: if(!bits) {
1.126 paf 291: const String* options=0;
292: if(params.count()>1)
293: options=¶ms.as_string(1, "options must not be code");
294:
1.118 paf 295: bits=split_options(options);
296: }
1.21 paf 297:
1.118 paf 298: bool right=(bits & SPLIT_RIGHT) != 0;
299: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.156 misha 300:
301: const String* column_name=0;
302: if(params.count()>2){
303: column_name=¶ms.as_string(2, COLUMN_NAME_MUST_BE_STRING);
304: if (horizontal && column_name->length())
305: throw Exception(PARSER_RUNTIME,
306: column_name,
307: "column name can't be specified with horisontal split");
308: }
309: if(!column_name || !column_name->length())
310: column_name=new String("piece");
311:
312: Table& table=horizontal?split_horizontal(pieces, right):split_vertical(pieces, right, column_name);
1.17 paf 313:
1.126 paf 314: r.write_no_lang(*new VTable(&table));
1.118 paf 315: }
1.126 paf 316: static void _split(Request& r, MethodParams& params) {
317: split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 318: }
1.126 paf 319: static void _lsplit(Request& r, MethodParams& params) {
320: split_with_options(r, params, SPLIT_LEFT);
1.118 paf 321: }
1.126 paf 322: static void _rsplit(Request& r, MethodParams& params) {
323: split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 324: }
325:
1.126 paf 326: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 327: if(row)
328: table+=row;
1.27 paf 329: }
330:
1.74 parser 331: #ifndef DOXYGEN
1.27 paf 332: struct Replace_action_info {
1.126 paf 333: Request* request;
334: const String* src; String* dest;
335: VTable* vtable;
336: Value* replacement_code;
1.27 paf 337: };
1.74 parser 338: #endif
1.105 paf 339: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.126 paf 340: static void replace_action(Table& table, ArrayString* row,
341: int prestart, int prefinish,
342: int poststart, int postfinish,
343: void *info) {
1.27 paf 344: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 345: if(row) { // begin&middle
1.104 paf 346: // piece from last match['prestart'] to beginning of this match['prefinish']
347: if(prestart!=prefinish)
348: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 349: // store found parts in one-record VTable
1.126 paf 350: if(table.count()) // middle
1.32 paf 351: table.put(0, row);
352: else // begin
1.30 paf 353: table+=row;
354: { // execute 'replacement_code' in 'table' context
1.105 paf 355: ai.vtable->set_table(table);
1.30 paf 356:
1.105 paf 357: *ai.dest << ai.request->process_to_string(*ai.replacement_code);
1.29 paf 358: }
359: } else // end
1.104 paf 360: *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 361: }
362:
1.50 parser 363: /// @todo use pcre:study somehow
1.126 paf 364: static void _match(Request& r, MethodParams& params) {
365: Value& regexp=params.as_no_junction(0, "regexp must not be code");
366:
367: const String* options=
368: params.count()>1?
369: ¶ms.as_no_junction(1, "options must not be code").as_string():0;
370:
371: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
372: const String& src=GET_SELF(r, VString).string();
1.152 misha 373: int matches_count=0;
1.126 paf 374: if(params.count()<3) { // search
375: Table* table=src.match(r.charsets.source(),
1.32 paf 376: regexp.as_string(), options,
1.64 parser 377: search_action, 0,
1.152 misha 378: matches_count);
379: // r.write_assign_lang(*new VTable(table));
380: if(table){
1.159 misha 381: r.write_assign_lang(*new VTable(table));
1.152 misha 382: } else {
383: r.write_assign_lang(*new VInt(matches_count));
384: }
385:
1.27 paf 386: } else { // replace
1.126 paf 387: Value& replacement_code=params.as_junction(2, "replacement param must be code");
1.106 paf 388:
1.126 paf 389: String result;
390: VTable* vtable=new VTable;
1.130 paf 391: Replace_action_info info={
392: &r,
393: &src,
394: &result,
395: vtable,
396: &replacement_code
397: };
1.105 paf 398: Temp_value_element temp_match_var(
1.119 paf 399: *replacement_code.get_junction()->method_frame,
1.126 paf 400: match_var_name, vtable);
401: src.match(r.charsets.source(),
1.99 paf 402: r.process_to_string(regexp), options,
1.126 paf 403: replace_action, &info,
1.152 misha 404: matches_count);
1.102 paf 405: r.write_assign_lang(result);
1.27 paf 406: }
1.24 paf 407: }
408:
1.128 paf 409: static void change_case(Request& r, MethodParams&,
1.49 parser 410: String::Change_case_kind kind) {
1.126 paf 411: const String& src=GET_SELF(r, VString).string();
1.49 parser 412:
1.126 paf 413: r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 414: }
1.126 paf 415: static void _upper(Request& r, MethodParams& params) {
416: change_case(r, params, String::CC_UPPER);
1.49 parser 417: }
1.126 paf 418: static void _lower(Request& r, MethodParams& params) {
419: change_case(r, params, String::CC_LOWER);
1.49 parser 420: }
421:
1.65 parser 422: #ifndef DOXYGEN
1.126 paf 423: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
424: const String& statement_string; const char* statement_cstr;
425: bool got_column;
1.65 parser 426: public:
1.126 paf 427: bool got_cell;
428: String& result;
429: public:
430: String_sql_event_handlers(
431: const String& astatement_string, const char* astatement_cstr):
432: statement_string(astatement_string), statement_cstr(astatement_cstr),
433: got_column(false),
434: got_cell(false),
435: result(*new String) {}
1.65 parser 436:
1.128 paf 437: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124 paf 438: if(got_column) {
1.153 misha 439: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 440: //statement_string,
1.65 parser 441: "result must contain exactly one column");
1.124 paf 442: return true;
443: }
1.65 parser 444: got_column=true;
1.124 paf 445: return false;
1.65 parser 446: }
1.124 paf 447: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
448: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.126 paf 449: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 450: if(got_cell) {
1.153 misha 451: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 452: //statement_string,
1.65 parser 453: "result must not contain more then one row");
1.124 paf 454: return true;
455: }
1.65 parser 456:
1.124 paf 457: try {
458: got_cell=true;
1.126 paf 459: result.append_know_length(str, length, String::L_TAINTED);
1.124 paf 460: return false;
461: } catch(...) {
462: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
463: return true;
464: }
1.65 parser 465: }
466: };
467: #endif
1.141 paf 468: extern String sql_bind_name;
1.126 paf 469: extern String sql_limit_name;
470: extern String sql_offset_name;
471: extern String sql_default_name;
472: extern String sql_distinct_name;
1.141 paf 473: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
474: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
475:
1.126 paf 476: const String* sql_result_string(Request& r, MethodParams& params,
477: HashStringValue*& options, Value*& default_code) {
478: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 479:
1.141 paf 480: HashStringValue* bind=0;
1.160 misha 481: ulong limit=SQL_NO_LIMIT;
1.70 parser 482: ulong offset=0;
1.81 parser 483: default_code=0;
1.126 paf 484: if(params.count()>1) {
485: Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.145 paf 486: if(voptions.is_defined() && !voptions.is_string())
1.130 paf 487: if((options=voptions.get_hash())) {
1.141 paf 488: int valid_options=0;
489: if(Value* vbind=options->get(sql_bind_name)) {
490: valid_options++;
491: bind=vbind->get_hash();
492: }
493: if(Value* vlimit=options->get(sql_limit_name)) {
1.161 misha 494: valid_options++;
495: limit=(ulong)r.process_to_value(*vlimit).as_double();
1.141 paf 496: }
497: if(Value* voffset=options->get(sql_offset_name)) {
498: valid_options++;
1.99 paf 499: offset=(ulong)r.process_to_value(*voffset).as_double();
1.141 paf 500: }
1.130 paf 501: if((default_code=options->get(sql_default_name))) {
1.141 paf 502: valid_options++;
1.81 parser 503: }
1.141 paf 504: if(valid_options!=options->count())
1.153 misha 505: throw Exception(PARSER_RUNTIME,
1.141 paf 506: 0,
507: "called with invalid option");
1.73 parser 508: } else
1.153 misha 509: throw Exception(PARSER_RUNTIME,
1.126 paf 510: 0,
1.73 parser 511: "options must be hash");
1.70 parser 512: } else
513: options=0;
514:
1.141 paf 515: SQL_Driver::Placeholder* placeholders=0;
516: uint placeholders_count=0;
517: if(bind)
518: placeholders_count=marshal_binds(*bind, placeholders);
519:
1.126 paf 520: Temp_lang temp_lang(r, String::L_SQL);
1.99 paf 521: const String& statement_string=r.process_to_string(statement);
1.126 paf 522: const char* statement_cstr=
523: statement_string.cstr(String::L_UNSPECIFIED, r.connection());
524: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.160 misha 525:
1.126 paf 526: r.connection()->query(
1.140 paf 527: statement_cstr,
1.142 paf 528: placeholders_count, placeholders,
1.140 paf 529: offset, limit,
1.117 paf 530: handlers,
531: statement_string);
1.53 parser 532:
1.141 paf 533: if(bind)
534: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
535:
1.65 parser 536: if(!handlers.got_cell)
537: return 0; // no lines, caller should return second param[default value]
1.62 parser 538:
1.126 paf 539: return &handlers.result;
1.53 parser 540: }
541:
1.126 paf 542: static void _sql(Request& r, MethodParams& params) {
1.53 parser 543:
1.126 paf 544: HashStringValue* options;
545: Value* default_code;
546: const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 547: if(!string) {
1.81 parser 548: if(default_code) {
1.99 paf 549: string=&r.process_to_string(*default_code);
1.68 parser 550: } else
1.153 misha 551: throw Exception(PARSER_RUNTIME,
1.126 paf 552: 0,
1.81 parser 553: "produced no result, but no default option specified");
1.62 parser 554: }
1.100 paf 555:
1.102 paf 556: r.write_assign_lang(*string);
1.53 parser 557: }
558:
1.126 paf 559: static void _replace(Request& r, MethodParams& params) {
560: const String& src=GET_SELF(r, VString).string();
1.71 parser 561:
1.157 misha 562: Table* table=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE).get_table();
1.71 parser 563: if(!table)
1.153 misha 564: throw Exception(PARSER_RUNTIME,
1.126 paf 565: 0,
1.71 parser 566: "parameter must be table");
567:
568: Dictionary dict(*table);
1.126 paf 569: r.write_assign_lang(src.replace(dict));
1.71 parser 570: }
1.79 parser 571:
1.126 paf 572: static void _save(Request& r, MethodParams& params) {
1.154 misha 573: const String& file_name=params.as_string(params.count()-1, FILE_NAME_MUST_BE_STRING);
1.79 parser 574:
1.126 paf 575: const String& src=GET_SELF(r, VString).string();
1.79 parser 576:
1.87 paf 577: bool do_append=false;
1.126 paf 578: if(params.count()>1) {
579: const String& mode=params.as_string(0, "mode must be string");
1.87 paf 580: if(mode=="append")
581: do_append=true;
582: else
1.153 misha 583: throw Exception(PARSER_RUNTIME,
1.87 paf 584: &mode,
585: "unknown mode, must be 'append'");
586: }
587:
1.79 parser 588: // write
1.126 paf 589: const char* buf=src.cstr(String::L_UNSPECIFIED, r.connection(false/*no error if none*/));
1.94 paf 590: file_write(r.absolute(file_name),
1.89 paf 591: buf, strlen(buf), true, do_append);
1.79 parser 592: }
593:
1.126 paf 594: static void _normalize(Request& r, MethodParams&) {
595: const String& src=GET_SELF(r, VString).string();
596:
597: r.write_assign_lang(src);
1.109 paf 598: }
599:
1.133 paf 600: static void _trim(Request& r, MethodParams& params) {
601: const String& src=GET_SELF(r, VString).string();
602:
603: String::Trim_kind kind=String::TRIM_BOTH;
604: const char* chars=0;
605: if(params.count()>0) {
1.135 paf 606: const String& skind=params.as_string(0,
607: "'where' must be string");
1.137 paf 608: if(skind.length())
1.162 misha 609: if(skind==TRIM_BOTH_OPTION)
610: kind=String::TRIM_BOTH;
611: else if(skind==TRIM_START_OPTION || skind=="start")
1.137 paf 612: kind=String::TRIM_START;
1.162 misha 613: else if(skind==TRIM_END_OPTION || skind=="end")
1.137 paf 614: kind=String::TRIM_END;
615: else
1.153 misha 616: throw Exception(PARSER_RUNTIME,
1.137 paf 617: &skind,
618: "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
1.133 paf 619:
1.136 paf 620: if(params.count()>1) {
621: const String& schars=params.as_string(1, "'chars' must be string");
1.137 paf 622: if(schars.length())
623: chars=schars.cstr();
1.136 paf 624: }
1.133 paf 625: }
626:
627: r.write_assign_lang(src.trim(kind, chars));
628: }
629:
1.139 paf 630: static void _append(Request& r, MethodParams& params) {
631: // c=a+b
632: VString& va=GET_SELF(r, VString);
633: const String& a=va.string();
1.155 misha 634: const String& b=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.139 paf 635: String& c=*new String(a);
636: c.append(b, String::L_PASS_APPENDED);
637: va.set_string(c);
638: }
639:
1.146 paf 640: static void _base64(Request& r, MethodParams& params) {
641: if(params.count()) {
1.169 ! misha 642: // decode: ^string:base64[encoded]
1.155 misha 643: const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.169 ! misha 644: char* decoded=0;
! 645: size_t length=0;
! 646: pa_base64_decode(cstr, strlen(cstr), decoded, length);
! 647: if(decoded && length){
! 648: if(memchr((const char*)decoded, 0, length))
! 649: throw Exception(PARSER_RUNTIME,
! 650: 0,
! 651: "Invalid \\x00 character found while decode to string. Decode it to file instead.");
! 652:
! 653: fix_line_breaks(decoded, length);
! 654: if(length){
! 655: r.write_assign_lang(*new String(decoded, length, true/*tainted*/));
! 656: }
! 657: }
1.147 paf 658: } else {
1.169 ! misha 659: // encode: ^str.base64[]
1.148 paf 660: VString& self=GET_SELF(r, VString);
661: const char* cstr=self.string().cstr();
1.147 paf 662: const char* encoded=pa_base64_encode(cstr, strlen(cstr));
1.169 ! misha 663: r.write_assign_lang(*new String(encoded, 0, true/*tainted. once ?param=base64(something) was needed*/));
1.146 paf 664: }
665: }
666:
1.167 misha 667: static void _escape(Request& r, MethodParams&){
668: const String& src=GET_SELF(r, VString).string();
669: r.write_assign_lang(src.escape(r.charsets.source()));
670: }
671:
672: static void _unescape(Request& r, MethodParams& params){
673: const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
674: if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true/* don't unescape '+' char */))
675: r.write_assign_lang(*new String(result));
676: }
677:
1.41 paf 678: // constructor
679:
1.126 paf 680: MString::MString(): Methoded("string") {
1.1 paf 681: // ^string.length[]
1.41 paf 682: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 683:
1.1 paf 684: // ^string.int[]
1.72 parser 685: // ^string.int(default)
686: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 687: // ^string.double[]
1.72 parser 688: // ^string.double(default)
689: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151 misha 690: // ^void.bool[]
691: // ^void.bool(default)
692: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9 paf 693:
1.165 misha 694: // ^string.format[format]
1.41 paf 695: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 696:
1.15 paf 697: // ^string.left(n)
1.41 paf 698: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 699: // ^string.right(n)
1.41 paf 700: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165 misha 701: // ^string.mid(p)
1.15 paf 702: // ^string.mid(p;n)
1.82 parser 703: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 704:
705: // ^string.pos[substr]
1.165 misha 706: // ^string.pos[substr](n)
707: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17 paf 708:
1.118 paf 709: // ^string.split[delim]
710: // ^string.split[delim][options]
1.156 misha 711: // ^string.split[delim][options][column name]
712: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.118 paf 713: // old names for backward compatibility
714: // ^string.lsplit[delim]
715: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
716: // ^string.rsplit[delim]
717: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
718:
1.32 paf 719: // ^string.match[regexp][options]
720: // ^string.match[regexp][options]{replacement-code}
1.41 paf 721: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 722:
1.165 misha 723: // ^string.upper[]
1.49 parser 724: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165 misha 725: // ^string.lower[]
1.49 parser 726: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 727:
1.70 parser 728: // ^sql[query]
1.141 paf 729: // ^sql[query][options hash]
1.67 parser 730: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 731:
732: // ^string.replace[table]
733: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 734:
735: // ^string.save[file]
1.87 paf 736: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 737:
1.112 paf 738: // ^string.normalize[]
739: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133 paf 740:
741: // ^string.trim[[start|both|end][;chars]]
742: add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139 paf 743:
744: // ^string.append[string]
745: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.146 paf 746:
747: // ^string.base64[] << encode
1.147 paf 748: // ^string:base64[encoded string] << decode
1.146 paf 749: add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.167 misha 750:
1.168 misha 751: // ^string.js-escape[]
752: // ^string:js-unescape[escaped%uXXXXstring]
753: add_native_method("js-escape", Method::CT_ANY, _escape, 0, 0);
754: add_native_method("js-unescape", Method::CT_STATIC, _unescape, 1, 1);
1.2 paf 755: }
E-mail: