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