--- parser3/src/classes/table.C 2001/03/19 22:11:07 1.20 +++ parser3/src/classes/table.C 2001/03/25 09:40:54 1.28 @@ -5,9 +5,10 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: table.C,v 1.20 2001/03/19 22:11:07 paf Exp $ + $Id: table.C,v 1.28 2001/03/25 09:40:54 paf Exp $ */ +#include "pa_config_includes.h" #include "pa_common.h" #include "pa_request.h" #include "_table.h" @@ -26,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; @@ -63,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); @@ -89,6 +97,61 @@ 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); + + Table& table=static_cast(r.self)->table(); + + String sdata(pool); + if(params->size()==1) { // not nameless=named output + // write out names line + if(const Array *columns=table.columns()) { // named table + for(int column=0; columnsize(); column++) { + if(column) + sdata.APPEND_CONST("\t"); + sdata.append(*static_cast(columns->quick_get(column)), + String::UL_TABLE); + } + } else { // nameless table + int size=table.size()?static_cast(table.get(0))->size():0; + if(size) + for(int column=0; column(table.quick_get(index)); + for(int column=0; columnsize(); column++) { + if(column) + sdata.APPEND_CONST("\t"); + sdata.append(*static_cast(row->quick_get(column)), + String::UL_TABLE); + } + sdata.APPEND_CONST("\n"); + } + + // write + char *cdata=sdata.cstr(); + file_write(pool, r.absolute(lfile_name), cdata, strlen(cdata), true); +} + static void _count(Request& r, const String&, Array *) { Pool& pool=r.pool(); Value& value=*new(pool) VInt(pool, static_cast(r.self)->table().size()); @@ -126,8 +189,8 @@ static void _menu(Request& r, const Stri Table& table=static_cast(r.self)->table(); bool need_delim=false; - for(int i=0; i