forked from enlightenment/edi
Load the content and verify from our test files.
Includes an eina_file_map_lines workaround for blank lines
This commit is contained in:
parent
0bb5201e3c
commit
6cf6df3886
|
@ -42,8 +42,8 @@ extern "C" {
|
||||||
|
|
||||||
typedef struct _Elm_Code_Line
|
typedef struct _Elm_Code_Line
|
||||||
{
|
{
|
||||||
Eina_File_Line content;
|
char *content;
|
||||||
Eina_File_Line modified;
|
unsigned int number;
|
||||||
|
|
||||||
} Elm_Code_Line;
|
} Elm_Code_Line;
|
||||||
|
|
||||||
|
@ -51,7 +51,6 @@ typedef struct _Elm_Code_File
|
||||||
{
|
{
|
||||||
Eina_List *lines;
|
Eina_List *lines;
|
||||||
Eina_File *file;
|
Eina_File *file;
|
||||||
void *map;
|
|
||||||
|
|
||||||
} Elm_Code_File;
|
} 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 int elm_code_lines_get(Elm_Code_File *file);
|
||||||
|
|
||||||
|
EAPI char *elm_code_line_content_get(Elm_Code_File *file, int line);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -57,29 +57,51 @@ elm_code_shutdown(void)
|
||||||
return _elm_code_init;
|
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)
|
EAPI Elm_Code_File *elm_code_open(const char *path)
|
||||||
{
|
{
|
||||||
Elm_Code_File *ret;
|
Elm_Code_File *ret;
|
||||||
Eina_File *file;
|
Eina_File *file;
|
||||||
Eina_File_Line *line;
|
Eina_File_Line *line;
|
||||||
Eina_Iterator *it;
|
Eina_Iterator *it;
|
||||||
void *map;
|
unsigned int lastindex;
|
||||||
|
|
||||||
file = eina_file_open(path, EINA_FALSE);
|
file = eina_file_open(path, EINA_FALSE);
|
||||||
map = eina_file_map_all(file, EINA_FILE_WILLNEED);
|
|
||||||
ret = calloc(1, sizeof(Elm_Code_File));
|
ret = calloc(1, sizeof(Elm_Code_File));
|
||||||
ret->file = file;
|
ret->file = file;
|
||||||
ret->map = map;
|
lastindex = 1;
|
||||||
|
|
||||||
it = eina_file_map_lines(file);
|
it = eina_file_map_lines(file);
|
||||||
EINA_ITERATOR_FOREACH(it, line)
|
EINA_ITERATOR_FOREACH(it, line)
|
||||||
{
|
{
|
||||||
Elm_Code_Line *ecl;
|
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;
|
if (!ecl) continue;
|
||||||
|
|
||||||
ecl->content = *line;
|
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);
|
ret->lines = eina_list_append(ret->lines, ecl);
|
||||||
}
|
}
|
||||||
eina_iterator_free(it);
|
eina_iterator_free(it);
|
||||||
|
@ -92,16 +114,19 @@ EAPI void elm_code_close(Elm_Code_File *file)
|
||||||
Elm_Code_Line *l;
|
Elm_Code_Line *l;
|
||||||
|
|
||||||
EINA_LIST_FREE(file->lines, l)
|
EINA_LIST_FREE(file->lines, l)
|
||||||
|
{
|
||||||
|
if (l->content)
|
||||||
|
free(l->content);
|
||||||
free(l);
|
free(l);
|
||||||
|
}
|
||||||
|
|
||||||
eina_file_map_free(file->file, file->map);
|
|
||||||
eina_file_close(file->file);
|
eina_file_close(file->file);
|
||||||
free(file);
|
free(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
EAPI const char *elm_code_filename_get(Elm_Code_File *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)
|
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);
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -31,9 +31,36 @@ START_TEST (elm_code_load_lines)
|
||||||
}
|
}
|
||||||
END_TEST
|
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)
|
void elm_code_test_load(TCase *tc)
|
||||||
{
|
{
|
||||||
tcase_add_test(tc, elm_code_load);
|
tcase_add_test(tc, elm_code_load);
|
||||||
tcase_add_test(tc, elm_code_load_lines);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
line 1
|
||||||
|
line2
|
||||||
|
|
||||||
|
another link
|
||||||
|
|
||||||
|
|
||||||
|
double blank
|
||||||
|
8
|
Loading…
Reference in New Issue