Annotation of parser3/src/classes/table.C, revision 1.33
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.33 ! paf 8: $Id: table.C,v 1.32 2001/03/27 14:50:44 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.2 paf 37: // data or file_name
1.25 paf 38: char *data;
1.21 paf 39: if(is_load) {
40: // forcing untaint language
41: String lfile_name(pool);
1.25 paf 42: lfile_name.append(vdata_or_filename->as_string(), String::UL_FILE_NAME, true);
43: // loading text
44: data=file_read_text(pool, r.absolute(lfile_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);
66: name->APPEND(lsplit(&row_chars, '\t'), 0, file, line++);
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);
80: cell->APPEND(cell_chars, 0, file, line);
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();
102: Value *vfile_name=static_cast<Value *>(params->get(params->size()-1));
103: // forcing
104: // ^save[this body type]
105: r.fail_if_junction_(true, *vfile_name,
106: method_name, "file name must not be junction");
107:
108: // forcing untaint language
109: String lfile_name(pool);
110: lfile_name.append(vfile_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
151: file_write(pool, r.absolute(lfile_name), sdata.cstr(), sdata.size(), true);
1.22 paf 152: }
153:
1.6 paf 154: static void _count(Request& r, const String&, Array *) {
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:
160: static void _line(Request& r, const String&, Array *) {
161: Pool& pool=r.pool();
1.18 paf 162: Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().get_current());
1.15 paf 163: r.write_no_lang(value);
1.6 paf 164: }
165:
166: static void _offset(Request& r, const String&, Array *params) {
167: Pool& pool=r.pool();
1.18 paf 168: Table& table=static_cast<VTable *>(r.self)->table();
1.6 paf 169: if(params->size()) {
170: if(int size=table.size()) {
1.9 paf 171: int offset=
172: (int)r.process(*static_cast<Value *>(params->get(0))).get_double();
1.6 paf 173: table.set_current((table.get_current()+offset+size)%size);
174: }
175: } else {
176: Value& value=*new(pool) VInt(pool, table.get_current());
1.15 paf 177: r.write_no_lang(value);
1.6 paf 178: }
179: }
180:
1.7 paf 181: static void _menu(Request& r, const String& method_name, Array *params) {
182: Value& body_code=*static_cast<Value *>(params->get(0));
183: // forcing ^menu{this param type}
184: r.fail_if_junction_(false, body_code,
185: method_name, "body must be junction");
186:
187: Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
188:
1.18 paf 189: Table& table=static_cast<VTable *>(r.self)->table();
1.7 paf 190: bool need_delim=false;
1.22 paf 191: for(int row=0; row<table.size(); row++) {
192: table.set_current(row);
1.7 paf 193:
1.12 paf 194: Value& processed_body=r.process(body_code);
1.7 paf 195: if(delim_code) { // delimiter set?
196: const String *string=processed_body.get_string();
197: if(need_delim && string && string->size()) // need delim & iteration produced string?
198: r.write_pass_lang(r.process(*delim_code));
199: need_delim=true;
200: }
201: r.write_pass_lang(processed_body);
202: }
203: }
204:
1.8 paf 205: static void _empty(Request& r, const String&, Array *params) {
1.18 paf 206: Table& table=static_cast<VTable *>(r.self)->table();
1.8 paf 207: if(table.size()==0) {
208: Value& value=r.process(*static_cast<Value *>(params->get(0)));
209: r.write_pass_lang(value);
210: } else if(params->size()==2) {
211: Value& value=r.process(*static_cast<Value *>(params->get(1)));
212: r.write_pass_lang(value);
213: }
214: }
1.15 paf 215:
1.29 paf 216: struct Record_info {
217: Pool *pool;
218: Table *table;
219: Hash *hash;
220: };
221: static void store_column_item_to_hash(Array::Item *item, void *info) {
222: Record_info& ri=*static_cast<Record_info *>(info);
223: String& column_name=*static_cast<String *>(item);
224: const String *column_item=ri.table->item(column_name);
225: Value *value;
226: if(column_item)
227: value=new(*ri.pool) VString(*column_item);
228: else
229: value=new(*ri.pool) VUnknown(*ri.pool);
230: ri.hash->put(column_name, value);
231: }
232: static void _record(Request& r, const String&, Array *params) {
233: Table& table=static_cast<VTable *>(r.self)->table();
234: if(const Array *columns=table.columns()) {
235: Pool& pool=r.pool();
236: Value& value=*new(pool) VHash(pool);
237: Record_info record_info={&pool, &table, value.get_hash()};
238: columns->for_each(store_column_item_to_hash, &record_info);
239:
240: r.write_no_lang(value);
241: }
242: }
243:
1.32 paf 244: struct Order_item {
245: int index;
246: Value *value;
247: };
248: static void _sort(Request& r, const String& method_name, Array *params) {
249: Value& key_maker=*(Value *)params->get(0);
250: // forcing ^sort{this} ^sort(or this) param type
251: r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
252:
253: bool reverse;
254: if(params->size()==2) { // ..[asc|desc]
255: Value& order=*(Value *)params->get(1);
256: // forcing ..[this param-type]
257: r.fail_if_junction_(false, order, method_name, "order must not be junction");
258: reverse=order.as_string()=="asc";
259: } else
260: reverse=false;
261:
262: // calculating key values
263: Table& table=static_cast<VTable *>(r.self)->table();
264: Order_item *order=(Order_item *)malloc(sizeof(Order_item)*table.size());
265: Order_item *current=order;
266: for(int i=0; i<table.size(); i++) {
267: table.set_current(i);
268: // calculate key value
269: current->index=i;
1.33 ! paf 270: current->value=r.process(key_maker).get_expr_result(true/*return string as-is*/);
1.32 paf 271: current++;
272: }
273: // sort keys
274: //\ todo
275:
276: // reorder table as they require in 'order'
277: //\ todo
278:
279: table.set_current(0);
280: }
281:
1.15 paf 282: // initialize
1.8 paf 283:
1.14 paf 284: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.22 paf 285: // ^table.set{data}
286: // ^table.set[nameless]{data}
1.1 paf 287: vclass.add_native_method("set", _set, 1, 2);
1.2 paf 288:
1.10 paf 289: // ^table.load[file]
290: // ^table.load[nameless;file]
1.2 paf 291: vclass.add_native_method("load", _load, 1, 2);
1.22 paf 292:
293: // ^table.save[file]
294: // ^table.save[nameless;file]
295: vclass.add_native_method("save", _save, 1, 2);
1.6 paf 296:
297: // ^table.count[]
298: vclass.add_native_method("count", _count, 0, 0);
299:
300: // ^table.line[]
301: vclass.add_native_method("line", _line, 0, 0);
302:
1.10 paf 303: // ^table.offset[]
304: // ^table.offset[offset]
1.6 paf 305: vclass.add_native_method("offset", _offset, 0, 1);
1.7 paf 306:
1.10 paf 307: // ^table.menu{code}
308: // ^table.menu{code}[delim]
1.7 paf 309: vclass.add_native_method("menu", _menu, 1, 2);
1.8 paf 310:
1.10 paf 311: // ^table.empty{code-when-empty}
312: // ^table.empty{code-when-empty}{code-when-not}
1.8 paf 313: vclass.add_native_method("empty", _empty, 1, 2);
1.29 paf 314:
315: // ^table.record[]
316: vclass.add_native_method("record", _record, 0, 0);
1.32 paf 317:
318: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
319: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
320: vclass.add_native_method("sort", _sort, 1, 2);
1.8 paf 321:
1.1 paf 322: }
E-mail: