Annotation of parser3/src/classes/table.C, revision 1.40
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.40 ! paf 8: $Id: table.C,v 1.39 2001/03/29 20:53:02 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
23:
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: }
90:
1.2 paf 91:
92: static void _set(Request& r, const String& method_name, Array *params) {
93: set_or_load(r, method_name, params, false);
94: }
95:
96: static void _load(Request& r, const String& method_name, Array *params) {
97: set_or_load(r, method_name, params, true);
98: }
99:
1.22 paf 100: static void _save(Request& r, const String& method_name, Array *params) {
101: Pool& pool=r.pool();
1.39 paf 102: Value *vtable_name=static_cast<Value *>(params->get(params->size()-1));
1.22 paf 103: // forcing
104: // ^save[this body type]
1.39 paf 105: r.fail_if_junction_(true, *vtable_name,
1.22 paf 106: method_name, "file name must not be junction");
107:
108: // forcing untaint language
1.39 paf 109: String ltable_name(pool);
110: ltable_name.append(vtable_name->as_string(),
1.23 paf 111: String::UL_FILE_NAME, true);
1.22 paf 112:
1.31 paf 113: Table& table=static_cast<VTable *>(r.self)->table();
114:
115: String sdata(pool);
116: if(params->size()==1) { // not nameless=named output
117: // write out names line
118: if(table.columns()) { // named table
119: for(int column=0; column<table.columns()->size(); column++) {
120: if(column)
121: sdata.APPEND_CONST("\t");
122: sdata.append(*static_cast<String *>(table.columns()->quick_get(column)),
123: String::UL_TABLE);
124: }
125: } else { // nameless table
126: int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0;
127: if(lsize)
128: for(int column=0; column<lsize; column++) {
129: char *cindex_tab=(char *)malloc(MAX_NUMBER);
130: snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
131: sdata.APPEND_CONST(cindex_tab);
132: }
133: else
134: sdata.APPEND_CONST("empty nameless table");
135: }
136: sdata.APPEND_CONST("\n");
137: }
138: // data lines
139: for(int index=0; index<table.size(); index++) {
140: Array *row=static_cast<Array *>(table.quick_get(index));
141: for(int column=0; column<row->size(); column++) {
142: if(column)
143: sdata.APPEND_CONST("\t");
144: sdata.append(*static_cast<String *>(row->quick_get(column)),
145: String::UL_TABLE);
146: }
147: sdata.APPEND_CONST("\n");
148: }
149:
150: // write
1.39 paf 151: file_write(pool, r.absolute(ltable_name), sdata.cstr(), sdata.size(), true);
1.22 paf 152: }
153:
1.39 paf 154: static void _count(Request& r, const String&method_name, Array *) {
1.6 paf 155: Pool& pool=r.pool();
1.18 paf 156: Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15 paf 157: r.write_no_lang(value);
1.6 paf 158: }
159:
1.39 paf 160: static void _line(Request& r, const String& method_name, Array *) {
1.6 paf 161: Pool& pool=r.pool();
1.37 paf 162: Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.15 paf 163: r.write_no_lang(value);
1.6 paf 164: }
165:
1.39 paf 166: static void _offset(Request& r, const String& method_name, Array *params) {
1.6 paf 167: Pool& pool=r.pool();
1.18 paf 168: Table& table=static_cast<VTable *>(r.self)->table();
1.37 paf 169: if(params->size())
170: table.shift((int)r.process(*static_cast<Value *>(params->get(0))).as_double());
171: else {
172: Value& value=*new(pool) VInt(pool, table.current());
1.15 paf 173: r.write_no_lang(value);
1.6 paf 174: }
175: }
176:
1.7 paf 177: static void _menu(Request& r, const String& method_name, Array *params) {
178: Value& body_code=*static_cast<Value *>(params->get(0));
179: // forcing ^menu{this param type}
180: r.fail_if_junction_(false, body_code,
181: method_name, "body must be junction");
182:
183: Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
184:
1.18 paf 185: Table& table=static_cast<VTable *>(r.self)->table();
1.7 paf 186: bool need_delim=false;
1.37 paf 187: int saved_current=table.current();
1.22 paf 188: for(int row=0; row<table.size(); row++) {
189: table.set_current(row);
1.7 paf 190:
1.12 paf 191: Value& processed_body=r.process(body_code);
1.7 paf 192: if(delim_code) { // delimiter set?
193: const String *string=processed_body.get_string();
194: if(need_delim && string && string->size()) // need delim & iteration produced string?
195: r.write_pass_lang(r.process(*delim_code));
196: need_delim=true;
197: }
198: r.write_pass_lang(processed_body);
199: }
1.37 paf 200: table.set_current(saved_current);
1.7 paf 201: }
202:
1.39 paf 203: static void _empty(Request& r, const String& method_name, Array *params) {
1.18 paf 204: Table& table=static_cast<VTable *>(r.self)->table();
1.8 paf 205: if(table.size()==0) {
206: Value& value=r.process(*static_cast<Value *>(params->get(0)));
207: r.write_pass_lang(value);
208: } else if(params->size()==2) {
209: Value& value=r.process(*static_cast<Value *>(params->get(1)));
210: r.write_pass_lang(value);
211: }
212: }
1.15 paf 213:
1.29 paf 214: struct Record_info {
215: Pool *pool;
216: Table *table;
217: Hash *hash;
218: };
219: static void store_column_item_to_hash(Array::Item *item, void *info) {
220: Record_info& ri=*static_cast<Record_info *>(info);
221: String& column_name=*static_cast<String *>(item);
222: const String *column_item=ri.table->item(column_name);
223: Value *value;
224: if(column_item)
225: value=new(*ri.pool) VString(*column_item);
226: else
227: value=new(*ri.pool) VUnknown(*ri.pool);
228: ri.hash->put(column_name, value);
229: }
1.39 paf 230: static void _record(Request& r, const String& method_name, Array *params) {
1.29 paf 231: Table& table=static_cast<VTable *>(r.self)->table();
232: if(const Array *columns=table.columns()) {
233: Pool& pool=r.pool();
234: Value& value=*new(pool) VHash(pool);
235: Record_info record_info={&pool, &table, value.get_hash()};
236: columns->for_each(store_column_item_to_hash, &record_info);
237:
238: r.write_no_lang(value);
239: }
240: }
241:
1.34 paf 242: struct Seq_item {
243: Array *row;
244: union {
245: char *c_str;
246: double d;
247: } value;
1.32 paf 248: };
1.34 paf 249: static int sort_cmp_string(const void *a, const void *b) {
250: return strcmp(
251: static_cast<const Seq_item *>(a)->value.c_str,
252: static_cast<const Seq_item *>(b)->value.c_str
253: );
254: }
255: static int sort_cmp_double(const void *a, const void *b) {
256: double va=static_cast<const Seq_item *>(a)->value.d;
257: double vb=static_cast<const Seq_item *>(b)->value.d;
258: if(va<vb)
259: return -1;
260: else if(va>vb)
261: return +1;
262: else
263: return 0;
264: }
1.32 paf 265: static void _sort(Request& r, const String& method_name, Array *params) {
266: Value& key_maker=*(Value *)params->get(0);
267: // forcing ^sort{this} ^sort(or this) param type
268: r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
269:
270: bool reverse;
271: if(params->size()==2) { // ..[asc|desc]
1.35 paf 272: Value& order=*(Value *)params->get(1);
1.32 paf 273: // forcing ..[this param-type]
1.35 paf 274: r.fail_if_junction_(true, order, method_name, "order must not be junction");
275: reverse=order.as_string()=="desc";
1.32 paf 276: } else
277: reverse=false;
278:
279: Table& table=static_cast<VTable *>(r.self)->table();
1.34 paf 280:
281: // anything to sort?
282: if(!table.size())
283: return;
284:
285: Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
286: int i;
287:
288: // calculate key values
289: bool key_values_are_strings=true;
290: for(i=0; i<table.size(); i++) {
1.32 paf 291: table.set_current(i);
292: // calculate key value
1.34 paf 293: seq[i].row=(Array *)table.get(i);
294: Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
295: if(i==0) // determining key values type by first one
296: key_values_are_strings=value.is_string();
297:
298: if(key_values_are_strings)
299: seq[i].value.c_str=value.as_string().cstr();
300: else
301: seq[i].value.d=value.as_double();
1.32 paf 302: }
303: // sort keys
1.34 paf 304: _qsort(seq, table.size(), sizeof(Seq_item),
305: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 306:
1.34 paf 307: // reorder table as they require in 'seq'
308: for(i=0; i<table.size(); i++)
309: table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32 paf 310:
1.34 paf 311: // reset 'current'
1.32 paf 312: table.set_current(0);
313: }
314:
1.39 paf 315: static void _locate(Request& r, const String& method_name, Array *params) {
1.36 paf 316: VTable& vtable=*static_cast<VTable *>(r.self);
317: Table& table=vtable.table();
318: vtable.last_locate_was_successful=table.locate(
319: static_cast<Value *>(params->get(0))->as_string(),
320: static_cast<Value *>(params->get(1))->as_string());
321: }
322:
1.37 paf 323: static void _found(Request& r, const String& method_name, Array *params) {
324: if(static_cast<VTable *>(r.self)->last_locate_was_successful) {
325: Value& then_code=*static_cast<Value *>(params->get(0));
326: // forcing ^found{this param type}
327: r.fail_if_junction_(false, then_code,
328: method_name, "found-parameter must be junction");
329: r.write_pass_lang(r.process(then_code));
330: } else if(params->size()==2) {
331: Value& else_code=*static_cast<Value *>(params->get(1));
332: // forcing ^found{this param type}
333: r.fail_if_junction_(false, else_code,
334: method_name, "not found-parameter must be junction");
335: r.write_pass_lang(r.process(else_code));
336: }
337: }
338:
1.39 paf 339: static void _flip(Request& r, const String& method_name, Array *params) {
340: Pool& pool=r.pool();
341: VTable& vtable=*static_cast<VTable *>(r.self);
342:
343: Table& old_table=*vtable.get_table();
344: Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
345: if(old_table.size())
346: if(int old_cols=old_table.at(0).size())
347: for(int column=0; column<old_cols; column++) {
348: Array& new_row=*new(pool) Array(pool, old_table.size());
349: for(int i=0; i<old_table.size(); i++) {
350: const Array& old_row=old_table.at(i);
351: new_row+=column<old_row.size()?old_row.get(column):empty_string;
352: }
353: new_table+=&new_row;
354: }
355:
356: vtable.set_table(new_table);
357: }
358:
1.15 paf 359: // initialize
1.8 paf 360:
1.14 paf 361: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.22 paf 362: // ^table.set{data}
363: // ^table.set[nameless]{data}
1.40 ! paf 364: vclass.add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2 paf 365:
1.10 paf 366: // ^table.load[file]
367: // ^table.load[nameless;file]
1.40 ! paf 368: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.22 paf 369:
370: // ^table.save[file]
371: // ^table.save[nameless;file]
1.40 ! paf 372: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 373:
374: // ^table.count[]
1.40 ! paf 375: vclass.add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 376:
377: // ^table.line[]
1.40 ! paf 378: vclass.add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 379:
1.10 paf 380: // ^table.offset[]
381: // ^table.offset[offset]
1.40 ! paf 382: vclass.add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7 paf 383:
1.10 paf 384: // ^table.menu{code}
385: // ^table.menu{code}[delim]
1.40 ! paf 386: vclass.add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8 paf 387:
1.10 paf 388: // ^table.empty{code-when-empty}
389: // ^table.empty{code-when-empty}{code-when-not}
1.40 ! paf 390: vclass.add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2);
1.29 paf 391:
392: // ^table.record[]
1.40 ! paf 393: vclass.add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0);
1.32 paf 394:
395: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
396: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
1.40 ! paf 397: vclass.add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 398:
1.36 paf 399: // ^table.locate[field;value]
1.40 ! paf 400: vclass.add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.37 paf 401: // ^table.found{when-found}
402: // ^table.found{when-found}{when-not-found}
1.40 ! paf 403: vclass.add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2);
1.39 paf 404:
405: // ^table.flip[]
1.40 ! paf 406: vclass.add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
! 407: }
E-mail: