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