Skip to content

Commit

Permalink
【llbc】标准化LLBC_File::ReadLine()函数行为,及修复bug #263
Browse files Browse the repository at this point in the history
  • Loading branch information
lailongwei committed Sep 5, 2024
1 parent 2691262 commit 485afa2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion llbc/include/llbc/core/file/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ class LLBC_EXPORT LLBC_File

/**
* Read oneline data to LLBC_String object.
* @return LLBC_String - the line data, if failed, LLBC_GetLastError() return value is non-zero.
* @return LLBC_String - the line data, if failed, LLBC_GetLastError() return value is non LLBC_ERROR_SUCCESS,
* otherwise, LLBC_GetLastError() return LLBC_ERROR_SUCCESS.
*/
LLBC_String ReadLine();

Expand Down
16 changes: 14 additions & 2 deletions llbc/include/llbc/core/file/FileInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ inline int LLBC_File::Read(ldouble &ldoubleVal)

inline LLBC_String LLBC_File::ReadLine()
{
LLBC_String line;

// Read line bytes.
char ch;
LLBC_String line;
bool hasBeenRead = false;
LLBC_SetLastError(LLBC_ERROR_SUCCESS);
while (Read(ch) != LLBC_FAILED)
{
hasBeenRead = true;
if (ch != LLBC_CR && ch != LLBC_LF)
{
line.append(1, ch);
Expand All @@ -119,6 +122,15 @@ inline LLBC_String LLBC_File::ReadLine()
break;
}

// Read first byte failed, return empty line(LLBC_GetLastError() return non LLBC_ERROR_SUCCESS).
if (!hasBeenRead)
return line;

// If read to end, set last error to SUCCESS.
if (LLBC_GetLastError() == LLBC_ERROR_TRUNCATED)
LLBC_SetLastError(LLBC_ERROR_SUCCESS);

// Return line.
return line;
}

Expand Down
10 changes: 8 additions & 2 deletions testsuite/core/file/TestCase_Core_File_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,17 @@ bool TestCase_Core_File_File::ReadWriteTest()
file.WriteLine("Hello World!");
LLBC_PrintLn("WriteLine(\"Hey, Judy!\"):");
file.WriteLine("Hey, Judy!");
LLBC_PrintLn("WriteLine(\"\"):");
file.WriteLine("");
LLBC_PrintLn("Write(\"Hey, Judy too!\"):");
file.Write("Hey, Judy too");

file.SetFilePosition(0);
LLBC_PrintLn("Read lines:");
LLBC_PrintLn("First line: %s", file.ReadLine().c_str());
LLBC_PrintLn("Second line: %s", file.ReadLine().c_str());
LLBC_PrintLn("First line: %s, last error:%d", file.ReadLine().c_str(), LLBC_GetLastError());
LLBC_PrintLn("Second line: %s, last error:%d", file.ReadLine().c_str(), LLBC_GetLastError());
LLBC_PrintLn("Third line: %s, last error:%d", file.ReadLine().c_str(), LLBC_GetLastError());
LLBC_PrintLn("Fourth line: %s, last error:%d", file.ReadLine().c_str(), LLBC_GetLastError());
const LLBC_String notExistLine = file.ReadLine();
LLBC_PrintLn("Not exist line: %s, error: %s", notExistLine.c_str(), LLBC_FormatLastError());

Expand Down

0 comments on commit 485afa2

Please sign in to comment.