void * om_dict_remove(om_dict_ptr dict, void *key) { void * toret = OM_NULL; om_uint64 hash = dict->hash_func(key); int bucket_index = hash % dict->bucket_count; om_list_ptr bucket = dict->buckets[bucket_index]; // om_list_ptr bucket = om_dict_get_bucket_for_key(dict,key); if( bucket==OM_NULL ) { return OM_NULL; } int end = om_list_count(bucket); for( int i=0; i<end; i++ ) { om_dict_entry_ptr ent = om_list_get(bucket,i); if( dict->hash_func(ent->key) == dict->hash_func(key) ) { toret = ent->value; om_list_remove(bucket,ent); break; } } // if it was the only value in the bucket, // then go ahead and free-up the list of the // bucket if(end==1) { om_list_release(dict->buckets[bucket_index]); dict->buckets[bucket_index] = OM_NULL; } return toret; }
om_bool om_dict_clear(om_dict_ptr dict) { for( int i=0; i < dict->bucket_count; i++ ) { if( dict->buckets[i]!=OM_NULL ) { om_list_release(dict->buckets[i]); dict->buckets[i]=OM_NULL; } } return OM_TRUE; }
om_dict_ptr om_dict_from_query_string(const char *queryString) { om_list_ptr keyValPairs = om_string_explode(queryString,'&'); om_dict_ptr ret = om_dict_new(5); ret->release_func=om_dict_release_default_func; int c = om_list_count(keyValPairs); for( int i=0; i<c; i++ ) { char * val = om_list_get(keyValPairs,i); om_list_ptr keyVal = om_string_explode( val, '=' ); if( om_list_count(keyVal)==2 ) { om_dict_put( ret, om_string_copy(om_list_get(keyVal,0)), om_string_copy(om_list_get(keyVal,1)) ); } else if( om_list_count(keyVal)==1 ) { om_dict_put( ret, om_string_copy(om_list_get(keyVal,0)), "" ); } om_list_release(keyVal); } om_list_release(keyValPairs); return ret; }
om_bool om_unzip_close_archive(om_unzip_archive_ptr archive) { om_bool ret = OM_TRUE; om_list_release(archive->error_log); om_free(archive->global_info_ptr); if( unzClose(archive->file) != UNZ_OK ) { ret=OM_FALSE; } om_free(archive); return ret; }
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_unzip_archive_ptr om_unzip_open_archive(const char *file_path) { int err; unz_global_info * gip = om_malloc(sizeof(unz_global_info)); if( gip==OM_NULL ) { return OM_NULL; } unzFile uf = unzOpen(file_path); err = unzGetGlobalInfo(uf,gip); if( err!=UNZ_OK ) { om_free(gip); om_error_set(OM_ERR_ZIP_GLOBALINFO,"Error getting global info for the zip archive."); return OM_NULL; } om_list_ptr error_log = om_list_new(); if( error_log==OM_NULL ) { om_free(gip); return OM_NULL; } om_unzip_archive_ptr arch = om_malloc(sizeof(om_unzip_archive)); if( arch==OM_NULL ) { om_list_release(error_log); om_free(gip); return OM_NULL; } arch->error_log = error_log; arch->file = uf; arch->global_info_ptr = gip; return arch; }