Annotation of parser3/src/classes/hashfile.C, revision 1.36

1.1       parser      1: /** @file
                      2:        Parser: @b hashfile parser class.
                      3: 
1.36    ! paf         4:        Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.22      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.24      paf         6: */
                      7: 
1.1       parser      8: 
1.36    ! paf         9: static const char * const IDENT="$Id: hashfile.C,v 1.35 2005/08/08 09:24:27 paf Exp $";
1.1       parser     10: 
1.4       parser     11: #include "classes.h"
1.1       parser     12: 
                     13: #include "pa_request.h"
1.24      paf        14: #include "pa_vmethod_frame.h"
1.1       parser     15: #include "pa_vhashfile.h"
                     16: #include "pa_vhash.h"
                     17: 
                     18: // class
                     19: 
                     20: class MHashfile : public Methoded {
                     21: public: // VStateless_class
1.34      paf        22:        Value *create_new_value(Pool& apool, HashStringValue&) { return new VHashfile(apool); }
1.1       parser     23: 
                     24: public:
1.24      paf        25:        MHashfile();
1.1       parser     26: public: // Methoded
                     27:        bool used_directly() { return true; }
                     28: };
                     29: 
1.24      paf        30: // global variable
                     31: 
                     32: DECLARE_CLASS_VAR(hashfile, new MHashfile, 0);
                     33: 
1.35      paf        34: // defines for statics
                     35: 
                     36: #define OPEN_DATA_NAME "HASHFILE-OPEN-DATA"
                     37: 
1.1       parser     38: // methods
                     39: 
1.35      paf        40: typedef Hash<const String::Body, bool> HashStringBool;
                     41: 
1.24      paf        42: static void _open(Request& r, MethodParams& params) {
1.35      paf        43:        HashStringBool* file_list=static_cast<HashStringBool*>(r.classes_conf.get(OPEN_DATA_NAME));
                     44:        if(!file_list) {
                     45:                file_list=new HashStringBool();
                     46:                r.classes_conf.put(OPEN_DATA_NAME, file_list);
                     47:        }
                     48: 
                     49:        const String& file_spec=r.absolute(params.as_string(0, "filename must be string"));
                     50:        if(file_list->get(file_spec))
                     51:                throw Exception("parser.runtime",
                     52:                        0,
                     53:                        "this hashfile is already opened, use existing variable");
                     54:        file_list->put(file_spec, true);
                     55: 
1.24      paf        56:        VHashfile& self=GET_SELF(r, VHashfile);
1.35      paf        57:        self.open(file_spec);
1.9       paf        58: }
1.1       parser     59: 
1.31      paf        60: static void _hash(Request& r, MethodParams&) {
1.24      paf        61:        VHashfile& self=GET_SELF(r, VHashfile);
1.5       parser     62:        
                     63:        // write out result
1.26      paf        64:        VHash& result=*new VHash(*self.get_hash());
1.5       parser     65:        r.write_no_lang(result);
                     66: }
1.26      paf        67: 
1.24      paf        68: static void _delete(Request& r, MethodParams& params) {
                     69:        VHashfile& self=GET_SELF(r, VHashfile);
1.8       parser     70:        
1.30      paf        71:        if(!params.count()) {
                     72:                // ^hashfile.delete[] asked to delete hashfile itself
                     73:                self.delete_files();
                     74:                return;
                     75:        }
1.25      paf        76:        // key
                     77:        const String &key=params.as_string(0, "key must be string");
                     78:        // remove
                     79:        self.remove(key);
1.8       parser     80: }
                     81: 
1.24      paf        82: static void _clear(Request& r, MethodParams&) {
                     83:        VHashfile& self=GET_SELF(r, VHashfile);
                     84:        self.clear();
1.12      paf        85: }
                     86: 
1.27      paf        87: #ifndef DOXYGEN
1.28      paf        88: struct Foreach_info {
1.27      paf        89:        Request* r;
                     90:        const String* key_var_name;
                     91:        const String* value_var_name;
                     92:        Value* body_code;
                     93:        Value* delim_maybe_code;
                     94: 
                     95:        Value* var_context;
                     96:        VString* vkey;
                     97:        VString* vvalue;
                     98:        bool need_delim;
                     99: };
                    100: #endif
                    101: static void one_foreach_cycle(const String::Body key, const String& value, void* ainfo) {
1.28      paf       102:        Foreach_info& info=*static_cast<Foreach_info*>(ainfo);
1.27      paf       103:        info.vkey->set_string(*new String(key, String::L_TAINTED));
                    104:        info.vvalue->set_string(value);
1.34      paf       105:        info.var_context->put_element(*info.var_context, *info.key_var_name, info.vkey, false);
                    106:        info.var_context->put_element(*info.var_context, *info.value_var_name, info.vvalue, false);
1.27      paf       107: 
                    108:        StringOrValue sv_processed=info.r->process(*info.body_code);
                    109:        const String* s_processed=sv_processed.get_string();
                    110:        if(info.delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
                    111:                if(info.need_delim) // need delim & iteration produced string?
                    112:                        info.r->write_pass_lang(info.r->process(*info.delim_maybe_code));
                    113:                info.need_delim=true;
                    114:        }
                    115:        info.r->write_pass_lang(sv_processed);
                    116: }
1.24      paf       117: static void _foreach(Request& r, MethodParams& params) {
                    118:        VHashfile& self=GET_SELF(r, VHashfile);
                    119: 
1.28      paf       120:        Foreach_info info;
1.27      paf       121:        
                    122:        info.r=&r;
                    123:        info.key_var_name=&params.as_string(0, "key-var name must be string");
                    124:        info.value_var_name=&params.as_string(1, "value-var name must be string");
                    125:        info.body_code=&params.as_junction(2, "body must be code");
                    126:        info.delim_maybe_code=params.count()>3?params.get(3):0;
                    127: 
                    128:        info.var_context=info.body_code->get_junction()->wcontext;
                    129:        info.vkey=new VString;
                    130:        info.vvalue=new VString;
                    131: 
                    132:        info.need_delim=false;
                    133: 
                    134:        self.for_each(one_foreach_cycle, &info);
1.14      paf       135: }
                    136: 
1.1       parser    137: // constructor
                    138: 
1.24      paf       139: MHashfile::MHashfile(): Methoded("hashfile") {
1.9       paf       140:        // ^hashfile::open[db_home;filename]
1.24      paf       141:        add_native_method("open", Method::CT_DYNAMIC, _open, 1, 1);
1.5       parser    142:        // ^hash[]
1.26      paf       143:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.8       parser    144:        // ^hashfile.delete[key]
1.30      paf       145:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.12      paf       146:        // ^hashfile.clear[]
                    147:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.14      paf       148:        // ^hashfile.foreach[key;value]{code}[delim]
1.27      paf       149:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       parser    150: }

E-mail: