--- parser3/src/classes/table.C 2001/03/25 08:52:28 1.26 +++ parser3/src/classes/table.C 2001/03/26 09:53:42 1.29 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: table.C,v 1.26 2001/03/25 08:52:28 paf Exp $ + $Id: table.C,v 1.29 2001/03/26 09:53:42 paf Exp $ */ #include "pa_config_includes.h" @@ -69,9 +69,11 @@ static void set_or_load( } // parse cells - Table& table=*new(pool) Table(pool, columns); + Table& table=*new(pool) Table(pool, &method_name, columns); char *row_chars; while(row_chars=getrow(&data)) { + if(!*row_chars) // remove empty lines + continue; Array *row=new(pool) Array(pool); while(char *cell_chars=lsplit(&row_chars, '\t')) { String *cell=new(pool) String(pool); @@ -212,6 +214,34 @@ static void _empty(Request& r, const Str } } +struct Record_info { + Pool *pool; + Table *table; + Hash *hash; +}; +static void store_column_item_to_hash(Array::Item *item, void *info) { + Record_info& ri=*static_cast(info); + String& column_name=*static_cast(item); + const String *column_item=ri.table->item(column_name); + Value *value; + if(column_item) + value=new(*ri.pool) VString(*column_item); + else + value=new(*ri.pool) VUnknown(*ri.pool); + ri.hash->put(column_name, value); +} +static void _record(Request& r, const String&, Array *params) { + Table& table=static_cast(r.self)->table(); + if(const Array *columns=table.columns()) { + Pool& pool=r.pool(); + Value& value=*new(pool) VHash(pool); + Record_info record_info={&pool, &table, value.get_hash()}; + columns->for_each(store_column_item_to_hash, &record_info); + + r.write_no_lang(value); + } +} + // initialize void initialize_table_class(Pool& pool, VStateless_class& vclass) { @@ -245,4 +275,7 @@ void initialize_table_class(Pool& pool, // ^table.empty{code-when-empty}{code-when-not} vclass.add_native_method("empty", _empty, 1, 2); + // ^table.record[] + vclass.add_native_method("record", _record, 0, 0); + }