om_bool om_dict_put(om_dict_ptr dict, void *key, void *val) { om_uint64 hash = dict->hash_func(key); int index = hash % dict->bucket_count; om_list_ptr bucket = __om_dict_get_bucket_for_key(dict,key); void * value = val; void * key_ins = key; // if the bucket has not been initialized if( bucket==OM_NULL ) { bucket = dict->buckets[index] = om_list_new(); if( bucket==OM_NULL ) { return OM_FALSE; } bucket->release_func = __om_dict_release_entry_func; bucket->release_data = dict; } // if a copy function for incoming values has been assigned if( dict->copy_value_func!=OM_NULL ) { value = dict->copy_value_func(val); if( value == OM_NULL ) { return OM_FALSE; } } // if a key copy function has been assigned if( dict->copy_key_func!=OM_NULL ) { key_ins = dict->copy_key_func(val); if( key_ins == OM_NULL ) { __om_dict_free_key_value(dict,key,value); return OM_FALSE; } } // try to find an existing dictionary entry for the key om_dict_entry_ptr ent = __om_dict_get_entry(dict,key_ins); if( ent==OM_NULL ) { ent = __om_dict_new_entry(key_ins,value); if( ent==OM_NULL ) { return OM_FALSE; } if( ! om_list_append(bucket,ent) ) { return OM_FALSE; } } // an existing entry was found, so we'll just replace the value else { __om_dict_free_key_value(dict,ent->key,ent->value); ent->key=key_ins; ent->value=val; } return OM_TRUE; }
om_list_ptr om_string_explode(const char *str, const char sep) { om_list_ptr list = om_list_new(); if( list==OM_NULL ) return OM_NULL; char * p = OM_NULL; char * seg = str; char * start = seg; while(1) { if(*seg==sep||(*seg=='\0'&&start!=seg)) { char * p = om_string_substr(start,0,(seg-start)); if( p == OM_NULL ) { // all or nothing om_list_release(list); return OM_NULL; } if( ! om_list_append(list,p) ) { // all or nothing om_list_release(list); return OM_NULL; } // we may be at the end, so check if(*seg=='\0') break; seg++; // get beyond the '/' start = seg; // start tracking a new segment } // we may be at the end, so check if(*seg=='\0') break; seg++; } return list; }
om_list_ptr om_dict_get_keys(om_dict_ptr dict) { om_list_ptr lst = om_list_new(); if( lst == OM_NULL ) { return OM_NULL; } // set the release_func to null, // so that the caller doesn't inadvertantly // free their keys lst->release_func=OM_NULL; for( int i=0; i<dict->bucket_count; i++ ) { if( dict->buckets[i]!=OM_NULL ) { int cnt = om_list_count(dict->buckets[i]); for( int j=0; j<cnt; j++ ) { om_dict_entry_ptr ent = om_list_get(dict->buckets[i],j); om_list_append(lst,ent->key); } } } return lst; }
om_bool __om_unzip_append_error(om_unzip_archive_ptr archive, char * err) { return om_list_append(archive->error_log,err); }