Load lines sequentially and insert to a structure for reference
This commit is contained in:
parent
6846385d44
commit
15bc74b80a
|
@ -42,15 +42,16 @@ extern "C" {
|
|||
|
||||
typedef struct _Elm_Code_Line
|
||||
{
|
||||
const char *content;
|
||||
const char *modified;
|
||||
Eina_File_Line content;
|
||||
Eina_File_Line modified;
|
||||
|
||||
} Elm_Code_Line;
|
||||
|
||||
typedef struct _Elm_Code_File
|
||||
{
|
||||
Elm_Code_Line *lines;
|
||||
Eina_List *lines;
|
||||
Eina_File *file;
|
||||
void *map;
|
||||
|
||||
} Elm_Code_File;
|
||||
|
||||
|
@ -108,6 +109,8 @@ EAPI const char *elm_code_filename_get(Elm_Code_File *file);
|
|||
|
||||
EAPI const char *elm_code_path_get(Elm_Code_File *file);
|
||||
|
||||
EAPI int elm_code_lines_get(Elm_Code_File *file);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -61,16 +61,40 @@ EAPI Elm_Code_File *elm_code_open(const char *path)
|
|||
{
|
||||
Elm_Code_File *ret;
|
||||
Eina_File *file;
|
||||
Eina_File_Line *line;
|
||||
Eina_Iterator *it;
|
||||
void *map;
|
||||
|
||||
file = eina_file_open(path, EINA_FALSE);
|
||||
ret = calloc(1, sizeof(ret));
|
||||
map = eina_file_map_all(file, EINA_FILE_WILLNEED);
|
||||
ret = calloc(1, sizeof(Elm_Code_File));
|
||||
ret->file = file;
|
||||
ret->map = map;
|
||||
|
||||
it = eina_file_map_lines(file);
|
||||
EINA_ITERATOR_FOREACH(it, line)
|
||||
{
|
||||
Elm_Code_Line *ecl;
|
||||
|
||||
ecl = calloc(1, sizeof(Elm_Code_Line));
|
||||
if (!ecl) continue;
|
||||
|
||||
ecl->content = *line;
|
||||
ret->lines = eina_list_append(ret->lines, ecl);
|
||||
}
|
||||
eina_iterator_free(it);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
EAPI void elm_code_close(Elm_Code_File *file)
|
||||
{
|
||||
Elm_Code_Line *l;
|
||||
|
||||
EINA_LIST_FREE(file->lines, l)
|
||||
free(l);
|
||||
|
||||
eina_file_map_free(file->file, file->map);
|
||||
eina_file_close(file->file);
|
||||
free(file);
|
||||
}
|
||||
|
@ -83,4 +107,9 @@ EAPI const char *elm_code_filename_get(Elm_Code_File *file)
|
|||
EAPI const char *elm_code_path_get(Elm_Code_File *file)
|
||||
{
|
||||
return eina_file_filename_get(file->file);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI int elm_code_lines_get(Elm_Code_File *file)
|
||||
{
|
||||
return eina_list_count(file->lines);
|
||||
}
|
||||
|
|
|
@ -19,8 +19,21 @@ START_TEST (elm_code_load)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (elm_code_load_lines)
|
||||
{
|
||||
char *path = "elm_code/tests/testfile.txt";
|
||||
Elm_Code_File *file;
|
||||
|
||||
file = elm_code_open(path);
|
||||
|
||||
ck_assert(4 == elm_code_lines_get(file));
|
||||
elm_code_close(file);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
void elm_code_test_load(TCase *tc)
|
||||
{
|
||||
tcase_add_test(tc, elm_code_load);
|
||||
tcase_add_test(tc, elm_code_load_lines);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
line 1
|
||||
line2
|
||||
|
||||
a third
|
||||
another line
|
||||
5
|
||||
|
|
Loading…
Reference in New Issue