Load the content and verify from our test files.

Includes an eina_file_map_lines workaround for blank lines
This commit is contained in:
Andy Williams 2014-10-19 22:33:49 +01:00
parent 0bb5201e3c
commit 6cf6df3886
4 changed files with 80 additions and 11 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
line 1
line2
another link
double blank
8