void SVSNickChange(struct userNode* user, const char *new_nick) { char *old_nick; unsigned int nn; /* don't do anything if there's no change */ old_nick = user->nick; if (!strncmp(new_nick, old_nick, NICKLEN)) return; /* remove old entry from clients dictionary */ dict_remove(clients, old_nick); #if !defined(WITH_PROTOCOL_P10) /* Remove from uplink's clients dict */ dict_remove(user->uplink->users, old_nick); #endif /* and reinsert */ user->nick = strdup(new_nick); dict_insert(clients, user->nick, user); #if !defined(WITH_PROTOCOL_P10) dict_insert(user->uplink->users, user->nick, user); #endif /* Make callbacks for nick changes. Do this with new nick in * place because that is slightly more useful. */ for (nn=0; (nn<ncf2_used) && !user->dead; nn++) ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]); user->timestamp = now; free(old_nick); }
int init_tokens(void) { g_tokens = dict_create(); if (!g_tokens) return (-1); dict_insert(g_tokens, "~", "HOME"); dict_insert(g_tokens, "-", "OLDPWD"); dict_insert(g_tokens, "~+", "PWD"); dict_insert(g_tokens, "~-", "OLDPWD"); return (0); }
// // Stolen from fixPolishesIID // void addToDict(dict_t *d, char *n) { dnode_t *node = 0L; char *dcpy = 0L; if (n == 0L) return; seqCache *F = new seqCache(n); seqInCore *S = F->getSequenceInCore(); while (S) { node = (dnode_t *)palloc(sizeof(dnode_t)); dcpy = (char *)palloc(sizeof(char) * S->headerLength() + 1); strcpy(dcpy, S->header()); dnode_init(node, (void *)(unsigned long)S->getIID()); dict_insert(d, node, dcpy); delete S; S = F->getSequenceInCore(); } delete F; }
void pdf_dict_insert_obj(dict *d, char *k, pdf_obj* o) { pdf_obj key = pdf_key_to_obj(k); dict_insert(d, (char*)key.value.k, (void*)pdf_obj_full_copy(o)); }
void pdf_dict_insert_ref(dict *d, char *k, int n, int g) { pdf_obj *val; pdf_obj key = pdf_key_to_obj(k); val = pdf_ref_to_obj_new(n, g); dict_insert(d, (char*)key.value.k, (void*)val); }
void cmd_register(struct command *cmd, const char *parent_name) { struct command *parent = parent_name ? cmd_find(parent_name, command_list) : NULL; assert(!parent_name || parent); if(parent && !parent->subcommands) parent->subcommands = dict_create(); dict_insert(parent ? parent->subcommands : command_list, (char *)cmd->name, cmd); }
void pdf_dict_insert_string(dict *d, char *k, char *s, int n) { pdf_obj *val; pdf_obj key = pdf_key_to_obj(k); val = pdf_string_new(s, n); dict_insert(d, (char*)key.value.k, (void*)val); }
void pdf_dict_insert_int(dict *d, char *k, int v) { pdf_obj *val; pdf_obj key = pdf_key_to_obj(k); val = pdf_int_to_obj(v); dict_insert(d, (char*)key.value.k, (void*)val); }
void Dict__set(caStack* stack) { caValue* dict = circa_output(stack, 0); copy(circa_input(stack, 0), dict); const char* key = circa_string_input(stack, 1); caValue* value = circa_input(stack, 2); copy(value, dict_insert(dict, key)); }
void ATTR_EXPORT test_dict(void) { VERBOSE(SYSTEM, "--- testing dict\n"); struct dict_t * dict = NULL; for (int i = 0; i < 100; i++) { dict_insert(&dict, 0x80048000 + i * 123, i); } destroy_dict(&dict); }
/************************************************************************* * dict_union: merges contents of 2 dictionaries into * a third * * dict1 - pointer to dictionary 1 * dict2 - pointer to dictionary 2 * * Returns: dictionary pointer * NULL - failed to merge * non-NULL - pointer to new dictionary * * Notes: entries of the same string have their counts * added and their flags ORed together. *************************************************************************/ DICTIONARY *dict_union( const DICTIONARY *dict1 , const DICTIONARY *dict2 ) { DICTIONARY *dict = NULL; STRING_ENTRY *se, *se2; long initial_string_count, initial_hash_chains, max_chain_length; long i, string_index; /*********** ** Initialize the new dictionary. ***********/ if ( dict1==NULL || dict2==NULL ) goto err_exit; if ((dict=(DICTIONARY *)malloc(sizeof(DICTIONARY))) == NULL) goto err_exit; initial_string_count = dict1->string_max; initial_hash_chains = dict1->table_size; max_chain_length = dict1->allowable_chain_length; dict = dict_create( 4, initial_string_count, initial_hash_chains, max_chain_length ); /*********** ** Copy the entries from dict1 into the new dictionary. ***********/ for ( i = 0 ; i < dict1->entry_count ; i++ ) { se = (STRING_ENTRY*)&dict1->string_table[i]; if ( se->count > 0 ) { se2 = dict_insert( dict, dict1->string_array+se->string_offset, se->count, se->flags, NULL, &string_index ); if ( se2 == NULL ) goto err_exit; } /* endif */ } /* endfor */ /* Merge the entries from dict2 into the new dictionary. */ if ( dict_merge(dict,dict2,FALSE) == FALSE ) goto err_exit; /* Success. Return a pointer to the new dictionary. */ return( dict ); /* Failure. Ignominiously erase our tracks and return NULL. */ err_exit: dict_destroy( dict ); return NULL; }
// shallow copying except when entry is a dictionary // // parameter: k: key, v: value, a: dictionary // static void dict_entry_copy(char *k, void *v, void *a) { dict_entry *e = (dict_entry *)v; pdf_obj *val; char *p; val = pdf_malloc(sizeof(pdf_obj)); #ifdef HASHMAP *val = *(pdf_obj*)v; #else *val = *(pdf_obj*)(e->v); #endif switch (val->t) { case eDict: val->value.d.dict = dict_copy(((pdf_obj*)(e->v))->value.d.dict); break; case eName: case eString: case eHexString: case eArray: break; default: break; } #ifdef HASHMAP if (pdf_keyword_find(k, strlen(k))) { p = k; } else { p = pdfname_search(k); if (!p) { // memory leak p = pdf_malloc(strlen(k)+1); memcpy(p, k, strlen(k)+1); } } #else if (pdf_keyword_find(e->k, strlen(e->k))) { p = e->k; } else { p = pdf_malloc(strlen(e->k)+1); memcpy(p, e->k, strlen(e->k)+1); } #endif dict_insert((dict*)a, p, val); }
int dict_alloc_insert(dict_t *dict, const void *key, void *data) { dnode_t *node = dict->allocnode(dict->context); if (node) { dnode_init(node, data); dict_insert(dict, node, key); return 1; } return 0; }
void pdf_dict_insert_name(dict *d, char *k, char *n) { pdf_obj *val; pdf_obj key = pdf_key_to_obj(k); val = pdf_malloc(sizeof(pdf_obj)); val->t = eKey; *val = pdf_key_to_obj(n); dict_insert(d, (char*)key.value.k, (void*)val); }
static dict_t parse_database_int(RECDB *recdb) { char *name; struct record_data *rd; dict_t db = alloc_database(); dict_set_free_keys(db, free); while (!dbeof(recdb)) { parse_record_int(recdb, &name, &rd); if (name) dict_insert(db, name, rd); } return db; }
/// Add a new PathAction into the specified CmdAction. /// No coalescing is attempted - PA objects are are always added /// here unless they are truly identical (same pointer == same object). /// @param[in] ca the CA object pointer /// @param[in] pa the PA object pointer void ca_record_pa(ca_o ca, pa_o pa) { dnode_t *dnp; _ca_verbosity_pa(pa, ca, "RECORDING"); // All data is in the key - that's why the value can be null. if (!(dnp = dnode_create(NULL))) { putil_syserr(2, "dnode_create()"); } dict_insert(ca->ca_raw_pa_dict, dnp, pa); }
void test_dict_large(const dict_api* api, uint64_t N) { dict* table; dict_init(&table, api); log_info("Testing inserting %" PRIu64 " elements into %s...", N, api->name); for (uint64_t i = 0; i < N; i++) { if (i % 100000 == 0) log_info("%" PRIu64, i); const uint64_t key = make_key(i), value = make_value(i); // log_info("%" PRIu64 " => %" PRIu64, key, value); CHECK(dict_insert(table, key, value), "cannot insert"); } dict_destroy(&table); }
int main(int argc, char ** argv) { if ( argc < 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") ) { print_usage(); return EXIT_SUCCESS; } FILE * fp = xfopen(argv[1], "r"); Dict keys = dict_create(DATATYPE_STRING, GDS_FREE_ON_DESTROY); char buffer[BUFFER_LEN]; while ( fgets(buffer, BUFFER_LEN, fp) ) { gds_trim(buffer); if ( *buffer != 0 && *buffer != '#' ) { struct pair_string * pair = pair_string_create(buffer, '='); if ( !pair ) { perror("couldn't create string pair"); return EXIT_FAILURE; } gds_trim(pair->first); gds_trim(pair->second); dict_insert(keys, pair->first, gds_strdup(pair->second)); pair_string_destroy(pair); } } size_t i = 2; while ( argv[i] ) { char * value; if ( dict_value_for_key(keys, argv[i], &value) ) { printf("'%s' is defined with value '%s'\n", argv[i], value); } else { printf("'%s' is not defined\n", argv[i]); } ++i; } if ( fclose(fp) ) { perror("couldn't close file"); return EXIT_FAILURE; } dict_destroy(keys); return 0; }
static void qserver_accept(UNUSED_ARG(struct io_fd *listener), struct io_fd *fd) { struct qserverClient *client; struct sockaddr_storage ss; socklen_t sa_len; unsigned int ii; unsigned int jj; int res; char nick[NICKLEN+1]; char host[HOSTLEN+1]; char ip[HOSTLEN+1]; client = calloc(1, sizeof(*client)); fd->data = client; fd->line_reads = 1; fd->readable_cb = qserver_readable; fd->destroy_cb = qserver_destroy_fd; for (ii = 0; ii < qserver_nbots; ++ii) if (qserver_clients[ii] == NULL) break; if (ii == qserver_nbots) { qserver_nbots += 8; qserver_clients = realloc(qserver_clients, qserver_nbots * sizeof(qserver_clients[0])); for (jj = ii; jj < qserver_nbots; ++jj) qserver_clients[jj] = NULL; } client->id = ii; client->fd = fd; qserver_clients[client->id] = client; snprintf(nick, sizeof(nick), " QServ%04d", client->id); safestrncpy(host, "srvx.dummy.user", sizeof(host)); safestrncpy(ip, "0.0.0.0", sizeof(ip)); sa_len = sizeof(ss); res = getpeername(fd->fd, (struct sockaddr*)&ss, &sa_len); if (res == 0) { getnameinfo((struct sockaddr*)&ss, sa_len, ip, sizeof(host), NULL, 0, NI_NUMERICHOST); if (getnameinfo((struct sockaddr*)&ss, sa_len, host, sizeof(host), NULL, 0, 0) != 0) safestrncpy(host, ip, sizeof(host)); } client->user = AddLocalUser(nick, nick+1, host, "qserver dummy user", "*+oi"); irc_pton(&client->user->ip, NULL, ip); dict_insert(qserver_dict, client->user->nick, client); reg_privmsg_func(client->user, qserver_privmsg); reg_notice_func(client->user, qserver_notice); }
void cmd_alias(const char *name, const char *cmd_name, const char *subcmd_name) { struct command *cmd, *alias; assert(strchr(name, ' ') == NULL); // not supported assert(cmd = cmd_find(cmd_name, command_list)); assert(!subcmd_name || (cmd->subcommands && (cmd = cmd_find(subcmd_name, cmd->subcommands)))); alias = malloc(sizeof(struct command)); memcpy(alias, cmd, sizeof(struct command)); alias->name = name; alias->alias = 1; dict_insert(command_list, (char *)alias->name, alias); if(!cmd->aliases) cmd->aliases = stringlist_create(); stringlist_add(cmd->aliases, strdup(alias->name)); }
int cxml_node_addnode(CLOG_INFO* info, CXMLNODE *parent, CXMLNODE *child) { int return_value=0; assert(NULL!=parent); assert(NULL!=child); // if the sub node is NULL create a new sub table if(NULL==parent->sub) { clog( info, CTRACE, "XML: xml_node_addnode(), creating sub node"); parent->sub = dict_create(DICTCOUNT_T_MAX, (dict_comp_t)strcmp); // allow duplicates dict_allow_dupes(parent->sub); } // if the previous stuff worked, add the name/value pair if(NULL!=parent->sub && NULL!=child->name && NULL!=child->name->string) { dnode_t *node = (parent->sub)->dict_allocnode((parent->sub)->dict_context); char *key = strdup(child->name->string); clog( info, CTRACE, "XML: xml_node_adddata(), Saving sub node name=\"%.80s\"", child->name->string); if (node) { dnode_init(node, child); dict_insert(parent->sub, node, key); // let the child know who they are child->me = node; // success! return_value=1; } } // let the child know whow their parent is if(NULL!=parent && NULL!=child) { child->parent = parent; } return return_value; }
dict_t parse_object(RECDB *recdb) { dict_t obj; char *name; struct record_data *rd; int c; if ((c = parse_skip_ws(recdb)) == EOF) return NULL; if (c != '{') ABORT(recdb, EXPECTED_OPEN_BRACE, c); obj = alloc_object(); dict_set_free_keys(obj, free); while (!dbeof(recdb)) { if ((c = parse_skip_ws(recdb)) == '}') break; if (c == EOF) break; dbungetc(c, recdb); parse_record_int(recdb, &name, &rd); dict_insert(obj, name, rd); } return obj; }
void try_with(uint64_t side) { dict* htable; CUCKOO_COUNTERS.full_rehashes = 0; ASSERT(!dict_init(&htable, &dict_htcuckoo, NULL)); for (uint64_t i = 0; i < side; ++i) { for (uint64_t j = 0; j < side; ++j) { for (uint64_t k = 0; k < side; ++k) { const uint64_t key = (i << 32ULL) | (j << 16ULL) | k; ASSERT(dict_insert(htable, key, 42)); } } } log_info("side=%5" PRIu64 " size=%" PRIu64 ": " "took %5" PRIu64 " full rehashes", side, side * side * side, CUCKOO_COUNTERS.full_rehashes); fprintf(output, "%" PRIu64 "\t%" PRIu64 "\n", side, CUCKOO_COUNTERS.full_rehashes); fflush(output); dict_destroy(&htable); }
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys, const struct closest_lookup_info *cl_infos, unsigned n_cl_infos) { dict_itor *itor = dict_itor_new(dct); CU_ASSERT_TRUE(dict_verify(dct)); for (unsigned i = 0; i < nkeys; ++i) { bool inserted = false; void **datum_location = dict_insert(dct, keys[i].key, &inserted); CU_ASSERT_TRUE(inserted); CU_ASSERT_PTR_NOT_NULL(datum_location); CU_ASSERT_PTR_NULL(*datum_location); *datum_location = keys[i].value; CU_ASSERT_TRUE(dict_verify(dct)); for (unsigned j = 0; j <= i; ++j) test_search(dct, itor, keys[j].key, keys[j].value); for (unsigned j = i + 1; j < nkeys; ++j) test_search(dct, itor, keys[j].key, NULL); } CU_ASSERT_EQUAL(dict_count(dct), nkeys); if (dct->_vtable->insert == (dict_insert_func)hashtable_insert || dct->_vtable->insert == (dict_insert_func)hashtable2_insert) { /* Verify that hashtable_resize works as expected. */ dict *clone = dict_clone(dct, NULL); CU_ASSERT_TRUE(dict_verify(dct)); if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) { CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3)); } else { CU_ASSERT_TRUE(hashtable2_resize(dict_private(clone), 3)); } CU_ASSERT_TRUE(dict_verify(dct)); for (unsigned j = 0; j < nkeys; ++j) test_search(clone, NULL, keys[j].key, keys[j].value); dict_free(clone); } if (dct->_vtable->clone) { dict *clone = dict_clone(dct, NULL); CU_ASSERT_PTR_NOT_NULL(clone); CU_ASSERT_TRUE(dict_verify(clone)); CU_ASSERT_EQUAL(dict_count(clone), nkeys); for (unsigned i = 0; i < nkeys; ++i) { test_search(clone, itor, keys[i].key, keys[i].value); } for (unsigned i = 0; i < nkeys; ++i) { CU_ASSERT_TRUE(dict_remove(clone, keys[i].key)); } dict_free(clone); } for (unsigned i = 0; i < nkeys; ++i) test_search(dct, itor, keys[i].key, keys[i].value); for (unsigned i = 0; i < nkeys; ++i) { bool inserted = false; void **datum_location = dict_insert(dct, keys[i].key, &inserted); CU_ASSERT_FALSE(inserted); CU_ASSERT_PTR_NOT_NULL(datum_location); CU_ASSERT_EQUAL(*datum_location, keys[i].value); CU_ASSERT_TRUE(dict_verify(dct)); } CU_ASSERT_EQUAL(dict_count(dct), nkeys); CU_ASSERT_PTR_NOT_NULL(itor); char *last_key = NULL; unsigned n = 0; for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) { CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor)); CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor)); CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor)); char *key = dict_itor_key(itor); bool key_matched = false; for (unsigned i = 0; i < nkeys; ++i) { if (keys[i].key == key) { CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value); key_matched = true; break; } } CU_ASSERT_TRUE(key_matched); if (dct->_vtable->insert != (dict_insert_func)hashtable_insert && dct->_vtable->insert != (dict_insert_func)hashtable2_insert) { if (last_key) { CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0); } last_key = dict_itor_key(itor); } ++n; } CU_ASSERT_EQUAL(n, nkeys); last_key = NULL; n = 0; for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) { CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor)); CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor)); CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor)); char *key = dict_itor_key(itor); bool key_matched = false; for (unsigned i = 0; i < nkeys; ++i) { if (keys[i].key == key) { CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value); key_matched = true; break; } } CU_ASSERT_TRUE(key_matched); if (dct->_vtable->insert != (dict_insert_func)hashtable_insert && dct->_vtable->insert != (dict_insert_func)hashtable2_insert) { if (last_key) { CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0); } last_key = dict_itor_key(itor); } ++n; } CU_ASSERT_EQUAL(n, nkeys); for (unsigned i = 0; i < nkeys; ++i) { bool inserted = false; void **datum_location = dict_insert(dct, keys[i].key, &inserted); CU_ASSERT_FALSE(inserted); CU_ASSERT_PTR_NOT_NULL(datum_location); CU_ASSERT_PTR_NOT_NULL(*datum_location); *datum_location = keys[i].alt; CU_ASSERT_TRUE(dict_verify(dct)); } CU_ASSERT_EQUAL(dict_count(dct), nkeys); for (unsigned i = 0; i < nkeys; ++i) test_search(dct, itor, keys[i].key, keys[i].alt); for (unsigned i = 0; i < nkeys; ++i) { test_search(dct, itor, keys[i].key, keys[i].alt); CU_ASSERT_TRUE(dict_remove(dct, keys[i].key)); CU_ASSERT_TRUE(dict_verify(dct)); CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false); for (unsigned j = 0; j <= i; ++j) { test_search(dct, itor, keys[j].key, NULL); } for (unsigned j = i + 1; j < nkeys; ++j) { test_search(dct, itor, keys[j].key, keys[j].alt); } } for (unsigned i = 0; i < nkeys; ++i) { bool inserted = false; void **datum_location = dict_insert(dct, keys[i].key, &inserted); CU_ASSERT_TRUE(inserted); CU_ASSERT_PTR_NOT_NULL(datum_location); CU_ASSERT_PTR_NULL(*datum_location); *datum_location = keys[i].value; CU_ASSERT_TRUE(dict_verify(dct)); } CU_ASSERT_EQUAL(dict_count(dct), nkeys); CU_ASSERT_EQUAL(dict_clear(dct), nkeys); for (unsigned i = 0; i < nkeys; ++i) { bool inserted = false; void **datum_location = dict_insert(dct, keys[i].key, &inserted); CU_ASSERT_TRUE(inserted); CU_ASSERT_PTR_NOT_NULL(datum_location); CU_ASSERT_PTR_NULL(*datum_location); *datum_location = keys[i].value; CU_ASSERT_TRUE(dict_verify(dct)); } test_closest_lookup(dct, cl_infos, n_cl_infos); dict_itor_free(itor); CU_ASSERT_EQUAL(dict_count(dct), nkeys); CU_ASSERT_EQUAL(dict_free(dct), nkeys); }
struct chanNode * AddChannel(const char *name, time_t time_, const char *modes, char *banlist, char *exemptlist) { struct chanNode *cNode; char new_modes[MAXLEN], *argv[MAXNUMPARAMS]; unsigned int nn; if (!IsChannelName(name)) { log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name); return NULL; } if (!modes) modes = ""; safestrncpy(new_modes, modes, sizeof(new_modes)); nn = split_line(new_modes, 0, ArrayLength(argv), argv); if (!(cNode = GetChannel(name))) { cNode = calloc(1, sizeof(*cNode) + strlen(name)); strcpy(cNode->name, name); banList_init(&cNode->banlist); exemptList_init(&cNode->exemptlist); modeList_init(&cNode->members); mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER); dict_insert(channels, cNode->name, cNode); cNode->timestamp = time_; rel_age = 1; } else if (cNode->timestamp > time_) { wipeout_channel(cNode, time_, argv, nn); rel_age = 1; } else if (cNode->timestamp == time_) { mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER); rel_age = 0; } else { rel_age = -1; } /* rel_age is the relative ages of our channel data versus what is * in a BURST command. 1 means ours is younger, 0 means both are * the same age, -1 means ours is older. */ /* if it's a new or updated channel, make callbacks */ if (rel_age > 0) for (nn=0; nn<ncf_used; nn++) ncf_list[nn](cNode, ncf_list_extra[nn]); /* go through list of bans and add each one */ if (banlist && (rel_age >= 0)) { for (nn=0; banlist[nn];) { char *ban = banlist + nn; struct banNode *bn; while (banlist[nn] != ' ' && banlist[nn]) nn++; while (banlist[nn] == ' ') banlist[nn++] = 0; bn = calloc(1, sizeof(*bn)); safestrncpy(bn->ban, ban, sizeof(bn->ban)); safestrncpy(bn->who, "<unknown>", sizeof(bn->who)); bn->set = now; banList_append(&cNode->banlist, bn); } } /* go through list of exempts and add each one */ if (exemptlist && (rel_age >= 0)) { for (nn=0; exemptlist[nn];) { char *exempt = exemptlist + nn; struct exemptNode *en; while (exemptlist[nn] != ' ' && exemptlist[nn]) nn++; while (exemptlist[nn] == ' ') exemptlist[nn++] = 0; en = calloc(1, sizeof(*en)); safestrncpy(en->exempt, exempt, sizeof(en->exempt)); safestrncpy(en->who, "<unknown>", sizeof(en->who)); en->set = now; exemptList_append(&cNode->exemptlist, en); } } return cNode; }
/************************************************************************* * dict_import: read in an ASCII dictionary. * * dict_fname - name of dictionary file * parameters to create a DICTIONARY structure (see dict_create) * * Returns: pointer to created DICTIONARY structure * (NULL on failure) * *************************************************************************/ DICTIONARY *dict_import( const char *dict_fname , const long initial_string_count , const long initial_hash_entries , const long max_chain_length ) { DICTIONARY *dict; char buffer[BUFLEN], ch; int index, c, c0; long number; FILE *fi = NULL; /*********** ** Dictionary setup. ***********/ dict = dict_create( 4, initial_string_count , initial_hash_entries , max_chain_length ); if ( dict == NULL ) goto err_exit; /*********** ** Read the dictionary file ** Each line should have one word or a string delimited by '|' ***********/ if ( (fi=fopen(dict_fname,"r")) == NULL ) goto err_exit; while( fgets(buffer,BUFLEN,fi) != NULL ) { c0 = 0; /* Skip to non-blank */ while ( (c0<BUFLEN-2) && (buffer[c0]==' ') ) ++c0; if ( buffer[c0] == '|' ) { c = ++c0; ch = '|'; } else { c = c0; ch = ' '; } /* endif */ /* Scan to blank or matching '|' */ while ( (c<BUFLEN-1) && (buffer[c]!='\0') && (buffer[c]!='\n') && (buffer[c]!=ch) ) ++c; buffer[c] = '\0'; /* Insert the word */ if ( dict_insert(dict,buffer+c0,1,0,NULL,&number) == NULL ) goto err_exit; } /* endwhile */ /*********** ** Fill in the dictionary parameter vector. ***********/ if ( dict_set_parm_values(dict) == FALSE ) goto err_exit; /*********** ** Update the table of contents for HASH STTB STAR ***********/ if ( (index=dict_toc_index(dict,"HASH")) == -1 ) goto err_exit; dict->toc[index].size = dict->table_size * sizeof(long); if ( (index=dict_toc_index(dict,"STTB")) == -1 ) goto err_exit; dict->toc[index].size = dict->string_max * sizeof(char); if ( (index=dict_toc_index(dict,"STAR")) == -1 ) goto err_exit; dict->toc[index].size = dict->array_size * sizeof(STRING_ENTRY); /* Success. Return a pointer to the new dictionary. */ fclose(fi); return( dict ); /* Failure. Ignominiously erase our tracks and return NULL. */ err_exit: if ( fi != NULL ) fclose(fi); dict_destroy( dict ); return NULL; }
int main(int argc,char *argv[],char *env[]) { pdict = dict_create(); FILE *fp = fopen(FILE_NAME,"r"); if(fp == NULL) { perror("fopen"); return FAILED; } char english[1000]; char chinese[1000]; while(!feof(fp)) { memset(english,0,sizeof(english)); memset(chinese,0,sizeof(chinese)); if(fgets(english,sizeof(english),fp) == NULL) break; *strchr(english,'\n') = 0; if(fgets(chinese,sizeof(chinese),fp) == NULL) break; *strchr(chinese,'\n') = 0; dict_insert(pdict,english,chinese); } signal(28,handle); show_window(); move_xy(3,15); char key; while(1) { key = get_key(); switch(key) { case ESC: clear_screen(); move_xy(1,1); return 0; case BACKSPACE: if(buf_i > 0) { move_left(1); putchar(' '); fflush(stdout); move_left(1); buf_i--; input_buf[buf_i] = 0; show_word(pdict,input_buf); } break; default: if(buf_i <= win_y - 20 && isprint(key)) { putchar(key); fflush(stdout); input_buf[buf_i] = key; buf_i++; input_buf[buf_i] = 0; show_word(pdict,input_buf); } } } return 0; }
/************************************************************************* * dict_merge: merges the contents of a dictionary into * another one, updating the contents of the destination * * dst - dictionary to update * src - dictionary to add * move - boolean flag * TRUE - move to dest * FALSE - copy to dest * * Returns: status code * TRUE - merge completed * FALSE - error on merge * * Notes: entries of the same string have their counts * added. At the end of a move, src is empty. If there * is an error during a move, the contents of both * dictionaries cannot be trusted. *************************************************************************/ BOOLEANC dict_merge(const DICTIONARY *dst, const DICTIONARY *src, const BOOLEANC move) { STRING_ENTRY *se, *se2; DICTIONARY *dict1, *dict2; long i, string_index, index; dict1 = (DICTIONARY*)src; dict2 = (DICTIONARY*)dst; /*********** ** Copy the dictionary entries into the new dictionary. ***********/ for ( i = 0 ; i < dict1->entry_count ; i++ ) { se = (STRING_ENTRY*)&dict1->string_table[i]; if ( se->count > 0 ) { se2 = dict_insert( dict2, dict1->string_array+se->string_offset, se->count, se->flags, NULL, &string_index ); if ( se2 == NULL ) goto err_exit; } /* endif */ } /* endfor */ /*********** ** Set up the dictionary parameter vector. ***********/ if ( dict_set_parm_values(dict2) == FALSE ) return( FALSE ); /*********** ** Update the table of contents. ** PARM HASH STTB STAR ***********/ if ( (index=dict_toc_index(dict2,"HASH")) == -1) goto err_exit; dict2->toc[index].size = dict2->table_size * sizeof(long); dict2->toc[index].ptr = dict2->chains; if ( (index=dict_toc_index(dict2,"STTB")) == -1) goto err_exit; dict2->toc[index].size = dict2->string_max * sizeof(char); dict2->toc[index].ptr = dict2->string_table; if ( (index=dict_toc_index(dict2,"STAR")) == -1) goto err_exit; dict2->toc[index].size = dict2->array_size * sizeof(STRING_ENTRY); dict2->toc[index].ptr = dict2->string_array; /*********** ** Update the signature ***********/ dict2->sig->checksum = compute_checksum( 4*sizeof(DICT_TOC_ENTRY) , (char*)(dict2->toc) ); /*********** ** If this is a move, destroy the source dictionary ***********/ if ( move == TRUE ) if ( dict_destroy(dict1) == FALSE ) goto err_exit; /*********** ** Success ***********/ return( TRUE ); /*********** ** Failure ***********/ err_exit: dict_destroy( dict2 ); return FALSE; }
void add_track_user(struct userNode *user) { dict_insert(track_db, strdup(user->nick), user); }
int main(int argc, char **argv) { char buf[512], *p, *ptr, *ptr2; int rv; dict *dct; if (argc != 2) quit("usage: %s [type]", appname); srand((unsigned)time(NULL)); dict_malloc_func = xmalloc; ++argv; switch (argv[0][0]) { case 'h': dct = hb_dict_new((dict_compare_func)strcmp, key_val_free); break; case 'p': dct = pr_dict_new((dict_compare_func)strcmp, key_val_free); break; case 'r': dct = rb_dict_new((dict_compare_func)strcmp, key_val_free); break; case 't': dct = tr_dict_new((dict_compare_func)strcmp, NULL, key_val_free); break; case 's': dct = sp_dict_new((dict_compare_func)strcmp, key_val_free); break; case 'w': dct = wb_dict_new((dict_compare_func)strcmp, key_val_free); break; case 'H': dct = hashtable_dict_new((dict_compare_func)strcmp, dict_str_hash, key_val_free, HSIZE); break; default: quit("type must be one of h, p, r, t, s, w, or H"); } if (!dct) quit("can't create container"); for (;;) { printf("> "); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) break; if ((p = strchr(buf, '\n')) != NULL) *p = 0; for (p = buf; isspace(*p); p++) /* void */; strcpy(buf, p); ptr2 = (ptr = strtok(buf, " ") ? strtok(NULL, " ") : NULL) ? strtok(NULL, " ") : NULL; if (*buf == 0) continue; if (strcmp(buf, "insert") == 0) { if (!ptr2) { printf("usage: insert <key> <data>\n"); continue; } void **datum_location; if (dict_insert(dct, xstrdup(ptr), &datum_location)) { *datum_location = xstrdup(ptr2); printf("inserted '%s': '%s'\n", ptr, *datum_location); } else { printf("key '%s' already in dict: '%s'\n", ptr, *datum_location); } } else if (strcmp(buf, "search") == 0) { if (ptr2) { printf("usage: search <key>\n"); continue; } ptr2 = dict_search(dct, ptr); if (ptr2) printf("found '%s': '%s'\n", ptr, ptr2); else printf("key '%s' not in dict!\n", ptr); } else if (strcmp(buf, "remove") == 0) { if (!ptr || ptr2) { printf("usage: remove <key>\n"); continue; } rv = dict_remove(dct, ptr); if (rv == 0) printf("removed '%s' from dict\n", ptr); else printf("key '%s' not in dict!\n", ptr); } else if (strcmp(buf, "show") == 0) { if (ptr) { printf("usage: show\n"); continue; } dict_itor *itor = dict_itor_new(dct); dict_itor_first(itor); for (; dict_itor_valid(itor); dict_itor_next(itor)) printf("'%s': '%s'\n", (char *)dict_itor_key(itor), (char *)dict_itor_data(itor)); dict_itor_free(itor); } else if (strcmp(buf, "reverse") == 0) { if (ptr) { printf("usage: reverse\n"); continue; } dict_itor *itor = dict_itor_new(dct); dict_itor_last(itor); for (; dict_itor_valid(itor); dict_itor_prev(itor)) printf("'%s': '%s'\n", (char *)dict_itor_key(itor), (char *)dict_itor_data(itor)); dict_itor_free(itor); } else if (strcmp(buf, "clear") == 0) { if (ptr) { printf("usage: clear\n"); continue; } dict_clear(dct); } else if (strcmp(buf, "count") == 0) { if (ptr) { printf("usage: count\n"); continue; } printf("count = %zu\n", dict_count(dct)); } else if (strcmp(buf, "quit") == 0) { break; } else { printf("Usage summary:\n"); printf(" insert <key> <data>\n"); printf(" search <key>\n"); printf(" remove <key>\n"); printf(" clear\n"); printf(" count\n"); printf(" show\n"); printf(" reverse\n"); printf(" quit\n"); } } dict_free(dct); exit(0); }