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

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

E-mail: