From 431d4380bfee4a385b120ca9a147b1c3f623af10 Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Tue, 12 Sep 2023 14:12:21 +0800 Subject: [PATCH] fix read line on windows --- source/os/src/osFile.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 1177ff562e..ff0737b9ad 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -819,14 +819,33 @@ int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { return -1; } #ifdef WINDOWS - *ptrBuf = taosMemoryMalloc(1024); + size_t bufferSize = 512; + *ptrBuf = taosMemoryMalloc(bufferSize); if (*ptrBuf == NULL) return -1; - if (fgets(*ptrBuf, 1023, pFile->fp) == NULL) { - taosMemoryFreeClear(*ptrBuf); - return -1; + + size_t bytesRead = 0; + size_t totalBytesRead = 0; + + while (1) { + char *result = fgets(*ptrBuf + totalBytesRead, bufferSize - totalBytesRead, pFile->fp); + if (result == NULL) { + taosMemoryFreeClear(*ptrBuf); + return -1; + } + bytesRead = strlen(*ptrBuf + totalBytesRead); + totalBytesRead += bytesRead; + + if (totalBytesRead < bufferSize - 1 || (*ptrBuf)[totalBytesRead - 1] == '\n') { + break; + } + + bufferSize += 512; + *ptrBuf = taosMemoryRealloc(*ptrBuf, bufferSize); + if (*ptrBuf == NULL) return -1; } - (*ptrBuf)[1023] = 0; - return strlen(*ptrBuf); + + (*ptrBuf)[totalBytesRead] = '\0'; + return totalBytesRead; #else size_t len = 0; return getline(ptrBuf, &len, pFile->fp);