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

1.24      paf         1: /** @file
                      2:        Parser: @b string parser class.
                      3: 
1.4       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.75      parser      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.24      paf         6: 
1.76    ! parser      7:        $Id: string.C,v 1.75 2001/09/26 10:32:25 parser Exp $
1.1       paf         8: */
                      9: 
1.43      paf        10: #include "classes.h"
1.1       paf        11: #include "pa_request.h"
                     12: #include "pa_vdouble.h"
                     13: #include "pa_vint.h"
1.17      paf        14: #include "pa_vtable.h"
1.25      paf        15: #include "pa_vbool.h"
1.27      paf        16: #include "pa_string.h"
1.53      parser     17: #include "pa_sql_connection.h"
1.71      parser     18: #include "pa_dictionary.h"
1.1       paf        19: 
1.41      paf        20: // defines
1.1       paf        21: 
1.41      paf        22: #define STRING_CLASS_NAME "string"
                     23: 
                     24: // class
                     25: 
                     26: class MString : public Methoded {
                     27: public:
                     28:        MString(Pool& pool);
1.44      paf        29: public: // Methoded
1.53      parser     30:        bool used_directly() { return true; }
1.41      paf        31: };
1.1       paf        32: 
                     33: // methods
                     34: 
1.47      paf        35: static void _length(Request& r, const String& method_name, MethodParams *) {
1.1       paf        36:        Pool& pool=r.pool();
1.47      paf        37:        Value& result=*new(pool) VDouble(pool, r.self->get_string()->size());
                     38:        result.set_name(method_name);
                     39:        r.write_no_lang(result);
1.1       paf        40: }
                     41: 
1.72      parser     42: static void _int(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        43:        Pool& pool=r.pool();
1.72      parser     44:        bool convert_problem=false; Exception rethrow_me;
                     45:        int converted;
                     46:        PTRY {
                     47:                converted=r.self->as_int();
                     48:        }
                     49:        PCATCH(e) { // convert problem
                     50:                if(convert_problem=params->size()==0) { // we have a problem when do not have default
                     51:                        rethrow_me=e;  
                     52:                        converted=0;
                     53:                } else
                     54:                        converted=params->as_int(0, r); // (default)
                     55:        }
                     56:        PEND_CATCH
                     57:        if(convert_problem)
                     58:                PTHROW(rethrow_me.type(), rethrow_me.code(), 
                     59:                        rethrow_me.problem_source(),
                     60:                        rethrow_me.comment());
                     61: 
                     62:        Value& result=*new(pool) VInt(pool, converted);
1.47      paf        63:        result.set_name(method_name);
                     64:        r.write_no_lang(result);
1.1       paf        65: }
                     66: 
1.72      parser     67: static void _double(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        68:        Pool& pool=r.pool();
1.72      parser     69:        bool convert_problem=false; Exception rethrow_me;
                     70:        double converted;
                     71:        PTRY {
                     72:                converted=r.self->as_double();
                     73:        }
                     74:        PCATCH(e) { // convert problem
                     75:                if(convert_problem=params->size()==0) { // we have a problem when do not have default
                     76:                        rethrow_me=e;  
                     77:                        converted=0;
                     78:                } else
                     79:                        converted=params->as_double(0, r); // (default)
                     80:        }
                     81:        PEND_CATCH
                     82:        if(convert_problem)
                     83:                PTHROW(rethrow_me.type(), rethrow_me.code(), 
                     84:                        rethrow_me.problem_source(),
                     85:                        rethrow_me.comment());
                     86: 
                     87:        Value& result=*new(pool) VDouble(pool, converted);
1.47      paf        88:        result.set_name(method_name);
                     89:        r.write_no_lang(result);
1.1       paf        90: }
                     91: 
1.38      paf        92: /*not static*/void _string_format(Request& r, const String& method_name, MethodParams *params) {
1.9       paf        93:        Pool& pool=r.pool();
                     94: 
1.59      parser     95:        Value& fmt=params->as_junction(0, "fmt must be code");
1.9       paf        96: 
1.24      paf        97:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
                     98:        char *buf=format(pool, r.self->as_double(), r.process(fmt).as_string().cstr());
1.63      parser     99: 
                    100:        String result(pool);
                    101:        result.APPEND_CLEAN(buf, 0, 
                    102:                method_name.origin().file,
                    103:                method_name.origin().line);
                    104:        r.write_no_lang(result);
1.9       paf       105: }
1.11      paf       106: 
1.38      paf       107: static void _left(Request& r, const String&, MethodParams *params) {
1.15      paf       108:        Pool& pool=r.pool();
                    109: 
1.38      paf       110:        size_t n=(size_t)r.process(params->get(0)).as_double();
1.15      paf       111:        
                    112:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf       113:        r.write_assign_lang(*new(pool) VString(string.mid(0, n)));
1.15      paf       114: }
                    115: 
1.38      paf       116: static void _right(Request& r, const String&, MethodParams *params) {
1.15      paf       117:        Pool& pool=r.pool();
                    118: 
1.38      paf       119:        size_t n=(size_t)r.process(params->get(0)).as_double();
1.15      paf       120:        
                    121:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf       122:        r.write_assign_lang(*new(pool) VString(string.mid(string.size()-n, string.size())));
1.15      paf       123: }
                    124: 
1.38      paf       125: static void _mid(Request& r, const String&, MethodParams *params) {
1.15      paf       126:        Pool& pool=r.pool();
                    127: 
1.38      paf       128:        size_t p=(size_t)r.process(params->get(0)).as_double();
                    129:        size_t n=(size_t)r.process(params->get(1)).as_double();
1.15      paf       130:        
                    131:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf       132:        r.write_assign_lang(*new(pool) VString(string.mid(p, p+n)));
1.15      paf       133: }
                    134: 
1.38      paf       135: static void _pos(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       136:        Pool& pool=r.pool();
                    137: 
1.59      parser    138:        Value& substr=params->as_no_junction(0, "substr must not be code");
1.16      paf       139:        
                    140:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    141:        r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
                    142: }
                    143: 
1.38      paf       144: static void split_list(Request& r, const String& method_name, MethodParams *params,
1.23      paf       145:                                           const String& string, 
                    146:                                           Array& result) {
1.59      parser    147:        Value& delim_value=params->as_no_junction(0, "delimiter must not be code");
1.23      paf       148: 
1.45      paf       149:        string.split(result, 0, delim_value.as_string());
1.19      paf       150: }
                    151: 
1.38      paf       152: static void _lsplit(Request& r, const String& method_name, MethodParams *params) {
1.19      paf       153:        Pool& pool=r.pool();
                    154:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    155: 
1.61      parser    156:        Array pieces(pool);
                    157:        split_list(r, method_name, params, string, pieces);
                    158: 
                    159:        Array& columns=*new(pool) Array(pool);
                    160:        columns+=new(pool) String(pool, "piece");
1.19      paf       161: 
1.21      paf       162:        Table& table=*new(pool) Table(pool, &string, 
1.61      parser    163:                &columns, pieces.size());
1.69      parser    164:        Array_iter i(pieces);
                    165:        while(i.has_next()) {
1.61      parser    166:                Array& row=*new(pool) Array(pool);
1.69      parser    167:                row+=i.next();
1.61      parser    168:                table+=&row;
                    169:        }
1.19      paf       170:        r.write_no_lang(*new(pool) VTable(pool, &table));
                    171: }
                    172: 
1.38      paf       173: static void _rsplit(Request& r, const String& method_name, MethodParams *params) {
1.19      paf       174:        Pool& pool=r.pool();
                    175:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    176: 
1.61      parser    177:        Array pieces(pool);
                    178:        split_list(r, method_name, params, string, pieces);
1.19      paf       179: 
1.61      parser    180:        Array& columns=*new(pool) Array(pool);
                    181:        columns+=new(pool) String(pool, "piece");
1.21      paf       182: 
                    183:        Table& table=*new(pool) Table(pool, &string, 
1.61      parser    184:                &columns, pieces.size());
                    185:        for(int i=pieces.size(); --i>=0; ) {
                    186:                Array& row=*new(pool) Array(pool);
                    187:                row+=pieces.get(i);
                    188:                table+=&row;
                    189:        }
1.17      paf       190: 
1.19      paf       191:        r.write_no_lang(*new(pool) VTable(pool, &table));
1.17      paf       192: }
                    193: 
1.33      paf       194: static void search_action(Table& table, Array *row, int, int, void *) {
1.28      paf       195:        if(row)
                    196:                table+=row;
1.27      paf       197: }
                    198: 
1.74      parser    199: #ifndef DOXYGEN
1.27      paf       200: struct Replace_action_info {
1.30      paf       201:        Request *request;  const String *origin;
1.31      paf       202:        const String *src;  String *dest;
1.27      paf       203:        Value *replacement_code;
1.29      paf       204:        const String *post_match;
1.27      paf       205: };
1.74      parser    206: #endif
1.33      paf       207: static void replace_action(Table& table, Array *row, int start, int finish, 
1.31      paf       208:                                                           void *info) {
1.27      paf       209:        Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31      paf       210:        if(row) { // begin&middle
                    211:                // piece from last match['start'] to beginning of this match['finish']
                    212:                if(start!=finish)
1.37      paf       213:                        *ai.dest << ai.src->mid(start, finish);//ai.dest->APPEND_CONST("-");
1.51      parser    214:                // store found parts in one-record VTable
1.32      paf       215:                if(table.size()) // middle
                    216:                        table.put(0, row);
                    217:                else // begin
1.30      paf       218:                        table+=row;
                    219:                { // execute 'replacement_code' in 'table' context
                    220:                        VTable& vtable=*new(table.pool()) VTable(table.pool(), &table);
                    221:                        vtable.set_name(*ai.origin);
                    222: 
                    223:                        Junction *junction=ai.replacement_code->get_junction();
1.66      parser    224:                        Value *saved_match_var_value=junction->root->get_element(*match_var_name);
                    225:                        junction->root->put_element(*match_var_name, &vtable);
1.30      paf       226:                        Value& replaced=ai.request->process(*ai.replacement_code, ai.origin, false);
1.66      parser    227:                        junction->root->put_element(*match_var_name, saved_match_var_value);
1.30      paf       228: 
1.34      paf       229:                        /*
1.31      paf       230:                        ai.dest->APPEND_CONST("(");
1.37      paf       231:                                *ai.dest << *(String *)row->get(1/*match* /);
1.31      paf       232:                        ai.dest->APPEND_CONST(")");
1.34      paf       233:                        */
1.37      paf       234:                        *ai.dest << replaced.as_string();
1.29      paf       235:                }
                    236:                ai.post_match=(String *)row->get(2/*post_match*/);
                    237:        } else // end
1.37      paf       238:                *ai.dest << *ai.post_match;
1.27      paf       239: }
                    240: 
1.50      parser    241: /// @todo use pcre:study somehow
1.38      paf       242: static void _match(Request& r, const String& method_name, MethodParams *params) {
1.24      paf       243:        Pool& pool=r.pool();
1.28      paf       244:        const String& src=*static_cast<VString *>(r.self)->get_string();
1.24      paf       245: 
1.59      parser    246:        Value& regexp=params->as_no_junction(0, "regexp must not be code");
1.38      paf       247: 
                    248:        const String *options=
                    249:                params->size()>1?
1.59      parser    250:                &params->as_no_junction(1, "options must not be code").as_string():0;
1.24      paf       251: 
1.27      paf       252:        Value *result;
1.24      paf       253:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.25      paf       254:        Table *table;
1.27      paf       255:        if(params->size()<3) { // search
1.64      parser    256:                bool was_global;
1.76    ! parser    257:                bool matched=src.match(r.pcre_tables(),
1.39      paf       258:                        &method_name, 
1.32      paf       259:                        regexp.as_string(), options,
1.27      paf       260:                        &table,
1.64      parser    261:                        search_action, 0,
                    262:                        &was_global);
1.60      parser    263:                // matched
1.64      parser    264:                // not (just matched[3=pre/match/post], no substrings) or Global search
                    265:                if(table->columns()->size()>3 || was_global) 
                    266:                        result=new(pool) VTable(pool, table); // table of pre/match/post+substrings
                    267:                else 
                    268:                        result=new(pool) VBool(pool, matched);                  
1.27      paf       269:        } else { // replace
1.59      parser    270:                Value& replacement_code=params->as_junction(2, "replacement code must be code");
1.24      paf       271: 
1.28      paf       272:                String& dest=*new(pool) String(pool);
1.27      paf       273:                Replace_action_info replace_action_info={
1.30      paf       274:                        &r, &method_name,
1.31      paf       275:                        &src, &dest,
1.28      paf       276:                        &replacement_code,
1.29      paf       277:                        &src
1.27      paf       278:                };
1.76    ! parser    279:                src.match(r.pcre_tables(),
1.39      paf       280:                        &method_name, 
1.27      paf       281:                        r.process(regexp).as_string(), options,
                    282:                        &table,
1.33      paf       283:                        replace_action, &replace_action_info);
1.28      paf       284:                result=new(pool) VString(dest);
1.27      paf       285:        }
1.26      paf       286:        result->set_name(method_name);
1.34      paf       287:        r.write_assign_lang(*result);
1.24      paf       288: }
                    289: 
1.49      parser    290: static void change_case(Request& r, const String& method_name, MethodParams *params, 
                    291:                                                String::Change_case_kind kind) {
                    292:        Pool& pool=r.pool();
                    293:        const String& src=*static_cast<VString *>(r.self)->get_string();
                    294: 
1.76    ! parser    295:        r.write_assign_lang(*new(pool) VString(src.change_case(pool, r.pcre_tables(),
1.49      parser    296:                kind)));
                    297: }
                    298: static void _upper(Request& r, const String& method_name, MethodParams *params) {
                    299:        change_case(r, method_name, params, String::CC_UPPER);
                    300: }
                    301: static void _lower(Request& r, const String& method_name, MethodParams *params) {
                    302:        change_case(r, method_name, params, String::CC_LOWER);
                    303: }
                    304: 
1.65      parser    305: #ifndef DOXYGEN
                    306: class String_sql_event_handlers : public SQL_Driver_query_event_handlers {
                    307: public:
                    308:        String_sql_event_handlers(Pool& apool, 
                    309:                const String& astatement_string, const char *astatement_cstr) :
                    310:                pool(apool), 
                    311:                statement_string(astatement_string),
                    312:                statement_cstr(astatement_cstr),
                    313:                got_column(false), got_cell(false) {
                    314:                result=new(pool) String(pool);
                    315:        }
                    316: 
                    317:        void add_column(void *ptr, size_t size) {
                    318:                if(got_column)
                    319:                        PTHROW(0, 0,
                    320:                                &statement_string,
                    321:                                "result must contain exactly one column");
                    322:                got_column=true;
                    323:        }
                    324:        void before_rows() { /* ignore */ }
                    325:        void add_row() { /* ignore */ }
                    326:        void add_row_cell(void *ptr, size_t size) {
                    327:                if(got_cell)
                    328:                        PTHROW(0, 0,
                    329:                                &statement_string,
                    330:                                "result must not contain more then one row");
                    331:                got_cell=true;
                    332: 
                    333:                result->APPEND_TAINTED((const char *)ptr, size, statement_cstr, 0);
                    334:        }
                    335: 
                    336: private:
                    337:        Pool& pool;
                    338:        const String& statement_string; const char *statement_cstr;
                    339:        bool got_column;
                    340: public:
                    341:        bool got_cell;
                    342:        String *result;
                    343: };
                    344: #endif
1.70      parser    345: const String* sql_result_string(Request& r, const String& method_name, MethodParams *params,
                    346:                                                                Hash *&options) {
1.53      parser    347:        Pool& pool=r.pool();
                    348: 
                    349:        if(!r.connection)
                    350:                PTHROW(0, 0,
                    351:                        &method_name,
                    352:                        "without connect");
                    353: 
1.59      parser    354:        Value& statement=params->as_junction(0, "statement must be code");
1.53      parser    355: 
1.70      parser    356:        ulong limit=0;
                    357:        ulong offset=0;
                    358:        if(params->size()>1) {
1.73      parser    359:                Value& voptions=params->as_no_junction(1, "options must be hash, not code");
                    360:                if(voptions.is_defined())
                    361:                        if(options=voptions.get_hash()) {
                    362:                                if(Value *vlimit=(Value *)options->get(*sql_limit_name))
                    363:                                        limit=(ulong)r.process(*vlimit).as_double();
                    364:                                if(Value *voffset=(Value *)options->get(*sql_offset_name))
                    365:                                        offset=(ulong)r.process(*voffset).as_double();
                    366:                        } else
                    367:                                PTHROW(0, 0,
                    368:                                        &method_name,
                    369:                                        "options must be hash");
1.70      parser    370:        } else
                    371:                options=0;
                    372: 
1.53      parser    373:        Temp_lang temp_lang(r, String::UL_SQL);
                    374:        const String& statement_string=r.process(statement).as_string();
                    375:        const char *statement_cstr=
                    376:                statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.65      parser    377:        String_sql_event_handlers handlers(pool, statement_string, statement_cstr);
1.53      parser    378:        bool need_rethrow=false; Exception rethrow_me;
                    379:        PTRY {
                    380:                r.connection->query(
1.70      parser    381:                        statement_cstr, offset, limit, 
1.65      parser    382:                        handlers);
1.53      parser    383:        }
                    384:        PCATCH(e) { // query problem
                    385:                rethrow_me=e;  need_rethrow=true;
                    386:        }
                    387:        PEND_CATCH
                    388:        if(need_rethrow)
                    389:                PTHROW(rethrow_me.type(), rethrow_me.code(),
                    390:                        &statement_string, // setting more specific source [were url]
                    391:                        rethrow_me.comment());
                    392:        
1.65      parser    393:        if(!handlers.got_cell)
                    394:                return 0; // no lines, caller should return second param[default value]
1.62      parser    395: 
1.65      parser    396:        return handlers.result;
1.53      parser    397: }
                    398: 
                    399: static void _sql(Request& r, const String& method_name, MethodParams *params) {
                    400:        Pool& pool=r.pool();
                    401: 
1.70      parser    402:        Hash *options;
                    403:        const String *string=sql_result_string(r, method_name, params, options);
1.62      parser    404:        if(!string) {
1.70      parser    405:                if(options) {
                    406:                        if(Value *vdefault=(Value *)options->get(*sql_default_name)) {
                    407:                                if(!vdefault->get_junction())
                    408:                                        PTHROW(0, 0,
                    409:                                                &method_name,
                    410:                                                "default option must be code");
                    411:                                string=r.process(*vdefault).get_string();
                    412:                                if(!string)
                    413:                                        string=empty_string;
                    414:                        } else
                    415:                                PTHROW(0, 0,
                    416:                                        &method_name,
                    417:                                        "produced no result, but no default option specified");
1.68      parser    418:                } else
                    419:                        PTHROW(0, 0,
                    420:                                &method_name,
1.70      parser    421:                                "produced no result, but no options (no default) specified");
1.62      parser    422:        }
                    423:        VString& result=*new(pool) VString(*string);
1.53      parser    424:        result.set_name(method_name);
                    425:        r.write_assign_lang(result);
                    426: }
                    427: 
1.71      parser    428: static void _replace(Request& r, const String& method_name, MethodParams *params) {
                    429:        Pool& pool=r.pool();
                    430:        const String& src=*static_cast<VString *>(r.self)->get_string();
                    431: 
                    432:        Table *table=params->as_no_junction(0, "parameter must not be code").get_table();
                    433:        if(!table)
                    434:                PTHROW(0, 0,
                    435:                        &method_name,
                    436:                        "parameter must be table");
                    437: 
                    438:        Dictionary dict(*table);
                    439:        r.write_assign_lang(*new(pool) VString(src.replace(pool, dict)));
                    440: }
1.41      paf       441: // constructor
                    442: 
                    443: MString::MString(Pool& apool) : Methoded(apool) {
                    444:        set_name(*NEW String(pool(), STRING_CLASS_NAME));
                    445: 
1.9       paf       446: 
1.1       paf       447:        // ^string.length[]
1.41      paf       448:        add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       449:        
1.1       paf       450:        // ^string.int[]
1.72      parser    451:        // ^string.int(default)
                    452:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1       paf       453:        // ^string.double[]
1.72      parser    454:        // ^string.double(default)
                    455:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9       paf       456: 
1.24      paf       457:        // ^string.format{format}
1.41      paf       458:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       459: 
1.15      paf       460:        // ^string.left(n)
1.41      paf       461:        add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       462:        // ^string.right(n)
1.41      paf       463:        add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15      paf       464:        // ^string.mid(p;n)
1.41      paf       465:        add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16      paf       466: 
                    467:        // ^string.pos[substr]
1.41      paf       468:        add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17      paf       469: 
                    470:        // ^string.lsplit[delim]
1.41      paf       471:        add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19      paf       472:        // ^string.rsplit[delim]
1.41      paf       473:        add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.24      paf       474: 
1.32      paf       475:        // ^string.match[regexp][options]
                    476:        // ^string.match[regexp][options]{replacement-code}
1.41      paf       477:        add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49      parser    478: 
                    479:        // ^string.toupper[]
                    480:        add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
                    481:        // ^string.tolower[]
                    482:        add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53      parser    483: 
1.70      parser    484:        // ^sql[query]
                    485:        // ^sql[query][$.limit(1) $.offset(2) $.default[n/a]]
1.67      parser    486:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71      parser    487: 
                    488:        // ^string.replace[table]
                    489:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.2       paf       490: }      
1.1       paf       491: 
1.41      paf       492: // global variable
                    493: 
                    494: Methoded *string_class;
                    495: 
                    496: // creator
                    497: 
                    498: Methoded *MString_create(Pool& pool) {
                    499:        return string_class=new(pool) MString(pool);
                    500: }

E-mail: