Annotation of parser3/src/classes/table.C, revision 1.41
1.20 paf 1: /** @file
2: Parser: table parser class.
3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.20 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.41 ! paf 8: $Id: table.C,v 1.40 2001/03/30 05:51:12 paf Exp $
1.1 paf 9: */
10:
1.24 paf 11: #include "pa_config_includes.h"
1.16 paf 12: #include "pa_common.h"
1.1 paf 13: #include "pa_request.h"
14: #include "_table.h"
15: #include "pa_vtable.h"
1.6 paf 16: #include "pa_vint.h"
1.1 paf 17:
18: // global var
19:
1.14 paf 20: VStateless_class *table_class;
1.1 paf 21:
22: // methods
1.41 ! paf 23: /// @todo now remove CONFIG untaint, make pos() searches in String
1.2 paf 24: static void set_or_load(
25: Request& r,
26: const String& method_name, Array *params,
27: bool is_load) {
1.1 paf 28: Pool& pool=r.pool();
29: // data is last parameter
1.21 paf 30: Value *vdata_or_filename=static_cast<Value *>(params->get(params->size()-1));
1.4 paf 31: // forcing
1.21 paf 32: // ^load[this file name type]
1.17 paf 33: // ^set{this body type}
1.21 paf 34: r.fail_if_junction_(is_load, *vdata_or_filename,
35: method_name, is_load?"file name must not be junction":"body must be junction");
1.1 paf 36:
1.39 paf 37: // data or table_name
1.25 paf 38: char *data;
1.21 paf 39: if(is_load) {
40: // forcing untaint language
1.39 paf 41: String ltable_name(pool);
42: ltable_name.append(vdata_or_filename->as_string(), String::UL_FILE_NAME, true);
1.25 paf 43: // loading text
1.39 paf 44: data=file_read_text(pool, r.absolute(ltable_name));
1.21 paf 45: } else {
46: // suggesting untaint language
1.23 paf 47: Temp_lang temp_lang(r, String::UL_TABLE);
1.25 paf 48: data=r.process(*vdata_or_filename).as_string().cstr();
1.17 paf 49: }
1.1 paf 50:
51: // parse columns
52: Array *columns;
53: #ifndef NO_STRING_ORIGIN
54: const Origin& origin=method_name.origin();
1.2 paf 55: const char *file=origin.file;
1.1 paf 56: uint line=origin.line;
57: #endif
58: if(params->size()==2) {
59: columns=0;
60: } else {
61: columns=new(pool) Array(pool);
62:
63: if(char *row_chars=getrow(&data))
64: do {
65: String *name=new(pool) String(pool);
1.38 paf 66: name->APPEND_CLEAN(lsplit(&row_chars, '\t'), 0, file, line++);
1.1 paf 67: *columns+=name;
68: } while(row_chars);
69: }
70:
71: // parse cells
1.27 paf 72: Table& table=*new(pool) Table(pool, &method_name, columns);
1.1 paf 73: char *row_chars;
74: while(row_chars=getrow(&data)) {
1.28 paf 75: if(!*row_chars) // remove empty lines
76: continue;
1.1 paf 77: Array *row=new(pool) Array(pool);
78: while(char *cell_chars=lsplit(&row_chars, '\t')) {
79: String *cell=new(pool) String(pool);
1.38 paf 80: cell->APPEND_CLEAN(cell_chars, 0, file, line);
1.1 paf 81: *row+=cell;
82: }
83: line++;
84: table+=row;
85: };
86:
87: // replace any previous table value
1.18 paf 88: static_cast<VTable *>(r.self)->set_table(table);
1.1 paf 89: }
1.2 paf 90: static void _set(Request& r, const String& method_name, Array *params) {
91: set_or_load(r, method_name, params, false);
92: }
93: static void _load(Request& r, const String& method_name, Array *params) {
94: set_or_load(r, method_name, params, true);
95: }
96:
1.22 paf 97: static void _save(Request& r, const String& method_name, Array *params) {
98: Pool& pool=r.pool();
1.39 paf 99: Value *vtable_name=static_cast<Value *>(params->get(params->size()-1));
1.22 paf 100: // forcing
101: // ^save[this body type]
1.39 paf 102: r.fail_if_junction_(true, *vtable_name,
1.22 paf 103: method_name, "file name must not be junction");
104:
105: // forcing untaint language
1.39 paf 106: String ltable_name(pool);
107: ltable_name.append(vtable_name->as_string(),
1.23 paf 108: String::UL_FILE_NAME, true);
1.22 paf 109:
1.31 paf 110: Table& table=static_cast<VTable *>(r.self)->table();
111:
112: String sdata(pool);
113: if(params->size()==1) { // not nameless=named output
114: // write out names line
115: if(table.columns()) { // named table
116: for(int column=0; column<table.columns()->size(); column++) {
117: if(column)
118: sdata.APPEND_CONST("\t");
119: sdata.append(*static_cast<String *>(table.columns()->quick_get(column)),
120: String::UL_TABLE);
121: }
122: } else { // nameless table
123: int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0;
124: if(lsize)
125: for(int column=0; column<lsize; column++) {
126: char *cindex_tab=(char *)malloc(MAX_NUMBER);
127: snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
128: sdata.APPEND_CONST(cindex_tab);
129: }
130: else
131: sdata.APPEND_CONST("empty nameless table");
132: }
133: sdata.APPEND_CONST("\n");
134: }
135: // data lines
136: for(int index=0; index<table.size(); index++) {
137: Array *row=static_cast<Array *>(table.quick_get(index));
138: for(int column=0; column<row->size(); column++) {
139: if(column)
140: sdata.APPEND_CONST("\t");
141: sdata.append(*static_cast<String *>(row->quick_get(column)),
142: String::UL_TABLE);
143: }
144: sdata.APPEND_CONST("\n");
145: }
146:
147: // write
1.39 paf 148: file_write(pool, r.absolute(ltable_name), sdata.cstr(), sdata.size(), true);
1.22 paf 149: }
150:
1.39 paf 151: static void _count(Request& r, const String&method_name, Array *) {
1.6 paf 152: Pool& pool=r.pool();
1.18 paf 153: Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15 paf 154: r.write_no_lang(value);
1.6 paf 155: }
156:
1.39 paf 157: static void _line(Request& r, const String& method_name, Array *) {
1.6 paf 158: Pool& pool=r.pool();
1.37 paf 159: Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.15 paf 160: r.write_no_lang(value);
1.6 paf 161: }
162:
1.39 paf 163: static void _offset(Request& r, const String& method_name, Array *params) {
1.6 paf 164: Pool& pool=r.pool();
1.18 paf 165: Table& table=static_cast<VTable *>(r.self)->table();
1.37 paf 166: if(params->size())
167: table.shift((int)r.process(*static_cast<Value *>(params->get(0))).as_double());
168: else {
169: Value& value=*new(pool) VInt(pool, table.current());
1.15 paf 170: r.write_no_lang(value);
1.6 paf 171: }
172: }
173:
1.7 paf 174: static void _menu(Request& r, const String& method_name, Array *params) {
175: Value& body_code=*static_cast<Value *>(params->get(0));
176: // forcing ^menu{this param type}
177: r.fail_if_junction_(false, body_code,
178: method_name, "body must be junction");
179:
180: Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
181:
1.18 paf 182: Table& table=static_cast<VTable *>(r.self)->table();
1.7 paf 183: bool need_delim=false;
1.37 paf 184: int saved_current=table.current();
1.22 paf 185: for(int row=0; row<table.size(); row++) {
186: table.set_current(row);
1.7 paf 187:
1.12 paf 188: Value& processed_body=r.process(body_code);
1.7 paf 189: if(delim_code) { // delimiter set?
190: const String *string=processed_body.get_string();
191: if(need_delim && string && string->size()) // need delim & iteration produced string?
192: r.write_pass_lang(r.process(*delim_code));
193: need_delim=true;
194: }
195: r.write_pass_lang(processed_body);
196: }
1.37 paf 197: table.set_current(saved_current);
1.7 paf 198: }
199:
1.39 paf 200: static void _empty(Request& r, const String& method_name, Array *params) {
1.18 paf 201: Table& table=static_cast<VTable *>(r.self)->table();
1.8 paf 202: if(table.size()==0) {
203: Value& value=r.process(*static_cast<Value *>(params->get(0)));
204: r.write_pass_lang(value);
205: } else if(params->size()==2) {
206: Value& value=r.process(*static_cast<Value *>(params->get(1)));
207: r.write_pass_lang(value);
208: }
209: }
1.15 paf 210:
1.29 paf 211: struct Record_info {
212: Pool *pool;
213: Table *table;
214: Hash *hash;
215: };
216: static void store_column_item_to_hash(Array::Item *item, void *info) {
217: Record_info& ri=*static_cast<Record_info *>(info);
218: String& column_name=*static_cast<String *>(item);
219: const String *column_item=ri.table->item(column_name);
220: Value *value;
221: if(column_item)
222: value=new(*ri.pool) VString(*column_item);
223: else
224: value=new(*ri.pool) VUnknown(*ri.pool);
225: ri.hash->put(column_name, value);
226: }
1.39 paf 227: static void _record(Request& r, const String& method_name, Array *params) {
1.29 paf 228: Table& table=static_cast<VTable *>(r.self)->table();
229: if(const Array *columns=table.columns()) {
230: Pool& pool=r.pool();
231: Value& value=*new(pool) VHash(pool);
232: Record_info record_info={&pool, &table, value.get_hash()};
233: columns->for_each(store_column_item_to_hash, &record_info);
234:
235: r.write_no_lang(value);
236: }
237: }
238:
1.34 paf 239: struct Seq_item {
240: Array *row;
241: union {
242: char *c_str;
243: double d;
244: } value;
1.32 paf 245: };
1.34 paf 246: static int sort_cmp_string(const void *a, const void *b) {
247: return strcmp(
248: static_cast<const Seq_item *>(a)->value.c_str,
249: static_cast<const Seq_item *>(b)->value.c_str
250: );
251: }
252: static int sort_cmp_double(const void *a, const void *b) {
253: double va=static_cast<const Seq_item *>(a)->value.d;
254: double vb=static_cast<const Seq_item *>(b)->value.d;
255: if(va<vb)
256: return -1;
257: else if(va>vb)
258: return +1;
259: else
260: return 0;
261: }
1.32 paf 262: static void _sort(Request& r, const String& method_name, Array *params) {
263: Value& key_maker=*(Value *)params->get(0);
264: // forcing ^sort{this} ^sort(or this) param type
265: r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
266:
267: bool reverse;
268: if(params->size()==2) { // ..[asc|desc]
1.35 paf 269: Value& order=*(Value *)params->get(1);
1.32 paf 270: // forcing ..[this param-type]
1.35 paf 271: r.fail_if_junction_(true, order, method_name, "order must not be junction");
272: reverse=order.as_string()=="desc";
1.32 paf 273: } else
274: reverse=false;
275:
276: Table& table=static_cast<VTable *>(r.self)->table();
1.34 paf 277:
278: // anything to sort?
279: if(!table.size())
280: return;
281:
282: Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
283: int i;
284:
285: // calculate key values
286: bool key_values_are_strings=true;
287: for(i=0; i<table.size(); i++) {
1.32 paf 288: table.set_current(i);
289: // calculate key value
1.34 paf 290: seq[i].row=(Array *)table.get(i);
291: Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
292: if(i==0) // determining key values type by first one
293: key_values_are_strings=value.is_string();
294:
295: if(key_values_are_strings)
296: seq[i].value.c_str=value.as_string().cstr();
297: else
298: seq[i].value.d=value.as_double();
1.32 paf 299: }
300: // sort keys
1.34 paf 301: _qsort(seq, table.size(), sizeof(Seq_item),
302: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 303:
1.34 paf 304: // reorder table as they require in 'seq'
305: for(i=0; i<table.size(); i++)
306: table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32 paf 307:
1.34 paf 308: // reset 'current'
1.32 paf 309: table.set_current(0);
310: }
311:
1.39 paf 312: static void _locate(Request& r, const String& method_name, Array *params) {
1.36 paf 313: VTable& vtable=*static_cast<VTable *>(r.self);
314: Table& table=vtable.table();
315: vtable.last_locate_was_successful=table.locate(
316: static_cast<Value *>(params->get(0))->as_string(),
317: static_cast<Value *>(params->get(1))->as_string());
318: }
319:
1.37 paf 320: static void _found(Request& r, const String& method_name, Array *params) {
321: if(static_cast<VTable *>(r.self)->last_locate_was_successful) {
322: Value& then_code=*static_cast<Value *>(params->get(0));
323: // forcing ^found{this param type}
324: r.fail_if_junction_(false, then_code,
325: method_name, "found-parameter must be junction");
326: r.write_pass_lang(r.process(then_code));
327: } else if(params->size()==2) {
328: Value& else_code=*static_cast<Value *>(params->get(1));
329: // forcing ^found{this param type}
330: r.fail_if_junction_(false, else_code,
331: method_name, "not found-parameter must be junction");
332: r.write_pass_lang(r.process(else_code));
333: }
334: }
335:
1.39 paf 336: static void _flip(Request& r, const String& method_name, Array *params) {
337: Pool& pool=r.pool();
338: VTable& vtable=*static_cast<VTable *>(r.self);
339:
340: Table& old_table=*vtable.get_table();
341: Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
342: if(old_table.size())
343: if(int old_cols=old_table.at(0).size())
344: for(int column=0; column<old_cols; column++) {
345: Array& new_row=*new(pool) Array(pool, old_table.size());
346: for(int i=0; i<old_table.size(); i++) {
347: const Array& old_row=old_table.at(i);
348: new_row+=column<old_row.size()?old_row.get(column):empty_string;
349: }
350: new_table+=&new_row;
351: }
352:
353: vtable.set_table(new_table);
354: }
355:
1.41 ! paf 356: /// @todo now require \t to be clean [UL_NO]
! 357: static void _append(Request& r, const String& method_name, Array *params) {
! 358: Pool& pool=r.pool();
! 359: // data is last parameter
! 360: Value *value=static_cast<Value *>(params->get(0));
! 361: // forcing [this body type]
! 362: r.fail_if_junction_(true, *value, method_name, "body must not be junction");
! 363:
! 364: const String& string=static_cast<VString *>(value)->as_string();
! 365:
! 366: // parse cells
! 367: Array& row=*new(pool) Array(pool);
! 368: size_t pos_after=0;
! 369: int pos_before;
! 370: while((pos_before=string.pos("\t", pos_after))>=0) {
! 371: row+=&string.piece(pos_after, pos_before);
! 372: pos_after=pos_before+1/*\t*/;
! 373: }
! 374: // last piece
! 375: if(pos_after<string.size())
! 376: row+=&string.piece(pos_after, string.size());
! 377:
! 378: static_cast<VTable *>(r.self)->table()+=&row;
! 379: }
! 380:
1.15 paf 381: // initialize
1.8 paf 382:
1.14 paf 383: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.22 paf 384: // ^table.set{data}
385: // ^table.set[nameless]{data}
1.40 paf 386: vclass.add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2 paf 387:
1.10 paf 388: // ^table.load[file]
389: // ^table.load[nameless;file]
1.40 paf 390: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.22 paf 391:
392: // ^table.save[file]
393: // ^table.save[nameless;file]
1.40 paf 394: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 395:
396: // ^table.count[]
1.40 paf 397: vclass.add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 398:
399: // ^table.line[]
1.40 paf 400: vclass.add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 401:
1.10 paf 402: // ^table.offset[]
403: // ^table.offset[offset]
1.40 paf 404: vclass.add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7 paf 405:
1.10 paf 406: // ^table.menu{code}
407: // ^table.menu{code}[delim]
1.40 paf 408: vclass.add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8 paf 409:
1.10 paf 410: // ^table.empty{code-when-empty}
411: // ^table.empty{code-when-empty}{code-when-not}
1.40 paf 412: vclass.add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2);
1.29 paf 413:
414: // ^table.record[]
1.40 paf 415: vclass.add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0);
1.32 paf 416:
417: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
418: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
1.40 paf 419: vclass.add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 420:
1.36 paf 421: // ^table.locate[field;value]
1.40 paf 422: vclass.add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.37 paf 423: // ^table.found{when-found}
424: // ^table.found{when-found}{when-not-found}
1.40 paf 425: vclass.add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2);
1.39 paf 426:
427: // ^table.flip[]
1.40 paf 428: vclass.add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 ! paf 429:
! 430: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
! 431: vclass.add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.40 paf 432: }
E-mail: