int dict_remove(struct dict *d, char *k, size_t sz) { struct bucket *b = NULL; unsigned long h = d->key_hash(k, sz); if(!(b = ht_get(d->ht, h, k, sz))) { if(d->ht_old && !(b = ht_get(d->ht_old, h, k, sz))) { return -1; } } /* re-attach elements */ if(b->prev) { b->prev->next = b->next; } if(b->next) { b->next->prev = b->prev; } /* free duplicated key */ d->key_free(b->k); /* remove bucket */ bucket_free(b); d->count--; return 0; }
int test_add_to_same_key(){ hashtable_t *ht = ht_create( 65536, free_jambo ); ht_set( ht, "key1", "blorg" ); ht_set( ht, "key1", "bloff" ); ht_set( ht, "key1", "gladd" ); ht_set( ht, "key1", "grutt" ); ht_set( ht, "key1", "twerky" ); ht_set( ht, "key1", "lennart" ); ht_set( ht, "key2", "sverker" ); ht_set( ht, "key3", "Rogge" ); ht_set( ht, "key4", "Swutt" ); check( strcmp( ht_get(ht, "key1"), "lennart") == 0 ); check( strcmp( ht_get(ht, "key2"), "sverker") == 0); check( strcmp( ht_get(ht, "key3"), "Rogge") == 0); check( strcmp( ht_get(ht, "key4"), "Swutt") == 0); check(ht_size(ht) == 4); ht_destroy(ht); return 0; }
int test_perfomance(){ hashtable_t *ht = ht_create( 65536, destroy_gv_t ); char key[10]; char s1[10]; char s2[10]; for(int i=0;i < 50000; i++){ sprintf(key,"key-%d", i); sprintf(s1,"s1-%d", i); sprintf(s2,"s2-%d", i); ht_set( ht, key, create_gv_t(i, s1, s2) ); check( strcmp( ((gv_t *) ht_get(ht, key))->s1, s1) == 0 ); check( strcmp( ((gv_t *) ht_get(ht, key))->s2, s2) == 0 ); check(((gv_t *) ht_get(ht, key))->i1 == i ); } ht_destroy(ht); return 0; }
char *webspy_nodeview(httpd *server) { char *tmp, *nodelist; int i = 0, max, offset = 1; struct ONLINE clone, *tmpn; online_t *who; strlist_t *nlist = 0; time_t now; struct macro_list *ml = 0; strlist_iterator_t *si; who = (online_t*) ht_get(get_context(), ONLINE_ATTR); max = who_online_max(who); now = time(0); for (i = 0; i < max; i++) { tmpn = who_getnode(who, i); if (!tmpn) continue; memcpy(&clone, tmpn, sizeof(struct ONLINE)); if (clone.procid == 0) continue; tmp = webspy_nodeview_item(&clone, offset++, now); nlist = str_add_last(nlist, tmp); free(tmp); } // concat list max = 0; si = str_iterator(nlist); while (str_iterator_hasnext(si)) max += strlen(str_iterator_next(si)) + 1; free(si); tmp = malloc(max + 1); *tmp = 0; si = str_iterator(nlist); while (str_iterator_hasnext(si)) strcat(tmp, str_iterator_next(si)); free(si); ml = ml_addstring(ml, NODEVIEW_NODEVIEWITEMS, tmp); free(tmp); tmp = ml_replacebuf(ml, ht_get(get_config(), PROPERTY_HTML_NODEVIEW)); ml_free(ml); return tmp; }
int test_add_custom_struct_as_value(){ hashtable_t *ht = ht_create( 65536, destroy_gv_t ); ht_set( ht, "key4", create_gv_t(10,"jamse", "fjamse") ); check( strcmp( ((gv_t *) ht_get(ht, "key4"))->s1, "jamse") == 0 ); check( strcmp( ((gv_t *) ht_get(ht, "key4"))->s2, "fjamse") == 0 ); check(((gv_t *) ht_get(ht, "key4"))->i1 == 10 ); ht_destroy(ht); return 0; }
int checkSegment(FILE *f, char ending) { struct ht_Table *status = ht_create(NUMBER_OF_SYMBOLS); struct ht_Table *opposites = ht_create(NUMBER_OF_OPPOSITES); initializeStatus(status); initializeOpposites(opposites); char CLOSERS[] = {')', '>', ']', '}'}; char OPENERS[] = {'(', '<', '[', '{'}; const int LISTS_LENGTH = 4; char last = 'a'; char curr; while((curr = fgetc(f)) != EOF) { printf("%c", curr); if(curr == '#') skipLineComment(f); else if(curr == '*' && last == '/') skipBlockComment(f); else if(curr == '/' && last == '/') skipLineComment(f); else if(curr == '\'' || curr == '"') { if(skipString(f, curr) != 0) return 1; } else if(search(OPENERS, LISTS_LENGTH, curr) != -1) { (*ht_get(status, curr))++; } // Struct pointer references screw up the normal algorithm. Don't tell Tim Peters or Linus Torvalds else if(curr == '>' && last == '-') {} else if(search(CLOSERS, LISTS_LENGTH, curr) != -1) { char correspondingOpener = (char) (*ht_get(opposites, curr)); int *num = ht_get(status, correspondingOpener); if(*num == 0) { printf("There is an unmatched %c!\n", curr); return 1; } else { (*num)--; } } last = curr; } return 0; }
void* dict_get(struct dict *d, char *k, size_t sz) { struct bucket *b; unsigned long h = d->key_hash(k, sz); if((b = ht_get(d->ht, h, k, sz))) { return b->v; } else if(d->ht_old && (b = ht_get(d->ht_old, h, k, sz))) { return b->v; } return NULL; }
int main(int argc, char **argv) { hashtable_t *hashtable = ht_new(65536); ht_set(hashtable, "key1", "inky"); ht_set(hashtable, "key2", "pinky"); ht_set(hashtable, "key3", "blinky"); ht_set(hashtable, "key4", "floyd"); printf("%s\n", ht_get(hashtable, "key1")); printf("%s\n", ht_get(hashtable, "key2")); printf("%s\n", ht_get(hashtable, "key3")); printf("%s\n", ht_get(hashtable, "key4")); return 0; }
/* * Returns a given property for a group. */ char *group_get_property(char *grp, char *prop) { hashtable_t *cfg = get_config(); char buf[300], *tmp; sprintf(buf, "group.%s.%s", grp, prop); tmp = ht_get(cfg, buf); if (!tmp) { // try to get default property. sprintf(buf, "group.DEFAULT.%s", prop); tmp = ht_get(cfg, buf); } return tmp; }
/* * Starts a new mailbox for the given user. */ MailboxResult *mailbox_start_1_svc(MailboxParams *argp, struct svc_req *rqstp) { static MailboxResult result = MailboxResultSuccess; // Create users hashtable if not exists. if (g_users_ht == NULL) { if (!(g_users_ht = ht_new(USERS_TABLE_INIT_SIZE, USERS_TABLE_EXPAN_SIZE, USERS_TABLE_LOAD_FACTOR))) { result = MailboxResultServerFailure; return &result; } } // Check if users hashtable contains given user name: if (ht_get(g_users_ht, argp->user, NULL)) { result = MailboxResultUserExists; return &result; } // Create user's mailbox. Stk *mailbox = stk_new(MAILBOX_INIT_SIZE); // Register the user's mailbox under their name. if (!ht_put_pointer(g_users_ht, argp->user, mailbox, NULL, NULL)) { result = MailboxResultServerFailure; return &result; } result = MailboxResultSuccess; return &result; }
bool ht_set(hashtable *ht, const char *key, uint32_t key_size, const char *value, uint32_t value_size) { if (sizeof(uint32_t) + key_size > ht->max_key_size || sizeof(uint32_t) + value_size > ht->max_value_size) { return false; } char *flag_base = ht_flag_base(ht); char *bucket_base = ht_bucket_base(ht); ht_str *bucket_key = NULL, *bucket_value = NULL; //if it exists: just find and modify it's value bucket_value = ht_get(ht, key, key_size); if (bucket_value) { fill_ht_str(bucket_value, value, value_size); return true; } //else: find an available bucket, which can be both 'empty' or 'removed' size_t i = ht_position(ht, key, key_size, true); if (ht->capacity * max_load_factor < ht->size) { return false; } ht->size += 1; flag_base[i] = used; char *bucket = bucket_base + i * ht->bucket_size; bucket_key = (ht_str*)bucket; bucket_value = (ht_str*)(bucket + ht->max_key_size); fill_ht_str(bucket_key, key, key_size); fill_ht_str(bucket_value, value, value_size); return true; }
int pre_move_catalog(char *src, char *dest) { char *tmp, movebuf[1024]; int rc; // try rename. rc = rename(src, dest); if (rc == 0) return 0; tmp = ht_get(get_config(), PROPERTY_MOVE_EXTERNAL); if (!tmp) return -1; printf(", external.."); strcpy(movebuf, tmp); pre_replace(movebuf, "%S", src); pre_replace(movebuf, "%D", dest); // try external move rc = system(movebuf); // return -1 if command did not return '0' = success. if (rc != 0) return -1; return 0; }
void reverse_dirlog_add(struct subdir_list *tsub, char *dest, char *rel, pwdfile *pass, chowninfo_t *chown) { char buf[300], *tmp; hashtable_t *cfg = get_config(); int uid, gid, addsub; tmp = ht_get(cfg, PROPERTY_ADDSUB); if (tmp) addsub = atoi(tmp); else addsub = 0; if (tsub) reverse_dirlog_add(tsub->next, dest, rel, pass, chown); else return; if (!addsub && (strlen(tsub->dir) > 0)) return; if (strlen(tsub->dir) > 0) sprintf(buf, "%s/%s", dest, tsub->dir); else strcpy(buf, dest); uid = (chown->uid == -1) ? pass->uid : chown->uid; gid = (chown->gid == -1) ? pass->gid : chown->gid; if (!gl_dirlog_add(buf, uid, gid, tsub->files, tsub->bytes)) printf(" * Error adding to dirlog: %s/%s (%dF %.1fMb)\n", rel, tsub->dir, tsub->files, (float)tsub->bytes/(1024*1024)); }
/* * Returns a strlist_t with the groups of an user. */ strlist_t * user_find_groups(char *u) { linefilereader_t lfr; char buf[1024], *tmp, *spc2; strlist_t *groups = 0; hashtable_t *cfg = get_config(); tmp = ht_get(cfg, PROPERTY_GL_USERDIR); if (!tmp) quit(" * Could not find '%s' property in config", PROPERTY_GL_USERDIR); sprintf(buf, "%s/%s", tmp, u); if (lfr_open(&lfr, buf) < 0) quit(" * Could not open your userfile %s", buf); while (lfr_getline(&lfr, buf, 1024) > -1) { if (!strncasecmp(buf, "GROUP ", 6) || !strncasecmp(buf, "PRIVATE ", 8)) { tmp = strchr(buf, ' ') + 1; // fix for glftpd2.x which has 'GROUP group num' spc2 = strchr(tmp, ' '); if (spc2) *spc2 = 0; groups = str_add(groups, tmp); } } lfr_close(&lfr); return groups; }
DataRecord* bc_get(Bitcask *bc, const char* key) { Item *item = ht_get(bc->tree, key); if (NULL == item) return NULL; if (item->ver < 0){ free(item); return NULL; } int bucket = item->pos & 0xff; uint32_t pos = item->pos & 0xffffff00; if (bucket > bc->curr) { fprintf(stderr, "BUG: invalid bucket %d > %d\n", bucket, bc->curr); ht_remove(bc->tree, key); free(item); return NULL; } DataRecord* r = NULL; if (bucket == bc->curr) { pthread_mutex_lock(&bc->buffer_lock); if (bucket == bc->curr && pos >= bc->wbuf_start_pos){ int p = pos - bc->wbuf_start_pos; r = decode_record(bc->write_buffer + p, bc->wbuf_curr_pos - p, true); } pthread_mutex_unlock(&bc->buffer_lock); if (r != NULL){ free(item); return r; } } char fname[20], data[255]; const char * path = mgr_base(bc->mgr); sprintf(fname, DATA_FILE, bucket); sprintf(data, "%s/%s", path, fname); int fd = open(data, O_RDONLY); if (-1 == fd){ goto GET_END; } r = fast_read_record(fd, pos, true); if (NULL == r){ fprintf(stderr, "Bug: get %s failed in %s %d %d\n", key, path, bucket, pos); }else{ // check key if (strcmp(key, r->key) != 0){ fprintf(stderr, "Bug: record %s is not expected %s\n", r->key, key); free_record(r); r = NULL; } } GET_END: if (NULL == r) ht_remove(bc->tree, key); if (fd != -1) close(fd); free(item); return r; }
// implemented from hammerprotect.h int hp_add_connection(hashtable_t *connections, char *ip, int timeout_seconds) { _connection_info_t *ci; _connection_list_t *cl; ci = (_connection_info_t*) ht_get(connections, ip); // create new node. if (ci == 0) { ci = malloc(sizeof(_connection_info_t)); strcpy(ci->ip, ip); ci->connects = 0; ht_put_obj(connections, ip, ci); } // create new connect attempt. cl = malloc(sizeof(_connection_list_t)); cl->time = time(0); // add to list. cl->next = ci->connects; ci->connects = cl; // call expire function to clean list. _hp_clean_expired(connections, cl->time - timeout_seconds); }
/** * Get or create the policy in the table * * @param[in] ifname * Interface name * * @param[in] af * Address family * * @return the entry in the table */ static policy_table_entry_t * get_or_create_policy(char * ifname, uint8_t af) { hash_key_t * k; policy_table_entry_t * v; // Create key k = (hash_key_t *)malloc(sizeof(hash_key_t)); INSIST(k != NULL); // Copy interface name and address family. strcpy(k->ifname, ifname); k->af = af; if((v = ht_get(if_table, k)) == NULL) { v = (policy_table_entry_t *)malloc(sizeof(policy_table_entry_t)); INSIST(v != NULL); bzero(v, sizeof(policy_table_entry_t)); strcpy(v->ifname, ifname); v->af = af; ht_insert(if_table, k, v); } else { free(k); } return v; }
void ht_merge(SortedListPtr list, const char* filepath, HashTable *hashtable, SortedListPtr wordlist) { FileCountPair *fcp; WordCountPair *wcp; SortedListIteratorPtr iterator; SortedListIteratorPtr mainListIterator; char *word; WordListPair *wlp; iterator = SLCreateIterator(wordlist); while ((word = SLNextItem(iterator)) != NULL) { mainListIterator = SLCreateIterator(list); wcp = ht_get(hashtable, word); fcp = malloc(sizeof(FileCountPair)); fcp->filepath = filepath; fcp->count = wcp->count; while ((wlp = SLNextItem(mainListIterator)) != NULL) { if (strcmp(wlp->word, word) == 0) { break; } } if (wlp == NULL) { //the word is not already in the main list SortedListPtr newlist = SLCreate(&compareFileCounts); SLInsert(newlist, fcp); wlp = malloc(sizeof(WordListPair)); wlp->word = word; wlp->list = newlist; SLInsert(list, wlp); } else { SLInsert(wlp->list, fcp); } SLDestroyIterator(mainListIterator); } SLDestroyIterator(iterator); }
int main(int argc, char *argv[]){ char op[3]; int v; int c; hashtable_t *hashtable = ht_create(64); ht_set(hashtable, (char*)&"mov", 0 ,2); ht_set(hashtable, (char*)&"lod", 1, 2); ht_set(hashtable, (char*)&"add", 2, 2); while (1){ scanf("%s", &op); v = ht_get(hashtable, (char*)&op); c = ht_get_count(hashtable, (char*)&op); printf("code: %d, count: %d\n", v, c); } //FILE *program; printf("%Opening [%s]\n", argv[1]); //program = fopen(argv[1],"w+"); //fclose(program); return 0; }
/* * Lists up to 20 of the user's messages in their mailbox. */ MailboxMessageListResponse *mailbox_list_all_messages_1_svc(MailboxParams *argp, struct svc_req *rqstp) { static MailboxMessageListResponse result; DSValue mailbox; // Initialize return values. result.result = MailboxResultSuccess; for (int i = 0; i < MAX_EMAIL; i++) { result.messages[i] = "\0"; } // Check that users hashtable exists and user has registered on server. if (g_users_ht == NULL || !ht_get(g_users_ht, argp->user, &mailbox)) { result.result = MailboxResultUserNotExists; return &result; } Stk *mailbox_stk = (Stk*)mailbox.pointerVal; // Get up to 20 emails. for (int i = 0, j = 0; i < stk_depth(mailbox_stk) && j < MAX_EMAIL; i++) { DSValue wrapper; if (stk_get(mailbox_stk, &wrapper, i) && wrapper.pointerVal != NULL) { // Copy message into response. result.messages[j++] = wrapper.pointerVal; } } result.result = MailboxResultSuccess; return &result; }
void shiki_set(struct shiki *lru, struct slice *sk, struct slice *sv) { size_t size; struct level_node *n; if (lru->buffer == 0) return; size = (sk->len + sv->len); n = (struct level_node*)ht_get(lru->ht, sk->data); if (n == NULL) { lru->level_old.used_size += size; lru->level_old.count++; n = calloc(1, sizeof(struct level_node)); n->sk.data = malloc(sk->len); n->sk.len = sk->len; memset(n->sk.data, 0, sk->len); memcpy(n->sk.data, sk->data, sk->len); n->sv.data = malloc(sv->len); n->sv.len = sv->len; memset(n->sv.data, 0, sv->len); memcpy(n->sv.data, sv->data, sv->len); n->size = size; n->hits = 1; n->pre = NULL; n->nxt = NULL; ht_set(lru->ht, n->sk.data, n); } _shiki_set_node(lru, n); }
uint8_t buscar_lista_elemento(lexical * lexer, uint8_t size) { uint8_t i=0; uint8_t token = 0; uint8_t validar = 0; uint8_t * indice = NULL; uint8_t data_type = 0; for(i=0;i<size;i++) { if(lexer->token == VARIABLE) { token = ht_get(lexer->valor); if(token == LIST) { validar = is_corchetes(lexer); if(validar == 0) { indice = get_list_indice(lexer); if(indice == NULL) return 0; data_type = get_data_type(lexer->valor, indice); if(data_type == 0) return 0; if(!asignar_nuevo_token(lexer,data_type)) return 0; free(indice); } } } lexer = lexer->next; } return 1; }
/*---------------------------------------------------------------------------- * static int * mon_type_for(char *p_unit, int p_tag) * look up monitor type * * Return Value -- * returns the monitor type to use for display * * Design -- * Gets unit from hash table, do array look up * defaults to how ever the enables are set for "default" * Side effects -- * none *---------------------------------------------------------------------------- */ static int mon_type_for(const char *p_unit, int p_tag) { UnitInfo *l_ui=(UnitInfo*)ht_get(ms.unit_tbl, p_unit); if(0==l_ui) l_ui=(UnitInfo*)ht_get(ms.unit_tbl, "default"); if(0==l_ui) { printf("ERROR(%s):Unit hash not set up right!\n", modulename); tf_dofinish(); } return l_ui->enable[p_tag] ? tag2mon_type(p_tag) : NO_MON_TYPE; }
/* * Returns a property for a section. */ char * section_get_property(char *sec, char *prop) { hashtable_t *cfg = get_config(); char buf[300]; sprintf(buf, "section.%s.%s", sec, prop); return ht_get(cfg, buf); }
static int compile(char *rx) { int r=0,p=0,d_r; d_r=add_r(rx); if((r=ht_get(&ht_r,i_r))==-1) { if(rx_compact&&i_p>=P_AVG_SIZE*LIM_P) {rx_clear(); d_r=add_r(rx);} ht_put(&ht_r,r=i_r); i_r+=d_r; bind(r); p=expression(); if(sym!=SYM_END) error(RX_ER_BADCH); r2p[i_2][0]=r; r2p[i_2][1]=p; ht_put(&ht_2,i_2++); if(i_2==len_2) r2p=(int(*)[2])m_stretch(r2p,len_2=2*i_2,i_2,sizeof(int[2])); } else { r2p[i_2][0]=r; p=r2p[ht_get(&ht_2,i_2)][1]; } return p; }
int ch_dispmon(char *l_unit_name, int l_tag, int l_value) { int i; if (l_tag < 1 || l_tag > 49) { printf("ERROR(ch_dispmon): tag should be between 1 and 49\n"); } else if (l_value < 0 || l_value > 1) { printf("ERROR(ch_dispmon): Use 0 to turn off the tag display or " "1 to turn it on (%0d)\n", l_value); } else if (0==strcmp(l_unit_name, "all")) { UnitInfo **l_ul; UnitInfo **l_uit; if (l_value == 0) { ms.default_unit.enable[l_tag] = l_value ; } else { for (i=1;i<=49;i++) { ms.default_unit.enable[i] = (i>=l_tag) ? l_value : ms.default_unit.enable[i]; } } if(!ms.unit_tbl) { init_unit_tbl(); cDispmon("disp", MON_ALWAYS, "All units added to monitor list"); } l_ul=(UnitInfo **)ht_sorted_list(ms.unit_tbl, UnitInfo_cmp); l_uit=l_ul; if (l_value == 0) { (*l_uit)->enable[l_tag] = l_value ; } else { for(l_uit=l_ul; 0!=*l_uit; l_uit++) { for (i=1;i<=49;i++) { (*l_uit)->enable[i]= (i>=l_tag) ? l_value : ms.default_unit.enable[i]; } } cDispmon("disp", MON_ALWAYS, "All Units, tags >= %d value changed to %d", l_tag, l_value); } free(l_ul); } else { UnitInfo *l_ui; if(!ms.unit_tbl) init_unit_tbl(); l_ui=(UnitInfo*)ht_get(ms.unit_tbl,l_unit_name); if(0==l_ui) { l_ui=UnitInfo_new(l_unit_name); ht_put(ms.unit_tbl,l_unit_name,l_ui); } if (l_value == 0) { l_ui->enable[l_tag] = l_value ; } else { for (i=1;i<=49;i++) { l_ui->enable[i]= (i>=l_tag) ? l_value : 0 ; } } cDispmon("disp", MON_NORMAL, "Unit \"%s\" tag >= %d value changed to %d", l_unit_name, l_tag, l_value); } return 0; }
__do d_lookup_path (char *path, __do pool, uint8_t flags) { if (path[0] == 0x2F && path[1] == 0x0) { return &di_base.nd_pool.pool; } mda path_b = { 0 }; md_init_le (&path_b, 256); int nd = split_string_l_le (path, 0x2F, &path_b, (size_t) path_b.count); if (nd < 1) { return NULL; } mutex_lock (&di_base.nd_pool.mutex); p_md_obj ptr = path_b.first; int depth = 0; while (ptr) { char *p = (char*) ptr->ptr; pool = (__do) ht_get((hashtable_t*)pool->d, (unsigned char*) p, strlen(p) + 1); if ( NULL == pool) { goto exit; } if (NULL == ptr->next) { md_g_free_l (&path_b); pthread_mutex_unlock (&di_base.nd_pool.mutex); return pool; } depth++; ptr = ptr->next; } exit: ; pthread_mutex_unlock (&di_base.nd_pool.mutex); md_g_free_l (&path_b); return NULL; }
int main( int argc, char **argv ) { int size = 4; struct hash_cell **hashtable = ht_create(size); ht_set( hashtable, "key1", "inky" ); ht_set( hashtable, "*", "pinky" ); ht_set( hashtable, "key3", "blinky" ); ht_set( hashtable, "key4", "floyd" ); printf( "%s\n", ht_get( hashtable, "key1" ) ); printf( "%s\n", ht_get( hashtable, "*" ) ); printf( "%s\n", ht_get( hashtable, "key3" ) ); printf( "%s\n", ht_get( hashtable, "key4" ) ); return 0; }
static void vfs_vdir_attach_node(VFSNode *vdir, const char *name, VFSNode *node) { VFSNode *oldnode = ht_get(VDIR_TABLE(vdir), name, NULL); if(oldnode) { vfs_decref(oldnode); } ht_set(VDIR_TABLE(vdir), name, node); }
static int accept_p(void) { int j; if((j=ht_get(&ht_p,i_p))==-1) { ht_put(&ht_p,j=i_p); i_p+=p_size[P_TYP(i_p)]; if(i_p+P_SIZE>len_p) pattern=(int*)m_stretch(pattern,len_p=2*(i_p+P_SIZE),i_p,sizeof(int)); } return j; }