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