|
|
| version 1.26, 2001/03/25 08:52:28 | version 1.30, 2001/03/26 10:36:52 |
|---|---|
| Line 69 static void set_or_load( | Line 69 static void set_or_load( |
| } | } |
| // parse cells | // parse cells |
| Table& table=*new(pool) Table(pool, 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 108 static void _save(Request& r, const Stri | Line 110 static void _save(Request& r, const Stri |
| lfile_name.append(vfile_name->as_string(), | lfile_name.append(vfile_name->as_string(), |
| String::UL_FILE_NAME, true); | String::UL_FILE_NAME, true); |
| Table& table=static_cast<VTable *>(r.self)->table(); | static_cast<VTable *>(r.self)->table().save( |
| params->size()==2/*nameless save*/, | |
| String sdata(pool); | r.absolute(lfile_name)); |
| if(params->size()==1) { // not nameless=named output | |
| // write out names line | |
| if(const Array *columns=table.columns()) { // named table | |
| for(int column=0; column<columns->size(); column++) { | |
| if(column) | |
| sdata.APPEND_CONST("\t"); | |
| sdata.append(*static_cast<String *>(columns->quick_get(column)), | |
| String::UL_TABLE); | |
| } | |
| } else { // nameless table | |
| int size=table.size()?static_cast<Array *>(table.get(0))->size():0; | |
| if(size) | |
| for(int column=0; column<size; column++) { | |
| char *cindex_tab=(char *)pool.malloc(MAX_NUMBER); | |
| snprintf(cindex_tab, MAX_NUMBER, "%d\t", column); | |
| sdata.APPEND_CONST(cindex_tab); | |
| } | |
| else | |
| sdata.APPEND_CONST("empty nameless table"); | |
| } | |
| sdata.APPEND_CONST("\n"); | |
| } | |
| // data lines | |
| for(int index=0; index<table.size(); index++) { | |
| Array *row=static_cast<Array *>(table.quick_get(index)); | |
| for(int column=0; column<row->size(); column++) { | |
| if(column) | |
| sdata.APPEND_CONST("\t"); | |
| sdata.append(*static_cast<String *>(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 *) { | static void _count(Request& r, const String&, Array *) { |
| Line 212 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) { |
| Line 245 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); | |
| } | } |