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

1.1       parser      1: /** @file
                      2:        Parser: @b hashfile parser class.
                      3: 
1.24      paf         4:        Copyright (c) 2001-2003 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.29    ! paf         9: static const char* IDENT="$Id: hashfile.C,v 1.28 2003/11/06 11:13:24 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.29    ! paf        22:        Value *create_new_value(Pool& apool) { 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.1       parser     34: // methods
                     35: 
1.24      paf        36: static void _open(Request& r, MethodParams& params) {
                     37:        VHashfile& self=GET_SELF(r, VHashfile);
1.1       parser     38:        
1.24      paf        39:        self.open(r.absolute(params.as_string(0, "filename must be string")));
1.9       paf        40: }
1.1       parser     41: 
1.24      paf        42: static void _hash(Request& r, MethodParams& params) {
                     43:        VHashfile& self=GET_SELF(r, VHashfile);
1.5       parser     44:        
                     45:        // write out result
1.26      paf        46:        VHash& result=*new VHash(*self.get_hash());
1.5       parser     47:        r.write_no_lang(result);
                     48: }
1.26      paf        49: 
1.24      paf        50: static void _delete(Request& r, MethodParams& params) {
                     51:        VHashfile& self=GET_SELF(r, VHashfile);
1.8       parser     52:        
1.25      paf        53:        // key
                     54:        const String &key=params.as_string(0, "key must be string");
                     55:        // remove
                     56:        self.remove(key);
1.8       parser     57: }
                     58: 
1.24      paf        59: static void _clear(Request& r, MethodParams&) {
                     60:        VHashfile& self=GET_SELF(r, VHashfile);
                     61:        self.clear();
1.12      paf        62: }
                     63: 
1.27      paf        64: #ifndef DOXYGEN
1.28      paf        65: struct Foreach_info {
1.27      paf        66:        Request* r;
                     67:        const String* key_var_name;
                     68:        const String* value_var_name;
                     69:        Value* body_code;
                     70:        Value* delim_maybe_code;
                     71: 
                     72:        Value* var_context;
                     73:        VString* vkey;
                     74:        VString* vvalue;
                     75:        bool need_delim;
                     76: };
                     77: #endif
                     78: static void one_foreach_cycle(const String::Body key, const String& value, void* ainfo) {
1.28      paf        79:        Foreach_info& info=*static_cast<Foreach_info*>(ainfo);
1.27      paf        80:        info.vkey->set_string(*new String(key, String::L_TAINTED));
                     81:        info.vvalue->set_string(value);
                     82:        info.var_context->put_element(*info.key_var_name, info.vkey, false);
                     83:        info.var_context->put_element(*info.value_var_name, info.vvalue, false);
                     84: 
                     85:        StringOrValue sv_processed=info.r->process(*info.body_code);
                     86:        const String* s_processed=sv_processed.get_string();
                     87:        if(info.delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
                     88:                if(info.need_delim) // need delim & iteration produced string?
                     89:                        info.r->write_pass_lang(info.r->process(*info.delim_maybe_code));
                     90:                info.need_delim=true;
                     91:        }
                     92:        info.r->write_pass_lang(sv_processed);
                     93: }
1.24      paf        94: static void _foreach(Request& r, MethodParams& params) {
                     95:        VHashfile& self=GET_SELF(r, VHashfile);
                     96: 
1.28      paf        97:        Foreach_info info;
1.27      paf        98:        
                     99:        info.r=&r;
                    100:        info.key_var_name=&params.as_string(0, "key-var name must be string");
                    101:        info.value_var_name=&params.as_string(1, "value-var name must be string");
                    102:        info.body_code=&params.as_junction(2, "body must be code");
                    103:        info.delim_maybe_code=params.count()>3?params.get(3):0;
                    104: 
                    105:        info.var_context=info.body_code->get_junction()->wcontext;
                    106:        info.vkey=new VString;
                    107:        info.vvalue=new VString;
                    108: 
                    109:        info.need_delim=false;
                    110: 
                    111:        self.for_each(one_foreach_cycle, &info);
1.14      paf       112: }
                    113: 
1.1       parser    114: // constructor
                    115: 
1.24      paf       116: MHashfile::MHashfile(): Methoded("hashfile") {
1.9       paf       117:        // ^hashfile::open[db_home;filename]
1.24      paf       118:        add_native_method("open", Method::CT_DYNAMIC, _open, 1, 1);
1.5       parser    119:        // ^hash[]
1.26      paf       120:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.8       parser    121:        // ^hashfile.delete[key]
1.25      paf       122:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.12      paf       123:        // ^hashfile.clear[]
                    124:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.14      paf       125:        // ^hashfile.foreach[key;value]{code}[delim]
1.27      paf       126:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       parser    127: }

E-mail: