Add basic structure for elm_code object - namespacing the elm_code_file properly to keep it clean

This commit is contained in:
Andy Williams 2014-10-26 22:47:27 +00:00
parent 3d35a3f949
commit 83c0e231ec
12 changed files with 298 additions and 189 deletions

View File

@ -5,6 +5,8 @@
#include <Eina.h>
#include <elm_code_file.h>
#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);
/**
* @}

View File

@ -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@

View File

@ -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);
}

View File

@ -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;
}

View File

@ -0,0 +1,71 @@
#ifndef ELM_CODE_FILE_H_
# define ELM_CODE_FILE_H_
#include <Eina.h>
#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_ */

View File

@ -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/ \

View File

@ -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);
}

View File

@ -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)

View File

@ -5,6 +5,7 @@
#include <Elm_Code.h>
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 */

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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 = \