int bansiteop(struct in_addr *from) { struct bansite *b; ght_iterator_t iterator; ght_hash_table_t *tmptable; void *key; static time_t last = 0; time_t now_t = time(NULL); FILE *fp; if (!bansitetable) bansitetable = ght_create(200); if (!bansitetable) return -1; if (from) { if ((b = ght_get(bansitetable, sizeof (*from), from))) { b->t = now_t; return 0; } b = malloc(sizeof (*b)); if (!b) return -1; b->t = time(NULL); b->from = *from; if (ght_insert(bansitetable, b, sizeof (*from), from) < 0) { free(b); return -1; } } if (!from && now_t - last < 100) return 0; fp = fopen(MY_BBS_HOME "/bbstmpfs/dynamic/bansite", "w"); if (!fp) return -1; last = now_t; tmptable = bansitetable; bansitetable = ght_create(200); if (!bansitetable) { bansitetable = tmptable; return -1; } for (b = ght_first(tmptable, &iterator, &key); b; b = ght_next(tmptable, &iterator, &key)) { if (b->t < now_t - 6000) { free(b); continue; } fprintf(fp, "%s\n", inet_ntoa(b->from)); if (ght_insert(bansitetable, b, sizeof (b->from), &b->from) < 0) { free(b); } } ght_finalize(tmptable); fclose(fp); return 0; }
struct cgi_applet * get_cgi_applet(char *needcgi) { static ght_hash_table_t *p_table = NULL; struct cgi_applet *a; if (p_table == NULL) { int i; a = applets; p_table = ght_create(250, NULL, 0); while (a->main != NULL) { a->count = 0; a->utime = 0; a->stime = 0; for (i = 0; a->name[i] != NULL; i++) ght_insert(p_table, (void *) a, strlen(a->name[i]), (void *) a->name[i]); a++; } } if (p_table == NULL) return NULL; return ght_get(p_table, strlen(needcgi), needcgi); }
void TextResource_setParameter(TextResource tr, const char* key, const char* value) { bstring val = ght_remove(tr->parameters,strlen(key),key); if (val) bdestroy(val); val = bfromcstr(value); ght_insert(tr->parameters,val,strlen(key),key); }
static int subscribe_channel(sub_client *c, sds channel) { TrieData data; size_t len; AlphaChar *chan_alpha; len = sdslen(channel); chan_alpha = (AlphaChar *) malloc(sizeof(AlphaChar) * len + 1); conv_to_alpha(server.dflt_to_alpha_conv, channel, chan_alpha, len+1); /* channel not exists in trie, create */ if (!trie_retrieve(server.sub_trie, chan_alpha, &data)) { if (!trie_store(server.sub_trie, chan_alpha, TRIE_DATA_DFLT)) { srv_log(LOG_ERROR, "Failed to insert key %s into sub trie", channel); return SUBCLI_ERR; } /* create hashtable mapping from subscribe-channel to client id set */ hset *hs = hset_create(SUB_SET_LEN); hset_insert(hs, CLIENT_ID_LEN, c->id, c); if (ght_insert(server.subscibe_table, hs, len, channel) == -1) { hset_release(hs); srv_log(LOG_ERROR, "Failed to insert key[%s]->set into subscribe hashtable", channel); return SUBCLI_ERR; } } return SUBCLI_OK; }
hashtable *sub_commands_init() { hashtable *commands; int num_cmds = sizeof(sub_command_table) / sizeof(sub_command); commands = ght_create(3 * num_cmds); int i; for (i = 0; i < num_cmds; i++) { sub_command *cmd = sub_command_table + i; ght_insert(commands, cmd, strlen(cmd->name), cmd->name); } return commands; }
int filter_passerr(int n, char *arg[]) { struct event *e; int *count; if (n < 4 || strcmp(arg[1], "system") || strcmp(arg[2], "passerr")) return 0; if (passerrtable == NULL) passerrtable = ght_create(10000); if (!passerrtable) goto ERROR; e = malloc(sizeof (struct event)); if (!e) goto ERROR; e->t = time(NULL); inet_aton(arg[3], &e->from); if ((count = ght_get(passerrtable, sizeof (e->from), &e->from))) { (*count)++; if (*count > site_limit(&e->from)) bansiteop(&e->from); } else { count = malloc(sizeof (int)); if (!count) goto ERROR1; *count = 1; if (ght_insert(passerrtable, count, sizeof (e->from), &e->from) < 0) goto ERROR2; } //add to list if (eventhead == NULL) { eventhead = e; eventtail = e; } else { eventtail->next = e; eventtail = e; } e->next = NULL; return 1; ERROR2: free(count); ERROR1: free(e); ERROR: return 1; }
/* init and insert a user into hash table. */ static int img_user(const char *userid) { UserNode *punode; int retv = -1; punode = calloc(1, sizeof(UserNode)); if(punode == NULL) return -1; strncpy(punode->userid, userid, sizeof(punode->userid)); punode->userid[sizeof(punode->userid)-1] = '\0'; punode->from = init_queue(); punode->to = init_queue(); if(punode->from == NULL || punode->to == NULL) goto END; retv = ght_insert(fidx, punode, strlen(punode->userid), punode->userid); END: if(retv == -1) free_afriend(punode); return retv; }
struct boardmem * getbcache(char *board) { int i, j; static int num = 0; char upperstr[STRLEN]; static ght_hash_table_t *p_table = NULL; static time_t uptime = 0; struct boardmem *ptr; if (board[0] == 0) return NULL; if (p_table && (num != shm_bcache->number || shm_bcache->uptime > uptime)) { ght_finalize(p_table); p_table = NULL; } if (p_table == NULL) { num = 0; p_table = ght_create(MAXBOARD); for (i = 0; i < MAXBOARD && i < shm_bcache->number; i++) { num++; if (!shm_bcache->bcache[i].header.filename[0]) continue; strsncpy(upperstr, shm_bcache->bcache[i].header.filename, sizeof (upperstr)); for (j = 0; upperstr[j]; j++) upperstr[j] = toupper(upperstr[j]); ght_insert(p_table, &shm_bcache->bcache[i], j, upperstr); } uptime = now_t; } strsncpy(upperstr, board, sizeof (upperstr)); for (j = 0; upperstr[j]; j++) upperstr[j] = toupper(upperstr[j]); ptr = ght_get(p_table, j, upperstr); if (!ptr) return numboard(board); return ptr; }
void accept_sub_handler(evutil_socket_t fd, short event, void *args) { int cfd = accept_tcp_handler(fd, event, args); if (cfd == -1) { return; } sub_client *c = sub_cli_create(cfd, ++server.sub_inc_counter); net_tcp_set_nonblock(NULL, cfd); net_enable_tcp_no_delay(NULL, cfd); struct event *sub_ev = event_new(server.evloop, cfd, EV_READ|EV_PERSIST, sub_ev_handler, c); if (sub_ev == NULL) { free(c); close(cfd); return; } event_add(sub_ev, NULL); c->ev = sub_ev; ght_insert(server.subcli_table, c, CLIENT_ID_LEN, c->id); }
static void insert_entry(ght_hash_table_t *p_table, int num, char *p_key) { int *p_data; if ( !(p_data = malloc(sizeof(int))) ) { fprintf(stderr, "Malloc\n"); exit(1); } *p_data = num; /* Insert the entry into the hash table */ if (ght_insert(p_table, p_data, sizeof(char)*strlen(p_key), p_key) < 0) { /* Just exit if we cannot insert. */ fprintf(stderr, "Could not insert into the hash table\n"); exit(1); } }
/* Rehash the hash table (i.e. change its size and reinsert all * items). This operation is slow and should not be used frequently. */ void ght_rehash(ght_hash_table_t *p_ht, unsigned int i_size) { ght_hash_table_t *p_tmp; ght_iterator_t iterator; const void *p_key; void *p; unsigned int i; DEBUG_ASSERT(p_ht); /* Recreate the hash table with the new size */ p_tmp = ght_create(i_size); DEBUG_ASSERT(p_tmp); /* Set the flags for the new hash table */ ght_set_hash(p_tmp, p_ht->fn_hash); ght_set_alloc(p_tmp, p_ht->fn_alloc, p_ht->fn_free); ght_set_heuristics(p_tmp, GHT_HEURISTICS_NONE); ght_set_rehash(p_tmp, FALSE); /* Walk through all elements in the table and insert them into the temporary one. */ for (p = ght_first(p_ht, &iterator, &p_key); p; p = ght_next(p_ht, &iterator, &p_key)) { DEBUG_ASSERT(iterator.p_entry); /* Insert the entry into the new table */ if (ght_insert(p_tmp, iterator.p_entry->p_data, iterator.p_entry->key.i_size, iterator.p_entry->key.p_key) < 0) { LOG_ERROR("Out of memory error or entry already in hash table when rehashing (internal error)\n"); } } /* Remove the old table... */ for (i=0; i<p_ht->i_size; i++) { if (p_ht->pp_entries[i]) { /* Delete the entries in the bucket */ free_entry_chain (p_ht, p_ht->pp_entries[i]); p_ht->pp_entries[i] = NULL; } } DefaultFreeFunction (p_ht->pp_entries); DefaultFreeFunction (p_ht->p_nr); /* ... and replace it with the new */ p_ht->i_size = p_tmp->i_size; p_ht->i_size_mask = p_tmp->i_size_mask; p_ht->i_items = p_tmp->i_items; p_ht->pp_entries = p_tmp->pp_entries; p_ht->p_nr = p_tmp->p_nr; p_ht->p_oldest = p_tmp->p_oldest; p_ht->p_newest = p_tmp->p_newest; /* Clean up */ p_tmp->pp_entries = NULL; p_tmp->p_nr = NULL; DefaultFreeFunction (p_tmp); }
int insert(void* data, const T & key) { check_init(); return ght_insert(m_ht, data, sizeof(key), &key); }
/******************************************************* * WriteMmdModelMaterials関数 * * PMDとPMXのテクスチャ画像データを書き出す * * 引数 * * model : テクスチャ画像データを書き出すモデル * * out_data_size : 書き出したデータのバイト数格納先 * * 返り値 * * 書き出したデータ * *******************************************************/ uint8* WriteMmdModelMaterials( MODEL_INTERFACE* model, size_t* out_data_size ) { // 画像データをまとめて書き出すバッファ MEMORY_STREAM *stream = CreateMemoryStream(1024 * 1024); // 作成結果のストリーム MEMORY_STREAM result_stream = {0}; // 作成結果データ uint8 *result; // テクスチャ画像配列へのポインタ MATERIAL_INTERFACE **materials; // 画像データの格納先 uint8 *image_data = NULL; // 画像データ格納バッファのサイズ size_t image_data_buffer_size = 0; // UTF-8での画像ファイルへのパス char utf8_path[8192]; // OSでの画像ファイルへのパス char *system_path; // 画像ファイルの読み込み、ファイルサイズ、存在確認用 FILE *fp; // 画像ファイルの数 int num_images = 0; // テクスチャ画像配列のサイズ int num_materials; // 32ビット書き出し用 uint32 data32; // 画像データの開始位置 long image_start = sizeof(data32); // 画像データの合計サイズ long total_image_size = 0; int counter; // 書き出す画像ファイルの名前・サイズ配列 MATERIAL_ARCHIVE_DATA *names; // 重複書き出し防止用 ght_hash_table_t *name_table; unsigned int name_length; uint32 diff; // for文用のカウンタ int i; // モデルに設定されているテクスチャ画像を取得 materials = (MATERIAL_INTERFACE**)model->get_materials(model, &num_materials); name_table = ght_create(num_materials+1); ght_set_hash(name_table, (ght_fn_hash_t)GetStringHash); // それぞれの画像ファイルのファイル名サイズを取得 for(i=0; i<num_materials; i++) { MATERIAL_INTERFACE *material = materials[i]; if(material->main_texture != NULL) { name_length = (unsigned int)strlen(material->main_texture); if(ght_get(name_table, name_length, material->main_texture) == NULL) { (void)ght_insert(name_table, (void*)1, name_length, material->main_texture); image_start += (long)name_length+1; image_start += sizeof(uint32) + sizeof(uint32) + sizeof(uint32); num_images++; } } if(material->sphere_texture != NULL) { name_length = (unsigned int)strlen(material->sphere_texture); if(ght_get(name_table, name_length, material->sphere_texture) == NULL) { (void)ght_insert(name_table, (void*)1, name_length, material->sphere_texture); image_start += (long)name_length+1; image_start += sizeof(uint32) + sizeof(uint32) + sizeof(uint32); num_images++; } } if(material->toon_texture != NULL) { // トゥーンテクスチャの場合はモデルのディレクトリに画像があるか確認 (void)sprintf(utf8_path, "%s/%s", model->model_path, material->toon_texture); system_path = LocaleFromUTF8(utf8_path); if((fp = fopen(system_path, "rb")) != NULL) { // 画像有 name_length = (unsigned int)strlen(material->toon_texture); if(ght_get(name_table, name_length, material->toon_texture) == NULL) { (void)ght_insert(name_table, (void*)1, name_length, material->toon_texture); image_start += (long)name_length+1; image_start += sizeof(uint32) + sizeof(uint32) + sizeof(uint32); num_images++; (void)fclose(fp); } } MEM_FREE_FUNC(system_path); } } ght_finalize(name_table); // 画像ファイルを一つのデータにまとめる name_table = ght_create(num_materials+1); ght_set_hash(name_table, (ght_fn_hash_t)GetStringHash); names = (MATERIAL_ARCHIVE_DATA*)MEM_ALLOC_FUNC(sizeof(*names)*num_images); counter = 0; for(i=0; i<num_materials; i++) { MATERIAL_INTERFACE *material = materials[i]; if(material->main_texture != NULL) { name_length = (unsigned int)strlen(material->main_texture); if(ght_get(name_table, name_length, material->main_texture) == NULL) { (void)ght_insert(name_table, (void*)1, name_length, material->main_texture); names[counter].data_start = image_start + total_image_size; (void)sprintf(utf8_path, "%s/%s", model->model_path, material->main_texture); system_path = LocaleFromUTF8(utf8_path); if((fp = fopen(system_path, "rb")) != NULL) { (void)fseek(fp, 0, SEEK_END); names[counter].name = material->main_texture; names[counter].data_size = ftell(fp); names[counter].data_start = total_image_size + image_start; total_image_size += names[counter].data_size; rewind(fp); if(image_data_buffer_size < names[counter].data_size) { image_data = (uint8*)MEM_REALLOC_FUNC(image_data, names[counter].data_size); image_data_buffer_size = names[counter].data_size; } (void)fread(image_data, 1, names[counter].data_size, fp); (void)MemWrite(image_data, 1, names[counter].data_size, stream); counter++; (void)fclose(fp); } else { char sp_path[8192] = {0}; char *extention = sp_path; char *p = extention; (void)strcpy(sp_path, system_path); while(*p != '\0') { if(*p == '.') { extention = p; } p++; } extention[1] = 's'; extention[2] = 'p'; extention[3] = 'a'; extention[4] = '\0'; if((fp = fopen(sp_path, "rb")) != NULL) { (void)fseek(fp, 0, SEEK_END); names[counter].name = material->main_texture; names[counter].data_size = ftell(fp); names[counter].data_start = total_image_size + image_start; total_image_size += names[counter].data_size; rewind(fp); if(image_data_buffer_size < names[counter].data_size) { image_data = (uint8*)MEM_REALLOC_FUNC(image_data, names[counter].data_size); image_data_buffer_size = names[counter].data_size; } (void)fread(image_data, 1, names[counter].data_size, fp); (void)MemWrite(image_data, 1, names[counter].data_size, stream); counter++; (void)fclose(fp); } else { extention[3] = 'h'; if((fp = fopen(sp_path, "rb")) != NULL) { (void)fseek(fp, 0, SEEK_END); names[counter].name = material->main_texture; names[counter].data_size = ftell(fp); names[counter].data_start = total_image_size + image_start; total_image_size += names[counter].data_size; rewind(fp); if(image_data_buffer_size < names[counter].data_size) { image_data = (uint8*)MEM_REALLOC_FUNC(image_data, names[counter].data_size); image_data_buffer_size = names[counter].data_size; } (void)fread(image_data, 1, names[counter].data_size, fp); (void)MemWrite(image_data, 1, names[counter].data_size, stream); counter++; (void)fclose(fp); } } } MEM_FREE_FUNC(system_path); } } if(material->sphere_texture != NULL) { name_length = (unsigned int)strlen(material->sphere_texture); if(ght_get(name_table, name_length, material->sphere_texture) == NULL) { (void)ght_insert(name_table, (void*)1, name_length, material->sphere_texture); names[counter].data_start = image_start + total_image_size; (void)sprintf(utf8_path, "%s/%s", model->model_path, material->sphere_texture); system_path = LocaleFromUTF8(utf8_path); if((fp = fopen(system_path, "rb")) != NULL) { (void)fseek(fp, 0, SEEK_END); names[counter].name = material->sphere_texture; names[counter].data_size = ftell(fp); names[counter].data_start = total_image_size + image_start; total_image_size += names[counter].data_size; rewind(fp); if(image_data_buffer_size < names[counter].data_size) { image_data = (uint8*)MEM_REALLOC_FUNC(image_data, names[counter].data_size); image_data_buffer_size = names[counter].data_size; } (void)fread(image_data, 1, names[counter].data_size, fp); (void)MemWrite(image_data, 1, names[counter].data_size, stream); counter++; (void)fclose(fp); } MEM_FREE_FUNC(system_path); } } if(material->toon_texture != NULL) { // トゥーンテクスチャの場合はモデルのディレクトリに画像があるか確認 (void)sprintf(utf8_path, "%s/%s", model->model_path, material->toon_texture); system_path = LocaleFromUTF8(utf8_path); name_length = (unsigned int)strlen(material->toon_texture); if(ght_get(name_table, name_length, material->toon_texture) == NULL) { if((fp = fopen(system_path, "rb")) != NULL) { // 画像有 (void)ght_insert(name_table, (void*)1, name_length, material->toon_texture); (void)fseek(fp, 0, SEEK_END); names[counter].name = material->toon_texture; names[counter].data_size = ftell(fp); names[counter].data_start = total_image_size + image_start; total_image_size += names[counter].data_size; rewind(fp); if(image_data_buffer_size < names[counter].data_size) { image_data = (uint8*)MEM_REALLOC_FUNC(image_data, names[counter].data_size); image_data_buffer_size = names[counter].data_size; } (void)fread(image_data, 1, names[counter].data_size, fp); (void)MemWrite(image_data, 1, names[counter].data_size, stream); counter++; (void)fclose(fp); } } MEM_FREE_FUNC(system_path); } } num_images = counter; ght_finalize(name_table); result = (uint8*)MEM_ALLOC_FUNC(image_start + total_image_size + sizeof(uint32)); result_stream.buff_ptr = result; result_stream.data_size = image_start + total_image_size; result_stream.block_size = 1; data32 = num_images; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); for(i=0; i<num_images; i++) { data32 = (uint32)strlen(names[i].name)+1; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); (void)MemWrite(names[i].name, 1, data32, &result_stream); data32 = names[i].data_start; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); data32 = names[i].data_size; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); } if((diff = (uint32)(image_start - result_stream.data_point)) > 0) { (void)MemSeek(&result_stream, sizeof(data32), SEEK_SET); for(i=0; i<num_images; i++) { data32 = (uint32)strlen(names[i].name)+1; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); (void)MemWrite(names[i].name, 1, data32, &result_stream); data32 = names[i].data_start - diff; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); data32 = names[i].data_start - diff; (void)MemWrite(&data32, sizeof(data32), 1, &result_stream); } image_start -= diff; } (void)MemWrite(stream->buff_ptr, 1, stream->data_point, &result_stream); if(out_data_size != NULL) { *out_data_size = image_start + total_image_size; } MEM_FREE_FUNC(image_data); MEM_FREE_FUNC(names); MEM_FREE_FUNC(materials); (void)DeleteMemoryStream(stream); return result; }
int logfile_refresh(logfile *log) { int rowcount = 0; char *line = NULL; logerror *err; logerror *err_in_table; logerror *newlist = NULL; logerror *newlist_filtered = NULL; unsigned int filtered_count = 0; while((line = linereader_getline(log->file)) != NULL) { err = parse_error_line(log->logformat, line); if (err == NULL) continue; update_top_errortype(&log->worstNewLine, err); //Get a possible duplicate entry in the hashtable err_in_table = ght_get(log->errortypes, err->keylength, err->key); if (err_in_table == NULL) { //Insert a completely new error ght_insert(log->errortypes, err, err->keylength, err->key); newlist = errorlist_insert(newlist, err); update_top_errortype(&log->worstNewType, err); } else { //Merge the two duplicate errors logerror_merge(err_in_table, err); if (err_in_table->is_new == 0) { log->errors = errorlist_remove_error(log->errors, err_in_table); newlist = errorlist_insert(newlist, err_in_table); } err_in_table->is_new = 1; } ++rowcount; } if (newlist != NULL) { //First we reset the is_new bits so that next refreshes work correctly err = newlist; while (err != NULL) { err->is_new = 0; err = err->next; } if (log->filter) { filtered_count = loglist_filter(&newlist, &newlist_filtered, log->filter, log->filter_data, FILTER_REMOVE_FAILED); newlist_filtered = errorlist_sort(newlist_filtered, log->sorting); } newlist = errorlist_sort(newlist, log->sorting); //printf("Sorted, final merge"); log->errors = errorlist_merge(log->errors, newlist, log->sorting); log->filtered_errors = errorlist_merge(log->filtered_errors, newlist_filtered, log->sorting); log->filtered_entries += filtered_count; } return rowcount; }
/* * This is an example program that reads words from a text-file (a * book or something like that) and uses those as keys in a hash table * (the actual data stored is unimportant). The words are case * sensitive. * * After this, the program opens another text-file and tries to match * the words in that with the words stored in the table. * * The meaning with this program is to test the speed of the table and * to provide an example of its use. */ int main(int argc, char *argv[]) { FILE *p_file; char *p_dict_name; char *p_text_name; ght_hash_table_t *p_table; ght_iterator_t iterator; struct stat stat_struct; int i_found; int i_cnt; char *p_buf; char *p_tok; const void *p_key; void *p_e; /* Create a new hash table */ if ( !(p_table = ght_create(1000)) ) { return 1; } ght_set_rehash(p_table, TRUE); /* Parse the arguments */ if (argc < 3) { printf("Usage: dict_example [-m|-t|-b] dictfile textfile\n\n" "Reads words from `dictfile' and looks up these words in `textfile'.\n" "Options:\n" " -m Use move-to-front heuristics\n" " -t Use transpose heuristics\n" " -b Use bounded buckets (use the hash table as a cache)\n" ); return 0; } else if (argc > 3) { if(strcmp(argv[1], "-m") == 0) ght_set_heuristics(p_table, GHT_HEURISTICS_MOVE_TO_FRONT); else if(strcmp(argv[1], "-t") == 0) ght_set_heuristics(p_table, GHT_HEURISTICS_TRANSPOSE); else if (strcmp(argv[1], "-b") == 0) { /* Rehashing makes no sense in "cache" mode */ ght_set_rehash(p_table, FALSE); ght_set_bounded_buckets(p_table, 3, bucket_free_callback); } p_dict_name = argv[2]; p_text_name = argv[3]; } else { /* 2 arguments */ p_dict_name = argv[1]; p_text_name = argv[2]; } /* Open the dictionary file (first check its size) */ if (stat(p_dict_name, &stat_struct) < 0) { perror("stat"); return 1; } if (!(p_buf = (char*)malloc(stat_struct.st_size))) { perror("malloc"); return 1; } /* Open the dictionary file and read that into the buffer. */ if (! (p_file = fopen(p_dict_name, "r")) ) { perror("fopen"); return 1; } fread(p_buf, sizeof(char), stat_struct.st_size, p_file); fclose(p_file); /* For each word in the dictionary file, insert it into the hash table. */ p_tok = strtok(p_buf, DELIMS); i_cnt = 0; i_found = 0; while (p_tok) { int *p_data; if ( !(p_data = (int*)malloc(sizeof(int))) ) { perror("malloc"); return 1; } *p_data = i_cnt++; /* Insert the word into the table */ if (ght_insert(p_table, p_data, strlen(p_tok), p_tok) < 0) free(p_data); /* Could not insert the item (already exists), free it. */ else i_found++; p_tok = strtok(NULL, DELIMS); } printf("Done reading %d unique words from the wordlist.\n" "Total number of words is %d.\n\n", i_found, i_cnt); free(p_buf); /* Check the size of the text file. */ if (stat(p_text_name, &stat_struct) < 0) { perror("stat"); return 1; } if (!(p_buf = (char*)malloc(stat_struct.st_size))) { perror("malloc"); return 1; } /* Open the text file and read that into the buffer. */ if (! (p_file = fopen(p_text_name, "r")) ) { perror("fopen"); return 1; } fread(p_buf, sizeof(char), stat_struct.st_size, p_file); fclose(p_file); /* For each word in the text file, check if it exists in the hash table. */ p_tok = strtok(p_buf, DELIMS); i_cnt = 0; i_found = 0; while (p_tok) { printf(" searching %s ;", p_tok); if (ght_get(p_table, strlen(p_tok), p_tok)) { i_found++; } i_cnt++; p_tok = strtok(NULL, DELIMS); } free(p_buf); printf("Found %d words out of %d words\n", i_found, i_cnt); /* Free the entry data in the table */ for(p_e = ght_first(p_table, &iterator, &p_key); p_e; p_e = ght_next(p_table, &iterator, &p_key)) { free(p_e); } /* Free the table */ ght_finalize(p_table); return 0; }
/*! * \em Insert the \em Data Pointer \p data at \p key of size \p key_data. * \return 0 upon success, -1 if data indexed by \p key already * existed in \p p_ht. */ int insert(void* data, unsigned int key_size, const void *key_data) { check_init(); return ght_insert(m_ht, data, key_size, key_data); }
int main(int argc,char *argv[]) { int rc; char *k, *v, *line, buf[BUFSIZ]; ght_hash_table_t *p_table=NULL; /* create hash table of size 256 */ p_table=ght_create(256); if (p_table == NULL) { print_it("Error: Could not create hash table\n"); return(1); } print_menu(); for (;;) { print_prompt(); line=fgets(buf,sizeof(buf)-1,stdin); if (line == NULL) break; switch(*line) { case 'i': { k=read_data("Enter key: "); v=read_data("Enter value: "); if (k && v) { /* insert to hash table */ rc=ght_insert(p_table, v, strlen(k), k); if (rc == 0) { print_it("Inserted: Key=%s, Value=%s\n", k,v); free(k); } else { print_it("Error: ght_insert() failed\n"); (void) free((char *) k); (void) free((char *) v); } } else { if (k) (void) free((char *) k); if (v) (void) free((char *) v); } break; } case 'r': /* replace */ { k=read_data("Enter key: "); v=read_data("Enter new value: "); if (k && v) { char * old_val; /* Replace a key in the hash table */ old_val = (char*)ght_replace(p_table, v, strlen(k), k); if (old_val != NULL) { print_it("Replaced: Key=%s, Old Value=%s, Value=%s\n", k,old_val, v); free(old_val); free(k); } else { print_it("Error: ght_replace() failed\n"); (void) free((char *) k); (void) free((char *) v); } } else { if (k) (void) free((char *) k); if (v) (void) free((char *) v); } break; } case 'h': { print_menu(); break; } case 'n': /* number of elements */ { print_it("Number elements in hash table: %d\n", ght_size(p_table)); break; } case 's': /* size of hash table */ { print_it("Hash table size: %d\n", ght_table_size(p_table)); break; } case 't': /* dump */ { const void *pk, *pv; ght_iterator_t iterator; for (pv=(char *) ght_first(p_table,&iterator, &pk); pv; pv=(char *) ght_next(p_table,&iterator, &pk)) { print_it("%s => %s\n",(char *) pk,(char *) pv); } break; } case 'd': { k=read_data("Enter key to delete: "); if (k) { v=ght_remove(p_table, strlen(k), k); if (v) { print_it("Removed %s => %s",k,v); (void) free((char *) v); } else { print_it("Error: could not find data for key %s\n", k); } (void) free((char *) k); } break; } case 'l': { k=read_data("Enter key: "); if (k) { v=(char *) ght_get(p_table,strlen(k),k); if (v) { print_it("Found: %s => %s\n",k,v); } else { print_it("No data found for key: %s\n",k); } (void) free((char *) k); } break; } case 'q': { if (p_table) { const void *pv, *pk; ght_iterator_t iterator; for (pv=(char *) ght_first(p_table,&iterator, &pk); pv; pv=(char *) ght_next(p_table,&iterator, &pk)) { (void) free((char *) pv); } ght_finalize(p_table); } return(0); break; } default: { print_it("Unknown option\n"); break; } } } return(0); }
int insert(const T & key) { check_init(); return ght_insert(m_ht, nullptr, sizeof(key), &key); }
size_t setAll(const char* filePath, const char* filenameAdditon, double** VecSig, double** VecStr, double** VecDsig, double** VecDstr, double** InvF,double** InvFT, double** G, double** P11q, double** P22, double** P21, double** T3, double** Fe1q, double** Fe2, double** Fs, double** ppu, double** lBase, double** XG, size_t** nrOfOrder, ght_hash_table_t** icoShellSections, ght_hash_table_t** elsets, ght_hash_table_t** issSizes, ght_hash_table_t** elsetSizes, ght_hash_table_t** matMap, struct CLElemQMHS4* cLElemQMHS4) { char path[512]; size_t initialsize = 64; size_t height = 1, width = 1, num = 1, offset = 0; size_t elements = 0; size_t elemsInObject = 0; double* data = NULL; sprintf(path, "%s%s", filePath, filenameAdditon); elemsInObject = setChangingFiles(path, VecSig, VecStr, VecDsig, VecDstr, InvF, InvFT, G, P11q, P22, P21, Fe1q, Fe2, Fs, &data); // speacial handling for G. It is not read and size is not consitent with the size in the input file *G = (double*)calloc(elemsInObject * 24 * 14, sizeof(double)); // load elements = readMatrix1D(&data, &height, &width, &initialsize, &offset, path, "T3.sav"); // copy data alocNcpy(T3, data, elements, height * width); // TODO fix handling of T3 cLElemQMHS4->T3Ptr = *T3; // load u // TODO works only if Manfred gives you the data for this iteration elemsInObject = readScalar(&data, &initialsize, path, "ppu.sav"); // copy data alocNcpy(ppu, data, elemsInObject, 1); cLElemQMHS4->u = *ppu; // iteration independent stuff initialsize = elements; // load nrOfOrder elements = readScalarUint(nrOfOrder, &initialsize, filePath, "nrOfOrder.sav"); cLElemQMHS4->nrOfOrder = *nrOfOrder; // load LBase elements = readBase(&data, &num, &height, &width, &initialsize, filePath, "lBase.out"); // copy data alocNcpy(lBase, data, elements, num*height*width); cLElemQMHS4->LBase = *lBase; // load XG elemsInObject = readMatrix1D(&data, &width, &height, &initialsize, &offset, filePath, "XG.sav"); // height and width interchanged! // copy data alocNcpy(XG, data, elemsInObject, height*width); cLElemQMHS4->XG = *XG; // set fields cLElemQMHS4->sigPtr = *VecSig; cLElemQMHS4->strPtr = *VecStr; cLElemQMHS4->Dsig = *VecDsig; cLElemQMHS4->Dstr = *VecDstr; cLElemQMHS4->invF = *InvF; cLElemQMHS4->invFT = *InvFT; cLElemQMHS4->G = *G; cLElemQMHS4->P11q = *P11q; cLElemQMHS4->P22 = *P22; cLElemQMHS4->P21 = *P21; cLElemQMHS4->fe1q = *Fe1q; cLElemQMHS4->fe2 = *Fe2; cLElemQMHS4->fs = *Fs; // TODO put one element in an IcoShellSection struct Material* mat = (struct Material*)malloc(sizeof(struct Material)); mat->materialName = "MAT"; setMaterialElastic(mat, 6.825e7, 0.30); *matMap = ght_create(1); ght_insert((void*)*matMap, mat, sizeof(char*), mat->materialName); cLElemQMHS4->mat = *matMap; const char* elemName = "fake"; struct icoSSData* isd = (struct icoSSData*)malloc(sizeof(struct icoSSData)); isd->ug = -0.02; isd->og = 0.02; isd->intPoints = 5; isd->matName = mat->materialName; (*icoShellSections) = ght_create(1); ght_insert((void*)*icoShellSections, isd, sizeof(char*), elemName); //(*icoShellSections)[elemName] = new icoSSData[1]; //(*icoShellSections)[elemName][0] = isd; // std::map<const char*, size_t> issSizes; *issSizes = ght_create(1); size_t* iss = (size_t*)malloc(sizeof(size_t)); *iss = 1u; ght_insert((void*)*issSizes, iss, sizeof(char*), elemName); // issSizes[elemName] = 1u; cLElemQMHS4->icoShellSections = *icoShellSections; cLElemQMHS4->issSize = *issSizes; // putting elemsInObject elements in elsets int** idxs = (int**)malloc(sizeof(int*)); *idxs = (int*)malloc(elemsInObject * sizeof(int)); // (*elsets)[elemName] = (int*)malloc(elemsInObject * sizeof(int)); for(size_t i = 0; i < elemsInObject; ++i) (*idxs)[i] = (i+1); (*elsets) = ght_create(1); ght_insert((void*)*elsets, idxs, sizeof(char*), elemName); size_t* n = (size_t*)malloc(sizeof(size_t)); *n = elemsInObject; *elsetSizes = ght_create(1); ght_insert((void*)*elsetSizes, n, sizeof(char*), elemName); cLElemQMHS4->elsets = *elsets; cLElemQMHS4->elsetSize = *elsetSizes; //set intersect to 0 cLElemQMHS4->interSec = (int*)calloc(elements*4, sizeof(int)); // set elements to 0 cLElemQMHS4->elements = setElementsZero(elements); //set elast to 1 cLElemQMHS4->elast = 1; free(data); return elements; }