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

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

E-mail: