static command_status_t fetch_domains(domain_set_t *ds, bool force, error_t **error) { if (!ds->uptodate || force) { request_t *req; bool request_success; json_document_t *doc; req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain"); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); if (request_success) { Iterator it; json_value_t root; root = json_document_get_root(doc); hashtable_clear(ds->domains); json_array_to_iterator(&it, root); for (iterator_first(&it); iterator_is_valid(&it); iterator_next(&it)) { json_value_t v; v = (json_value_t) iterator_current(&it, NULL); hashtable_put(ds->domains, 0, json_get_string(v), domain_new(), NULL); // ds->domains has strdup as key_duper, don't need to strdup it ourself } iterator_close(&it); ds->uptodate = TRUE; json_document_destroy(doc); } else { return COMMAND_FAILURE; } } return COMMAND_SUCCESS; }
// Removes and frees all entries from the hashtable as well as the table. void hashtable_free(Hashtable* h) { if (h == NULL) return; hashtable_clear(h); free(h->entries); free(h); }
size_t hashtable_free(hashtable* table) { ASSERT(table != NULL); size_t count = hashtable_clear(table); FREE(table->table); FREE(table); return count; }
int json_object_clear(json_t *json) { json_object_t *object; if(!json_is_object(json)) return -1; object = json_to_object(json); hashtable_clear(&object->hashtable); return 0; }
int main(int argc, char* argv[]) { hashtable_t* table = NULL; hashnode_t* node = NULL; int i; int size; table = (hashtable_t*)malloc(sizeof(hashtable_t)); assert(table != NULL); size = 50; hashtable_init(table, size); for (i = 0; i < size; i++) { assert(table->buckets[i] == NULL); } assert(table->size == 50); hashtable_insert(table, "abcd"); node = hashtable_find(table, "abcd"); assert(node != NULL); hashtable_insert(table, "bcde"); node = hashtable_find(table, "bcde"); assert(node != NULL); size = 80; hashtable_resize(&table, size); assert(table->size == size); node = hashtable_find(table, "abcd"); assert(node != NULL); node = hashtable_find(table, "bcde"); assert(node != NULL); hashtable_clear(table); free(table); return 0; }
void exampleHashtable() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_hashtable(16); Hashtable* H = newHashtable(8); hashtable_put(H, 23, "Hello"); hashtable_put(H, 16, "World"); // Hash another entry to 0 (besides World) hashtable_put(H, 8, "Again"); hashtable_put(H, 24, "And again"); hashtable_put(H, 40, "And again again"); // Print out the size printf("The hashtable has %d entries.\n", H->size); hashtable_display(H, &toString); // Test the get function based on the keys printf("%s ", (char*)hashtable_get(H, 23)); printf("%s\n", (char*)hashtable_get(H, 16)); printf("%s\n", (char*)hashtable_get(H, 8)); printf("%s\n", (char*)hashtable_get(H, 24)); // Try one that doesn't exist and goes to a completely empty entry if (hashtable_get(H, 1) == NULL) printf("Entry with key 1 not found.\n"); // Try one that doesn't exist and goes to an existing entry if (hashtable_get(H, 32) == NULL) printf("Entry with key 32 not found.\n"); // Test exists on all added data if (hashtable_exists(H, 23) && hashtable_exists(H, 16) && hashtable_exists(H, 8) && hashtable_exists(H, 24) && hashtable_exists(H, 40)) printf("The hashtable's entries are sound.\n"); // Test removing of an entry that doesn't exist in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 32)); // Test removing of the first entry in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 16)); // Test removing of a middle entry in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 24)); // Test removing of an end entry in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 40)); // Test removing of the only entry printf("Removed: %s\n", (char*)hashtable_remove(H, 8)); // Test setting the last entry remaining printf("Before: %s", (char*)hashtable_get(H, 23)); hashtable_set(H, 23, "Changed!"); printf("\tAfter: %s\n", (char*)hashtable_get(H, 23)); // Test the set method for a non existing key if (!hashtable_set(H, 45, "Foo")) printf("Cannot set 45 to 'Foo', key 45 doesn't exist.\n"); // Print out the size printf("The hashtable has %d entries.\n", H->size); // Clear the table and print out the size hashtable_clear(H); printf("Cleared. The hashtable has %d entries.\n", H->size); // This will clear the list of any nodes and pool them and then free // the list itself from memory hashtable_free(H); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_hashtable(); }
void pc_map_clear(pc_map_t *map) { hashtable_clear(&map->table); }
void hashtable_destroy(hashtable_t *hashtable) { hashtable_clear(hashtable); }
/* * Main loop */ int sess_main(struct irc_session *sess) { /* Jump into mainloop */ while (!sess->kill) { time_t lastidle = time(NULL); time_t last_sign_of_life = time(NULL); if (sess_connect(sess) < 0) break; sess->session_start = time(NULL); sess_login(sess); /* Inner loop, receive and handle data */ while (!sess->kill) { /* * Since both idle and flood protection are based on second * precision, this is an optimal timeout */ struct timeval timeout = { .tv_sec = 1, .tv_usec = 0 }; int res = sess_handle_data(sess, &timeout); if (res < 0) { break; } else if (res > 0) { last_sign_of_life = time(NULL); } else { if ((time(NULL) - last_sign_of_life) >= TIMEOUT) { struct tm *tm = TIME_GETTIME(&last_sign_of_life); char buffer[TIMEBUF_MAX] = {0}; strftime(buffer, sizeof(buffer), STRFTIME_FORMAT, tm); log_info("Last sign of life was %d seconds " "ago (%s). Pinging server...", time(NULL) - last_sign_of_life, buffer); struct irc_message ping; irc_mkmessage(&ping, CMD_PING, NULL, 0, "%s", sess->hostname); if (sess_sendmsg_real(sess, &ping) <= 0) break; } } /* Try to send messages in outbuffer */ while (sess->buffer_out_start != sess->buffer_out_end) { struct irc_message *next = &(sess->buffer_out[sess->buffer_out_start]); unsigned len = irc_message_size(next); len = MAX(len, FLOODPROT_MIN); if (tokenbucket_consume(&sess->quota, len)) { sess_sendmsg_real(sess, next); sess->buffer_out_start = (sess->buffer_out_start + 1) % FLOODPROT_BUFFER; } else { break; } } /* Check if we have to emit an idle event */ if ((time(NULL) - lastidle) >= IDLE_INTERVAL) { if (sess->cb.on_idle) sess->cb.on_idle(sess->cb.arg, lastidle); lastidle = time(NULL); } /* And regenerate some tokens */ tokenbucket_generate(&sess->quota); } if (sess->cb.on_disconnect) sess->cb.on_disconnect(sess->cb.arg); /* Session is finished, free resources and start again unless killed */ hashtable_clear(sess->channels); hashtable_clear(sess->capabilities); sess_disconnect(sess); } return 0; } int sess_login(struct irc_session *sess) { struct irc_message nick; struct irc_message user; irc_mkmessage(&nick, CMD_NICK, (const char *[]){ sess->nick }, 1, NULL); irc_mkmessage(&user, CMD_USER, (const char *[]){ sess->user, "*", "*" }, 3, "%s", sess->real);