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

1.1       paf         1: /** @file
                      2:        Parser: Value class.
                      3: 
1.10      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.11    ! paf         8: static const char* IDENT_VALUE_C="$Date: 2003/07/24 11:31:25 $";
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.10      paf        13: #include "pa_vdate.h"
                     14: #include "pa_vobject.h"
                     15: 
                     16: // defines for globals
                     17: 
                     18: #define NAME_NAME "name"
                     19: 
                     20: // globals
                     21: 
                     22: const String name_name(NAME_NAME);
                     23: 
                     24: const String value_name(VALUE_NAME);
                     25: const String content_type_name(CONTENT_TYPE_NAME);
                     26: 
                     27: // methods
                     28: 
                     29: VObject* Value::set_derived(VObject* /*aderived*/) { return 0; }
                     30: 
                     31: Junction* Value::get_junction() { return 0; }
                     32: 
1.11    ! paf        33: Value* Value::base_object() { return bark("is '%s', it has no base object"); }
1.10      paf        34: 
                     35: Value* Value::get_element(const String& /*aname*/, Value& /*aself*/, bool /*looking_up*/) {
1.11    ! paf        36:        return bark("is '%s', it has no elements");
1.10      paf        37: }
                     38: 
                     39: 
                     40: VFile* Value::as_vfile(String::Language /*lang*/, const Request_charsets* /*charsets*/) { 
1.11    ! paf        41:        bark("is '%s', it does not have file value"); return 0;
1.10      paf        42: }
1.1       paf        43: 
                     44: /// call this before invoking to ensure proper actual numbered params count
1.10      paf        45: void Method::check_actual_numbered_params(Value& self, 
                     46:                                          MethodParams* actual_numbered_params) const {
1.1       paf        47: 
1.10      paf        48:        int actual_count=actual_numbered_params?actual_numbered_params->count():0;
1.1       paf        49:        if(actual_count<min_numbered_params_count) // not proper count? bark
                     50:                throw Exception("parser.runtime",
1.10      paf        51:                        0,
1.1       paf        52:                        "native method of %s (%s) accepts minimum %d parameter(s) (%d present)", 
1.8       paf        53:                                self.get_class()->name_cstr(),
                     54:                                self.type(),
1.1       paf        55:                                min_numbered_params_count,
                     56:                                actual_count);
                     57: 
                     58: }
1.4       paf        59: 
1.10      paf        60: Junction::Junction(Value& aself,
                     61:        const Method* amethod,
                     62:        VMethodFrame* amethod_frame,
                     63:        Value* arcontext,
                     64:        WContext* awcontext,
                     65:        ArrayOperation* acode): self(aself),
1.7       paf        66:        method(amethod),
1.4       paf        67:        method_frame(amethod_frame),
                     68:        rcontext(arcontext),
                     69:        wcontext(awcontext),
                     70:        code(acode) {
1.5       paf        71:        if(wcontext)
1.10      paf        72:                wcontext->attach_junction(this);
1.4       paf        73: }
                     74: 
1.5       paf        75: void Junction::reattach(WContext *new_wcontext) {
                     76:        if(new_wcontext)
                     77:                wcontext=new_wcontext;
                     78:        else {
                     79:                method_frame=0;
                     80:                rcontext=0;
                     81:                wcontext=0;
                     82:        }
1.4       paf        83: }
                     84: 
                     85: /*
                     86: void Junction::change_context(Junction *source) {
                     87:        if(source) {
                     88:                method_frame=source->method_frame;
                     89:                rcontext=source->rcontext;
                     90:                wcontext=source->wcontext;
                     91:        } else {
                     92:                method_frame=rcontext=0;
                     93:                wcontext=0;
                     94:        }
                     95: }
                     96: */
                     97: 
1.10      paf        98: // attributed meaning
                     99: 
                    100: static String::C date_attribute(const VDate& vdate) {
                    101:        /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
                    102:        const char month_names[12][4]={
                    103:                "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
                    104:        const char days[7][4]={
                    105:                "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
                    106:                        
                    107:        time_t when=vdate.get_time();
                    108:        struct tm *tms=gmtime(&when);
                    109:        if(!tms)
                    110:                throw Exception(0,
                    111:                        0,
                    112:                        "bad time in attribute value (seconds from epoch=%ld)", when);
                    113: 
                    114:        char *buf=new(PointerFreeGC) char[MAX_STRING];
                    115:        return String::C(buf, 
                    116:                snprintf(buf, MAX_STRING, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT", 
                    117:                days[tms->tm_wday],
                    118:                tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
                    119:                tms->tm_hour,tms->tm_min,tms->tm_sec));
                    120: }
                    121: static void append_attribute_meaning(String& result,
                    122:                                     Value& value, String::Language lang, bool forced) {
                    123:        if(const String* string=value.get_string())
                    124:                result.append(*string, lang, forced);
                    125:        else
                    126:                if(Value* vdate=value.as(VDATE_TYPE, false)) {
                    127:                        String::C attribute=date_attribute(static_cast<VDate&>(*vdate));
                    128: 
                    129:                        result.append_help_length(attribute.str, attribute.length, String::L_CLEAN);
                    130:                } else
                    131:                        throw Exception("parser.runtime",
                    132:                                &result,
                    133:                                "trying to append here neither string nor date (%s)",
                    134:                                        value.type());
                    135: }
                    136: #ifndef DOXYGEN
                    137: struct Attributed_meaning_info {
                    138:        String* header; // header line being constructed
                    139:        String::Language lang; // language in which to append to that line
                    140:        bool forced; // do they force that lang?
                    141: };
                    142: #endif
                    143: static void append_attribute_subattribute(HashStringValue::key_type akey, 
                    144:                                          HashStringValue::value_type avalue, 
                    145:                                          Attributed_meaning_info *info) {
                    146:        if(akey==VALUE_NAME)
                    147:                return;
                    148: 
                    149:        // ...; charset=windows1251
                    150:        *info->header << "; ";
                    151:        info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
                    152:        *info->header << "=";
                    153:        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    154: }
                    155: const String& attributed_meaning_to_string(Value& meaning, 
                    156:                                           String::Language lang, bool forced) {
                    157:        String& result=*new String;
                    158:        if(HashStringValue *hash=meaning.get_hash()) {
                    159:                // $value(value) $subattribute(subattribute value)
                    160:                if(Value* value=hash->get(value_name))
                    161:                        append_attribute_meaning(result, *value, lang, forced);
                    162: 
                    163:                Attributed_meaning_info attributed_meaning_info={&result, lang};
                    164:                hash->for_each(append_attribute_subattribute, &attributed_meaning_info);
                    165:        } else // result value
                    166:                append_attribute_meaning(result, meaning, lang, forced);
                    167: 
                    168:        return result;
                    169: }

E-mail: