|
|
| version 1.19, 2001/03/19 20:46:35 | version 1.30, 2001/03/26 10:36:52 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| Parser | Parser: table parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | $Id$ |
| */ | */ |
| #include "pa_config_includes.h" | |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "_table.h" | #include "_table.h" |
| Line 24 static void set_or_load( | Line 27 static void set_or_load( |
| bool is_load) { | bool is_load) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // data is last parameter | // data is last parameter |
| Value *vdata=static_cast<Value *>(params->get(params->size()-1)); | Value *vdata_or_filename=static_cast<Value *>(params->get(params->size()-1)); |
| // forcing | // forcing |
| // ^load{this body type} | // ^load[this file name type] |
| // ^set{this body 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 | // data or file_name |
| char *data_or_filename; | char *data; |
| { | if(is_load) { |
| Temp_lang temp_lang(r, | // forcing untaint language |
| is_load ? String::Untaint_lang::FILE : String::Untaint_lang::TABLE); | String lfile_name(pool); |
| data_or_filename=r.process(*vdata).as_string().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::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 | // parse columns |
| Array *columns; | Array *columns; |
| Line 61 static void set_or_load( | Line 69 static void set_or_load( |
| } | } |
| // parse cells | // parse cells |
| Table& table=*new(pool) Table(pool, method_name, columns); | Table& table=*new(pool) Table(pool, &method_name, columns); |
| char *row_chars; | char *row_chars; |
| while(row_chars=getrow(&data)) { | while(row_chars=getrow(&data)) { |
| if(!*row_chars) // remove empty lines | |
| continue; | |
| Array *row=new(pool) Array(pool); | Array *row=new(pool) Array(pool); |
| while(char *cell_chars=lsplit(&row_chars, '\t')) { | while(char *cell_chars=lsplit(&row_chars, '\t')) { |
| String *cell=new(pool) String(pool); | String *cell=new(pool) String(pool); |
| Line 87 static void _load(Request& r, const Stri | Line 97 static void _load(Request& r, const Stri |
| set_or_load(r, method_name, params, true); | 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<Value *>(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<VTable *>(r.self)->table().save( | |
| params->size()==2/*nameless save*/, | |
| r.absolute(lfile_name)); | |
| } | |
| static void _count(Request& r, const String&, Array *) { | static void _count(Request& r, const String&, Array *) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size()); | Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size()); |
| Line 124 static void _menu(Request& r, const Stri | Line 152 static void _menu(Request& r, const Stri |
| Table& table=static_cast<VTable *>(r.self)->table(); | Table& table=static_cast<VTable *>(r.self)->table(); |
| bool need_delim=false; | bool need_delim=false; |
| for(int i=0; i<table.size(); i++) { | for(int row=0; row<table.size(); row++) { |
| table.set_current(i); | table.set_current(row); |
| Value& processed_body=r.process(body_code); | Value& processed_body=r.process(body_code); |
| if(delim_code) { // delimiter set? | if(delim_code) { // delimiter set? |
| Line 149 static void _empty(Request& r, const Str | Line 177 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<Record_info *>(info); | |
| String& column_name=*static_cast<String *>(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<VTable *>(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 | // initialize |
| void initialize_table_class(Pool& pool, VStateless_class& vclass) { | void initialize_table_class(Pool& pool, VStateless_class& vclass) { |
| // ^table.set[data] | // ^table.set{data} |
| // ^table.set[nameless;data] | // ^table.set[nameless]{data} |
| vclass.add_native_method("set", _set, 1, 2); | vclass.add_native_method("set", _set, 1, 2); |
| // ^table.load[file] | // ^table.load[file] |
| // ^table.load[nameless;file] | // ^table.load[nameless;file] |
| vclass.add_native_method("load", _load, 1, 2); | 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[] | // ^table.count[] |
| vclass.add_native_method("count", _count, 0, 0); | vclass.add_native_method("count", _count, 0, 0); |
| Line 178 void initialize_table_class(Pool& pool, | Line 238 void initialize_table_class(Pool& pool, |
| // ^table.empty{code-when-empty}{code-when-not} | // ^table.empty{code-when-empty}{code-when-not} |
| vclass.add_native_method("empty", _empty, 1, 2); | vclass.add_native_method("empty", _empty, 1, 2); |
| // ^table.record[] | |
| vclass.add_native_method("record", _record, 0, 0); | |
| } | } |