[CMD] Do not use an intermediate buffer when reading lines from batch files

This is easier on the heap and improves cmd:batch winetest nicely
This commit is contained in:
Jérôme Gardou 2021-06-23 16:26:50 +02:00 committed by Jérôme Gardou
parent 5ca83e516c
commit aae161d061

View file

@ -75,7 +75,6 @@ BOOL bEcho = TRUE; /* The echo flag */
/* Buffer for reading Batch file lines */
TCHAR textline[BATCH_BUFFSIZE];
/*
* Returns a pointer to the n'th parameter of the current batch file.
* If no such parameter exists returns pointer to empty string.
@ -527,47 +526,31 @@ VOID AddBatchRedirection(REDIRECTION **RedirList)
*/
BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength)
{
LPSTR lpString;
INT len = 0;
#ifdef _UNICODE
lpString = cmd_alloc(nBufferLength);
if (!lpString)
{
WARN("Cannot allocate memory for lpString\n");
error_out_of_memory();
return FALSE;
}
#else
lpString = lpBuffer;
#endif
/* read all chars from memory until a '\n' is encountered */
if (bc->mem)
{
for (; (bc->mempos < bc->memsize && len < (nBufferLength-1)); len++)
{
lpString[len] = bc->mem[bc->mempos++];
if (lpString[len] == '\n' )
for (; ((bc->mempos + len) < bc->memsize && len < (nBufferLength-1)); len++)
{
#ifndef _UNICODE
lpBuffer[len] = bc->mem[bc->mempos + len];
#endif
if (bc->mem[bc->mempos + len] == '\n')
{
len++;
break;
}
}
#ifdef _UNICODE
nBufferLength = MultiByteToWideChar(OutputCodePage, 0, &bc->mem[bc->mempos], len, lpBuffer, nBufferLength);
lpBuffer[nBufferLength] = L'\0';
lpBuffer[len] = '\0';
#endif
bc->mempos += len;
}
if (!len)
{
#ifdef _UNICODE
cmd_free(lpString);
#endif
return FALSE;
}
lpString[len++] = '\0';
#ifdef _UNICODE
MultiByteToWideChar(OutputCodePage, 0, lpString, -1, lpBuffer, len);
cmd_free(lpString);
#endif
return TRUE;
return len != 0;
}
/*