EAPI Elm_Code_File *elm_code_file_open(Elm_Code *code, const char *path) { Elm_Code_File *ret; Eina_File *file; Eina_File_Line *line; Eina_Iterator *it; unsigned int lastindex; ret = elm_code_file_new(code); file = eina_file_open(path, EINA_FALSE); ret->file = file; ret->mime = efreet_mime_type_get(path); lastindex = 1; ret->map = eina_file_map_all(file, EINA_FILE_POPULATE); it = eina_file_map_lines(file); EINA_ITERATOR_FOREACH(it, line) { Elm_Code_Line *ecl; if (lastindex == 1) ret->line_ending = _elm_code_line_ending_get(line->start + line->length); /* Working around the issue that eina_file_map_lines does not trigger an item for empty lines */ /* This was fixed in 1.13.99 so once we depend on 1.14 minimum this can go */ while (lastindex < line->index - 1) { ecl = _elm_code_file_line_blank_create(ret, ++lastindex, NULL); if (!ecl) continue; ret->lines = eina_list_append(ret->lines, ecl); } _elm_code_file_line_insert_data(ret, line->start, line->length, lastindex = line->index, EINA_TRUE, NULL); }
static void read_watch_file(const char *file) { Eina_File *f; Eina_Iterator *it; Eina_File_Line *ln; Eio_Monitor *mon; Eina_List *r = NULL; f = eina_file_open(file, EINA_FALSE); if (!f) return ; it = eina_file_map_lines(f); if (!it) goto err; EINA_ITERATOR_FOREACH(it, ln) { const char *path; Eina_Bool do_append = EINA_TRUE; if (ln->length < 4) continue ; if (anotate) { path = eina_stringshare_add_length(ln->start + 3, ln->length - 3); fprintf(stdout, "%c: %s\n", *ln->start, path); if (*ln->start == 'O') do_append = EINA_FALSE; } else { path = eina_stringshare_add_length(ln->start, ln->length); } if (do_append) r = eina_list_append(r, eio_monitor_add(path)); eina_stringshare_del(path); } eina_iterator_free(it); EINA_LIST_FREE(watching, mon) eio_monitor_del(mon); watching = r; err: eina_file_close(f); }
/** * @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; }
int main(int argc, char **argv) { Eina_File *f; Eina_Iterator *it; Eina_File_Line *l; GeoIP *geo; Community_Country *country; unsigned long long correct = 0; unsigned long long lines = 0; int i; if (argc != 2) return -1; eina_init(); community = eina_hash_stringshared_new(NULL); days = eina_hash_new(_community_day_key_length, _community_day_key_cmp, _community_day_key_hash, NULL, 5); countries = eina_hash_string_superfast_new(NULL); geo = GeoIP_new(GEOIP_STANDARD); memset(months, 0, sizeof (months)); /* Read real name country to tld file */ f = eina_file_open("country_tld.csv", EINA_FALSE); if (!f) return -1; it = eina_file_map_lines(f); EINA_ITERATOR_FOREACH(it, l) { const char *s; int i; s = memchr(l->start, ',', l->length); if (!s) continue ; country = calloc(1, sizeof (Community_Country)); country->tld = _eina_stringshare_up(l->start, s - l->start); country->country = _eina_stringshare_up(s + 1, l->length - (s - l->start + 1)); country->access = eina_hash_stringshared_new(NULL); eina_hash_direct_add(countries, country->tld, country); } eina_iterator_free(it); eina_file_close(f); /* Read population information */ f = eina_file_open("country_population.csv", EINA_FALSE); if (!f) return -1; it = eina_file_map_lines(f); EINA_ITERATOR_FOREACH(it, l) { Eina_Iterator *it2; const char *s; const char *country_name; const char *r; for (s = l->start; s < l->end; s++) { const char *convert = VIGRID_NUMBER_SEARCH; r = strchr(convert, *s); if (r) break ; } if (!r) continue; country = NULL; country_name = _eina_stringshare_up(l->start, s - l->start - 1); it2 = eina_hash_iterator_data_new(countries); EINA_ITERATOR_FOREACH(it2, country) { if (country->country == country_name) { unsigned long long offset = s - l->start; country->population = _eina_file_int_get(l, &offset); break; } } eina_iterator_free(it2); }