Annotation of parser3/src/types/pa_value.C, revision 1.9.2.1

1.1       paf         1: /** @file
                      2:        Parser: Value class.
                      3: 
1.9       paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.1       paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.2       paf         6: */
1.1       paf         7: 
1.9.2.1 ! paf         8: static const char* IDENT_VALUE_C="$Date: 2003/01/21 15:51:17 $";
1.1       paf         9: 
                     10: #include "pa_value.h"
                     11: #include "pa_vstateless_class.h"
1.4       paf        12: #include "pa_vmethod_frame.h"
1.1       paf        13: 
                     14: /// call this before invoking to ensure proper actual numbered params count
                     15: void Method::check_actual_numbered_params(
1.8       paf        16:        Value& self, const String& actual_name, Array *actual_numbered_params) const {
1.1       paf        17: 
                     18:        int actual_count=actual_numbered_params?actual_numbered_params->size():0;
                     19:        if(actual_count<min_numbered_params_count) // not proper count? bark
                     20:                throw Exception("parser.runtime",
                     21:                        &actual_name,
                     22:                        "native method of %s (%s) accepts minimum %d parameter(s) (%d present)", 
1.8       paf        23:                                self.get_class()->name_cstr(),
                     24:                                self.type(),
1.1       paf        25:                                min_numbered_params_count,
                     26:                                actual_count);
                     27: 
                     28: }
1.4       paf        29: 
                     30: Junction::Junction(Pool& apool,
1.8       paf        31:        Value& aself,
1.7       paf        32:        const Method *amethod,
1.4       paf        33:        VMethodFrame *amethod_frame,
                     34:        Value *arcontext,
                     35:        WContext *awcontext,
                     36:        const Array *acode) : Pooled(apool),
                     37:        
                     38:        self(aself),
1.7       paf        39:        method(amethod),
1.4       paf        40:        method_frame(amethod_frame),
                     41:        rcontext(arcontext),
                     42:        wcontext(awcontext),
                     43:        code(acode) {
1.5       paf        44:        if(wcontext)
                     45:                wcontext->attach_junction(*this);
1.4       paf        46: }
                     47: 
1.5       paf        48: void Junction::reattach(WContext *new_wcontext) {
                     49:        if(new_wcontext)
                     50:                wcontext=new_wcontext;
                     51:        else {
                     52:                method_frame=0;
                     53:                rcontext=0;
                     54:                wcontext=0;
                     55:        }
1.4       paf        56: }
                     57: 
                     58: /*
                     59: void Junction::change_context(Junction *source) {
                     60:        if(source) {
                     61:                method_frame=source->method_frame;
                     62:                rcontext=source->rcontext;
                     63:                wcontext=source->wcontext;
                     64:        } else {
                     65:                method_frame=rcontext=0;
                     66:                wcontext=0;
                     67:        }
                     68: }
                     69: */
                     70: 
1.9.2.1 ! paf        71: // attributed meaning
        !            72: 
        !            73: static size_t date_attribute(const VDate& vdate, char *buf, size_t buf_size) {
        !            74:     const char month_names[12][4]={
        !            75:                "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        !            76:     const char days[7][4]={
        !            77:                "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
        !            78: 
        !            79:        time_t when=vdate.get_time();
        !            80:        struct tm *tms=gmtime(&when);
        !            81:        if(!tms)
        !            82:                throw Exception(0,
        !            83:                        0,
        !            84:                        "bad time in attribute value (seconds from epoch=%ld)", when);
        !            85:        return snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT", 
        !            86:                days[tms->tm_wday],
        !            87:                tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
        !            88:                tms->tm_hour,tms->tm_min,tms->tm_sec);
        !            89: }
        !            90: static void append_attribute_meaning(String& result,
        !            91:                                                                         Value& value, String::Untaint_lang lang, bool forced) {
        !            92:        if(const String *string=value.get_string())
        !            93:                result.append(string->join_chains(result.pool(), 0), lang, forced);
        !            94:        else
        !            95:                if(Value *vdate=value.as(VDATE_TYPE, false)) {
        !            96:                        char *buf=(char *)result.malloc(MAX_STRING);
        !            97:                        size_t size=date_attribute(*static_cast<VDate *>(vdate), 
        !            98:                                buf, MAX_STRING);
        !            99: 
        !           100:                        result.APPEND_CLEAN(buf, size, "converted from date", 0);
        !           101:                } else
        !           102:                        throw Exception("parser.runtime",
        !           103:                                &result,
        !           104:                                "trying to append here neither string nor date (%s)",
        !           105:                                        value.type());
        !           106: }
        !           107: #ifndef DOXYGEN
        !           108: struct Attributed_meaning_info {
        !           109:        String *header; // header line being constructed
        !           110:        String::Untaint_lang lang; // language in which to append to that line
        !           111:        bool forced; // do they force that lang?
        !           112: };
        !           113: #endif
        !           114: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, 
        !           115:                                                                                  void *info) {
        !           116:        if(akey==VALUE_NAME)
        !           117:                return;
        !           118: 
        !           119:        Attributed_meaning_info& ami=*static_cast<Attributed_meaning_info *>(info);
        !           120: 
        !           121:        // ...; charset=windows1251
        !           122:        *ami.header << "; ";
        !           123:        ami.header->append(akey, ami.lang, ami.forced);
        !           124:        *ami.header << "=";
        !           125:        append_attribute_meaning(*ami.header, *static_cast<Value *>(avalue), ami.lang, ami.forced);
        !           126: }
        !           127: const String& attributed_meaning_to_string(Value& meaning, 
        !           128:                                                                                   String::Untaint_lang lang, bool forced) {
        !           129:        String &result=*new(meaning.pool()) String(meaning.pool());
        !           130:        if(Hash *hash=meaning.get_hash(0)) {
        !           131:                // $value(value) $subattribute(subattribute value)
        !           132:                if(Value *value=static_cast<Value *>(hash->get(*value_name)))
        !           133:                        append_attribute_meaning(result, *value, lang, forced);
        !           134: 
        !           135:                Attributed_meaning_info attributed_meaning_info={&result, lang};
        !           136:                hash->for_each(append_attribute_subattribute, &attributed_meaning_info);
        !           137:        } else // result value
        !           138:                append_attribute_meaning(result, meaning, lang, forced);
        !           139: 
        !           140:        return result;
        !           141: }

E-mail: