From 6cf6df3886fef15f32cab2cee7634ad11213eee4 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sun, 19 Oct 2014 22:33:49 +0100 Subject: [PATCH] Load the content and verify from our test files. Includes an eina_file_map_lines workaround for blank lines --- elm_code/lib/Elm_Code.h | 7 ++-- elm_code/lib/elm_code.c | 49 +++++++++++++++++++++----- elm_code/tests/elm_code_test_load.c | 27 ++++++++++++++ elm_code/tests/testfile-withblanks.txt | 8 +++++ 4 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 elm_code/tests/testfile-withblanks.txt diff --git a/elm_code/lib/Elm_Code.h b/elm_code/lib/Elm_Code.h index 090a900..e03aa26 100644 --- a/elm_code/lib/Elm_Code.h +++ b/elm_code/lib/Elm_Code.h @@ -42,8 +42,8 @@ extern "C" { typedef struct _Elm_Code_Line { - Eina_File_Line content; - Eina_File_Line modified; + char *content; + unsigned int number; } Elm_Code_Line; @@ -51,7 +51,6 @@ typedef struct _Elm_Code_File { Eina_List *lines; Eina_File *file; - void *map; } Elm_Code_File; @@ -111,6 +110,8 @@ EAPI const char *elm_code_path_get(Elm_Code_File *file); EAPI int elm_code_lines_get(Elm_Code_File *file); +EAPI char *elm_code_line_content_get(Elm_Code_File *file, int line); + /** * @} */ diff --git a/elm_code/lib/elm_code.c b/elm_code/lib/elm_code.c index af2b6de..66cb04c 100644 --- a/elm_code/lib/elm_code.c +++ b/elm_code/lib/elm_code.c @@ -57,29 +57,51 @@ elm_code_shutdown(void) return _elm_code_init; } +static Elm_Code_Line *_elm_code_blank_create(int line) +{ + Elm_Code_Line *ecl; + + ecl = calloc(1, sizeof(Elm_Code_Line)); + if (!ecl) return NULL; + + ecl->number = line; + return ecl; +} + 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; + unsigned int lastindex; file = eina_file_open(path, EINA_FALSE); - map = eina_file_map_all(file, EINA_FILE_WILLNEED); ret = calloc(1, sizeof(Elm_Code_File)); ret->file = file; - ret->map = map; + lastindex = 1; it = eina_file_map_lines(file); EINA_ITERATOR_FOREACH(it, line) { Elm_Code_Line *ecl; - ecl = calloc(1, sizeof(Elm_Code_Line)); + /* Working around the issue that eina_file_map_lines does not trigger an item for empty lines */ + while (lastindex < line->index - 1) + { + ecl = _elm_code_blank_create(++lastindex); + if (!ecl) continue; + + ret->lines = eina_list_append(ret->lines, ecl); + } + + ecl = _elm_code_blank_create(lastindex = line->index); if (!ecl) continue; - ecl->content = *line; + ecl->content = malloc(sizeof(char) * (line->length + 1)); + strncpy(ecl->content, line->start, line->length); + ecl->content[line->length] = 0; + ret->lines = eina_list_append(ret->lines, ecl); } eina_iterator_free(it); @@ -92,16 +114,19 @@ EAPI void elm_code_close(Elm_Code_File *file) Elm_Code_Line *l; EINA_LIST_FREE(file->lines, l) - free(l); + { + if (l->content) + free(l->content); + free(l); + } - eina_file_map_free(file->file, file->map); eina_file_close(file->file); free(file); } EAPI const char *elm_code_filename_get(Elm_Code_File *file) { - return basename(eina_file_filename_get(file->file)); + return basename((char *)eina_file_filename_get(file->file)); } EAPI const char *elm_code_path_get(Elm_Code_File *file) @@ -113,3 +138,11 @@ EAPI int elm_code_lines_get(Elm_Code_File *file) { return eina_list_count(file->lines); } + +EAPI char *elm_code_line_content_get(Elm_Code_File *file, int number) +{ + Elm_Code_Line *line; + + line = eina_list_nth(file->lines, number); + return line->content; +} diff --git a/elm_code/tests/elm_code_test_load.c b/elm_code/tests/elm_code_test_load.c index 13610db..2431603 100644 --- a/elm_code/tests/elm_code_test_load.c +++ b/elm_code/tests/elm_code_test_load.c @@ -31,9 +31,36 @@ START_TEST (elm_code_load_lines) } END_TEST +START_TEST (elm_code_load_blank_lines) +{ + char *path = "elm_code/tests/testfile-withblanks.txt"; + Elm_Code_File *file; + + file = elm_code_open(path); + + ck_assert(8 == elm_code_lines_get(file)); + elm_code_close(file); +} +END_TEST + +START_TEST (elm_code_load_content) +{ + char *path = "elm_code/tests/testfile.txt"; + Elm_Code_File *file; + + file = elm_code_open(path); + + ck_assert_str_eq("line2", elm_code_line_content_get(file, 2 - 1)); + ck_assert_str_eq("another line", elm_code_line_content_get(file, 4 - 1)); + 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); + tcase_add_test(tc, elm_code_load_blank_lines); + tcase_add_test(tc, elm_code_load_content); } diff --git a/elm_code/tests/testfile-withblanks.txt b/elm_code/tests/testfile-withblanks.txt new file mode 100644 index 0000000..0f2ead3 --- /dev/null +++ b/elm_code/tests/testfile-withblanks.txt @@ -0,0 +1,8 @@ +line 1 +line2 + +another link + + +double blank +8