Annotation of parser3/src/types/pa_value.C, revision 1.29
1.1 paf 1: /** @file
2: Parser: Value class.
3:
1.26 misha 4: Copyright (c) 2001-2009 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.29 ! misha 8: static const char * const IDENT_VALUE_C="$Date: 2009-08-08 13:30:21 $";
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: // 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:
36: /// call this before invoking to ensure proper actual numbered params count
1.10 paf 37: void Method::check_actual_numbered_params(Value& self,
1.28 misha 38: MethodParams* actual_numbered_params) const {
1.10 paf 39: int actual_count=actual_numbered_params?actual_numbered_params->count():0;
1.29 ! misha 40: if(actual_count<min_numbered_params_count || actual_count>max_numbered_params_count)
1.22 misha 41: throw Exception(PARSER_RUNTIME,
1.10 paf 42: 0,
1.29 ! misha 43: "native method of %s (%s) accepts %s %d parameter(s) (%d present)",
1.8 paf 44: self.get_class()->name_cstr(),
45: self.type(),
1.29 ! misha 46: actual_count<min_numbered_params_count ? "minimum" : "maximum",
! 47: actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
1.1 paf 48: actual_count);
49: }
1.4 paf 50:
1.10 paf 51: // attributed meaning
52:
53: static String::C date_attribute(const VDate& vdate) {
54: time_t when=vdate.get_time();
55: struct tm *tms=gmtime(&when);
56: if(!tms)
1.25 misha 57: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.10 paf 58: 0,
1.15 paf 59: "bad time in attribute value (seconds from epoch=%u)", when);
1.24 misha 60: return date_gmt_string(tms);
61: }
1.10 paf 62:
63: static void append_attribute_meaning(String& result,
1.28 misha 64: Value& value, String::Language lang, bool forced) {
1.10 paf 65: if(const String* string=value.get_string())
66: result.append(*string, lang, forced);
67: else
1.28 misha 68: if(Value* vdate=value.as(VDATE_TYPE)) {
1.10 paf 69: String::C attribute=date_attribute(static_cast<VDate&>(*vdate));
70:
71: result.append_help_length(attribute.str, attribute.length, String::L_CLEAN);
72: } else
1.22 misha 73: throw Exception(PARSER_RUNTIME,
1.10 paf 74: &result,
75: "trying to append here neither string nor date (%s)",
76: value.type());
77: }
78: #ifndef DOXYGEN
79: struct Attributed_meaning_info {
80: String* header; // header line being constructed
81: String::Language lang; // language in which to append to that line
82: bool forced; // do they force that lang?
1.23 misha 83: bool allow_bool; // allow bool types during print attributes
1.10 paf 84: };
85: #endif
86: static void append_attribute_subattribute(HashStringValue::key_type akey,
87: HashStringValue::value_type avalue,
88: Attributed_meaning_info *info) {
89: if(akey==VALUE_NAME)
90: return;
91:
1.27 misha 92: if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
93: return;
94:
1.10 paf 95: // ...; charset=windows1251
96: *info->header << "; ";
97: info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.27 misha 98: if(!avalue->is_bool()){
1.21 misha 99: *info->header << "=";
100: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
101: }
1.10 paf 102: }
103: const String& attributed_meaning_to_string(Value& meaning,
1.21 misha 104: String::Language lang, bool forced, bool allow_bool) {
1.10 paf 105: String& result=*new String;
106: if(HashStringValue *hash=meaning.get_hash()) {
107: // $value(value) $subattribute(subattribute value)
108: if(Value* value=hash->get(value_name))
109: append_attribute_meaning(result, *value, lang, forced);
110:
1.21 misha 111: Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20 paf 112: hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10 paf 113: } else // result value
114: append_attribute_meaning(result, meaning, lang, forced);
115:
116: return result;
117: }
E-mail: