From 0bb5201e3c29c7f871b2872f2343dc9c1c71cd08 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sun, 19 Oct 2014 22:29:44 +0100 Subject: [PATCH] Load lines sequentially and insert to a structure for reference --- elm_code/lib/Elm_Code.h | 9 +++++--- elm_code/lib/elm_code.c | 33 +++++++++++++++++++++++++++-- elm_code/tests/elm_code_test_load.c | 13 ++++++++++++ elm_code/tests/testfile.txt | 3 +-- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/elm_code/lib/Elm_Code.h b/elm_code/lib/Elm_Code.h index f560bbc..090a900 100644 --- a/elm_code/lib/Elm_Code.h +++ b/elm_code/lib/Elm_Code.h @@ -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); + /** * @} */ diff --git a/elm_code/lib/elm_code.c b/elm_code/lib/elm_code.c index 44b251a..af2b6de 100644 --- a/elm_code/lib/elm_code.c +++ b/elm_code/lib/elm_code.c @@ -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); -} \ No newline at end of file +} + +EAPI int elm_code_lines_get(Elm_Code_File *file) +{ + return eina_list_count(file->lines); +} diff --git a/elm_code/tests/elm_code_test_load.c b/elm_code/tests/elm_code_test_load.c index 926b2aa..13610db 100644 --- a/elm_code/tests/elm_code_test_load.c +++ b/elm_code/tests/elm_code_test_load.c @@ -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); } diff --git a/elm_code/tests/testfile.txt b/elm_code/tests/testfile.txt index 246d68e..8fd6a8b 100644 --- a/elm_code/tests/testfile.txt +++ b/elm_code/tests/testfile.txt @@ -1,5 +1,4 @@ line 1 line2 - +a third another line -5