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