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

1.1       parser      1: /** @file
                      2:        Parser: @b hashfile parser class.
                      3: 
1.66    ! moko        4:        Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.65      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.24      paf         6: */
                      7: 
1.4       parser      8: #include "classes.h"
1.1       parser      9: 
                     10: #include "pa_request.h"
1.24      paf        11: #include "pa_vmethod_frame.h"
1.1       parser     12: #include "pa_vhashfile.h"
                     13: #include "pa_vhash.h"
                     14: 
1.66    ! moko       15: volatile const char * IDENT_HASHFILE_C="$Id: hashfile.C,v 1.65 2023/09/26 20:49:05 moko Exp $";
1.53      moko       16: 
1.1       parser     17: // class
                     18: 
                     19: class MHashfile : public Methoded {
                     20: public: // VStateless_class
1.51      misha      21:        Value *create_new_value(Pool& apool) { return new VHashfile(apool); }
1.1       parser     22: public:
1.24      paf        23:        MHashfile();
1.1       parser     24: };
                     25: 
1.24      paf        26: // global variable
                     27: 
1.57      moko       28: DECLARE_CLASS_VAR(hashfile, new MHashfile);
1.24      paf        29: 
1.35      paf        30: // defines for statics
                     31: 
                     32: #define OPEN_DATA_NAME "HASHFILE-OPEN-DATA"
                     33: 
1.1       parser     34: // methods
                     35: 
1.46      misha      36: typedef HashString<bool> HashStringBool;
1.35      paf        37: 
1.24      paf        38: static void _open(Request& r, MethodParams& params) {
1.35      paf        39:        HashStringBool* file_list=static_cast<HashStringBool*>(r.classes_conf.get(OPEN_DATA_NAME));
                     40:        if(!file_list) {
                     41:                file_list=new HashStringBool();
                     42:                r.classes_conf.put(OPEN_DATA_NAME, file_list);
                     43:        }
                     44: 
1.63      moko       45:        const String& file_spec=r.full_disk_path(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.35      paf        46:        if(file_list->get(file_spec))
1.39      misha      47:                throw Exception(PARSER_RUNTIME,
1.35      paf        48:                        0,
                     49:                        "this hashfile is already opened, use existing variable");
                     50:        file_list->put(file_spec, true);
                     51: 
1.24      paf        52:        VHashfile& self=GET_SELF(r, VHashfile);
1.35      paf        53:        self.open(file_spec);
1.9       paf        54: }
1.1       parser     55: 
1.31      paf        56: static void _hash(Request& r, MethodParams&) {
1.24      paf        57:        VHashfile& self=GET_SELF(r, VHashfile);
1.5       parser     58:        
                     59:        // write out result
1.26      paf        60:        VHash& result=*new VHash(*self.get_hash());
1.60      moko       61:        r.write(result);
1.5       parser     62: }
1.26      paf        63: 
1.24      paf        64: static void _delete(Request& r, MethodParams& params) {
                     65:        VHashfile& self=GET_SELF(r, VHashfile);
1.8       parser     66:        
1.30      paf        67:        if(!params.count()) {
                     68:                // ^hashfile.delete[] asked to delete hashfile itself
                     69:                self.delete_files();
                     70:                return;
                     71:        }
1.25      paf        72:        // key
                     73:        const String &key=params.as_string(0, "key must be string");
                     74:        // remove
                     75:        self.remove(key);
1.8       parser     76: }
                     77: 
1.24      paf        78: static void _clear(Request& r, MethodParams&) {
                     79:        VHashfile& self=GET_SELF(r, VHashfile);
1.39      misha      80:        self.delete_files();
1.12      paf        81: }
                     82: 
1.27      paf        83: #ifndef DOXYGEN
1.28      paf        84: struct Foreach_info {
1.27      paf        85:        Request* r;
                     86:        const String* key_var_name;
                     87:        const String* value_var_name;
                     88:        Value* body_code;
                     89:        Value* delim_maybe_code;
                     90: 
                     91:        Value* var_context;
                     92:        bool need_delim;
                     93: };
                     94: #endif
1.42      misha      95: static bool one_foreach_cycle(
1.51      misha      96:                                const String::Body key,
                     97:                                const String& value,
                     98:                                void* ainfo) {
1.28      paf        99:        Foreach_info& info=*static_cast<Foreach_info*>(ainfo);
1.44      misha     100:        if(info.key_var_name){
1.49      misha     101:                VString* vkey=new VString(*new String(key, String::L_TAINTED));
1.55      moko      102:                info.r->put_element(*info.var_context, *info.key_var_name, vkey);
1.44      misha     103:        }
                    104:        if(info.value_var_name){
1.49      misha     105:                VString* vvalue=new VString(value);
1.55      moko      106:                info.r->put_element(*info.var_context, *info.value_var_name, vvalue);
1.44      misha     107:        }
1.27      paf       108: 
1.58      moko      109:        Value& sv_processed=info.r->process(*info.body_code);
1.61      moko      110:        TempSkip4Delimiter skip(*info.r);
1.42      misha     111: 
1.27      paf       112:        const String* s_processed=sv_processed.get_string();
1.45      misha     113:        if(info.delim_maybe_code && s_processed && !s_processed->is_empty()) { // delimiter set and we have body
1.27      paf       114:                if(info.need_delim) // need delim & iteration produced string?
1.60      moko      115:                        info.r->write(info.r->process(*info.delim_maybe_code));
1.42      misha     116:                else
                    117:                        info.need_delim=true;
1.27      paf       118:        }
1.42      misha     119: 
1.60      moko      120:        info.r->write(sv_processed);
1.37      paf       121: 
1.61      moko      122:        return skip.check_break();
1.27      paf       123: }
1.24      paf       124: static void _foreach(Request& r, MethodParams& params) {
1.50      misha     125:        InCycle temp(r);
1.37      paf       126: 
1.44      misha     127:        const String& key_var_name=params.as_string(0, "key-var name must be string");
                    128:        const String& value_var_name=params.as_string(1, "value-var name must be string");
                    129: 
1.43      misha     130:        Foreach_info info={
                    131:                &r,
1.44      misha     132:                key_var_name.is_empty()? 0 : &key_var_name,
                    133:                value_var_name.is_empty()? 0 : &value_var_name,
1.43      misha     134:                &params.as_junction(2, "body must be code"),
1.59      moko      135:                /*delimiter*/params.count()>3 ? &params[3] : 0,
1.43      misha     136:                /*var_context*/r.get_method_frame()->caller(),
1.44      misha     137:                false
1.43      misha     138:        };
                    139: 
1.24      paf       140:        VHashfile& self=GET_SELF(r, VHashfile);
1.27      paf       141:        self.for_each(one_foreach_cycle, &info);
1.14      paf       142: }
                    143: 
1.38      misha     144: static bool one_cleanup_cycle(const String::Body, const String&, void*) {
                    145:        return false;
                    146: }
                    147: static void _cleanup(Request& r, MethodParams&) {
                    148:        VHashfile& self=GET_SELF(r, VHashfile);
                    149: 
                    150:        self.for_each(one_cleanup_cycle, 0);
                    151: }
                    152: 
                    153: static void _release(Request& r, MethodParams&) {
                    154:        VHashfile& self=GET_SELF(r, VHashfile);
                    155:        self.close();
                    156: }
                    157: 
1.1       parser    158: // constructor
                    159: 
1.24      paf       160: MHashfile::MHashfile(): Methoded("hashfile") {
1.38      misha     161:        // ^hashfile::open[filename]
1.24      paf       162:        add_native_method("open", Method::CT_DYNAMIC, _open, 1, 1);
1.38      misha     163:        // ^hashfile.hash[]
1.26      paf       164:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.8       parser    165:        // ^hashfile.delete[key]
1.30      paf       166:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.39      misha     167:        // ^hashfile.clear[] -- for backward compatibility. use .delete[] instead.
1.12      paf       168:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.40      misha     169:        // ^hashfile.release[]
1.38      misha     170:        add_native_method("release", Method::CT_DYNAMIC, _release, 0, 0);
                    171:        // ^hashfile.cleanup[]
                    172:        add_native_method("cleanup", Method::CT_DYNAMIC, _cleanup, 0, 0);
                    173:        add_native_method("defecate", Method::CT_DYNAMIC, _cleanup, 0, 0);
1.14      paf       174:        // ^hashfile.foreach[key;value]{code}[delim]
1.27      paf       175:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       parser    176: }

E-mail: