Annotation of parser3/src/classes/string.C, revision 1.125.2.15.2.23
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.125.2.1 paf 4: Copyright (c) 2001-2003 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.125.2.15.2. 3(paf 8:3): static const char* IDENT_STRING_C="$Date: 2003/04/08 11:08:11 $";
1.1 paf 9:
1.43 paf 10: #include "classes.h"
1.125.2.4 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.125.2.9 paf 25: class MString: public Methoded {
1.41 paf 26: public:
1.125.2.7 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.125.2.7 paf 32: // global variable
33:
1.125.2.12 paf 34: DECLARE_CLASS_VAR(string, new MString, 0);
1.125.2.7 paf 35:
1.125.2.8 paf 36: // defines for statics
37:
38: #define MATCH_VAR_NAME "match"
39:
40: // statics
41:
1.125.2.15.2. 1(paf 42:3): static const String match_var_name(MATCH_VAR_NAME);
1.125.2.8 paf 43:
1.1 paf 44: // methods
45:
1.125.2.15.2. 0(paf 46:3): static void _length(Request& r, MethodParams&) {
3(paf 47:3): double result=GET_SELF(r, VString).string().length();
48:3): r.write_no_lang(*new VDouble(result));
1.1 paf 49: }
50:
1.125.2.15.2. 0(paf 51:3): static void _int(Request& r, MethodParams& params) {
3(paf 52:3): const String& self_string=GET_SELF(r, VString).string();
1.72 parser 53: int converted;
1.125.2.15.2. 0(paf 54:3): Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be int")
(paf 55:): :0; // (default)
1.84 parser 56: try {
1.125.2.15.2. 3(paf 57:3): if(self_string.is_empty())
1.121 paf 58: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 59:3): 0,
1.121 paf 60: "parameter is empty string, error converting");
1.125.2.15.2. 3(paf 61:3): converted=self_string.as_int();
1.84 parser 62: } catch(...) { // convert problem
1.125.2.15.2. 3(paf 63:3): if(default_code)
64:3): converted=r.process_to_value(*default_code).as_int();
1.84 parser 65: else
1.125.2.15.2. 3(paf 66:3): rethrow; // we have a problem when no default
1.72 parser 67: }
1.125.2.15.2. 3(paf 68:3): r.write_no_lang(*new VInt(converted));
1.1 paf 69: }
70:
1.125.2.15.2. 0(paf 71:3): static void _double(Request& r, MethodParams& params) {
(paf 72:): const String& self_string=GET_SELF(r, VString).string();
1.72 parser 73: double converted;
1.125.2.15.2. 0(paf 74:3): Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be double")
(paf 75:): :0; // (default)
1.84 parser 76: try {
1.125.2.15.2. 3(paf 77:3): if(self_string.is_empty())
1.121 paf 78: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 79:3): 0,
1.121 paf 80: "parameter is empty string, error converting");
1.125.2.15.2. 3(paf 81:3): converted=self_string.as_double();
1.84 parser 82: } catch(...) { // convert problem
1.125.2.15.2. 3(paf 83:3): if(default_code)
84:3): converted=r.process_to_value(*default_code).as_double();
1.84 parser 85: else
1.125.2.15.2. 3(paf 86:3): rethrow; // we have a problem when no default
1.72 parser 87: }
88:
1.125.2.15.2. 3(paf 89:3): r.write_no_lang(*new VDouble(converted));
1.1 paf 90: }
91:
1.125.2.15.2. 0(paf 92:3): /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9 paf 93:
1.125.2.15.2. 1(paf 94:3): Value& fmt_maybe_code=params[0];
1.95 paf 95: // for some time due to stupid {} in original design
1.125.2.15.2. (paf 96:): const String& fmt=r.process_to_string(fmt_maybe_code);
1.9 paf 97:
1.125.2.15.2. 3(paf 98:3): const char* buf=format(r.get_self().as_double(), fmt.cstrm());
1.63 parser 99:
1.125.2.15.2. 3(paf 100:3): r.write_no_lang(String(buf));
1.9 paf 101: }
1.11 paf 102:
1.125.2.15.2. 0(paf 103:3): static void _left(Request& r, MethodParams& params) {
104:3): size_t n=(size_t)params.as_int(0, "n must be int", r);
1.15 paf 105:
1.125.2.15.2. (paf 106:): const String& string=GET_SELF(r, VString).string();
3(paf 107:3): r.write_assign_lang(string.mid(0, n));
1.15 paf 108: }
109:
1.125.2.15.2. 0(paf 110:3): static void _right(Request& r, MethodParams& params) {
111:3): size_t n=(size_t)params.as_int(0, "n must be int", r);
1.15 paf 112:
1.125.2.15.2. (paf 113:): const String& string=GET_SELF(r, VString).string();
3(paf 114:3): r.write_assign_lang(string.mid(string.length()-n, string.length()));
1.15 paf 115: }
116:
1.125.2.15.2. 0(paf 117:3): static void _mid(Request& r, MethodParams& params) {
(paf 118:): const String& string=GET_SELF(r, VString).string();
1.83 parser 119:
1.125.2.15.2. 0(paf 120:3): size_t p=(size_t)max(0, params.as_int(0, "p must be int", r));
121:3): size_t n=params.count()>1?
122:3): (size_t)max(0, params.as_int(1, "n must be int", r)):string.length();
1.15 paf 123:
1.125.2.15.2. 3(paf 124:3): r.write_assign_lang(string.mid(p, p+n));
1.15 paf 125: }
126:
1.125.2.15.2. 0(paf 127:3): static void _pos(Request& r, MethodParams& params) {
128:3): Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 129:
1.125.2.15.2. (paf 130:): const String& string=GET_SELF(r, VString).string();
3(paf 131:3): r.write_assign_lang(*new VInt(string.pos(substr.as_string())));
1.16 paf 132: }
133:
1.125.2.15.2. (paf 134:): static void split_list(Request& r,
0(paf 135:3): MethodParams& params, int paramIndex,
3(paf 136:3): const String& string,
137:3): ArrayString& result) {
0(paf 138:3): Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
1.23 paf 139:
1.125.2.15.2. 3(paf 140:3): size_t pos_after=0;
141:3): string.split(result, pos_after, delim_value.as_string());
1.19 paf 142: }
143:
1.118 paf 144: #define SPLIT_LEFT 0x0001
145: #define SPLIT_RIGHT 0x0010
146: #define SPLIT_HORIZONTAL 0x0100
147: #define SPLIT_VERTICAL 0x1000
148:
1.125.2.15.2. 3(paf 149:3): static int split_options(const String* options) {
1.118 paf 150: struct Split_option {
1.125.2.1 paf 151: const char* keyL;
152: const char* keyU;
1.118 paf 153: int setBit;
154: int checkBit;
155: } split_option[]={
156: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
157: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
158: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
159: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
160: {0}
161: };
162:
163: int result=0;
1.125.2.15.2. 3(paf 164:3): if(options) {
1.118 paf 165: for(Split_option *o=split_option; o->keyL; o++)
166: if(options->pos(o->keyL)>=0
167: || (o->keyU && options->pos(o->keyU)>=0)) {
168: if(result & o->checkBit)
169: throw Exception("parser.runtime",
170: options,
171: "conflicting split options");
172: result |= o->setBit;
173: }
174: }
175:
176: return result;
177: }
178:
1.125.2.15.2. 3(paf 179:3): static Table& split_vertical(Request& r, ArrayString& pieces, bool right) {
1.61 parser 180:
1.125.2.7 paf 181: Table::columns_type columns(new ArrayString);
1.125.2.15.2. 3(paf 182:3): *columns+=new String("piece");
1.19 paf 183:
1.125.2.15.2. 3(paf 184:3): Table& table=*new Table(columns, pieces.count());
1.118 paf 185: if(right) { // right
1.125.2.7 paf 186: for(int i=pieces.count(); --i>=0; ) {
187: Table::element_type row(new ArrayString);
188: *row+=pieces[i];
1.125.2.15.2. 3(paf 189:3): table+=row;
1.118 paf 190: }
191: } else { // left
1.125.2.15.2. 3(paf 192:3): Array_iterator<const String*> i(pieces);
1.118 paf 193: while(i.has_next()) {
1.125.2.7 paf 194: Table::element_type row(new ArrayString);
195: *row+=i.next();
1.125.2.15.2. 3(paf 196:3): table+=row;
1.118 paf 197: }
1.61 parser 198: }
1.118 paf 199:
1.125.2.7 paf 200: return table;
1.19 paf 201: }
202:
1.125.2.15.2. 3(paf 203:3): static Table& split_horizontal(Request& r, ArrayString& pieces, bool right) {
204:3): Table& table=*new Table(Table::columns_type(0) /* nameless */);
1.125.2.7 paf 205: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 206: if(right) { // right
1.125.2.15.2. 7(paf 207:3): for(size_t i=pieces.count(); --i>=0; )
1.125.2.7 paf 208: *row+=pieces[i];
1.118 paf 209: } else { // left
1.125.2.15.2. 7(paf 210:3): for(Array_iterator<const String*> i(pieces); i.has_next(); )
1.125.2.7 paf 211: *row+=i.next();
1.118 paf 212: }
1.125.2.15.2. 3(paf 213:3): table+=row;
1.118 paf 214:
1.125.2.7 paf 215: return table;
1.118 paf 216: }
217:
1.125.2.15.2. 0(paf 218:3): static void split_with_options(Request& r, MethodParams& params,
1.118 paf 219: int bits) {
1.125.2.15.2. (paf 220:): const String& string=GET_SELF(r, VString).string();
1.19 paf 221:
1.125.2.7 paf 222: ArrayString pieces;
1.125.2.15.2. 3(paf 223:3): split_list(r, params, 0, string, pieces);
1.19 paf 224:
1.118 paf 225: if(!bits) {
1.125.2.15.2. 3(paf 226:3): const String* options=0;
0(paf 227:3): if(params.count()>1)
228:3): options=¶ms.as_string(1, "options must not be code");
3(paf 229:3):
1.118 paf 230: bits=split_options(options);
231: }
1.21 paf 232:
1.118 paf 233: bool right=(bits & SPLIT_RIGHT) != 0;
234: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.125.2.15.2. 3(paf 235:3): Table& table=horizontal?split_horizontal(r, pieces, right)
236:3): :split_vertical(r, pieces, right);
1.17 paf 237:
1.125.2.15.2. 3(paf 238:3): r.write_no_lang(*new VTable(&table));
1.118 paf 239: }
1.125.2.15.2. 0(paf 240:3): static void _split(Request& r, MethodParams& params) {
3(paf 241:3): split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 242: }
1.125.2.15.2. 0(paf 243:3): static void _lsplit(Request& r, MethodParams& params) {
3(paf 244:3): split_with_options(r, params, SPLIT_LEFT);
1.118 paf 245: }
1.125.2.15.2. 0(paf 246:3): static void _rsplit(Request& r, MethodParams& params) {
3(paf 247:3): split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 248: }
249:
1.125.2.15.2. 4(paf 250:3): static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 251: if(row)
1.125.2.15.2. 4(paf 252:3): table+=row;
1.27 paf 253: }
254:
1.74 parser 255: #ifndef DOXYGEN
1.27 paf 256: struct Replace_action_info {
1.125.2.15.2. 1(paf 257:3): Request* request;
258:3): const String* src; String* dest;
(paf 259:): VTable* vtable;
260:): Value* replacement_code;
1.27 paf 261: };
1.74 parser 262: #endif
1.105 paf 263: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.125.2.15.2. 4(paf 264:3): static void replace_action(Table& table, ArrayString* row,
0(paf 265:3): int prestart, int prefinish,
266:3): int poststart, int postfinish,
267:3): void *info) {
2(paf 268:3): fprintf(stderr, "+");
1.27 paf 269: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 270: if(row) { // begin&middle
1.104 paf 271: // piece from last match['prestart'] to beginning of this match['prefinish']
272: if(prestart!=prefinish)
273: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 274: // store found parts in one-record VTable
1.125.2.15.2. 4(paf 275:3): if(table.count()) // middle
276:3): table.put(0, row);
1.32 paf 277: else // begin
1.125.2.15.2. 4(paf 278:3): table+=row;
1.30 paf 279: { // execute 'replacement_code' in 'table' context
1.105 paf 280: ai.vtable->set_table(table);
1.30 paf 281:
1.125.2.15.2. 3(paf 282:3): *ai.dest << ai.request->process_to_string(*ai.replacement_code);
1.29 paf 283: }
284: } else // end
1.125.2.15.2. 3(paf 285:3): *ai.dest << ai.src->mid(poststart, postfinish);
2(paf 286:3): fprintf(stderr, "-");
1.27 paf 287: }
288:
1.50 parser 289: /// @todo use pcre:study somehow
1.125.2.15.2. 0(paf 290:3): static void _match(Request& r, MethodParams& params) {
291:3): Value& regexp=params.as_no_junction(0, "regexp must not be code");
1.38 paf 292:
1.125.2.15.2. 3(paf 293:3): const String* options=
0(paf 294:3): params.count()>1?
295:3): ¶ms.as_no_junction(1, "options must not be code").as_string():0;
1.24 paf 296:
1.125.2.15.2. (paf 297:): Temp_lang temp_lang(r, String::L_PASS_APPENDED);
298:): const String& src=GET_SELF(r, VString).string();
1.125.2.14 paf 299: bool just_matched;
1.125.2.15.2. 0(paf 300:3): if(params.count()<3) { // search
(paf 301:): Table* table=src.match(r.charsets.source(),
3(paf 302:3): regexp.as_string(), options,
1.64 parser 303: search_action, 0,
1.125.2.14 paf 304: just_matched);
1.125.2.15.2. (paf 305:): Value* result;
1.125.2.14 paf 306: if(table)
1.125.2.15.2. 3(paf 307:3): result=new VTable(table); // table of pre/match/post+substrings
1.64 parser 308: else
1.125.2.15.2. 3(paf 309:3): result=new VBool(just_matched);
310:3): r.write_assign_lang(*result);
1.27 paf 311: } else { // replace
1.125.2.15.2. 0(paf 312:3): Value& replacement_code=params.as_junction(2, "replacement param must be code");
1.24 paf 313:
1.125.2.7 paf 314: String result;
1.125.2.15.2. 3(paf 315:3): VTable* vtable=new VTable;
1.125.2.7 paf 316: Replace_action_info info;
317: info.request=&r;
1.125.2.15.2. 3(paf 318:3): info.src=&src;
1.125.2.7 paf 319: info.dest=&result;
320: info.vtable=vtable;
1.125.2.15.2. 3(paf 321:3): info.replacement_code=&replacement_code;
1.105 paf 322: Temp_value_element temp_match_var(
1.125.2.15.2. 3(paf 323:3): *replacement_code.get_junction()->method_frame,
1.125.2.7 paf 324: match_var_name, vtable);
1.125.2.15.2. 3(paf 325:3): src.match(r.charsets.source(),
326:3): r.process_to_string(regexp), options,
1.125.2.14 paf 327: replace_action, &info,
328: just_matched);
1.102 paf 329: r.write_assign_lang(result);
1.27 paf 330: }
1.24 paf 331: }
332:
1.125.2.15.2. 0(paf 333:3): static void change_case(Request& r, MethodParams& params,
1.49 parser 334: String::Change_case_kind kind) {
1.125.2.15.2. (paf 335:): const String& src=GET_SELF(r, VString).string();
1.49 parser 336:
1.125.2.15.2. 0(paf 337:3): r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 338: }
1.125.2.15.2. 0(paf 339:3): static void _upper(Request& r, MethodParams& params) {
3(paf 340:3): change_case(r, params, String::CC_UPPER);
1.49 parser 341: }
1.125.2.15.2. 0(paf 342:3): static void _lower(Request& r, MethodParams& params) {
3(paf 343:3): change_case(r, params, String::CC_LOWER);
1.49 parser 344: }
345:
1.65 parser 346: #ifndef DOXYGEN
1.125.2.7 paf 347: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.125.2.15.2. (paf 348:): const String& statement_string; const char* statement_cstr;
1.125.2.7 paf 349: bool got_column;
1.65 parser 350: public:
1.125.2.7 paf 351: bool got_cell;
1.125.2.15.2. 3(paf 352:3): String& result;
1.125.2.7 paf 353: public:
1.125.2.15 paf 354: String_sql_event_handlers(
1.125.2.15.2. (paf 355:): const String& astatement_string, const char* astatement_cstr):
1.125.2.15 paf 356: statement_string(astatement_string), statement_cstr(astatement_cstr),
357: got_column(false),
358: got_cell(false),
1.125.2.15.2. 3(paf 359:3): result(*new String) {}
1.65 parser 360:
1.125.2.15.2. 9(paf 361:3): bool add_column(SQL_Error& error, const char* str, size_t /*length*/) {
1.124 paf 362: if(got_column) {
363: error=SQL_Error("parser.runtime",
1.125.2.7 paf 364: //statement_string,
1.65 parser 365: "result must contain exactly one column");
1.124 paf 366: return true;
367: }
1.65 parser 368: got_column=true;
1.124 paf 369: return false;
1.65 parser 370: }
1.124 paf 371: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
372: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.125.2.15.2. 9(paf 373:3): bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 374: if(got_cell) {
375: error=SQL_Error("parser.runtime",
1.125.2.7 paf 376: //statement_string,
1.65 parser 377: "result must not contain more then one row");
1.124 paf 378: return true;
379: }
1.65 parser 380:
1.124 paf 381: try {
382: got_cell=true;
1.125.2.15.2. 3(paf 383:3): result.append_know_length(str, length, String::L_TAINTED);
1.124 paf 384: return false;
385: } catch(...) {
386: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
387: return true;
388: }
1.65 parser 389: }
390: };
391: #endif
1.125.2.15.2. 6(paf 392:3): extern String sql_limit_name;
393:3): extern String sql_offset_name;
394:3): extern String sql_default_name;
395:3): extern String sql_distinct_name;
0(paf 396:3): const String* sql_result_string(Request& r, MethodParams& params,
3(paf 397:3): HashStringValue*& options, Value*& default_code) {
0(paf 398:3): Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 399:
1.70 parser 400: ulong limit=0;
401: ulong offset=0;
1.125.2.15.2. (paf 402:): default_code=0;
0(paf 403:3): if(params.count()>1) {
404:3): Value& voptions=params.as_no_junction(1, "options must be hash, not code");
3(paf 405:3): if(!voptions.is_string())
406:3): if(options=voptions.get_hash()) {
(paf 407:): if(Value* vlimit=options->get(sql_limit_name))
3(paf 408:3): limit=(ulong)r.process_to_value(*vlimit).as_double();
(paf 409:): if(Value* voffset=options->get(sql_offset_name))
3(paf 410:3): offset=(ulong)r.process_to_value(*voffset).as_double();
1.125.2.7 paf 411: if(default_code=options->get(sql_default_name)) {
1.125.2.15.2. (paf 412:): if(Junction* default_junction=default_code->get_junction())
1.119 paf 413: ;//default_junction->change_context(statement.get_junction());
1.110 paf 414: else
1.98 paf 415: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 416:3): 0,
1.81 parser 417: "default option must be code");
418: }
1.73 parser 419: } else
1.98 paf 420: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 421:3): 0,
1.73 parser 422: "options must be hash");
1.70 parser 423: } else
424: options=0;
425:
1.125.2.15.2. (paf 426:): Temp_lang temp_lang(r, String::L_SQL);
427:): const String& statement_string=r.process_to_string(statement);
1.125.2.1 paf 428: const char* statement_cstr=
1.125.2.15.2. 3(paf 429:3): statement_string.cstr(String::L_UNSPECIFIED, r.connection());
1.125.2.7 paf 430: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.125.2.15.2. (paf 431:): r.connection()->query(
1.117 paf 432: statement_cstr, offset, limit,
433: handlers,
434: statement_string);
1.53 parser 435:
1.65 parser 436: if(!handlers.got_cell)
1.125.2.15.2. (paf 437:): return 0; // no lines, caller should return second param[default value]
1.62 parser 438:
1.125.2.15.2. 3(paf 439:3): return &handlers.result;
1.53 parser 440: }
441:
1.125.2.15.2. 0(paf 442:3): static void _sql(Request& r, MethodParams& params) {
1.53 parser 443:
1.125.2.7 paf 444: HashStringValue* options;
1.125.2.15.2. (paf 445:): Value* default_code;
3(paf 446:3): const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 447: if(!string) {
1.81 parser 448: if(default_code) {
1.125.2.15.2. 3(paf 449:3): string=&r.process_to_string(*default_code);
1.68 parser 450: } else
1.98 paf 451: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 452:3): 0,
1.81 parser 453: "produced no result, but no default option specified");
1.62 parser 454: }
1.100 paf 455:
1.102 paf 456: r.write_assign_lang(*string);
1.53 parser 457: }
458:
1.125.2.15.2. 0(paf 459:3): static void _replace(Request& r, MethodParams& params) {
(paf 460:): const String& src=GET_SELF(r, VString).string();
1.71 parser 461:
1.125.2.15.2. 0(paf 462:3): Table* table=params.as_no_junction(0, "parameter must not be code").get_table();
1.71 parser 463: if(!table)
1.98 paf 464: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 465:3): 0,
1.71 parser 466: "parameter must be table");
467:
1.125.2.15.2. 3(paf 468:3): Dictionary dict(*table);
469:3): r.write_assign_lang(src.replace(dict));
1.71 parser 470: }
1.79 parser 471:
1.125.2.15.2. 0(paf 472:3): static void _save(Request& r, MethodParams& params) {
473:3): const String& file_name=params.as_string(params.count()-1,
1.87 paf 474: "file name must be string");
1.79 parser 475:
1.125.2.15.2. (paf 476:): const String& src=GET_SELF(r, VString).string();
1.79 parser 477:
1.87 paf 478: bool do_append=false;
1.125.2.15.2. 0(paf 479:3): if(params.count()>1) {
480:3): const String& mode=params.as_string(0, "mode must be string");
3(paf 481:3): if(mode=="append")
1.87 paf 482: do_append=true;
483: else
1.98 paf 484: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 485:3): &mode,
1.87 paf 486: "unknown mode, must be 'append'");
487: }
488:
1.79 parser 489: // write
1.125.2.15.2. 3(paf 490:3): const char* buf=src.cstr(String::L_UNSPECIFIED, r.connection(false/*no error if none*/));
1.94 paf 491: file_write(r.absolute(file_name),
1.89 paf 492: buf, strlen(buf), true, do_append);
1.79 parser 493: }
494:
1.125.2.15.2. 0(paf 495:3): static void _normalize(Request& r, MethodParams&) {
(paf 496:): const String& src=GET_SELF(r, VString).string();
1.125.2.7 paf 497:
1.125.2.15.2. 3(paf 498:3): r.write_assign_lang(src);
1.109 paf 499: }
500:
1.41 paf 501: // constructor
502:
1.125.2.7 paf 503: MString::MString(): Methoded("string") {
1.1 paf 504: // ^string.length[]
1.41 paf 505: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 506:
1.1 paf 507: // ^string.int[]
1.72 parser 508: // ^string.int(default)
509: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 510: // ^string.double[]
1.72 parser 511: // ^string.double(default)
512: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9 paf 513:
1.24 paf 514: // ^string.format{format}
1.41 paf 515: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 516:
1.15 paf 517: // ^string.left(n)
1.41 paf 518: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 519: // ^string.right(n)
1.41 paf 520: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 521: // ^string.mid(p;n)
1.82 parser 522: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 523:
524: // ^string.pos[substr]
1.41 paf 525: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 526:
1.118 paf 527: // ^string.split[delim]
528: // ^string.split[delim][options]
529: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 2);
530: // old names for backward compatibility
531: // ^string.lsplit[delim]
532: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
533: // ^string.rsplit[delim]
534: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
535:
1.32 paf 536: // ^string.match[regexp][options]
537: // ^string.match[regexp][options]{replacement-code}
1.41 paf 538: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 539:
540: // ^string.toupper[]
541: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
542: // ^string.tolower[]
543: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 544:
1.70 parser 545: // ^sql[query]
546: // ^sql[query][$.limit(1) $.offset(2) $.default[n/a]]
1.67 parser 547: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 548:
549: // ^string.replace[table]
550: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 551:
552: // ^string.save[file]
1.87 paf 553: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 554:
1.112 paf 555: // ^string.normalize[]
556: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.2 paf 557: }
E-mail: