--- parser3/src/classes/table.C 2001/03/19 20:46:35 1.19 +++ parser3/src/classes/table.C 2001/03/26 10:36:52 1.30 @@ -1,11 +1,14 @@ -/* - Parser +/** @file + Parser: table parser class. + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://design.ru/paf) - $Id: table.C,v 1.19 2001/03/19 20:46:35 paf Exp $ + $Id: table.C,v 1.30 2001/03/26 10:36:52 paf Exp $ */ +#include "pa_config_includes.h" #include "pa_common.h" #include "pa_request.h" #include "_table.h" @@ -24,21 +27,26 @@ static void set_or_load( bool is_load) { Pool& pool=r.pool(); // data is last parameter - Value *vdata=static_cast(params->get(params->size()-1)); + Value *vdata_or_filename=static_cast(params->get(params->size()-1)); // forcing - // ^load{this body type} + // ^load[this file name type] // ^set{this body type} - r.fail_if_junction_(false, *vdata, method_name, "body must be junction"); + r.fail_if_junction_(is_load, *vdata_or_filename, + method_name, is_load?"file name must not be junction":"body must be junction"); // data or file_name - char *data_or_filename; - { - Temp_lang temp_lang(r, - is_load ? String::Untaint_lang::FILE : String::Untaint_lang::TABLE); - data_or_filename=r.process(*vdata).as_string().cstr(); + char *data; + if(is_load) { + // forcing untaint language + String lfile_name(pool); + lfile_name.append(vdata_or_filename->as_string(), String::UL_FILE_NAME, true); + // loading text + data=file_read_text(pool, r.absolute(lfile_name)); + } else { + // suggesting untaint language + Temp_lang temp_lang(r, String::UL_TABLE); + data=r.process(*vdata_or_filename).as_string().cstr(); } - // data - char *data=is_load?file_read(pool, r.absolute(data_or_filename)):data_or_filename; // parse columns Array *columns; @@ -61,9 +69,11 @@ static void set_or_load( } // parse cells - Table& table=*new(pool) Table(pool, method_name, 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); @@ -87,6 +97,24 @@ static void _load(Request& r, const Stri set_or_load(r, method_name, params, true); } +static void _save(Request& r, const String& method_name, Array *params) { + Pool& pool=r.pool(); + Value *vfile_name=static_cast(params->get(params->size()-1)); + // forcing + // ^save[this body type] + r.fail_if_junction_(true, *vfile_name, + method_name, "file name must not be junction"); + + // forcing untaint language + String lfile_name(pool); + lfile_name.append(vfile_name->as_string(), + String::UL_FILE_NAME, true); + + static_cast(r.self)->table().save( + params->size()==2/*nameless save*/, + r.absolute(lfile_name)); +} + static void _count(Request& r, const String&, Array *) { Pool& pool=r.pool(); Value& value=*new(pool) VInt(pool, static_cast(r.self)->table().size()); @@ -124,8 +152,8 @@ static void _menu(Request& r, const Stri Table& table=static_cast(r.self)->table(); bool need_delim=false; - for(int i=0; i(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) { - // ^table.set[data] - // ^table.set[nameless;data] + // ^table.set{data} + // ^table.set[nameless]{data} vclass.add_native_method("set", _set, 1, 2); // ^table.load[file] // ^table.load[nameless;file] vclass.add_native_method("load", _load, 1, 2); + // ^table.save[file] + // ^table.save[nameless;file] + vclass.add_native_method("save", _save, 1, 2); + // ^table.count[] vclass.add_native_method("count", _count, 0, 0); @@ -178,4 +238,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); + }