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

1.1       paf         1: /** @file
                      2:        Parser: Value class.
                      3: 
1.32    ! moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (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: 
                      8: #include "pa_value.h"
                      9: #include "pa_vstateless_class.h"
1.4       paf        10: #include "pa_vmethod_frame.h"
1.10      paf        11: #include "pa_vdate.h"
                     12: #include "pa_vobject.h"
                     13: 
1.32    ! moko       14: volatile const char * IDENT_PA_VALUE_C="$Id: 2011-05-28 22:54:14 $" IDENT_PA_VALUE_H IDENT_PA_PROPERTY_H;
        !            15: 
1.10      paf        16: // globals
                     17: 
                     18: const String name_name(NAME_NAME);
                     19: 
                     20: const String value_name(VALUE_NAME);
1.12      paf        21: const String expires_name(EXPIRES_NAME);
1.10      paf        22: const String content_type_name(CONTENT_TYPE_NAME);
                     23: 
                     24: // methods
                     25: 
                     26: Junction* Value::get_junction() { return 0; }
                     27: 
1.28      misha      28: Value* Value::get_element(const String& /*aname*/) {
1.11      paf        29:        return bark("is '%s', it has no elements");
1.10      paf        30: }
                     31: 
                     32: VFile* Value::as_vfile(String::Language /*lang*/, const Request_charsets* /*charsets*/) { 
1.11      paf        33:        bark("is '%s', it does not have file value"); return 0;
1.10      paf        34: }
1.1       paf        35: 
1.30      misha      36: const String* Value::get_json_string(Json_options* options) {
                     37:        if(!options || !options->skip_unknown)
                     38:                throw Exception(PARSER_RUNTIME, 0, "Unsupported value's type (%s)", type());
                     39:        return new String("null");
                     40: }
                     41: 
1.1       paf        42: /// call this before invoking to ensure proper actual numbered params count
1.10      paf        43: void Method::check_actual_numbered_params(Value& self, 
1.28      misha      44:                                        MethodParams* actual_numbered_params) const {
1.10      paf        45:        int actual_count=actual_numbered_params?actual_numbered_params->count():0;
1.29      misha      46:        if(actual_count<min_numbered_params_count || actual_count>max_numbered_params_count)
1.22      misha      47:                throw Exception(PARSER_RUNTIME,
1.10      paf        48:                        0,
1.29      misha      49:                        "native method of %s (%s) accepts %s %d parameter(s) (%d present)", 
1.8       paf        50:                                self.get_class()->name_cstr(),
                     51:                                self.type(),
1.29      misha      52:                                actual_count<min_numbered_params_count ? "minimum" : "maximum",
                     53:                                actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
1.1       paf        54:                                actual_count);
                     55: }
1.4       paf        56: 
1.10      paf        57: // attributed meaning
                     58: 
                     59: static String::C date_attribute(const VDate& vdate) {
                     60:        time_t when=vdate.get_time();
                     61:        struct tm *tms=gmtime(&when);
                     62:        if(!tms)
1.25      misha      63:                throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.10      paf        64:                        0,
1.15      paf        65:                        "bad time in attribute value (seconds from epoch=%u)", when);
1.24      misha      66:        return date_gmt_string(tms);
                     67: }
1.10      paf        68: 
                     69: static void append_attribute_meaning(String& result,
1.28      misha      70:                                        Value& value, String::Language lang, bool forced) {
1.10      paf        71:        if(const String* string=value.get_string())
                     72:                result.append(*string, lang, forced);
                     73:        else
1.28      misha      74:                if(Value* vdate=value.as(VDATE_TYPE)) {
1.10      paf        75:                        String::C attribute=date_attribute(static_cast<VDate&>(*vdate));
                     76: 
                     77:                        result.append_help_length(attribute.str, attribute.length, String::L_CLEAN);
                     78:                } else
1.22      misha      79:                        throw Exception(PARSER_RUNTIME,
1.10      paf        80:                                &result,
                     81:                                "trying to append here neither string nor date (%s)",
                     82:                                        value.type());
                     83: }
                     84: #ifndef DOXYGEN
                     85: struct Attributed_meaning_info {
1.31      misha      86:        String* header;        // header line being constructed
1.10      paf        87:        String::Language lang; // language in which to append to that line
1.31      misha      88:        bool forced;           // do they force that lang?
                     89:        bool allow_bool;       // allow bool types during print attributes
1.10      paf        90: };
                     91: #endif
                     92: static void append_attribute_subattribute(HashStringValue::key_type akey, 
1.31      misha      93:                                        HashStringValue::value_type avalue, 
                     94:                                        Attributed_meaning_info *info) {
1.10      paf        95:        if(akey==VALUE_NAME)
                     96:                return;
                     97: 
1.27      misha      98:        if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
                     99:                return;
                    100: 
1.10      paf       101:        // ...; charset=windows1251
                    102:        *info->header << "; ";
                    103:        info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.31      misha     104:        if(!avalue->is_bool()) {
                    105:                if( akey==content_disposition_filename_name ) {
                    106:                        *info->header << "=\"";
                    107:                        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    108:                        *info->header << "\"";
                    109:                } else {
                    110:                        *info->header << "=";
                    111:                        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    112:                }
1.21      misha     113:        }
1.10      paf       114: }
                    115: const String& attributed_meaning_to_string(Value& meaning, 
1.31      misha     116:                                        String::Language lang, bool forced, bool allow_bool) {
1.10      paf       117:        String& result=*new String;
                    118:        if(HashStringValue *hash=meaning.get_hash()) {
                    119:                // $value(value) $subattribute(subattribute value)
                    120:                if(Value* value=hash->get(value_name))
                    121:                        append_attribute_meaning(result, *value, lang, forced);
                    122: 
1.21      misha     123:                Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20      paf       124:                hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10      paf       125:        } else // result value
                    126:                append_attribute_meaning(result, meaning, lang, forced);
                    127: 
                    128:        return result;
                    129: }

E-mail: