Annotation of parser3/src/classes/string.C, revision 1.160
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.160 ! misha 8: static const char * const IDENT_STRING_C="$Date: 2008-05-22 17:43:30 $";
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.133 paf 39: #define TRIM_START_OPTION "start"
40: #define TRIM_BOTH_OPTION "both"
41: #define TRIM_END_OPTION "end"
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&) {
50: double result=GET_SELF(r, VString).string().length();
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.102 paf 142: r.write_assign_lang(string.mid(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.127 paf 154: size_t length=string.length();
155: r.write_assign_lang(n<length?string.mid(length-n, string.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;
169: if(params.count()>1) {
170: ssize_t sn=params.as_int(1, "n must be int", r);
171: if(sn<0)
1.153 misha 172: throw Exception(PARSER_RUNTIME,
1.138 paf 173: 0,
174: "n(%d) must be >=0", sn);
175: end=begin+(size_t)sn;
176: } else
177: end=string.length();
1.15 paf 178:
1.138 paf 179: r.write_assign_lang(string.mid(begin, end));
1.15 paf 180: }
181:
1.126 paf 182: static void _pos(Request& r, MethodParams& params) {
183: Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 184:
1.126 paf 185: const String& string=GET_SELF(r, VString).string();
186: r.write_assign_lang(*new VInt((int)string.pos(substr.as_string())));
1.16 paf 187: }
188:
1.128 paf 189: static void split_list(MethodParams& params, int paramIndex,
1.126 paf 190: const String& string,
191: ArrayString& result) {
192: Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
1.23 paf 193:
1.126 paf 194: size_t pos_after=0;
195: string.split(result, pos_after, delim_value.as_string());
1.19 paf 196: }
197:
1.118 paf 198: #define SPLIT_LEFT 0x0001
199: #define SPLIT_RIGHT 0x0010
200: #define SPLIT_HORIZONTAL 0x0100
201: #define SPLIT_VERTICAL 0x1000
202:
1.126 paf 203: static int split_options(const String* options) {
1.118 paf 204: struct Split_option {
1.126 paf 205: const char* keyL;
206: const char* keyU;
1.118 paf 207: int setBit;
208: int checkBit;
209: } split_option[]={
210: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
211: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
212: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
213: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
1.130 paf 214: {0, 0, 0, 0}
1.118 paf 215: };
216:
217: int result=0;
1.126 paf 218: if(options) {
1.118 paf 219: for(Split_option *o=split_option; o->keyL; o++)
1.126 paf 220: if(options->pos(o->keyL)!=STRING_NOT_FOUND
221: || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.118 paf 222: if(result & o->checkBit)
1.153 misha 223: throw Exception(PARSER_RUNTIME,
1.118 paf 224: options,
225: "conflicting split options");
226: result |= o->setBit;
227: }
228: }
229:
230: return result;
231: }
232:
1.156 misha 233: static Table& split_vertical(ArrayString& pieces, bool right, const String* column_name) {
1.126 paf 234: Table::columns_type columns(new ArrayString);
1.156 misha 235: *columns+=column_name;
1.19 paf 236:
1.126 paf 237: Table& table=*new Table(columns, pieces.count());
1.118 paf 238: if(right) { // right
1.126 paf 239: for(int i=pieces.count(); --i>=0; ) {
240: Table::element_type row(new ArrayString);
241: *row+=pieces[i];
242: table+=row;
1.118 paf 243: }
244: } else { // left
1.126 paf 245: Array_iterator<const String*> i(pieces);
1.118 paf 246: while(i.has_next()) {
1.126 paf 247: Table::element_type row(new ArrayString);
248: *row+=i.next();
249: table+=row;
1.118 paf 250: }
1.61 parser 251: }
1.118 paf 252:
1.126 paf 253: return table;
1.19 paf 254: }
255:
1.128 paf 256: static Table& split_horizontal(ArrayString& pieces, bool right) {
1.126 paf 257: Table& table=*new Table(Table::columns_type(0) /* nameless */);
258: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 259: if(right) { // right
1.131 paf 260: for(int i=pieces.count(); --i>=0; )
1.126 paf 261: *row+=pieces[i];
1.118 paf 262: } else { // left
1.126 paf 263: for(Array_iterator<const String*> i(pieces); i.has_next(); )
264: *row+=i.next();
1.118 paf 265: }
1.126 paf 266: table+=row;
1.118 paf 267:
1.126 paf 268: return table;
1.118 paf 269: }
270:
1.126 paf 271: static void split_with_options(Request& r, MethodParams& params,
1.118 paf 272: int bits) {
1.126 paf 273: const String& string=GET_SELF(r, VString).string();
1.19 paf 274:
1.126 paf 275: ArrayString pieces;
1.128 paf 276: split_list(params, 0, string, pieces);
1.19 paf 277:
1.118 paf 278: if(!bits) {
1.126 paf 279: const String* options=0;
280: if(params.count()>1)
281: options=¶ms.as_string(1, "options must not be code");
282:
1.118 paf 283: bits=split_options(options);
284: }
1.21 paf 285:
1.118 paf 286: bool right=(bits & SPLIT_RIGHT) != 0;
287: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.156 misha 288:
289: const String* column_name=0;
290: if(params.count()>2){
291: column_name=¶ms.as_string(2, COLUMN_NAME_MUST_BE_STRING);
292: if (horizontal && column_name->length())
293: throw Exception(PARSER_RUNTIME,
294: column_name,
295: "column name can't be specified with horisontal split");
296: }
297: if(!column_name || !column_name->length())
298: column_name=new String("piece");
299:
300: Table& table=horizontal?split_horizontal(pieces, right):split_vertical(pieces, right, column_name);
1.17 paf 301:
1.126 paf 302: r.write_no_lang(*new VTable(&table));
1.118 paf 303: }
1.126 paf 304: static void _split(Request& r, MethodParams& params) {
305: split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 306: }
1.126 paf 307: static void _lsplit(Request& r, MethodParams& params) {
308: split_with_options(r, params, SPLIT_LEFT);
1.118 paf 309: }
1.126 paf 310: static void _rsplit(Request& r, MethodParams& params) {
311: split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 312: }
313:
1.126 paf 314: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 315: if(row)
316: table+=row;
1.27 paf 317: }
318:
1.74 parser 319: #ifndef DOXYGEN
1.27 paf 320: struct Replace_action_info {
1.126 paf 321: Request* request;
322: const String* src; String* dest;
323: VTable* vtable;
324: Value* replacement_code;
1.27 paf 325: };
1.74 parser 326: #endif
1.105 paf 327: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.126 paf 328: static void replace_action(Table& table, ArrayString* row,
329: int prestart, int prefinish,
330: int poststart, int postfinish,
331: void *info) {
1.27 paf 332: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 333: if(row) { // begin&middle
1.104 paf 334: // piece from last match['prestart'] to beginning of this match['prefinish']
335: if(prestart!=prefinish)
336: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 337: // store found parts in one-record VTable
1.126 paf 338: if(table.count()) // middle
1.32 paf 339: table.put(0, row);
340: else // begin
1.30 paf 341: table+=row;
342: { // execute 'replacement_code' in 'table' context
1.105 paf 343: ai.vtable->set_table(table);
1.30 paf 344:
1.105 paf 345: *ai.dest << ai.request->process_to_string(*ai.replacement_code);
1.29 paf 346: }
347: } else // end
1.104 paf 348: *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 349: }
350:
1.50 parser 351: /// @todo use pcre:study somehow
1.126 paf 352: static void _match(Request& r, MethodParams& params) {
353: Value& regexp=params.as_no_junction(0, "regexp must not be code");
354:
355: const String* options=
356: params.count()>1?
357: ¶ms.as_no_junction(1, "options must not be code").as_string():0;
358:
359: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
360: const String& src=GET_SELF(r, VString).string();
1.152 misha 361: int matches_count=0;
1.126 paf 362: if(params.count()<3) { // search
363: Table* table=src.match(r.charsets.source(),
1.32 paf 364: regexp.as_string(), options,
1.64 parser 365: search_action, 0,
1.152 misha 366: matches_count);
367: // r.write_assign_lang(*new VTable(table));
368: if(table){
1.159 misha 369: r.write_assign_lang(*new VTable(table));
1.152 misha 370: } else {
371: r.write_assign_lang(*new VInt(matches_count));
372: }
373:
1.27 paf 374: } else { // replace
1.126 paf 375: Value& replacement_code=params.as_junction(2, "replacement param must be code");
1.106 paf 376:
1.126 paf 377: String result;
378: VTable* vtable=new VTable;
1.130 paf 379: Replace_action_info info={
380: &r,
381: &src,
382: &result,
383: vtable,
384: &replacement_code
385: };
1.105 paf 386: Temp_value_element temp_match_var(
1.119 paf 387: *replacement_code.get_junction()->method_frame,
1.126 paf 388: match_var_name, vtable);
389: src.match(r.charsets.source(),
1.99 paf 390: r.process_to_string(regexp), options,
1.126 paf 391: replace_action, &info,
1.152 misha 392: matches_count);
1.102 paf 393: r.write_assign_lang(result);
1.27 paf 394: }
1.24 paf 395: }
396:
1.128 paf 397: static void change_case(Request& r, MethodParams&,
1.49 parser 398: String::Change_case_kind kind) {
1.126 paf 399: const String& src=GET_SELF(r, VString).string();
1.49 parser 400:
1.126 paf 401: r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 402: }
1.126 paf 403: static void _upper(Request& r, MethodParams& params) {
404: change_case(r, params, String::CC_UPPER);
1.49 parser 405: }
1.126 paf 406: static void _lower(Request& r, MethodParams& params) {
407: change_case(r, params, String::CC_LOWER);
1.49 parser 408: }
409:
1.65 parser 410: #ifndef DOXYGEN
1.126 paf 411: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
412: const String& statement_string; const char* statement_cstr;
413: bool got_column;
1.65 parser 414: public:
1.126 paf 415: bool got_cell;
416: String& result;
417: public:
418: String_sql_event_handlers(
419: const String& astatement_string, const char* astatement_cstr):
420: statement_string(astatement_string), statement_cstr(astatement_cstr),
421: got_column(false),
422: got_cell(false),
423: result(*new String) {}
1.65 parser 424:
1.128 paf 425: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124 paf 426: if(got_column) {
1.153 misha 427: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 428: //statement_string,
1.65 parser 429: "result must contain exactly one column");
1.124 paf 430: return true;
431: }
1.65 parser 432: got_column=true;
1.124 paf 433: return false;
1.65 parser 434: }
1.124 paf 435: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
436: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.126 paf 437: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 438: if(got_cell) {
1.153 misha 439: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 440: //statement_string,
1.65 parser 441: "result must not contain more then one row");
1.124 paf 442: return true;
443: }
1.65 parser 444:
1.124 paf 445: try {
446: got_cell=true;
1.126 paf 447: result.append_know_length(str, length, String::L_TAINTED);
1.124 paf 448: return false;
449: } catch(...) {
450: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
451: return true;
452: }
1.65 parser 453: }
454: };
455: #endif
1.141 paf 456: extern String sql_bind_name;
1.126 paf 457: extern String sql_limit_name;
458: extern String sql_offset_name;
459: extern String sql_default_name;
460: extern String sql_distinct_name;
1.141 paf 461: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
462: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
463:
1.126 paf 464: const String* sql_result_string(Request& r, MethodParams& params,
465: HashStringValue*& options, Value*& default_code) {
466: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 467:
1.141 paf 468: HashStringValue* bind=0;
1.160 ! misha 469: ulong limit=SQL_NO_LIMIT;
1.70 parser 470: ulong offset=0;
1.81 parser 471: default_code=0;
1.126 paf 472: if(params.count()>1) {
473: Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.145 paf 474: if(voptions.is_defined() && !voptions.is_string())
1.130 paf 475: if((options=voptions.get_hash())) {
1.141 paf 476: int valid_options=0;
477: if(Value* vbind=options->get(sql_bind_name)) {
478: valid_options++;
479: bind=vbind->get_hash();
480: }
481: if(Value* vlimit=options->get(sql_limit_name)) {
1.160 ! misha 482: if(vlimit->is_defined()){
! 483: valid_options++;
! 484: limit=(ulong)r.process_to_value(*vlimit).as_double();
! 485: } else
! 486: throw Exception(PARSER_RUNTIME,
! 487: 0,
! 488: "limit must be defined");
1.141 paf 489: }
490: if(Value* voffset=options->get(sql_offset_name)) {
491: valid_options++;
1.99 paf 492: offset=(ulong)r.process_to_value(*voffset).as_double();
1.141 paf 493: }
1.130 paf 494: if((default_code=options->get(sql_default_name))) {
1.141 paf 495: valid_options++;
1.81 parser 496: }
1.141 paf 497: if(valid_options!=options->count())
1.153 misha 498: throw Exception(PARSER_RUNTIME,
1.141 paf 499: 0,
500: "called with invalid option");
1.73 parser 501: } else
1.153 misha 502: throw Exception(PARSER_RUNTIME,
1.126 paf 503: 0,
1.73 parser 504: "options must be hash");
1.70 parser 505: } else
506: options=0;
507:
1.141 paf 508: SQL_Driver::Placeholder* placeholders=0;
509: uint placeholders_count=0;
510: if(bind)
511: placeholders_count=marshal_binds(*bind, placeholders);
512:
1.126 paf 513: Temp_lang temp_lang(r, String::L_SQL);
1.99 paf 514: const String& statement_string=r.process_to_string(statement);
1.126 paf 515: const char* statement_cstr=
516: statement_string.cstr(String::L_UNSPECIFIED, r.connection());
517: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.160 ! misha 518:
1.126 paf 519: r.connection()->query(
1.140 paf 520: statement_cstr,
1.142 paf 521: placeholders_count, placeholders,
1.140 paf 522: offset, limit,
1.117 paf 523: handlers,
524: statement_string);
1.53 parser 525:
1.141 paf 526: if(bind)
527: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
528:
1.65 parser 529: if(!handlers.got_cell)
530: return 0; // no lines, caller should return second param[default value]
1.62 parser 531:
1.126 paf 532: return &handlers.result;
1.53 parser 533: }
534:
1.126 paf 535: static void _sql(Request& r, MethodParams& params) {
1.53 parser 536:
1.126 paf 537: HashStringValue* options;
538: Value* default_code;
539: const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 540: if(!string) {
1.81 parser 541: if(default_code) {
1.99 paf 542: string=&r.process_to_string(*default_code);
1.68 parser 543: } else
1.153 misha 544: throw Exception(PARSER_RUNTIME,
1.126 paf 545: 0,
1.81 parser 546: "produced no result, but no default option specified");
1.62 parser 547: }
1.100 paf 548:
1.102 paf 549: r.write_assign_lang(*string);
1.53 parser 550: }
551:
1.126 paf 552: static void _replace(Request& r, MethodParams& params) {
553: const String& src=GET_SELF(r, VString).string();
1.71 parser 554:
1.157 misha 555: Table* table=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE).get_table();
1.71 parser 556: if(!table)
1.153 misha 557: throw Exception(PARSER_RUNTIME,
1.126 paf 558: 0,
1.71 parser 559: "parameter must be table");
560:
561: Dictionary dict(*table);
1.126 paf 562: r.write_assign_lang(src.replace(dict));
1.71 parser 563: }
1.79 parser 564:
1.126 paf 565: static void _save(Request& r, MethodParams& params) {
1.154 misha 566: const String& file_name=params.as_string(params.count()-1, FILE_NAME_MUST_BE_STRING);
1.79 parser 567:
1.126 paf 568: const String& src=GET_SELF(r, VString).string();
1.79 parser 569:
1.87 paf 570: bool do_append=false;
1.126 paf 571: if(params.count()>1) {
572: const String& mode=params.as_string(0, "mode must be string");
1.87 paf 573: if(mode=="append")
574: do_append=true;
575: else
1.153 misha 576: throw Exception(PARSER_RUNTIME,
1.87 paf 577: &mode,
578: "unknown mode, must be 'append'");
579: }
580:
1.79 parser 581: // write
1.126 paf 582: const char* buf=src.cstr(String::L_UNSPECIFIED, r.connection(false/*no error if none*/));
1.94 paf 583: file_write(r.absolute(file_name),
1.89 paf 584: buf, strlen(buf), true, do_append);
1.79 parser 585: }
586:
1.126 paf 587: static void _normalize(Request& r, MethodParams&) {
588: const String& src=GET_SELF(r, VString).string();
589:
590: r.write_assign_lang(src);
1.109 paf 591: }
592:
1.133 paf 593: static void _trim(Request& r, MethodParams& params) {
594: const String& src=GET_SELF(r, VString).string();
595:
596: String::Trim_kind kind=String::TRIM_BOTH;
597: const char* chars=0;
598: if(params.count()>0) {
1.135 paf 599: const String& skind=params.as_string(0,
600: "'where' must be string");
1.137 paf 601: if(skind.length())
602: if(skind==TRIM_START_OPTION)
603: kind=String::TRIM_START;
604: else if(skind==TRIM_END_OPTION)
605: kind=String::TRIM_END;
606: else if(skind==TRIM_BOTH_OPTION)
607: kind=String::TRIM_BOTH;
608: else
1.153 misha 609: throw Exception(PARSER_RUNTIME,
1.137 paf 610: &skind,
611: "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
1.133 paf 612:
1.136 paf 613: if(params.count()>1) {
614: const String& schars=params.as_string(1, "'chars' must be string");
1.137 paf 615: if(schars.length())
616: chars=schars.cstr();
1.136 paf 617: }
1.133 paf 618: }
619:
620: r.write_assign_lang(src.trim(kind, chars));
621: }
622:
1.139 paf 623: static void _append(Request& r, MethodParams& params) {
624: // c=a+b
625: VString& va=GET_SELF(r, VString);
626: const String& a=va.string();
1.155 misha 627: const String& b=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.139 paf 628: String& c=*new String(a);
629: c.append(b, String::L_PASS_APPENDED);
630: va.set_string(c);
631: }
632:
1.146 paf 633: static void _base64(Request& r, MethodParams& params) {
634: if(params.count()) {
1.147 paf 635: // decode
1.155 misha 636: const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.148 paf 637: char* decoded_cstr=0;
1.146 paf 638: size_t decoded_size=0;
639: pa_base64_decode(cstr, strlen(cstr), decoded_cstr, decoded_size);
640: if(decoded_cstr && decoded_size)
1.148 paf 641: r.write_assign_lang(*new String(decoded_cstr, decoded_size, true));
1.147 paf 642: } else {
643: // encode
1.148 paf 644: VString& self=GET_SELF(r, VString);
645: const char* cstr=self.string().cstr();
1.147 paf 646: const char* encoded=pa_base64_encode(cstr, strlen(cstr));
647: r.write_assign_lang(*new String(encoded, 0, true/*once ?param=base64(something) was needed*/));
1.146 paf 648: }
649: }
650:
1.41 paf 651: // constructor
652:
1.126 paf 653: MString::MString(): Methoded("string") {
1.1 paf 654: // ^string.length[]
1.41 paf 655: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 656:
1.1 paf 657: // ^string.int[]
1.72 parser 658: // ^string.int(default)
659: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 660: // ^string.double[]
1.72 parser 661: // ^string.double(default)
662: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151 misha 663: // ^void.bool[]
664: // ^void.bool(default)
665: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9 paf 666:
1.24 paf 667: // ^string.format{format}
1.41 paf 668: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 669:
1.15 paf 670: // ^string.left(n)
1.41 paf 671: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 672: // ^string.right(n)
1.41 paf 673: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 674: // ^string.mid(p;n)
1.82 parser 675: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 676:
677: // ^string.pos[substr]
1.41 paf 678: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 679:
1.118 paf 680: // ^string.split[delim]
681: // ^string.split[delim][options]
1.156 misha 682: // ^string.split[delim][options][column name]
683: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.118 paf 684: // old names for backward compatibility
685: // ^string.lsplit[delim]
686: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
687: // ^string.rsplit[delim]
688: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
689:
1.32 paf 690: // ^string.match[regexp][options]
691: // ^string.match[regexp][options]{replacement-code}
1.41 paf 692: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 693:
694: // ^string.toupper[]
695: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
696: // ^string.tolower[]
697: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 698:
1.70 parser 699: // ^sql[query]
1.141 paf 700: // ^sql[query][options hash]
1.67 parser 701: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 702:
703: // ^string.replace[table]
704: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 705:
706: // ^string.save[file]
1.87 paf 707: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 708:
1.112 paf 709: // ^string.normalize[]
710: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133 paf 711:
712: // ^string.trim[[start|both|end][;chars]]
713: add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139 paf 714:
715: // ^string.append[string]
716: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.146 paf 717:
718: // ^string.base64[] << encode
1.147 paf 719: // ^string:base64[encoded string] << decode
1.146 paf 720: add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.2 paf 721: }
E-mail: