END_TEST START_TEST(eina_hash_all_int) { Eina_Hash *hash; int64_t j[] = { 4321312301243122, 6, 7, 128 }; int i[] = { 42, 6, 7, 0 }; int64_t *test2; int *test; int it; fail_if(eina_init() != 2); hash = eina_hash_int32_new(NULL); fail_if(hash == NULL); for (it = 0; it < 4; ++it) fail_if(eina_hash_add(hash, &i[it], &i[it]) != EINA_TRUE); fail_if(eina_hash_del(hash, &i[1], &i[1]) != EINA_TRUE); test = eina_hash_find(hash, &i[2]); fail_if(test != &i[2]); test = eina_hash_find(hash, &i[3]); fail_if(test != &i[3]); eina_hash_free(hash); hash = eina_hash_int64_new(NULL); fail_if(hash == NULL); for (it = 0; it < 4; ++it) fail_if(eina_hash_add(hash, &j[it], &j[it]) != EINA_TRUE); fail_if(eina_hash_del(hash, &j[1], &j[1]) != EINA_TRUE); test2 = eina_hash_find(hash, &j[0]); fail_if(test2 != &j[0]); eina_hash_free(hash); fail_if(eina_shutdown() != 1); }
int main(int argc, const char *argv[]) { Eina_Hash *phone_book = NULL; int i; int64_t entry_id = 4; char *phone = NULL; Eina_Bool r; Eina_Iterator *it; void *data; eina_init(); phone_book = eina_hash_int64_new(_phone_entry_free_cb); // Add initial entries to our hash for (i = 0; _start_entries[i].id != -1; i++) { eina_hash_add(phone_book, &_start_entries[i].id, strdup(_start_entries[i].number)); } // Look for a specific entry and get its phone number phone = eina_hash_find(phone_book, &entry_id); if (phone) { printf("Printing entry.\n"); printf("Id: %lld\n", entry_id); printf("Number: %s\n\n", phone); } // Delete this entry r = eina_hash_del(phone_book, &entry_id, NULL); printf("Hash entry successfully deleted? %d\n\n", r); // Modify the pointer data of an entry and free the old one int64_t id3 = 3; phone = eina_hash_modify(phone_book, &id3, strdup("+23 45 111-11111")); free(phone); // Modify or add an entry to the hash with eina_hash_set // Let's first add a new entry int64_t id5 = 5; eina_error_set(0); phone = eina_hash_set(phone_book, &id5, strdup("+55 01 234-56789")); if (!phone) { Eina_Error err = eina_error_get(); if (!err) { printf("No previous phone found for id5. "); printf("Creating new entry.\n"); } else printf("Error when setting phone for Raul Seixas\n"); } else { printf("Old phone for id5 was %s\n", phone); free(phone); } printf("\n"); // Now change the phone number eina_error_set(0); phone = eina_hash_set(phone_book, &id5, strdup("+55 02 222-22222")); if (phone) { printf("Changing phone for id5 to +55 02 222-22222. "); printf("Old phone was %s\n", phone); free(phone); } else { Eina_Error err = eina_error_get(); if (err) printf("Error when changing phone for id5\n"); else { printf("No previous phone found for id5. "); printf("Creating new entry.\n"); } } // There are many ways to iterate over our Phone book. // First, iterate showing the names and associated numbers. printf("List of phones:\n"); eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); printf("\n"); // Now iterate using an iterator printf("List of phones:\n"); it = eina_hash_iterator_tuple_new(phone_book); while (eina_iterator_next(it, &data)) { Eina_Hash_Tuple *t = data; const int64_t *id = t->key; const char *number = t->data; printf("%lld: %s\n", *id, number); } eina_iterator_free(it); // Always free the iterator after its use printf("\n"); // Just iterate over the keys (names) printf("List of ids in the phone book:\n"); it = eina_hash_iterator_key_new(phone_book); while (eina_iterator_next(it, &data)) { const int64_t *id = data; printf("%lld\n", *id); } eina_iterator_free(it); printf("\n"); // Just iterate over the data (numbers) printf("List of numbers in the phone book:\n"); it = eina_hash_iterator_data_new(phone_book); while (eina_iterator_next(it, &data)) { const char *number = data; printf("%s\n", number); } eina_iterator_free(it); printf("\n"); // Check how many items are in the phone book printf("There are %d items in the hash.\n\n", eina_hash_population(phone_book)); // Change the name (key) on an entry int64_t id6 = 6; eina_hash_move(phone_book, &id5, &id6); printf("List of phones after change:\n"); eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); printf("\n"); // Empty the phone book, but don't destroy it eina_hash_free_buckets(phone_book); printf("There are %d items in the hash.\n\n", eina_hash_population(phone_book)); // Phone book could still be used, but we are freeing it since we are // done for now eina_hash_free(phone_book); eina_shutdown(); }
/** * Prepare the library for use through initialization of any required connections and / or * variables * * @return boolean EINA_TRUE for success, EINA_FALSE on error. */ Eina_Bool init(DB *db, Zrpcdb_Connect_Cb cb, Ecore_Event_Handler_Cb error_cb, Ecore_Event_Handler_Cb result_cb, zentific_config *conf){ Esql *e; if ((!db) || (!cb) || (!conf)) return EINA_FALSE; eina_init(); zrpcdb_log_dom = eina_log_domain_register("zrpcdb", EINA_COLOR_ORANGE); if (zrpcdb_log_dom < 0) { fprintf(stderr, "Could not init zrpcdb log domain\n"); goto err; } INF("Initializing source 'zrpcdb'"); if (!db_config_parse(db, conf->DB_MODULES_PATH)){ ERR("Could not parse config file"); goto log; } /* make sure that we have all config options */ if ((!db->config->backend) || (!db->config->username) || (!db->config->password) || (!db->config->database)) { ERR("Missing options in db->config file. Requires HOSTNAME, USERNAME, PASSWORD, and DATABASE fields."); goto cfg; } /* FIXME: better value here */ esql_init(); if (db->config->conns < 5){ WRN("DB init: DB pool size must be at least 5. Resizing to 5."); db->config->conns = 5; } e = esql_pool_new(db->config->conns, ESQL_TYPE_MYSQL); EINA_SAFETY_ON_NULL_GOTO(e, esql); esql_data_set(e, db); db->pool_size = db->config->conns; esql_connect_callback_set(e, (Esql_Connect_Cb)init_cb, cb); esql_database_set(e, db->config->database); if (!esql_connect(e, db->config->backend, db->config->username, db->config->password)){ ERR("Could not begin database connection!"); goto conn; } db->e = e; if (!init_count++){ ZRPCDB_EVENT_ERROR = ecore_event_type_new(); zrpc_err = ecore_event_handler_add(ZRPCDB_EVENT_ERROR, error_cb, db); ZRPCDB_EVENT_RESULT = ecore_event_type_new(); zrpc_res = ecore_event_handler_add(ZRPCDB_EVENT_RESULT, result_cb, db); type_cbs = eina_hash_int64_new(NULL); db_cbs = eina_hash_int64_new(NULL); db_cb_params = eina_hash_int64_new(NULL); } // eina_log_domain_level_set("zrpcdb", db->config->loglevel); return EINA_TRUE; conn: esql_free(e); esql: esql_shutdown(); cfg: db_config_free(db->config); log: eina_log_domain_unregister(zrpcdb_log_dom); zrpcdb_log_dom = -1; err: eina_shutdown(); return EINA_FALSE; }