cmd: Avoid reading char by char from files.
This commit is contained in:
parent
5e3cb1867b
commit
cd30c52b35
@ -188,38 +188,42 @@ WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **where, WCHAR **end) {
|
|||||||
|
|
||||||
WCHAR *WCMD_fgets(WCHAR *buf, int noChars, HANDLE h)
|
WCHAR *WCMD_fgets(WCHAR *buf, int noChars, HANDLE h)
|
||||||
{
|
{
|
||||||
DWORD bytes, charsRead;
|
DWORD charsRead;
|
||||||
BOOL status;
|
BOOL status;
|
||||||
WCHAR *p;
|
LARGE_INTEGER filepos;
|
||||||
|
int i;
|
||||||
|
|
||||||
/* We can't use the native f* functions because of the filename syntax differences
|
/* We can't use the native f* functions because of the filename syntax differences
|
||||||
between DOS and Unix. Also need to lose the LF (or CRLF) from the line. */
|
between DOS and Unix. Also need to lose the LF (or CRLF) from the line. */
|
||||||
|
|
||||||
p = buf;
|
if (!WCMD_is_console_handle(h)) {
|
||||||
if (WCMD_is_console_handle(h)) {
|
/* Save current file position */
|
||||||
status = ReadConsoleW(h, buf, noChars, &charsRead, NULL);
|
filepos.QuadPart = 0;
|
||||||
if (!status) return NULL;
|
SetFilePointerEx(h, filepos, &filepos, FILE_CURRENT);
|
||||||
if (buf[charsRead-2] == '\r')
|
|
||||||
buf[charsRead-2] = '\0'; /* Strip \r\n */
|
|
||||||
else {
|
|
||||||
/* Truncate */
|
|
||||||
buf[noChars-1] = '\0';
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: More intelligent buffering for reading lines from files */
|
status = WCMD_ReadFile(h, buf, noChars, &charsRead);
|
||||||
do {
|
if (!status || charsRead == 0) return NULL;
|
||||||
status = WCMD_ReadFile(h, buf, 1, &bytes);
|
|
||||||
if ((status == 0) || ((bytes == 0) && (buf == p))) return NULL;
|
/* Find first EOL */
|
||||||
if (*buf == '\n') bytes = 0;
|
for (i = 0; i < charsRead; i++) {
|
||||||
else if (*buf != '\r') {
|
if (buf[i] == '\n' || buf[i] == '\r')
|
||||||
buf++;
|
break;
|
||||||
noChars--;
|
}
|
||||||
}
|
|
||||||
*buf = '\0';
|
if (!WCMD_is_console_handle(h) && i != charsRead) {
|
||||||
} while ((bytes == 1) && (noChars > 1));
|
/* Sets file pointer to the start of the next line, if any */
|
||||||
return p;
|
filepos.QuadPart += i + 1 + (buf[i] == '\r' ? 1 : 0);
|
||||||
|
SetFilePointerEx(h, filepos, NULL, FILE_BEGIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Truncate at EOL (or end of buffer) */
|
||||||
|
if (i == noChars)
|
||||||
|
i--;
|
||||||
|
|
||||||
|
buf[i] = '\0';
|
||||||
|
|
||||||
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* WCMD_splitpath - copied from winefile as no obvious way to use it otherwise */
|
/* WCMD_splitpath - copied from winefile as no obvious way to use it otherwise */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user