Annotation of parser3/src/types/pa_vtable.C, revision 1.29
1.1 parser 1: /** @file
2: Parser: @b table class.
3:
1.28 misha 4: Copyright(c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.11 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.14 paf 6: */
1.1 parser 7:
1.29 ! misha 8: static const char * const IDENT_VTABLE_C="$Date: 2009-04-15 04:49:50 $";
1.1 parser 9:
10: #include "pa_vtable.h"
11: #include "pa_vstring.h"
1.5 parser 12: #include "pa_vhash.h"
1.22 paf 13: #include "pa_vvoid.h"
1.1 parser 14:
1.3 parser 15: #ifndef DOXYGEN
1.1 parser 16: struct Record_info {
1.22 paf 17: Table* table;
18: HashStringValue* hash;
1.1 parser 19: };
1.3 parser 20: #endif
1.29 ! misha 21:
! 22: static void store_column_item_to_hash(const String* column_name, Record_info *info) {
1.22 paf 23: Value* value;
1.29 ! misha 24: const String* column_item=info->table->item(*column_name);
! 25: info->hash->put(*column_name,
! 26: (column_item && !column_item->is_empty())
! 27: ?new VString(*column_item)
! 28: :new VString()
! 29: );
1.1 parser 30: }
1.29 ! misha 31:
1.22 paf 32: Value* VTable::fields_element() {
1.29 ! misha 33: Value& result=*new VHash;
1.22 paf 34: Table& ltable=table();
1.29 ! misha 35: if(!ltable.count())
! 36: return &result;
! 37:
! 38: HashStringValue* hash=result.get_hash();
! 39:
! 40: if(Table::columns_type columns=ltable.columns()) { // named
! 41: Record_info record_info={<able, hash};
1.1 parser 42: columns->for_each(store_column_item_to_hash, &record_info);
1.29 ! misha 43: } else { // nameless
! 44: size_t row_size=ltable[ltable.current()]->count(); // number of columns in current row
! 45: for(size_t index=0; index<row_size; index++){
! 46: const String* column_item=ltable.item(index);
! 47: hash->put(String::Body::Format(index),
! 48: (column_item && !column_item->is_empty())
! 49: ?new VString(*column_item)
! 50: :new VString()
! 51: );
! 52: }
1.1 parser 53: }
1.29 ! misha 54:
! 55: return &result;
1.1 parser 56: }
57:
1.22 paf 58: Value* VTable::get_element(const String& aname, Value& aself, bool looking_up) {
1.1 parser 59: // fields
1.16 paf 60: if(aname==TABLE_FIELDS_ELEMENT_NAME)
1.1 parser 61: return fields_element();
62:
1.2 parser 63: // methods
1.22 paf 64: if(Value* result=VStateless_object::get_element(aname, aself, looking_up))
1.2 parser 65: return result;
66:
1.1 parser 67: // columns
68: if(ftable) {
1.16 paf 69: int index=ftable->column_name2index(aname, false);
70: if(index>=0) // column aname|number valid
1.22 paf 71: if(const String* string=ftable->item(index)) // there is such column
72: return new VString(*string);
1.1 parser 73: else
1.28 misha 74: return VVoid::get();
1.1 parser 75: }
76:
1.27 misha 77: throw Exception(PARSER_RUNTIME,
1.16 paf 78: &aname,
1.1 parser 79: "column not found");
80: }
E-mail: