forked from enlightenment/edi
Allow appending of lines to an Elm_Code_File, allow Elm_Code_File objects to be created empty without reading from a filesystem source
This commit is contained in:
parent
83c0e231ec
commit
0ad14fac14
|
@ -18,6 +18,29 @@ static Elm_Code_Line *_elm_code_blank_create(int line)
|
|||
return ecl;
|
||||
}
|
||||
|
||||
static void _elm_code_file_line_append_data(Elm_Code_File *file, const char *content, int length, int row)
|
||||
{
|
||||
Elm_Code_Line *line;
|
||||
|
||||
line = _elm_code_blank_create(row);
|
||||
if (!line) return;
|
||||
|
||||
line->content = malloc(sizeof(char) * (length + 1));
|
||||
strncpy(line->content, content, length);
|
||||
line->content[length] = 0;
|
||||
|
||||
file->lines = eina_list_append(file->lines, line);
|
||||
}
|
||||
|
||||
EAPI Elm_Code_File *elm_code_file_new()
|
||||
{
|
||||
Elm_Code_File *ret;
|
||||
|
||||
ret = calloc(1, sizeof(Elm_Code_File));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
EAPI Elm_Code_File *elm_code_file_open(const char *path)
|
||||
{
|
||||
Elm_Code_File *ret;
|
||||
|
@ -26,8 +49,8 @@ EAPI Elm_Code_File *elm_code_file_open(const char *path)
|
|||
Eina_Iterator *it;
|
||||
unsigned int lastindex;
|
||||
|
||||
ret = elm_code_file_new();
|
||||
file = eina_file_open(path, EINA_FALSE);
|
||||
ret = calloc(1, sizeof(Elm_Code_File));
|
||||
ret->file = file;
|
||||
lastindex = 1;
|
||||
|
||||
|
@ -45,21 +68,14 @@ EAPI Elm_Code_File *elm_code_file_open(const char *path)
|
|||
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);
|
||||
_elm_code_file_line_append_data(ret, line->start, line->length, lastindex = line->index);
|
||||
}
|
||||
eina_iterator_free(it);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
EAPI void elm_code_file_close(Elm_Code_File *file)
|
||||
EAPI void elm_code_file_free(Elm_Code_File *file)
|
||||
{
|
||||
Elm_Code_Line *l;
|
||||
|
||||
|
@ -70,10 +86,16 @@ EAPI void elm_code_file_close(Elm_Code_File *file)
|
|||
free(l);
|
||||
}
|
||||
|
||||
eina_file_close(file->file);
|
||||
free(file);
|
||||
}
|
||||
|
||||
EAPI void elm_code_file_close(Elm_Code_File *file)
|
||||
{
|
||||
eina_file_close(file->file);
|
||||
|
||||
elm_code_file_free(file);
|
||||
}
|
||||
|
||||
EAPI const char *elm_code_file_filename_get(Elm_Code_File *file)
|
||||
{
|
||||
return basename((char *)eina_file_filename_get(file->file));
|
||||
|
@ -89,6 +111,15 @@ EAPI unsigned int elm_code_file_lines_get(Elm_Code_File *file)
|
|||
return eina_list_count(file->lines);
|
||||
}
|
||||
|
||||
|
||||
EAPI void elm_code_file_line_append(Elm_Code_File *file, const char *line)
|
||||
{
|
||||
int row;
|
||||
|
||||
row = elm_code_file_lines_get(file);
|
||||
_elm_code_file_line_append_data(file, line, strlen(line), row+1);
|
||||
}
|
||||
|
||||
EAPI char *elm_code_file_line_content_get(Elm_Code_File *file, int number)
|
||||
{
|
||||
Elm_Code_Line *line;
|
||||
|
|
|
@ -36,8 +36,12 @@ typedef struct _Elm_Code_File
|
|||
*
|
||||
*/
|
||||
|
||||
EAPI Elm_Code_File *elm_code_file_new();
|
||||
|
||||
EAPI Elm_Code_File *elm_code_file_open(const char *path);
|
||||
|
||||
EAPI void elm_code_file_free(Elm_Code_File *file);
|
||||
|
||||
EAPI void elm_code_file_close(Elm_Code_File *file);
|
||||
|
||||
EAPI const char *elm_code_file_filename_get(Elm_Code_File *file);
|
||||
|
@ -58,6 +62,8 @@ EAPI const char *elm_code_file_path_get(Elm_Code_File *file);
|
|||
|
||||
EAPI unsigned int elm_code_file_lines_get(Elm_Code_File *file);
|
||||
|
||||
EAPI void elm_code_file_line_append(Elm_Code_File *file, const char *line);
|
||||
|
||||
EAPI char *elm_code_file_line_content_get(Elm_Code_File *file, int line);
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,6 +5,7 @@ check_PROGRAMS = elm_code_suite
|
|||
|
||||
elm_code_suite_SOURCES = \
|
||||
elm_code_file_test_load.c \
|
||||
elm_code_file_test_memory.c \
|
||||
elm_code_test_basic.c \
|
||||
elm_code_suite.c
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include "elm_code_suite.h"
|
||||
|
||||
START_TEST (elm_code_file_memory_lines)
|
||||
{
|
||||
Elm_Code_File *file;
|
||||
|
||||
file = elm_code_file_new();
|
||||
ck_assert_uint_eq(0, elm_code_file_lines_get(file));
|
||||
|
||||
elm_code_file_line_append(file, "a line");
|
||||
|
||||
ck_assert_uint_eq(1, elm_code_file_lines_get(file));
|
||||
elm_code_file_free(file);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
void elm_code_file_test_memory(TCase *tc)
|
||||
{
|
||||
tcase_add_test(tc, elm_code_file_memory_lines);
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ static const struct {
|
|||
void (*build)(TCase *tc);
|
||||
} tests[] = {
|
||||
{ "file_load", elm_code_file_test_load },
|
||||
{ "file_memory", elm_code_file_test_memory },
|
||||
{ "basic", elm_code_test_basic },
|
||||
};
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <Elm_Code.h>
|
||||
|
||||
void elm_code_file_test_load(TCase *tc);
|
||||
void elm_code_file_test_memory(TCase *tc);
|
||||
void elm_code_test_basic(TCase *tc);
|
||||
|
||||
#endif /* _EDLM_CODE_SUITE_H */
|
||||
|
|
Loading…
Reference in New Issue