From 83c0e231ecdcc499c44b716afab4a6fcc7e39256 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sun, 26 Oct 2014 22:47:27 +0000 Subject: [PATCH] Add basic structure for elm_code object - namespacing the elm_code_file properly to keep it clean --- elm_code/lib/Elm_Code.h | 54 ++++--------- elm_code/lib/Makefile.am | 5 +- elm_code/lib/elm_code.c | 88 +++------------------ elm_code/lib/elm_code_file.c | 98 ++++++++++++++++++++++++ elm_code/lib/elm_code_file.h | 71 +++++++++++++++++ elm_code/tests/Makefile.am | 3 +- elm_code/tests/elm_code_file_test_load.c | 66 ++++++++++++++++ elm_code/tests/elm_code_suite.c | 3 +- elm_code/tests/elm_code_suite.h | 3 +- elm_code/tests/elm_code_test_basic.c | 25 ++++++ elm_code/tests/elm_code_test_load.c | 66 ---------------- src/lib/Makefile.am | 5 +- 12 files changed, 298 insertions(+), 189 deletions(-) create mode 100644 elm_code/lib/elm_code_file.c create mode 100644 elm_code/lib/elm_code_file.h create mode 100644 elm_code/tests/elm_code_file_test_load.c create mode 100644 elm_code/tests/elm_code_test_basic.c delete mode 100644 elm_code/tests/elm_code_test_load.c diff --git a/elm_code/lib/Elm_Code.h b/elm_code/lib/Elm_Code.h index 571434c..d105310 100644 --- a/elm_code/lib/Elm_Code.h +++ b/elm_code/lib/Elm_Code.h @@ -5,6 +5,8 @@ #include +#include + #ifdef EAPI # undef EAPI #endif @@ -37,22 +39,15 @@ extern "C" { /** * @file - * @brief These routines are used for interacting with files using Elm Code. + * @brief These routines are used for loading Elm Code widgets. */ -typedef struct _Elm_Code_Line +typedef struct _Elm_Code { - char *content; - unsigned int number; + Elm_Code_File *file; + Eina_List *widgets; -} Elm_Code_Line; - -typedef struct _Elm_Code_File -{ - Eina_List *lines; - Eina_File *file; - -} Elm_Code_File; +} Elm_Code; /** * @brief Init / shutdown functions. @@ -101,40 +96,21 @@ EAPI int elm_code_init(void); EAPI int elm_code_shutdown(void); /** - * @} + * Create a new Elm Code instance for an existing file * - * @brief File handling functions. - * @defgroup File I/O at a file level - * - * @{ - * - * Functions for file handling within elm code. + * This method creates a new Elm Code instance backing to the specified file. + * Once an Elm Code has been created you can create widgets that render the content. * + * "return an allocated Elm_Code that references the given file */ - -EAPI Elm_Code_File *elm_code_open(const char *path); - -EAPI void elm_code_close(Elm_Code_File *file); - -EAPI const char *elm_code_filename_get(Elm_Code_File *file); - -EAPI const char *elm_code_path_get(Elm_Code_File *file); +EAPI Elm_Code *elm_code_create(Elm_Code_File *file); /** - * @} - * - * @brief Content functions. - * @defgroup Content Functions for accessing file content - * - * @{ - * - * File content handling functions. + * Free an Elm Code instance * + * Releases the resources retained by the code instance and any files it references. */ - -EAPI unsigned int elm_code_lines_get(Elm_Code_File *file); - -EAPI char *elm_code_line_content_get(Elm_Code_File *file, int line); +EAPI void elm_code_free(Elm_Code *code); /** * @} diff --git a/elm_code/lib/Makefile.am b/elm_code/lib/Makefile.am index 468b75a..43b95b7 100644 --- a/elm_code/lib/Makefile.am +++ b/elm_code/lib/Makefile.am @@ -10,10 +10,13 @@ AM_CPPFLAGS = \ lib_LTLIBRARIES = libelm_code.la -includes_HEADERS = Elm_Code.h +includes_HEADERS = \ +elm_code_file.h \ +Elm_Code.h includesdir = $(includedir)/edi-@VMAJ@ libelm_code_la_SOURCES = \ +elm_code_file.c \ elm_code.c libelm_code_la_LIBADD = @EFL_LIBS@ -lm libelm_code_la_LDFLAGS = @EFL_LTLIBRARY_FLAGS@ diff --git a/elm_code/lib/elm_code.c b/elm_code/lib/elm_code.c index 4dd8f95..932168e 100644 --- a/elm_code/lib/elm_code.c +++ b/elm_code/lib/elm_code.c @@ -57,92 +57,22 @@ elm_code_shutdown(void) return _elm_code_init; } -static Elm_Code_Line *_elm_code_blank_create(int line) +EAPI Elm_Code * +elm_code_create(Elm_Code_File *file) { - Elm_Code_Line *ecl; + Elm_Code *ret; - 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; - unsigned int lastindex; - - file = eina_file_open(path, EINA_FALSE); - ret = calloc(1, sizeof(Elm_Code_File)); + ret = calloc(1, sizeof(Elm_Code)); ret->file = file; - lastindex = 1; - - it = eina_file_map_lines(file); - EINA_ITERATOR_FOREACH(it, line) - { - Elm_Code_Line *ecl; - - /* 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 = 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); return ret; } -EAPI void elm_code_close(Elm_Code_File *file) +EAPI void +elm_code_free(Elm_Code *code) { - Elm_Code_Line *l; + if (code->file) + free(code->file); - EINA_LIST_FREE(file->lines, l) - { - if (l->content) - free(l->content); - free(l); - } - - eina_file_close(file->file); - free(file); -} - -EAPI const char *elm_code_filename_get(Elm_Code_File *file) -{ - return basename((char *)eina_file_filename_get(file->file)); -} - -EAPI const char *elm_code_path_get(Elm_Code_File *file) -{ - return eina_file_filename_get(file->file); -} - -EAPI unsigned 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; + free(code); } diff --git a/elm_code/lib/elm_code_file.c b/elm_code/lib/elm_code_file.c new file mode 100644 index 0000000..3ad76b9 --- /dev/null +++ b/elm_code/lib/elm_code_file.c @@ -0,0 +1,98 @@ +#ifdef HAVE_CONFIG +# include "config.h" +#endif + +#include "Elm_Code.h" +#include "elm_code_file.h" + +#include "elm_code_private.h" + +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_file_open(const char *path) +{ + Elm_Code_File *ret; + Eina_File *file; + Eina_File_Line *line; + Eina_Iterator *it; + unsigned int lastindex; + + file = eina_file_open(path, EINA_FALSE); + ret = calloc(1, sizeof(Elm_Code_File)); + ret->file = file; + lastindex = 1; + + it = eina_file_map_lines(file); + EINA_ITERATOR_FOREACH(it, line) + { + Elm_Code_Line *ecl; + + /* 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 = 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); + + return ret; +} + +EAPI void elm_code_file_close(Elm_Code_File *file) +{ + Elm_Code_Line *l; + + EINA_LIST_FREE(file->lines, l) + { + if (l->content) + free(l->content); + free(l); + } + + eina_file_close(file->file); + free(file); +} + +EAPI const char *elm_code_file_filename_get(Elm_Code_File *file) +{ + return basename((char *)eina_file_filename_get(file->file)); +} + +EAPI const char *elm_code_file_path_get(Elm_Code_File *file) +{ + return eina_file_filename_get(file->file); +} + +EAPI unsigned int elm_code_file_lines_get(Elm_Code_File *file) +{ + return eina_list_count(file->lines); +} + +EAPI char *elm_code_file_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/lib/elm_code_file.h b/elm_code/lib/elm_code_file.h new file mode 100644 index 0000000..7ad7ff8 --- /dev/null +++ b/elm_code/lib/elm_code_file.h @@ -0,0 +1,71 @@ +#ifndef ELM_CODE_FILE_H_ +# define ELM_CODE_FILE_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file + * @brief These routines are used for interacting with files using Elm Code. + */ + +typedef struct _Elm_Code_Line +{ + char *content; + unsigned int number; + +} Elm_Code_Line; + +typedef struct _Elm_Code_File +{ + Eina_List *lines; + Eina_File *file; + +} Elm_Code_File; + +/** + * @brief File handling functions. + * @defgroup File I/O at a file level + * + * @{ + * + * Functions for file handling within elm code. + * + */ + +EAPI Elm_Code_File *elm_code_file_open(const char *path); + +EAPI void elm_code_file_close(Elm_Code_File *file); + +EAPI const char *elm_code_file_filename_get(Elm_Code_File *file); + +EAPI const char *elm_code_file_path_get(Elm_Code_File *file); + +/** + * @} + * + * @brief Content functions. + * @defgroup Content Functions for accessing file content + * + * @{ + * + * File content handling functions. + * + */ + +EAPI unsigned int elm_code_file_lines_get(Elm_Code_File *file); + +EAPI char *elm_code_file_line_content_get(Elm_Code_File *file, int line); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* ELM_CODE_FILE_H_ */ diff --git a/elm_code/tests/Makefile.am b/elm_code/tests/Makefile.am index 5652799..a874f1c 100644 --- a/elm_code/tests/Makefile.am +++ b/elm_code/tests/Makefile.am @@ -4,7 +4,8 @@ if EFL_HAVE_TESTS check_PROGRAMS = elm_code_suite elm_code_suite_SOURCES = \ -elm_code_test_load.c \ +elm_code_file_test_load.c \ +elm_code_test_basic.c \ elm_code_suite.c elm_code_suite_CPPFLAGS = -I$(top_builddir)/elm_code/lib/ \ diff --git a/elm_code/tests/elm_code_file_test_load.c b/elm_code/tests/elm_code_file_test_load.c new file mode 100644 index 0000000..d7933ac --- /dev/null +++ b/elm_code/tests/elm_code_file_test_load.c @@ -0,0 +1,66 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "elm_code_suite.h" + +START_TEST (elm_code_file_load) +{ + char *path = "elm_code/tests/testfile.txt"; + char real[EINA_PATH_MAX]; + Elm_Code_File *file; + + file = elm_code_file_open(path); + realpath(path, real); + + ck_assert_str_eq(basename(path), elm_code_file_filename_get(file)); + ck_assert_str_eq(real, elm_code_file_path_get(file)); + elm_code_file_close(file); +} +END_TEST + +START_TEST (elm_code_file_load_lines) +{ + char *path = "elm_code/tests/testfile.txt"; + Elm_Code_File *file; + + file = elm_code_file_open(path); + + ck_assert_uint_eq(4, elm_code_file_lines_get(file)); + elm_code_file_close(file); +} +END_TEST + +START_TEST (elm_code_file_load_blank_lines) +{ + char *path = "elm_code/tests/testfile-withblanks.txt"; + Elm_Code_File *file; + + file = elm_code_file_open(path); + + ck_assert_uint_eq(8, elm_code_file_lines_get(file)); + elm_code_file_close(file); +} +END_TEST + +START_TEST (elm_code_file_load_content) +{ + char *path = "elm_code/tests/testfile.txt"; + Elm_Code_File *file; + + file = elm_code_file_open(path); + + ck_assert_str_eq("line2", elm_code_file_line_content_get(file, 2 - 1)); + ck_assert_str_eq("another line", elm_code_file_line_content_get(file, 4 - 1)); + elm_code_file_close(file); +} +END_TEST + +void elm_code_file_test_load(TCase *tc) +{ + tcase_add_test(tc, elm_code_file_load); + tcase_add_test(tc, elm_code_file_load_lines); + tcase_add_test(tc, elm_code_file_load_blank_lines); + tcase_add_test(tc, elm_code_file_load_content); +} + diff --git a/elm_code/tests/elm_code_suite.c b/elm_code/tests/elm_code_suite.c index 7b74ec2..9b869e8 100644 --- a/elm_code/tests/elm_code_suite.c +++ b/elm_code/tests/elm_code_suite.c @@ -13,7 +13,8 @@ static const struct { const char *name; void (*build)(TCase *tc); } tests[] = { - { "load", elm_code_test_load }, + { "file_load", elm_code_file_test_load }, + { "basic", elm_code_test_basic }, }; START_TEST(elm_code_initialization) diff --git a/elm_code/tests/elm_code_suite.h b/elm_code/tests/elm_code_suite.h index 2cdf523..2ae75a1 100644 --- a/elm_code/tests/elm_code_suite.h +++ b/elm_code/tests/elm_code_suite.h @@ -5,6 +5,7 @@ #include -void elm_code_test_load(TCase *tc); +void elm_code_file_test_load(TCase *tc); +void elm_code_test_basic(TCase *tc); #endif /* _EDLM_CODE_SUITE_H */ diff --git a/elm_code/tests/elm_code_test_basic.c b/elm_code/tests/elm_code_test_basic.c new file mode 100644 index 0000000..f127cfb --- /dev/null +++ b/elm_code/tests/elm_code_test_basic.c @@ -0,0 +1,25 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "elm_code_suite.h" + +START_TEST (elm_code_create_test) +{ + char *path = "elm_code/tests/testfile.txt"; + Elm_Code_File *file; + Elm_Code *code; + + file = elm_code_file_open(path); + code = elm_code_create(file); + + ck_assert(code); + elm_code_free(code); +} +END_TEST + +void elm_code_test_basic(TCase *tc) +{ + tcase_add_test(tc, elm_code_create_test); +} + diff --git a/elm_code/tests/elm_code_test_load.c b/elm_code/tests/elm_code_test_load.c deleted file mode 100644 index 7dabf18..0000000 --- a/elm_code/tests/elm_code_test_load.c +++ /dev/null @@ -1,66 +0,0 @@ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "elm_code_suite.h" - -START_TEST (elm_code_load) -{ - char *path = "elm_code/tests/testfile.txt"; - char real[EINA_PATH_MAX]; - Elm_Code_File *file; - - file = elm_code_open(path); - realpath(path, real); - - ck_assert_str_eq(basename(path), elm_code_filename_get(file)); - ck_assert_str_eq(real, elm_code_path_get(file)); - elm_code_close(file); -} -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_uint_eq(4, elm_code_lines_get(file)); - elm_code_close(file); -} -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_uint_eq(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/src/lib/Makefile.am b/src/lib/Makefile.am index 39bb0d0..760c16d 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -10,7 +10,10 @@ AM_CPPFLAGS = \ lib_LTLIBRARIES = libedi.la -includes_HEADERS = Edi.h +includes_HEADERS = \ +edi_builder.h \ +edi_path.h \ +Edi.h includesdir = $(includedir)/edi-@VMAJ@ libedi_la_SOURCES = \