Annotation of parser3/src/classes/string.C, revision 1.156

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

E-mail: