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

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.25    ! paf         9: static const char* IDENT="$Id: hashfile.C,v 1.24 2003/11/06 08:22:48 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.24      paf        22:        Value *create_new_value() { return new VHashfile; }
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: #if LATER
                     43: static void _hash(Request& r, MethodParams& params) {
                     44:        VHashfile& self=GET_SELF(r, VHashfile);
1.5       parser     45:        
                     46:        // write out result
                     47:        VHash& result=*new(pool) VHash(pool, *self.get_hash(&method_name));
                     48:        result.set_name(method_name);
                     49:        r.write_no_lang(result);
                     50: }
1.25    ! paf        51: #endif
1.24      paf        52: static void _delete(Request& r, MethodParams& params) {
                     53:        VHashfile& self=GET_SELF(r, VHashfile);
1.8       parser     54:        
1.25    ! paf        55:        // key
        !            56:        const String &key=params.as_string(0, "key must be string");
        !            57:        // remove
        !            58:        self.remove(key);
1.8       parser     59: }
                     60: 
1.24      paf        61: static void _clear(Request& r, MethodParams&) {
                     62:        VHashfile& self=GET_SELF(r, VHashfile);
                     63:        self.clear();
1.12      paf        64: }
                     65: 
1.24      paf        66: #if LATER
                     67: static void _foreach(Request& r, MethodParams& params) {
                     68:        VHashfile& self=GET_SELF(r, VHashfile);
                     69: 
                     70:        const String& key_var_name=params.as_string(0, "key-var name must be string");
                     71:        const String& value_var_name=params.as_string(1, "value-var name must be string");
                     72:        Value& body_code=params.as_junction(2, "body must be code");
                     73:        Value *delim_maybe_code=params.size()>3?&params.get(3):0;
1.14      paf        74: 
                     75:        bool need_delim=false;
                     76:        Value& var_context=*body_code.get_junction()->wcontext;
                     77:        VString& vkey=*new(pool) VString(pool);
                     78:        VString& vvalue=*new(pool) VString(pool);
                     79: 
1.20      paf        80:        DB_Cursor cursor(*self.get_table_ptr(&method_name), &method_name);
1.14      paf        81:        while(true) {
                     82:                String *key;
                     83:                String *data;
                     84:                if(!cursor.get(pool, key, data, DB_NEXT))
                     85:                        break;
                     86: 
                     87:                if(!key) 
                     88:                        continue; // expired
                     89: 
                     90:                vkey.set_string(*key);
                     91:                vvalue.set_string(*data);
                     92:                var_context.put_element(key_var_name, &vkey);
                     93:                var_context.put_element(value_var_name, &vvalue);
                     94: 
                     95:                Value& processed_body=r.process(body_code);
                     96:                if(delim_maybe_code) { // delimiter set?
                     97:                        const String *string=processed_body.get_string();
                     98:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                     99:                                r.write_pass_lang(r.process(*delim_maybe_code));
                    100:                        need_delim=true;
                    101:                }
                    102:                r.write_pass_lang(processed_body);
                    103:        }
                    104: }
                    105: 
1.24      paf       106: #endif
1.1       parser    107: // constructor
                    108: 
1.24      paf       109: MHashfile::MHashfile(): Methoded("hashfile") {
1.9       paf       110:        // ^hashfile::open[db_home;filename]
1.24      paf       111:        add_native_method("open", Method::CT_DYNAMIC, _open, 1, 1);
1.5       parser    112:        // ^hash[]
1.24      paf       113: //     add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.8       parser    114:        // ^hashfile.delete[key]
1.25    ! paf       115:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.12      paf       116:        // ^hashfile.clear[]
                    117:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.14      paf       118:        // ^hashfile.foreach[key;value]{code}[delim]
1.24      paf       119: //     add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       parser    120: }

E-mail: