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

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

E-mail: