|
|
| version 1.122, 2001/10/09 09:17:43 | version 1.124, 2001/10/09 13:17:45 |
|---|---|
| Line 129 static void _load(Request& r, const Stri | Line 129 static void _load(Request& r, const Stri |
| static_cast<VTable *>(r.self)->set_table(table); | static_cast<VTable *>(r.self)->set_table(table); |
| } | } |
| static Table *fill_month_days(Request& r, | |
| const String& method_name, MethodParams *params, bool rus){ | |
| Pool& pool=r.pool(); | |
| Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/); | |
| int year=params->as_int(1, "year must be int", r); | |
| int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1; | |
| tm tmIn={0, 0, 0, 1, month, year-1900}; | |
| time_t t=mktime(&tmIn); | |
| if(t<0) | |
| PTHROW(0, 0, | |
| &method_name, | |
| "invalid date"); | |
| tm *tmOut=localtime(&t); | |
| int weekDay1=tmOut->tm_wday; | |
| if(rus) | |
| weekDay1=weekDay1?weekDay1-1:6; //sunday last | |
| int monthDays=getMonthDays(year, month); | |
| for(int _day=1-weekDay1; _day<=monthDays;) { | |
| Array& row=*new(pool) Array(pool, 7); | |
| for(int wday=0; wday<7; wday++, _day++) { | |
| String *cell=new(pool) String(pool); | |
| if(_day>=1 && _day<=monthDays) { | |
| char *buf=(char *)pool.malloc(2+1); | |
| cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), | |
| method_name.origin().file, method_name.origin().line); | |
| } | |
| row+=cell; | |
| } | |
| *result+=&row; | |
| } | |
| return result; | |
| } | |
| static Table *fill_week_days(Request& r, | |
| const String& method_name, MethodParams *params, bool rus){ | |
| Pool& pool=r.pool(); | |
| Array& columns=*new(pool) Array(pool, 4); | |
| columns+=new(pool) String(pool, "year"); | |
| columns+=new(pool) String(pool, "month"); | |
| columns+=new(pool) String(pool, "day"); | |
| columns+=new(pool) String(pool, "weekday"); | |
| Table *result=new(pool) Table(pool, &method_name, &columns); | |
| int year=params->as_int(1, "year must be int", r); | |
| int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1; | |
| int day=params->as_int(3, "day must be int", r); | |
| tm tmIn={0, 0, 18, day, month, year-1900}; | |
| time_t t=mktime(&tmIn); | |
| if(t<0) | |
| PTHROW(0, 0, | |
| &method_name, | |
| "invalid date"); | |
| tm *tmOut=localtime(&t); | |
| int baseWeekDay=tmOut->tm_wday; | |
| if(rus) | |
| baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last | |
| t-=baseWeekDay*SECS_PER_DAY; | |
| for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) { | |
| tm *tmOut=localtime(&t); | |
| Array& row=*new(pool) Array(pool, 4); | |
| #define WDFILL(size, value) { \ | |
| char *buf=(char *)pool.malloc(size+1); \ | |
| String *cell=new(pool) String(pool); \ | |
| cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \ | |
| method_name.origin().file, \ | |
| method_name.origin().line); \ | |
| row+=cell; \ | |
| } | |
| WDFILL(4, 1900+tmOut->tm_year); | |
| WDFILL(2, 1+tmOut->tm_mon); | |
| WDFILL(2, tmOut->tm_mday); | |
| WDFILL(2, tmOut->tm_wday); | |
| *result+=&row; | |
| } | |
| return result; | |
| } | |
| static void _calendar(Request& r, const String& method_name, MethodParams *params) { | |
| Pool& pool=r.pool(); | |
| const String& what=params->as_string(0, "format must be strig"); | |
| bool rus=false; | |
| if(what=="rus") | |
| rus=true; | |
| else if(what=="eng") | |
| rus=false; | |
| else | |
| PTHROW(0, 0, | |
| &what, | |
| "must be rus|eng"); | |
| Table *result=0; | |
| if(params->size()==1+2) | |
| result=fill_month_days(r, method_name, params, rus); | |
| else // 1+3 | |
| result=fill_week_days(r, method_name, params, rus); | |
| // replace any previous table value | |
| static_cast<VTable *>(r.self)->set_table(*result); | |
| } | |
| static void _save(Request& r, const String& method_name, MethodParams *params) { | static void _save(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& vtable_name=params->as_no_junction(params->size()-1, | Value& vtable_name=params->as_no_junction(params->size()-1, |
| Line 372 static void table_row_to_hash(Array::Ite | Line 258 static void table_row_to_hash(Array::Ite |
| } | } |
| static void _hash(Request& r, const String& method_name, MethodParams *params) { | static void _hash(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Table& table=static_cast<VTable *>(r.self)->table(); | Table& self_table=static_cast<VTable *>(r.self)->table(); |
| Value& result=*new(pool) VHash(pool); | Value& result=*new(pool) VHash(pool); |
| if(const Array *columns=table.columns()) | if(const Array *columns=self_table.columns()) |
| if(columns->size()>1) { | if(columns->size()>1) { |
| const String& key_field_name=params->as_no_junction(0, | const String& key_field_name=params->as_no_junction(0, |
| "key field name must not be code").as_string(); | "key field name must not be code").as_string(); |
| int key_field=table.column_name2index(key_field_name, true); | int key_field=self_table.column_name2index(key_field_name, true); |
| int value_fields_count=params->size()-1; | |
| bool value_fields_by_params=value_fields_count!=0; | Array value_fields(pool); |
| if(!value_fields_by_params) | if(params->size()>1) { |
| value_fields_count=columns->size()-1; // all columns except key | Value& value_fields_param=params->as_no_junction(1, "value field(s) must not be code"); |
| Array value_fields(pool, value_fields_count); | if(value_fields_param.is_string()) { |
| if(value_fields_by_params) { | value_fields+=self_table.column_name2index(value_fields_param.as_string(), true); |
| for(int i=1; i<params->size(); i++) { | } else if(Table *value_fields_table=value_fields_param.get_table()) { |
| const String& value_field_name=params->as_no_junction(i, | for(int i=0; i<value_fields_table->size(); i++) { |
| "value field name must not be code").as_string(); | const String& value_field_name= |
| value_fields+=table.column_name2index(value_field_name, true); | *static_cast<Array *>(value_fields_table->get(i))->get_string(0); |
| } | value_fields+=self_table.column_name2index(value_field_name, true); |
| } else { // by all columns except key | } |
| } else | |
| PTHROW(0, 0, | |
| &method_name, | |
| "value field(s) must be string or self_table" | |
| ); | |
| } else { // by all columns, including key | |
| for(int i=0; i<columns->size(); i++) | for(int i=0; i<columns->size(); i++) |
| if(i!=key_field) | value_fields+=i; |
| value_fields+=i; | |
| } | } |
| // integers: key_field & value_fields | // integers: key_field & value_fields |
| Row_info row_info={&table, key_field, &value_fields, result.get_hash()}; | Row_info row_info={&self_table, key_field, &value_fields, result.get_hash()}; |
| table.for_each(table_row_to_hash, &row_info); | self_table.for_each(table_row_to_hash, &row_info); |
| } | } |
| result.set_name(method_name); | result.set_name(method_name); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| Line 692 MTable::MTable(Pool& apool) : Methoded(a | Line 582 MTable::MTable(Pool& apool) : Methoded(a |
| // ^table:load[nameless;file] | // ^table:load[nameless;file] |
| add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); | add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); |
| // ^table:calendar[month|montheng;year;month] | |
| // ^table:calendar[week|weekeng;year;month;day] | |
| add_native_method("calendar", Method::CT_DYNAMIC, _calendar, 3, 4); | |
| // ^table.save[file] | // ^table.save[file] |
| // ^table.save[nameless;file] | // ^table.save[nameless;file] |
| add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2); | add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2); |
| Line 715 MTable::MTable(Pool& apool) : Methoded(a | Line 601 MTable::MTable(Pool& apool) : Methoded(a |
| add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2); | add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2); |
| // ^table:hash[key field name] | // ^table:hash[key field name] |
| // ^table:hash[key field name][value field name;...] | // ^table:hash[key field name][value field name(s) string/table] |
| add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000); | add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 2); |
| // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc] | // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc] |
| // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc] | // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc] |