list_t * process_job_results(Eina_Hash *map_histo, list_t *files, float threshold) { list_t * similar_files = NULL; list_t * lst_files = NULL; list_t * list_histo = NULL; list_t * current = NULL; while(files) { histogram_t *cached_elem = eina_hash_find(map_histo, files->value); list_histo = list_append(list_histo, cached_elem); files = files->next; } printf("Looking for similarities in %i elements with threshold %.2f\n" , eina_hash_population(map_histo), threshold); current = list_histo; while(current) { lst_files = search_similar(current->value , current->next, threshold); current = current->next; similar_files = list_append(similar_files, lst_files); } list_release(list_histo); return similar_files; }
E_API void e_datastore_del(char *key) { eina_hash_del(store, key, NULL); if (eina_hash_population(store)) return; eina_hash_free(store); store = NULL; }
list_t * push_jobs(list_t * files, clinfo_t * clinfo , Eina_Hash *map_histo) { int code; list_t * job_waits = NULL; int count = 0; int histo_done = eina_hash_population(map_histo); int global = list_size(files); while(files != NULL) { char * filename = files->value; files = files->next; histogram_t *cached_elem = eina_hash_find(map_histo, filename); if(cached_elem) continue; image_t * image = image_init(); image->path = filename; image = read_image(image); if (image == NULL) { continue; } if (image->size[0] > *clinfo->max_width || image->size[1] > *clinfo->max_heigth) { printf("Ignoring %s, width=%i, height=%i\n", filename , image->size[0], image->size[1]); image_free(image); continue; } job_t * job = job_init(); code = init_job_from_image(image, job); if(code == EXIT_FAILURE) { fprintf(stderr, "Could not init job from image %i\n" , code); return NULL; } generate_histogram(clinfo, image, job); clFlush(clinfo->command_queue); count++; if ( count > 50 ) { histogram_t *histo = wait_and_fetch_histo_from_job(job); eina_hash_add(map_histo, strdup(histo->file), histo); histo_done++; count--; if(histo_done % 50 == 0) { printf("Processed %i / %i\n", histo_done, global); write_histogram_to_file(CACHE_FILE, map_histo); } } else { job_waits = list_append(job_waits, job); } } return job_waits; }
EAPI void elixir_context_delete(const char *key) { if (!context) return ; eina_hash_del(context, key, NULL); if (eina_hash_population(context) == 0) { eina_hash_free(context); context = NULL; } }
void write_histogram_to_file(char * output_file, Eina_Hash *map_histo) { Eet_File *ef; histogram_cache_t *cache_histo; printf("Writing cache map with %i entries\n" , eina_hash_population(map_histo)); unlink(output_file); cache_histo = malloc(sizeof(histogram_cache_t)); cache_histo->map_histo = map_histo; ef = eet_open(output_file, EET_FILE_MODE_WRITE); eet_data_write(ef, _histogram_cache_descriptor , CACHE_FILE_ENTRY, cache_histo, EINA_TRUE); eet_close(ef); free(cache_histo); }
EAPI void e_stolen_win_del(Ecore_X_Window win) { E_Stolen_Window *esw; esw = eina_hash_find(_e_stolen_windows, e_util_winid_str_get(win)); if (esw) { esw->refcount--; if (esw->refcount == 0) { eina_hash_del(_e_stolen_windows, e_util_winid_str_get(win), esw); if (!eina_hash_population(_e_stolen_windows)) { eina_hash_free(_e_stolen_windows); _e_stolen_windows = NULL; } free(esw); } } return; }
static bool ewk_js_properties_enumerate(NPObject* npObject, NPIdentifier** value, uint32_t* count) { Ewk_JS_Object* object = reinterpret_cast<Ewk_JS_Object*>(npObject); Eina_Iterator* it; char* key; int i = 0; EINA_SAFETY_ON_NULL_RETURN_VAL(npObject, false); EINA_MAGIC_CHECK_OR_RETURN(object, false); *count = eina_hash_population(object->properties); *value = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier) * *count)); if (!*value) { ERR("Could not allocate memory for NPIdentifier"); return false; } it = eina_hash_iterator_key_new(object->properties); EINA_ITERATOR_FOREACH(it, key) (*value)[i++] = _NPN_GetStringIdentifier(key); eina_iterator_free(it); return true; }
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(); }
/** * @internal * @param file The file to parse * @return Returns an Eina_Hash with the contents of @a file, or NULL if the * file fails to parse or if the file doesn't exist * @brief Parses the ini file @a file into an Eina_Hash */ static Eina_Hash * efreet_ini_parse(const char *file) { Eina_Hash *data = NULL, *section = NULL; Eina_Iterator *it = NULL; Eina_File_Line *line; Eina_File *f; EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL); f = eina_file_open(file, EINA_FALSE); if (!f) return NULL; data = eina_hash_string_small_new(EINA_FREE_CB(eina_hash_free)); if (!data) goto error; /* let's make mmap safe and just get 0 pages for IO erro */ eina_mmap_safety_enabled_set(EINA_TRUE); it = eina_file_map_lines(f); if (!it) goto error; EINA_ITERATOR_FOREACH(it, line) { const char *eq; unsigned int start = 0; /* skip empty lines */ if (line->length == 0) continue; /* skip white space at start of line */ while ((start < line->length) && (isspace((unsigned char)line->start[start]))) start++; /* skip empty lines */ if (start == line->length) continue; /* skip comments */ if (line->start[start] == '#') continue; /* new section */ if (line->start[start] == '[') { const char *head_start; const char *head_end; head_start = &(line->start[start]) + 1; head_end = memchr(line->start, ']', line->length); if (head_end) { char *header; size_t len; len = head_end - head_start + 1; header = alloca(len); memcpy(header, head_start, len - 1); header[len - 1] = '\0'; section = eina_hash_string_small_new(EINA_FREE_CB(eina_stringshare_del)); eina_hash_del_by_key(data, header); eina_hash_add(data, header, section); } else { /* invalid file - skip line? or refuse to parse file? */ /* just printf for now till we figure out what to do */ // ERR("Invalid file (%s) (missing ] on group name)", file); } continue; } if (!section) { INF("Invalid file (%s) (missing section)", file); goto error; } eq = memchr(line->start, '=', line->length); if (eq) { const char *key_start, *key_end; const char *value_start, *value_end; char *key, *value; size_t len; key_start = &(line->start[start]); key_end = eq - 1; /* trim whitespace from end of key */ while ((isspace((unsigned char)*key_end)) && (key_end > key_start)) key_end--; key_end++; /* make sure we have a key */ if (key_start == key_end) continue; value_start = eq + 1; value_end = line->end; /* line->end points to char after '\n' or '\r' */ value_end--; /* trim whitespace from end of value */ while ((isspace((unsigned char)*value_end)) && (value_end > value_start)) value_end--; value_end++; /* trim whitespace from start of value */ while ((isspace((unsigned char)*value_start)) && (value_start < value_end)) value_start++; len = key_end - key_start + 1; key = alloca(len); memcpy(key, key_start, len - 1); key[len - 1] = '\0'; /* empty value allowed */ if (value_end == value_start) { eina_hash_del_by_key(section, key); eina_hash_add(section, key, ""); } else { len = value_end - value_start + 1; value = alloca(len); memcpy(value, value_start, len - 1); value[len - 1] = '\0'; eina_hash_del_by_key(section, key); eina_hash_add(section, key, efreet_ini_unescape(value)); } } else { /* invalid file... */ INF("Invalid file (%s) (missing = from key=value pair)", file); goto error; } } eina_iterator_free(it); eina_file_close(f); #if 0 if (!eina_hash_population(data)) { eina_hash_free(data); return NULL; } #endif return data; error: if (data) eina_hash_free(data); if (it) eina_iterator_free(it); eina_file_close(f); return NULL; }
BorghenSize borghen_backend_size(BorghenObject hash) { return eina_hash_population(hash); }
Ewk_JS_Object* ewk_js_object_new(const Ewk_JS_Class_Meta* jsMetaClass) { #if ENABLE(NETSCAPE_PLUGIN_API) Ewk_JS_Object* object; EINA_SAFETY_ON_NULL_RETURN_VAL(jsMetaClass, 0); object = static_cast<Ewk_JS_Object*>(malloc(sizeof(Ewk_JS_Object))); if (!object) { ERR("Could not allocate memory for ewk_js_object"); return 0; } EINA_MAGIC_SET(object, EWK_JS_OBJECT_MAGIC); object->cls = ewk_js_class_new(jsMetaClass); object->view = 0; object->name = 0; object->type = EWK_JS_OBJECT_OBJECT; if (eina_hash_population(object->cls->properties) < 25) object->properties = eina_hash_string_small_new(reinterpret_cast<Eina_Free_Cb>(ewk_js_variant_free)); else object->properties = eina_hash_string_superfast_new(reinterpret_cast<Eina_Free_Cb>(ewk_js_variant_free)); for (int i = 0; object->cls->meta->properties && object->cls->meta->properties[i].name; i++) { Ewk_JS_Property prop = object->cls->meta->properties[i]; const char* key = object->cls->meta->properties[i].name; Ewk_JS_Variant* value = static_cast<Ewk_JS_Variant*>(malloc(sizeof(Ewk_JS_Variant))); if (!value) { ERR("Could not allocate memory for ewk_js_variant"); goto error; } if (prop.get) prop.get(object, key, value); else { value->type = prop.value.type; switch (value->type) { case EWK_JS_VARIANT_VOID: case EWK_JS_VARIANT_NULL: value->value.o = 0; break; case EWK_JS_VARIANT_STRING: value->value.s = eina_stringshare_add(prop.value.value.s); break; case EWK_JS_VARIANT_BOOL: value->value.b = prop.value.value.b; break; case EWK_JS_VARIANT_INT32: value->value.i = prop.value.value.i; break; case EWK_JS_VARIANT_DOUBLE: value->value.d = prop.value.value.d; break; case EWK_JS_VARIANT_OBJECT: value->value.o = prop.value.value.o; break; } } eina_hash_add(object->properties, key, value); } object->base.object.referenceCount = 1; object->base.object._class = &EWK_NPCLASS; return object; error: ewk_js_object_free(object); return 0; #else UNUSED_PARAM(jsMetaClass); return 0; #endif }
static Ewk_JS_Object* ewk_js_npobject_to_object(NPObject* npObject) { NPIdentifier* values; uint32_t np_props_count; Ewk_JS_Class* cls; Ewk_JS_Object* object; Eina_Iterator* it; Ewk_JS_Property* prop; JavaScriptObject* jso; if (EINA_MAGIC_CHECK(reinterpret_cast<Ewk_JS_Object*>(npObject), EWK_JS_OBJECT_MAGIC)) return reinterpret_cast<Ewk_JS_Object*>(npObject); if (!_NPN_Enumerate(0, npObject, &values, &np_props_count)) return 0; cls = static_cast<Ewk_JS_Class*>(malloc(sizeof(Ewk_JS_Class))); if (!cls) { ERR("Could not allocate memory for ewk_js_class"); return 0; } cls->meta = 0; Ewk_JS_Default def = { ewk_js_npobject_property_set, ewk_js_npobject_property_get, ewk_js_npobject_property_del }; cls->default_prop = def; cls->methods = eina_hash_pointer_new(0); cls->properties = eina_hash_pointer_new(reinterpret_cast<Eina_Free_Cb>(ewk_js_property_free)); for (uint32_t i = 0; i < np_props_count; i++) { if (_NPN_HasProperty(0, npObject, values[i])) { NPVariant var; Ewk_JS_Property* prop = static_cast<Ewk_JS_Property*>(calloc(sizeof(Ewk_JS_Property), 1)); if (!prop) { ERR("Could not allocate memory for ewk_js_property"); goto error; } _NPN_GetProperty(0, npObject, values[i], &var); ewk_js_npvariant_to_variant(&(prop->value), &var); prop->name = _NPN_UTF8FromIdentifier(values[i]); eina_hash_add(cls->properties, values[i], prop); } } // Can't use ewk_js_object_new(cls) because it expects cls->meta to exist. object = static_cast<Ewk_JS_Object*>(malloc(sizeof(Ewk_JS_Object))); if (!object) { ERR("Could not allocate memory for ewk_js_object"); goto error; } free(values); EINA_MAGIC_SET(object, EWK_JS_OBJECT_MAGIC); object->name = 0; object->cls = cls; object->view = 0; jso = reinterpret_cast<JavaScriptObject*>(npObject); if (!strcmp("Array", jso->imp->methodTable()->className(jso->imp).ascii().data())) object->type = EWK_JS_OBJECT_ARRAY; else if (!strcmp("Function", jso->imp->methodTable()->className(jso->imp).ascii().data())) object->type = EWK_JS_OBJECT_FUNCTION; else object->type = EWK_JS_OBJECT_OBJECT; if (eina_hash_population(cls->properties) < 25) object->properties = eina_hash_string_small_new(0); else object->properties = eina_hash_string_superfast_new(0); it = eina_hash_iterator_data_new(cls->properties); EINA_ITERATOR_FOREACH(it, prop) { const char* key = prop->name; Ewk_JS_Variant* value = &prop->value; eina_hash_add(object->properties, key, value); } eina_iterator_free(it); object->base = *reinterpret_cast<JavaScriptObject*>(npObject); return object; error: ewk_js_class_free(cls); free(values); return 0; }
/** * @internal * @param file The file to parse * @return Returns an Eina_Hash with the contents of @a file, or NULL if the * file fails to parse or if the file doesn't exist * @brief Parses the ini file @a file into an Eina_Hash */ static Eina_Hash * efreet_ini_parse(const char *file) { const char *buffer, *line_start; FILE *f; Eina_Hash *data, *section = NULL; struct stat file_stat; int line_length, left; if (!file) return NULL; f = fopen(file, "rb"); if (!f) return NULL; if (fstat(fileno(f), &file_stat) || (file_stat.st_size < 1)) { fclose(f); return NULL; } if (!S_ISREG(file_stat.st_mode)) /* if not a regular file - close */ { fclose(f); return NULL; } left = file_stat.st_size; buffer = mmap(NULL, left, PROT_READ, MAP_SHARED, fileno(f), 0); if (buffer == MAP_FAILED) { fclose(f); return NULL; } data = eina_hash_string_small_new(EINA_FREE_CB(eina_hash_free)); line_start = buffer; while (left > 0) { int sep; /* find the end of line */ for (line_length = 0; (line_length < left) && (line_start[line_length] != '\n'); line_length++) ; /* check for all white space */ while (isspace(line_start[0]) && (line_length > 0)) { line_start++; line_length--; } /* skip empty lines and comments */ if ((line_length == 0) || (line_start[0] == '\r') || (line_start[0] == '\n') || (line_start[0] == '#') || (line_start[0] == '\0')) goto next_line; /* new section */ if (line_start[0] == '[') { int header_length; /* find the ']' */ for (header_length = 1; (header_length < line_length) && (line_start[header_length] != ']'); ++header_length) ; if (line_start[header_length] == ']') { const char *header; header = alloca(header_length * sizeof(unsigned char)); if (!header) goto next_line; memcpy((char*)header, line_start + 1, header_length - 1); ((char*)header)[header_length - 1] = '\0'; section = eina_hash_string_small_new(EINA_FREE_CB(eina_stringshare_del)); eina_hash_del_by_key(data, header); // if (old) INF("[efreet] Warning: duplicate section '%s' " // "in file '%s'", header, file); eina_hash_add(data, header, section); } else { /* invalid file - skip line? or refuse to parse file? */ /* just printf for now till we figure out what to do */ // printf("Invalid file (%s) (missing ] on group name)\n", file); } goto next_line; } if (!section) { // INF("Invalid file (%s) (missing section)", file); goto next_line; } /* find for '=' */ for (sep = 0; (sep < line_length) && (line_start[sep] != '='); ++sep) ; if (sep < line_length) { char *key, *value; int key_end, value_start, value_end; /* trim whitespace from end of key */ for (key_end = sep - 1; (key_end > 0) && isspace(line_start[key_end]); --key_end) ; if (!isspace(line_start[key_end])) key_end++; /* trim whitespace from start of value */ for (value_start = sep + 1; (value_start < line_length) && isspace(line_start[value_start]); ++value_start) ; /* trim \n off of end of value */ for (value_end = line_length; (value_end > value_start) && ((line_start[value_end] == '\n') || (line_start[value_end] == '\r')); --value_end) ; if (line_start[value_end] != '\n' && line_start[value_end] != '\r' && value_end < line_length) value_end++; /* make sure we have a key. blank values are allowed */ if (key_end == 0) { /* invalid file... */ // INF("Invalid file (%s) (invalid key=value pair)", file); goto next_line; } key = alloca(key_end + 1); value = alloca(value_end - value_start + 1); if (!key || !value) goto next_line; memcpy(key, line_start, key_end); key[key_end] = '\0'; memcpy(value, line_start + value_start, value_end - value_start); value[value_end - value_start] = '\0'; eina_hash_del_by_key(section, key); eina_hash_add(section, key, efreet_ini_unescape(value)); } // else // { // /* invalid file... */ // INF("Invalid file (%s) (missing = from key=value pair)", file); // } next_line: left -= line_length + 1; line_start += line_length + 1; } munmap((char*) buffer, file_stat.st_size); fclose(f); #if 0 if (!eina_hash_population(data)) { eina_hash_free(data); return NULL; } #endif return data; }