/* needs serious testing */ int put_request_header (struct request *req, char *name, char *value) { /* Change the content of the actual request. An alternative would be to only create a new content string when a request is being sent */ if (hash_table_get (req->headers, name) == NULL) { char *ptr; int new_length = req->content_len + strlen (name) + 2 + strlen (value) + 1; req->content = realloc (req->content, new_length); hash_table_put (req->headers, name, value); ptr = req->content + req->content_len - 2; memset (ptr, 0, new_length - req->content_len); APPEND(ptr, name, strlen (name)); APPEND(ptr, ": ", 2); APPEND(ptr, value, strlen (value)); APPEND(ptr, "\r\n", 2); APPEND(ptr, "\r\n", 2); req->content_len = new_length - 1; } else { hash_table_put (req->headers, name, value); printf ("Key-value pair (%s, %s)\n", name, hash_table_get (req->headers, name)); req = make_request (req->url, req->headers, req->method); } return 0; }
struct request * make_request (url_t *url, struct hash_table *table, char *method) { char *ptr; struct request *req = malloc (sizeof (struct request)); int content_len = 0; int i; req->position = 0; req->url = url; req->method = strdup (method); req->http_version = strdup ("HTTP/1.0"); req->headers = hash_table_copy (table); content_len += strlen (method) + 1 + strlen (url->path) + 1 + strlen (req->http_version) + 2; for (i = 0; i < table->size; i++) { if (table->table[i].state == OCCUPIED) { char *key = table->table[i].key; char *value = hash_table_get (table, key); content_len += strlen (key) + 2 + strlen (value) + 2; } } content_len += 2; /* Final \r\n */ req->content = malloc (content_len + 1); if (!req->content) return NULL; req->content_len = content_len; ptr = req->content; APPEND(ptr, method, strlen (method)); *ptr++ = ' '; APPEND(ptr, url->path, strlen (url->path)); *ptr++ = ' '; APPEND(ptr, req->http_version, strlen (req->http_version)); APPEND(ptr, "\r\n", 2); for (i = 0; i < table->size; i++) { if (table->table[i].state == OCCUPIED) { char *key = table->table[i].key; char *value = hash_table_get (table, key); APPEND(ptr, key, strlen (key)); APPEND(ptr, ": ", 2); APPEND(ptr, value, strlen (value)); APPEND(ptr, "\r\n", 2); } } APPEND(ptr, "\r\n", 2); return req; }
downloaded_file_t downloaded_file (downloaded_file_t mode, const char *file) { downloaded_file_t *ptr; if (mode == CHECK_FOR_FILE) { if (!downloaded_files_hash) return FILE_NOT_ALREADY_DOWNLOADED; ptr = hash_table_get (downloaded_files_hash, file); if (!ptr) return FILE_NOT_ALREADY_DOWNLOADED; return *ptr; } if (!downloaded_files_hash) downloaded_files_hash = make_string_hash_table (0); ptr = hash_table_get (downloaded_files_hash, file); if (ptr) return *ptr; ptr = downloaded_mode_to_ptr (mode); hash_table_put (downloaded_files_hash, xstrdup (file), ptr); return FILE_NOT_ALREADY_DOWNLOADED; }
static void bsp_tree_dump_node(const struct bsp_tree *root, struct node_map_context *ctx) { int this_idx, front_idx, back_idx; this_idx = (int)hash_table_get(ctx->node_map, root); front_idx = root->front ? (int)hash_table_get(ctx->node_map, root->front) : -1; back_idx = root->back ? (int)hash_table_get(ctx->node_map, root->back) : -1; assert(this_idx && front_idx && back_idx); fprintf(ctx->out, "\t\t\t\t%d %d %d,\n", this_idx, front_idx, back_idx); }
// tag_attr_list不能为空,为空表示所有属性解析过程中都会返回,可以杜撰一个不存在的属性。 void extract_text_init(spec_tag_t *spec_tag_list, int tag_list_size, struct hash_table ** spec_tag_ht_pp, tag_attr_t *tag_attr_list, int attr_list_size, struct hash_table ** tag_attr_ht_pp) { int i; struct hash_table * ht; tag_attr_t * tagattr; ht = make_nocase_string_hash_table(tag_list_size); for (i = 0; i < tag_list_size; i++) hash_table_put(ht, spec_tag_list[i].tag_name, spec_tag_list + i); *spec_tag_ht_pp = ht; ht = make_nocase_string_hash_table(attr_list_size); for (i = 0; i < attr_list_size; i++) { // 先检查属性是否存在... tagattr = hash_table_get(ht,tag_attr_list[i].tag_attr); if(!tagattr) { // 不存在,插入为首节点 tag_attr_list[i].tail = tag_attr_list + i; hash_table_put(ht, tag_attr_list[i].tag_attr, tag_attr_list + i); } else { // 存在,插入到链表的末尾 tagattr->tail->next = tag_attr_list + i; tagattr->tail = tag_attr_list + i; } } *tag_attr_ht_pp = ht; }
void index_file(char * file, struct hash_table * table) { /* get a reverse hash table of the terms in the file */ FILE * file_fd; struct Parser * parser; char * word; file_fd = fopen(file, "r"); parser = parser_new(file_fd); if (file_fd == NULL) { fprintf(stderr, "couldn't get handler for %s\n", file); free_hash_table(table); } while ( (word = parser_next_word(parser)) ) { strtolower(word); struct hash_node * tmp = hash_table_get(table, word); if(tmp) { hash_node_add_occurrence(tmp, file); } else { struct hash_node * node = new_hash_node(word); node->appears_in = new_file_node(file); hash_table_store(table, word, node); } free(word); } parser_destroy(parser); }
struct net_t *net_find(char *name) { /* No network list */ if (!net_table) return NULL; /* Find network in list */ return hash_table_get(net_table, name); }
int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg) { hash_table_t *file_hash = &conf->file_hash; pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name); int file_name_len = strlen(file_name); if (file_name[file_name_len -1] == '/') return 0; if (conf->offline_root) { int len = strlen(conf->offline_root); if (strncmp(file_name, conf->offline_root, len) == 0) { file_name += len; } } // ipkg_message(conf, IPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name); hash_table_insert(file_hash, file_name, owning_pkg); if (old_owning_pkg) { str_list_remove_elt(old_owning_pkg->installed_files, file_name); /* mark this package to have its filelist written */ old_owning_pkg->state_flag |= SF_FILELIST_CHANGED; owning_pkg->state_flag |= SF_FILELIST_CHANGED; } return 0; }
int main(int argc, char ** argv) { int i; struct hash_table * table = hash_table_new(1); struct client * cli = malloc(sizeof(struct client)); cli->name = "foo bar"; cli->credit = 5; hash_table_store(table, cli->name, cli); cli = malloc(sizeof(struct client)); cli->name = "far bar"; cli->credit = 6; hash_table_store(table, cli->name, cli); cli = hash_table_delete(table, "foo bar"); /* returns the deleted value */ printf("deleting: %i, should be 1\n", cli != NULL); cli = hash_table_delete(table, "doesn't exist"); /* returns NULL */ printf("deleting: %i, should be 1\n", cli == NULL); struct client * cl2 = hash_table_get(table, "far bar"); printf("%s has %i money\n", cl2->name, cl2->credit); /* get all of the keys and list them */ char ** keys = hash_table_get_all_keys(table); for (i = 0; i < table->population; i++) { printf("key: %s\n", keys[i]); } free(keys); free(cli); free(cl2); hash_table_destroy(table, free_fn); return 0; }
static void trace_winsys_buffer_unmap(struct pipe_winsys *_winsys, struct pipe_buffer *buffer) { struct trace_winsys *tr_ws = trace_winsys(_winsys); struct pipe_winsys *winsys = tr_ws->winsys; const void *map; map = hash_table_get(tr_ws->buffer_maps, buffer); if(map) { trace_dump_call_begin("pipe_winsys", "buffer_write"); trace_dump_arg(ptr, winsys); trace_dump_arg(ptr, buffer); trace_dump_arg_begin("data"); trace_dump_bytes(map, buffer->size); trace_dump_arg_end(); trace_dump_arg_begin("size"); trace_dump_uint(buffer->size); trace_dump_arg_end(); trace_dump_call_end(); hash_table_remove(tr_ws->buffer_maps, buffer); } winsys->buffer_unmap(winsys, buffer); }
static void collect_tags_mapper (struct taginfo *tag, void *arg) { struct map_context *ctx = (struct map_context *)arg; /* Find the tag in our table of tags. This must not fail because map_html_tags only returns tags found in interesting_tags. I've changed this for now, I'm passing NULL as interesting_tags to map_html_tags. This way we can check all tags for a style attribute. */ struct known_tag *t = hash_table_get (interesting_tags, tag->name); if (t != NULL) t->handler (t->tagid, tag, ctx); check_style_attr (tag, ctx); if (tag->end_tag_p && (0 == strcasecmp (tag->name, "style")) && tag->contents_begin && tag->contents_end && tag->contents_begin <= tag->contents_end) { /* parse contents */ get_urls_css (ctx, tag->contents_begin - ctx->text, tag->contents_end - tag->contents_begin); } }
char lisp_get_symbol(const char* name, void** atom) { if(!lisp_symtbl) lisp_symtbl=new_hash_table(LPSYMTBLSIZE,hash_symbol); *atom=hash_table_get(lisp_symtbl,name); return *atom?1:0; }
struct robot_specs * res_get_specs (const char *host, int port) { char *hp; SET_HOSTPORT (host, port, hp); if (!registered_specs) return NULL; return hash_table_get (registered_specs, hp); }
static struct cookie * find_cookie_chain_exact (const char *domain, int port) { char *key; if (!cookies_hash_table) return NULL; SET_HOSTPORT (domain, port, key); return hash_table_get (cookies_hash_table, key); }
void test_hash_table_get(void) { // It should be possible to retrieve item values from the hash table. hash_table_t table; create_hash_table(20, &table); hash_table_set("potato", 3, &table); hash_table_set("monkey", 8, &table); TEST_CHECK(hash_table_get("potato", &table) == 3); TEST_CHECK(hash_table_get("monkey", &table) == 8); // TODO: the value returned by hash_table_get can be from a null pointer. // A default sentinel value could be returned instead e.g. -(2^30) // TEST_CHECK(hash_table_get("radio", &table) == -(2 << 30)); destroy_hash_table(&table); }
int main(int argc, const char *argv[]) { hash_table* table = hash_table_create(); something smth; smth.field = 2; hash_table_add(table, "something", &smth); something* smth2 = hash_table_get(table, "something"); printf("Asked for 'something'->field, got: %d\n", smth2->field); char* data = "dsadsdsfdsfasfds"; hash_table_add(table, "wat", data); char* retrieved = hash_table_get(table, "wat"); printf("Asked for 'wat', got: %s\n", retrieved); hash_table_free(table); return 0; }
static void init_interesting (void) { /* Init the variables interesting_tags and interesting_attributes that are used by the HTML parser to know which tags and attributes we're interested in. We initialize this only once, for performance reasons. Here we also make sure that what we put in interesting_tags matches the user's preferences as specified through --ignore-tags and --follow-tags. */ int i; interesting_tags = make_nocase_string_hash_table (countof (known_tags)); /* First, add all the tags we know hot to handle, mapped to their respective entries in known_tags. */ for (i = 0; i < countof (known_tags); i++) hash_table_put (interesting_tags, known_tags[i].name, known_tags + i); /* Then remove the tags ignored through --ignore-tags. */ if (opt.ignore_tags) { char **ignored; for (ignored = opt.ignore_tags; *ignored; ignored++) hash_table_remove (interesting_tags, *ignored); } /* If --follow-tags is specified, use only those tags. */ if (opt.follow_tags) { /* Create a new table intersecting --follow-tags and known_tags, and use it as interesting_tags. */ struct hash_table *intersect = make_nocase_string_hash_table (0); char **followed; for (followed = opt.follow_tags; *followed; followed++) { struct known_tag *t = hash_table_get (interesting_tags, *followed); if (!t) continue; /* ignore unknown --follow-tags entries. */ hash_table_put (intersect, *followed, t); } hash_table_destroy (interesting_tags); interesting_tags = intersect; } /* Add the attributes we care about. */ interesting_attributes = make_nocase_string_hash_table (10); for (i = 0; i < countof (additional_attributes); i++) hash_table_put (interesting_attributes, additional_attributes[i], "1"); for (i = 0; i < countof (tag_url_attributes); i++) hash_table_put (interesting_attributes, tag_url_attributes[i].attr_name, "1"); }
void register_redirection (const char *from, const char *to) { char *file; ENSURE_TABLES_EXIST; file = hash_table_get (dl_url_file_map, to); assert (file != NULL); if (!hash_table_contains (dl_url_file_map, from)) hash_table_put (dl_url_file_map, xstrdup (from), xstrdup (file)); }
static void cache_remove (const char *host) { struct address_list *al; if (!host_name_addresses_map) return; al = hash_table_get (host_name_addresses_map, host); if (al) { address_list_release (al); hash_table_remove (host_name_addresses_map, host); } }
bool hash_table_test() { hash_table *table = hash_table_new(); if (table == NULL) { printf("ERROR: Could not create array\n"); return false; } char *test_val_0 = "These are the times that try men's souls..."; char *test_val_1 = "We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness."; char *test_val_2 = "Now, therefore, I, Gerald R. Ford, President of the United States, pursuant to the pardon power conferred upon me by Article II, Section 2, of the Constitution, have granted and by these presents do grant a full, free, and absolute pardon unto Richard Nixon for all offenses against the United States which he, Richard Nixon, has committed or may have committed or taken part in during the period from July (January) 20, 1969, through August 9, 1974."; hash_table_set(table, test_val_0, "test0", NULL); hash_table_set(table, test_val_1, "test1", NULL); hash_table_set(table, test_val_2, "another_test", NULL); char *value = hash_table_get(table, "test0"); if (value != NULL && strcmp(value, test_val_0) != 0) { printf("ERROR: When reading hash table, expected \"%s\" but got \"%s\"\n", test_val_0, value); return false; } value = hash_table_get(table, "test1"); if (value != NULL && strcmp(value, test_val_1) != 0) { printf("ERROR: When reading hash table, expected \"%s\" but got \"%s\"\n", test_val_1, value); return false; } value = hash_table_get(table, "another_test"); if (value != NULL && strcmp(value, test_val_2) != 0) { printf("ERROR: When reading hash table, expected \"%s\" but got \"%s\"\n", test_val_2, value); return false; } hash_table_free(table); return true; }
static int find_matching_chains (const char *host, int port, struct cookie *store[], int size) { struct cookie *chain; int dot_count; char *hash_key; int count = 0; if (!cookies_hash_table) return 0; SET_HOSTPORT (host, port, hash_key); /* Exact match. */ chain = hash_table_get (cookies_hash_table, hash_key); if (chain) STORE_CHAIN (chain, store, size, count); dot_count = count_char (host, '.'); /* Match less and less specific domains. For instance, given fly.srk.fer.hr, we match .srk.fer.hr, then .fer.hr. */ while (dot_count-- > 1) { /* Note: we operate directly on hash_key (in form host:port) because we don't want to allocate new hash keys in a loop. */ char *p = strchr (hash_key, '.'); assert (p != NULL); chain = hash_table_get (cookies_hash_table, p); if (chain) STORE_CHAIN (chain, store, size, count); hash_key = p + 1; } return count; }
static void test_resize(void) { Hash_table table; hash_table_init(&table); for (int i = 0; i < 512; ++i) { for (int j = 0; j < i; ++j) { int n; hash_table_get(&table, gen_key(j), &n); VERIFY(n == j); } hash_table_set(&table, gen_key(i), i, NULL); } hash_table_free(&table); }
static struct address_list * cache_query (const char *host) { struct address_list *al; if (!host_name_addresses_map) return NULL; al = hash_table_get (host_name_addresses_map, host); if (al) { DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al)); ++al->refcount; return al; } return NULL; }
static void * trace_winsys_buffer_map(struct pipe_winsys *_winsys, struct pipe_buffer *buffer, unsigned usage) { struct trace_winsys *tr_ws = trace_winsys(_winsys); struct pipe_winsys *winsys = tr_ws->winsys; void *map; map = winsys->buffer_map(winsys, buffer, usage); if(map) { if(usage & PIPE_BUFFER_USAGE_CPU_WRITE) { assert(!hash_table_get(tr_ws->buffer_maps, buffer)); hash_table_set(tr_ws->buffer_maps, buffer, map); } } return map; }
int main() { hash_table* h = hash_table_new(); hash_table_register_print(h, int_print); hash_table_register_copy(h, int_copy); hash_table_register_delete(h, int_delete); hash_table_print(h); char buff[3]; int ints[20]; for(int i=0; i < 20; i++) { ints[i] = i; sprintf(buff, "%i", i); hash_table_add(h, buff, &(ints[i])); } hash_table_print(h); hash_table_get(h, "1"); hash_table_remove(h, "1"); hash_table_print(h); hash_table_delete(h); return 0; }
struct bin_config_elem_t *bin_config_get(struct bin_config_t *bin_config, struct bin_config_elem_t *parent_elem, char *var, void **data_ptr, int *size_ptr) { struct hash_table_t *child_elem_list; struct bin_config_elem_t *elem; /* Check parent element */ if (parent_elem && parent_elem->bin_config != bin_config) { bin_config->error_code = BIN_CONFIG_ERR_PARENT; return 0; } /* Get list of child elements */ child_elem_list = parent_elem ? parent_elem->child_elem_list : bin_config->elem_list; if (!child_elem_list) { bin_config->error_code = BIN_CONFIG_ERR_NOT_FOUND; return 0; } /* Remove element */ elem = hash_table_get(child_elem_list, var); if (!elem) { bin_config->error_code = BIN_CONFIG_ERR_NOT_FOUND; return 0; } /* Return data and size */ if (data_ptr) *data_ptr = elem->data; if (size_ptr) *size_ptr = elem->size; /* Return element */ bin_config->error_code = BIN_CONFIG_ERR_OK; return elem; }
void vi_x86_context_get_desc(char *context_name, char *buf, int size) { char *title_begin = "<span color=\"blue\"><b>"; char *title_end = "</b></span>"; char parent_id_str[MAX_STRING_SIZE]; struct vi_x86_context_t *context; long long cycle; /* Go to current cycle */ cycle = vi_cycle_bar_get_cycle(); vi_state_go_to_cycle(cycle); /* Look for context */ context = hash_table_get(vi_x86_cpu->context_table, context_name); if (!context) panic("%s: %s: invalid context", __FUNCTION__, context_name); /* Title */ str_printf(&buf, &size, "%sDescription for context %s%s\n\n", title_begin, context->name, title_end); /* Parent PID */ if (context->parent_id) snprintf(parent_id_str, sizeof parent_id_str, "%d", context->parent_id); else snprintf(parent_id_str, sizeof parent_id_str, "-"); /* Fields */ str_printf(&buf, &size, "<b>Context PID:</b> %d\n", context->id); str_printf(&buf, &size, "<b>Parent PID:</b> %s\n", parent_id_str); str_printf(&buf, &size, "<b>Creation cycle:</b> %lld\n", context->creation_cycle); }
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: %s number_below_10000\n", argv[0]); exit(1); } hash_table table = hash_table_new(); for (size_t i = 0; i < 10000; i++) { char *key; asprintf(&key, "%zu", i); hash_table_set(&table, key, i); free(key); } int *entry = hash_table_get(&table, argv[1]); if (entry) { printf("%d\n", *entry); } else { fprintf(stderr, "Not found\n"); } }
pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name) { hash_table_t *file_hash = &conf->file_hash; return hash_table_get(file_hash, file_name); }
abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name) { return (abstract_pkg_t *)hash_table_get(hash, pkg_name); }