--- parser3/src/classes/table.C 2001/03/20 06:45:16 1.21 +++ parser3/src/classes/table.C 2001/03/26 10:36:52 1.30 @@ -5,9 +5,10 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: table.C,v 1.21 2001/03/20 06:45:16 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" @@ -34,21 +35,18 @@ static void set_or_load( method_name, is_load?"file name must not be junction":"body must be junction"); // data or file_name - char *ldata_or_filename; + char *data; if(is_load) { // forcing untaint language String lfile_name(pool); - lfile_name.append(vdata_or_filename->as_string(), - String::Untaint_lang::FILE_NAME, true); - ldata_or_filename=lfile_name.cstr(); + 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::Untaint_lang::TABLE); - ldata_or_filename=r.process(*vdata_or_filename).as_string().cstr(); + 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(ldata_or_filename)/*\, false*/):ldata_or_filename; // parse columns Array *columns; @@ -71,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); @@ -97,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()); @@ -134,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); @@ -188,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); + }