--- parser3/src/classes/hash.C 2009/07/07 05:47:43 1.99 +++ parser3/src/classes/hash.C 2012/02/28 05:07:44 1.112 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HASH_C="$Date: 2009/07/07 05:47:43 $"; +static const char * const IDENT="$Id: hash.C,v 1.112 2012/02/28 05:07:44 moko Exp $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -22,22 +22,16 @@ static const char * const IDENT_HASH_C=" class MHash: public Methoded { public: // VStateless_class - Value* create_new_value(Pool&, HashStringValue*) { return new VHash(); } + Value* create_new_value(Pool&) { return new VHash(); } public: MHash(); -public: // Methoded - bool used_directly() { return true; } }; // global variable DECLARE_CLASS_VAR(hash, new MHash, 0); -// externs - -extern String cycle_data_name; - // methods #ifndef DOXYGEN @@ -74,9 +68,9 @@ public: empty(0) { } - bool add_column(SQL_Error& error, const char* str, size_t length) { + bool add_column(SQL_Error& error, const char* str, size_t ) { try { - columns+=new String(str, String::L_TAINTED, length); + columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */); return false; } catch(...) { error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column"); @@ -113,9 +107,9 @@ public: return false; } - bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) { + bool add_row_cell(SQL_Error& error, const char *ptr, size_t ) { try { - String& cell=*new String(ptr, String::L_TAINTED, length); + String& cell=*new String(ptr, String::L_TAINTED /* no length as 0x00 can be inside */); bool duplicate=false; if(one_bool_column) { @@ -197,7 +191,7 @@ VBool Hash_sql_event_handlers::only_one_ static void _create_or_add(Request& r, MethodParams& params) { if(params.count()) { - Value& vsrc=params.as_no_junction(0, "param must be hash"); + Value& vsrc=params.as_no_junction(0, PARAM_MUST_BE_HASH); if(HashStringValue* src=vsrc.get_hash()) { VHash& self=GET_SELF(r, VHash); HashStringValue* self_hash=&(self.hash()); @@ -205,23 +199,16 @@ static void _create_or_add(Request& r, M return; src->for_each(copy_all_overwrite_to, self_hash); - if(VHash* vhash_src=static_cast(vsrc.as(VHASH_TYPE, false))) - { + if(VHash* vhash_src=static_cast(vsrc.as(VHASH_TYPE))) if(Value* vdefault=vhash_src->get_default()) - { if(vdefault->is_defined()) - { self.set_default(vdefault); - } - } - } } } } static void _sub(Request& r, MethodParams& params) { - Value& vsrc=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* src=vsrc.get_hash()) { + if(HashStringValue* src=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash()) { HashStringValue* self=&(GET_SELF(r, VHash).hash()); if(src==self) { // same: clearing self->clear(); @@ -232,17 +219,16 @@ static void _sub(Request& r, MethodParam } static void copy_all_dontoverwrite_to( - HashStringValue::key_type key, - HashStringValue::value_type value, - HashStringValue* dest) { + HashStringValue::key_type key, + HashStringValue::value_type value, + HashStringValue* dest) { dest->put_dont_replace(key, value); } static void _union(Request& r, MethodParams& params) { // dest = copy of self Value& result=*new VHash(GET_SELF(r, VHash).hash()); // dest += b - Value& vsrc=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* src=vsrc.get_hash()) + if(HashStringValue* src=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash()) src->for_each(copy_all_dontoverwrite_to, result.get_hash()); // return result @@ -256,17 +242,16 @@ struct Copy_intersection_to_info { }; #endif static void copy_intersection_to( - HashStringValue::key_type key, - HashStringValue::value_type value, - Copy_intersection_to_info *info) { + HashStringValue::key_type key, + HashStringValue::value_type value, + Copy_intersection_to_info *info) { if(info->b->get(key)) info->dest->put_dont_replace(key, value); } static void _intersection(Request& r, MethodParams& params) { Value& result=*new VHash; // dest += b - Value& vb=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* b=vb.get_hash()) { + if(HashStringValue* b=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash()) { Copy_intersection_to_info info={b, result.get_hash()}; GET_SELF(r, VHash).hash().for_each(copy_intersection_to, &info); } @@ -276,17 +261,16 @@ static void _intersection(Request& r, Me } static bool intersects( - HashStringValue::key_type key, - HashStringValue::value_type /*value*/, - HashStringValue* b) { + HashStringValue::key_type key, + HashStringValue::value_type /*value*/, + HashStringValue* b) { return b->get(key)!=0; } static void _intersects(Request& r, MethodParams& params) { bool result=false; - Value& vb=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* b=vb.get_hash()) + if(HashStringValue* b=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash()) result=GET_SELF(r, VHash).hash().first_that(intersects, b)!=0; // return result @@ -312,40 +296,32 @@ static void _sql(Request& r, MethodParam ulong offset=0; bool distinct=false; Table2hash_value_type value_type=C_HASH; - if(params.count()>1) { - Value& voptions=params.as_no_junction(1, "options must be hash, not code"); - if(voptions.is_defined() && !voptions.is_string()) - if(HashStringValue* options=voptions.get_hash()) { - int valid_options=0; - if(Value* vbind=options->get(sql_bind_name)) { - valid_options++; - bind=vbind->get_hash(); - } - if(Value* vlimit=options->get(sql_limit_name)) { - valid_options++; - limit=(ulong)r.process_to_value(*vlimit).as_double(); - } - if(Value* voffset=options->get(sql_offset_name)) { - valid_options++; - offset=(ulong)r.process_to_value(*voffset).as_double(); - } - if(Value* vdistinct=options->get(sql_distinct_name)) { - valid_options++; - distinct=r.process_to_value(*vdistinct).as_bool(); - } - if(Value* vvalue_type=options->get(sql_value_type_name)) { - valid_options++; - value_type=get_value_type(r.process_to_value(*vvalue_type)); - } - if(valid_options!=options->count()) - throw Exception(PARSER_RUNTIME, - 0, - "called with invalid option"); - } else - throw Exception(PARSER_RUNTIME, - 0, - "options must be hash"); - } + if(params.count()>1) + if(HashStringValue* options=params.as_hash(1)) { + int valid_options=0; + if(Value* vbind=options->get(sql_bind_name)) { + valid_options++; + bind=vbind->get_hash(); + } + if(Value* vlimit=options->get(sql_limit_name)) { + valid_options++; + limit=(ulong)r.process_to_value(*vlimit).as_double(); + } + if(Value* voffset=options->get(sql_offset_name)) { + valid_options++; + offset=(ulong)r.process_to_value(*voffset).as_double(); + } + if(Value* vdistinct=options->get(sql_distinct_name)) { + valid_options++; + distinct=r.process_to_value(*vdistinct).as_bool(); + } + if(Value* vvalue_type=options->get(sql_value_type_name)) { + valid_options++; + value_type=get_value_type(r.process_to_value(*vvalue_type)); + } + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } SQL_Driver::Placeholder* placeholders=0; uint placeholders_count=0; @@ -376,10 +352,10 @@ static void _sql(Request& r, MethodParam } static void keys_collector( - HashStringValue::key_type key, - HashStringValue::value_type, - Table *table) { - Table::element_type row(new ArrayString); + HashStringValue::key_type key, + HashStringValue::value_type, + Table *table) { + Table::element_type row(new ArrayString(1)); *row+=new String(key, String::L_TAINTED); *table+=row; } @@ -390,7 +366,7 @@ static void _keys(Request& r, MethodPara else keys_column_name=new String("key"); - Table::columns_type columns(new ArrayString); + Table::columns_type columns(new ArrayString(1)); *columns+=keys_column_name; Table* table=new Table(columns); @@ -432,10 +408,10 @@ static bool one_foreach_cycle( Value& var_context=*info->var_context; if(info->key_var_name){ VString* vkey=new VString(*new String(akey, String::L_TAINTED)); - var_context.put_element(var_context, *info->key_var_name, vkey, false); + var_context.put_element(*info->key_var_name, vkey, false); } if(info->value_var_name) - var_context.put_element(var_context, *info->value_var_name, avalue, false); + var_context.put_element(*info->value_var_name, avalue, false); if(info->delim_maybe_code){ // delimiter set StringOrValue sv_processed=info->r->process(*info->body_code); @@ -457,8 +433,7 @@ static bool one_foreach_cycle( } } static void _foreach(Request& r, MethodParams& params) { - Temp_hash_value - cycle_data_setter(r.classes_conf, cycle_data_name, /*any not null flag*/&r); + InCycle temp(r); const String& key_var_name=params.as_string(0, "key-var name must be string"); const String& value_var_name=params.as_string(1, "value-var name must be string"); @@ -479,6 +454,42 @@ static void _foreach(Request& r, MethodP hash.first_that(one_foreach_cycle, &info); } +static void _at(Request& r, MethodParams& params) { + HashStringValue& hash=GET_SELF(r, VHash).hash(); + size_t count=hash.count(); + + int pos=0; + + Value& vwhence=*params.get(0); + if(vwhence.is_string()){ + const String& swhence=*vwhence.get_string(); + if(swhence == "last") + pos=count-1; + else if(swhence != "first") + throw Exception(PARSER_RUNTIME, + &swhence, + "whence must be 'first', 'last' or expression"); + } else { + pos=r.process_to_value(vwhence).as_int(); + if(pos < 0) + pos+=count; + } + + if(count && pos >= 0 && (size_t)pos < count){ + if(pos == 0) + r.write_assign_lang(*hash.first_value()); + else if((size_t)pos == count-1) + r.write_assign_lang(*hash.last_value()); + else + for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) + if(!pos){ + r.write_assign_lang(*i.value()); + break; + } + } + +} + // constructor MHash::MHash(): Methoded("hash") @@ -515,4 +526,8 @@ MHash::MHash(): Methoded("hash") // ^hash.foreach[key;value]{code}[delim] add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1); + + // ^hash._at[first|last] + // ^hash._at([-]offset) + add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 1); }