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