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