elm_code file: detect line endings

Check for Windows otherwise default to unix for now.
This commit is contained in:
Andy Williams 2015-03-15 23:07:31 +00:00
parent 7c5d0e3873
commit 433bac2c1b
4 changed files with 65 additions and 0 deletions

View File

@ -21,6 +21,17 @@ static Elm_Code_Line *_elm_code_file_line_blank_create(Elm_Code_File *file, int
return ecl;
}
static Elm_Code_File_Line_Ending _elm_code_line_ending_get(const char *ending)
{
switch (*ending)
{
case '\r':
return ELM_CODE_FILE_LINE_ENDING_WINDOWS;
default:
return ELM_CODE_FILE_LINE_ENDING_UNIX;
}
}
static void _elm_code_file_line_insert_data(Elm_Code_File *file, const char *content, unsigned int length,
unsigned int row, Eina_Bool mapped, void *data)
{
@ -93,6 +104,9 @@ EAPI Elm_Code_File *elm_code_file_open(Elm_Code *code, const char *path)
{
Elm_Code_Line *ecl;
if (lastindex == 1)
ret->line_ending = _elm_code_line_ending_get(line->start + line->length);
/* Working around the issue that eina_file_map_lines does not trigger an item for empty lines */
while (lastindex < line->index - 1)
{
@ -148,6 +162,11 @@ EAPI const char *elm_code_file_path_get(Elm_Code_File *file)
return eina_file_filename_get(file->file);
}
EAPI Elm_Code_File_Line_Ending elm_code_file_line_ending_get(Elm_Code_File *file)
{
return file->line_ending;
}
EAPI void elm_code_file_clear(Elm_Code_File *file)
{
Elm_Code_Line *l;

View File

@ -10,6 +10,11 @@ extern "C" {
* @brief These routines are used for interacting with files using Elm Code.
*/
typedef enum {
ELM_CODE_FILE_LINE_ENDING_UNIX = 0,
ELM_CODE_FILE_LINE_ENDING_WINDOWS
} Elm_Code_File_Line_Ending;
struct _Elm_Code_File
{
void *parent;
@ -18,6 +23,7 @@ struct _Elm_Code_File
Eina_File *file;
void *map;
Elm_Code_File_Line_Ending line_ending;
};
/**
@ -42,6 +48,8 @@ EAPI const char *elm_code_file_filename_get(Elm_Code_File *file);
EAPI const char *elm_code_file_path_get(Elm_Code_File *file);
EAPI Elm_Code_File_Line_Ending elm_code_file_line_ending_get(Elm_Code_File *file);
/**
* @}
*

View File

@ -80,11 +80,45 @@ START_TEST (elm_code_file_load_content)
}
END_TEST
START_TEST (elm_code_file_line_ending_unix)
{
char *path = TESTS_DIR "testfile.txt";
Elm_Code_File *file;
Elm_Code *code;
code = elm_code_create();
file = elm_code_file_open(code, path);
ck_assert_int_eq(ELM_CODE_FILE_LINE_ENDING_UNIX, elm_code_file_line_ending_get(file));
elm_code_file_close(file);
elm_code_free(code);
}
END_TEST
START_TEST (elm_code_file_line_ending_windows)
{
char *path = TESTS_DIR "testfile-windows.txt";
Elm_Code_File *file;
Elm_Code *code;
code = elm_code_create();
file = elm_code_file_open(code, path);
ck_assert_int_eq(ELM_CODE_FILE_LINE_ENDING_WINDOWS, elm_code_file_line_ending_get(file));
elm_code_file_close(file);
elm_code_free(code);
}
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);
tcase_add_test(tc, elm_code_file_line_ending_unix);
tcase_add_test(tc, elm_code_file_line_ending_windows);
}

View File

@ -0,0 +1,4 @@
line 1
line2
a third
another line