diff --git a/src/tkc/istream.c b/src/tkc/istream.c index bcc157d55..f2e1be6be 100644 --- a/src/tkc/istream.c +++ b/src/tkc/istream.c @@ -173,19 +173,20 @@ ret_t tk_istream_read_line_str(tk_istream_t* stream, str_t* str) { int32_t size = 0; bool_t got_end_line = FALSE; return_value_if_fail(stream != NULL && str != NULL, RET_BAD_PARAMS); - return_value_if_fail(stream->tell != NULL && stream->seek != NULL && stream->eos != NULL, RET_BAD_PARAMS); + return_value_if_fail(stream->tell != NULL && stream->seek != NULL && stream->eos != NULL, + RET_BAD_PARAMS); str_set(str, ""); - if(tk_istream_eos(stream)) { + if (tk_istream_eos(stream)) { return RET_DONE; } - while(!tk_istream_eos(stream) && got_end_line == FALSE) { - size = tk_istream_read(stream, line, sizeof(line)-1); - if(size >= 0) { + while (!tk_istream_eos(stream) && got_end_line == FALSE) { + size = tk_istream_read(stream, line, sizeof(line) - 1); + if (size >= 0) { int32_t i = 0; - for(i = 0; i < size; i++) { - if(line[i] == '\r' || line[i] == '\n') { + for (i = 0; i < size; i++) { + if (line[i] == '\r' || line[i] == '\n') { got_end_line = TRUE; break; } @@ -197,16 +198,16 @@ ret_t tk_istream_read_line_str(tk_istream_t* stream, str_t* str) { continue; } - if(line[i] == '\r') { + if (line[i] == '\r') { i++; - if(line[i] == '\n') { + if (line[i] == '\n') { i++; } - } else if(line[i] == '\n') { + } else if (line[i] == '\n') { i++; } - if(i < size) { + if (i < size) { int32_t pos = tk_istream_tell(stream); pos = pos - (size - i); ENSURE(tk_istream_seek(stream, pos) == RET_OK); @@ -218,4 +219,3 @@ ret_t tk_istream_read_line_str(tk_istream_t* stream, str_t* str) { return RET_OK; } - diff --git a/tests/istream_file_test.cc b/tests/istream_file_test.cc index 438d18e6d..ba8bd6c7b 100644 --- a/tests/istream_file_test.cc +++ b/tests/istream_file_test.cc @@ -3,7 +3,6 @@ #include "gtest/gtest.h" #include "streams/file/istream_file.h" - TEST(IStreamFile, readline) { str_t str; tk_istream_t* is = tk_istream_file_create("tests/testdata/test_lines.txt"); @@ -11,19 +10,21 @@ TEST(IStreamFile, readline) { str_init(&str, 1024); ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, ""); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, "line2"); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, ""); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, "line4"); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); - ASSERT_STREQ(str.str, "line5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); - + ASSERT_STREQ(str.str, + "line5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxx"); + ASSERT_NE(tk_istream_read_line_str(is, &str), RET_OK); object_unref(OBJECT(is)); diff --git a/tests/istream_mem_test.cc b/tests/istream_mem_test.cc index a3b9d9250..1d18f3e37 100644 --- a/tests/istream_mem_test.cc +++ b/tests/istream_mem_test.cc @@ -87,18 +87,17 @@ TEST(IStreamMem, read_line) { str_init(&str, 1024); ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, "1111"); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, "2222"); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, ""); - + ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK); ASSERT_STREQ(str.str, "3333"); - + ASSERT_NE(tk_istream_read_line_str(is, &str), RET_OK); object_unref(OBJECT(is)); } -