Merge pull request #22858 from taosdata/fix/xsren/TS-3957/readLineOnWindows

fix read line on windows
This commit is contained in:
wade zhang 2023-09-12 18:15:07 +08:00 committed by GitHub
commit e832908589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 6 deletions

View File

@ -841,14 +841,33 @@ int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) {
return -1; return -1;
} }
#ifdef WINDOWS #ifdef WINDOWS
*ptrBuf = taosMemoryMalloc(1024); size_t bufferSize = 512;
*ptrBuf = taosMemoryMalloc(bufferSize);
if (*ptrBuf == NULL) return -1; if (*ptrBuf == NULL) return -1;
if (fgets(*ptrBuf, 1023, pFile->fp) == NULL) {
taosMemoryFreeClear(*ptrBuf); size_t bytesRead = 0;
return -1; 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 #else
size_t len = 0; size_t len = 0;
return getline(ptrBuf, &len, pFile->fp); return getline(ptrBuf, &len, pFile->fp);