Annotation of parser3/src/classes/table.C, revision 1.30
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.30 ! paf 8: $Id: table.C,v 1.29 2001/03/26 09:53:42 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.30 ! paf 113: static_cast<VTable *>(r.self)->table().save(
! 114: params->size()==2/*nameless save*/,
! 115: r.absolute(lfile_name));
1.22 paf 116: }
117:
1.6 paf 118: static void _count(Request& r, const String&, Array *) {
119: Pool& pool=r.pool();
1.18 paf 120: Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15 paf 121: r.write_no_lang(value);
1.6 paf 122: }
123:
124: static void _line(Request& r, const String&, Array *) {
125: Pool& pool=r.pool();
1.18 paf 126: Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().get_current());
1.15 paf 127: r.write_no_lang(value);
1.6 paf 128: }
129:
130: static void _offset(Request& r, const String&, Array *params) {
131: Pool& pool=r.pool();
1.18 paf 132: Table& table=static_cast<VTable *>(r.self)->table();
1.6 paf 133: if(params->size()) {
134: if(int size=table.size()) {
1.9 paf 135: int offset=
136: (int)r.process(*static_cast<Value *>(params->get(0))).get_double();
1.6 paf 137: table.set_current((table.get_current()+offset+size)%size);
138: }
139: } else {
140: Value& value=*new(pool) VInt(pool, table.get_current());
1.15 paf 141: r.write_no_lang(value);
1.6 paf 142: }
143: }
144:
1.7 paf 145: static void _menu(Request& r, const String& method_name, Array *params) {
146: Value& body_code=*static_cast<Value *>(params->get(0));
147: // forcing ^menu{this param type}
148: r.fail_if_junction_(false, body_code,
149: method_name, "body must be junction");
150:
151: Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
152:
1.18 paf 153: Table& table=static_cast<VTable *>(r.self)->table();
1.7 paf 154: bool need_delim=false;
1.22 paf 155: for(int row=0; row<table.size(); row++) {
156: table.set_current(row);
1.7 paf 157:
1.12 paf 158: Value& processed_body=r.process(body_code);
1.7 paf 159: if(delim_code) { // delimiter set?
160: const String *string=processed_body.get_string();
161: if(need_delim && string && string->size()) // need delim & iteration produced string?
162: r.write_pass_lang(r.process(*delim_code));
163: need_delim=true;
164: }
165: r.write_pass_lang(processed_body);
166: }
167: }
168:
1.8 paf 169: static void _empty(Request& r, const String&, Array *params) {
1.18 paf 170: Table& table=static_cast<VTable *>(r.self)->table();
1.8 paf 171: if(table.size()==0) {
172: Value& value=r.process(*static_cast<Value *>(params->get(0)));
173: r.write_pass_lang(value);
174: } else if(params->size()==2) {
175: Value& value=r.process(*static_cast<Value *>(params->get(1)));
176: r.write_pass_lang(value);
177: }
178: }
1.15 paf 179:
1.29 paf 180: struct Record_info {
181: Pool *pool;
182: Table *table;
183: Hash *hash;
184: };
185: static void store_column_item_to_hash(Array::Item *item, void *info) {
186: Record_info& ri=*static_cast<Record_info *>(info);
187: String& column_name=*static_cast<String *>(item);
188: const String *column_item=ri.table->item(column_name);
189: Value *value;
190: if(column_item)
191: value=new(*ri.pool) VString(*column_item);
192: else
193: value=new(*ri.pool) VUnknown(*ri.pool);
194: ri.hash->put(column_name, value);
195: }
196: static void _record(Request& r, const String&, Array *params) {
197: Table& table=static_cast<VTable *>(r.self)->table();
198: if(const Array *columns=table.columns()) {
199: Pool& pool=r.pool();
200: Value& value=*new(pool) VHash(pool);
201: Record_info record_info={&pool, &table, value.get_hash()};
202: columns->for_each(store_column_item_to_hash, &record_info);
203:
204: r.write_no_lang(value);
205: }
206: }
207:
1.15 paf 208: // initialize
1.8 paf 209:
1.14 paf 210: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.22 paf 211: // ^table.set{data}
212: // ^table.set[nameless]{data}
1.1 paf 213: vclass.add_native_method("set", _set, 1, 2);
1.2 paf 214:
1.10 paf 215: // ^table.load[file]
216: // ^table.load[nameless;file]
1.2 paf 217: vclass.add_native_method("load", _load, 1, 2);
1.22 paf 218:
219: // ^table.save[file]
220: // ^table.save[nameless;file]
221: vclass.add_native_method("save", _save, 1, 2);
1.6 paf 222:
223: // ^table.count[]
224: vclass.add_native_method("count", _count, 0, 0);
225:
226: // ^table.line[]
227: vclass.add_native_method("line", _line, 0, 0);
228:
1.10 paf 229: // ^table.offset[]
230: // ^table.offset[offset]
1.6 paf 231: vclass.add_native_method("offset", _offset, 0, 1);
1.7 paf 232:
1.10 paf 233: // ^table.menu{code}
234: // ^table.menu{code}[delim]
1.7 paf 235: vclass.add_native_method("menu", _menu, 1, 2);
1.8 paf 236:
1.10 paf 237: // ^table.empty{code-when-empty}
238: // ^table.empty{code-when-empty}{code-when-not}
1.8 paf 239: vclass.add_native_method("empty", _empty, 1, 2);
1.29 paf 240:
241: // ^table.record[]
242: vclass.add_native_method("record", _record, 0, 0);
1.8 paf 243:
1.1 paf 244: }
E-mail: