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

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

E-mail: