[CONUTILS] Use const string pointers for strings that are not modified by the functions.

This commit is contained in:
Hermès Bélusca-Maïto 2020-08-23 19:10:11 +02:00
parent dff2d58693
commit 4aaf54e0a4
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
7 changed files with 60 additions and 46 deletions

View file

@ -84,12 +84,12 @@ INT
__stdcall __stdcall
ConWrite( ConWrite(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN PTCHAR szStr, IN PCTCH szStr,
IN DWORD len) IN DWORD len)
{ {
#ifndef USE_CRT #ifndef USE_CRT
DWORD TotalLen = len, dwNumBytes = 0; DWORD TotalLen = len, dwNumBytes = 0;
PVOID p; LPCVOID p;
/* If we do not write anything, just return */ /* If we do not write anything, just return */
if (!szStr || len == 0) if (!szStr || len == 0)
@ -168,30 +168,30 @@ ConWrite(
{ {
/* Loop until we find a newline character */ /* Loop until we find a newline character */
p = szStr; p = szStr;
while (len > 0 && *(PWCHAR)p != L'\n') while (len > 0 && *(PCWCH)p != L'\n')
{ {
/* Advance one character */ /* Advance one character */
p = (PVOID)((PWCHAR)p + 1); p = (LPCVOID)((PCWCH)p + 1);
--len; --len;
} }
/* Write everything up to \n */ /* Write everything up to \n */
dwNumBytes = ((PWCHAR)p - (PWCHAR)szStr) * sizeof(WCHAR); dwNumBytes = ((PCWCH)p - (PCWCH)szStr) * sizeof(WCHAR);
WriteFile(Stream->hHandle, szStr, dwNumBytes, &dwNumBytes, NULL); WriteFile(Stream->hHandle, szStr, dwNumBytes, &dwNumBytes, NULL);
/* /*
* If we hit a newline and the previous character is not a carriage-return, * If we hit a newline and the previous character is not a carriage-return,
* emit a carriage-return + newline sequence, otherwise just emit the newline. * emit a carriage-return + newline sequence, otherwise just emit the newline.
*/ */
if (len > 0 && *(PWCHAR)p == L'\n') if (len > 0 && *(PCWCH)p == L'\n')
{ {
if (p == (PVOID)szStr || (p > (PVOID)szStr && *((PWCHAR)p - 1) != L'\r')) if (p == (LPCVOID)szStr || (p > (LPCVOID)szStr && *((PCWCH)p - 1) != L'\r'))
WriteFile(Stream->hHandle, L"\r\n", 2 * sizeof(WCHAR), &dwNumBytes, NULL); WriteFile(Stream->hHandle, L"\r\n", 2 * sizeof(WCHAR), &dwNumBytes, NULL);
else else
WriteFile(Stream->hHandle, L"\n", sizeof(WCHAR), &dwNumBytes, NULL); WriteFile(Stream->hHandle, L"\n", sizeof(WCHAR), &dwNumBytes, NULL);
/* Skip \n */ /* Skip \n */
p = (PVOID)((PWCHAR)p + 1); p = (LPCVOID)((PCWCH)p + 1);
--len; --len;
} }
szStr = p; szStr = p;
@ -252,30 +252,30 @@ ConWrite(
{ {
/* Loop until we find a newline character */ /* Loop until we find a newline character */
p = szStr; p = szStr;
while (len > 0 && *(PCHAR)p != '\n') while (len > 0 && *(PCCH)p != '\n')
{ {
/* Advance one character */ /* Advance one character */
p = (PVOID)((PCHAR)p + 1); p = (LPCVOID)((PCCH)p + 1);
--len; --len;
} }
/* Write everything up to \n */ /* Write everything up to \n */
dwNumBytes = ((PCHAR)p - (PCHAR)szStr) * sizeof(CHAR); dwNumBytes = ((PCCH)p - (PCCH)szStr) * sizeof(CHAR);
WriteFile(Stream->hHandle, szStr, dwNumBytes, &dwNumBytes, NULL); WriteFile(Stream->hHandle, szStr, dwNumBytes, &dwNumBytes, NULL);
/* /*
* If we hit a newline and the previous character is not a carriage-return, * If we hit a newline and the previous character is not a carriage-return,
* emit a carriage-return + newline sequence, otherwise just emit the newline. * emit a carriage-return + newline sequence, otherwise just emit the newline.
*/ */
if (len > 0 && *(PCHAR)p == '\n') if (len > 0 && *(PCCH)p == '\n')
{ {
if (p == (PVOID)szStr || (p > (PVOID)szStr && *((PCHAR)p - 1) != '\r')) if (p == (LPCVOID)szStr || (p > (LPCVOID)szStr && *((PCCH)p - 1) != '\r'))
WriteFile(Stream->hHandle, "\r\n", 2, &dwNumBytes, NULL); WriteFile(Stream->hHandle, "\r\n", 2, &dwNumBytes, NULL);
else else
WriteFile(Stream->hHandle, "\n", 1, &dwNumBytes, NULL); WriteFile(Stream->hHandle, "\n", 1, &dwNumBytes, NULL);
/* Skip \n */ /* Skip \n */
p = (PVOID)((PCHAR)p + 1); p = (LPCVOID)((PCCH)p + 1);
--len; --len;
} }
szStr = p; szStr = p;
@ -397,8 +397,8 @@ do { \
INT INT
ConStreamWrite( ConStreamWrite(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN PTCHAR szStr, IN PCTCH szStr,
IN DWORD len) IN DWORD len)
{ {
INT Len; INT Len;
CON_STREAM_WRITE2(Stream, szStr, len, Len); CON_STREAM_WRITE2(Stream, szStr, len, Len);
@ -426,7 +426,7 @@ ConStreamWrite(
INT INT
ConPuts( ConPuts(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN LPWSTR szStr) IN PCWSTR szStr)
{ {
INT Len; INT Len;
@ -465,7 +465,7 @@ ConPuts(
INT INT
ConPrintfV( ConPrintfV(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN LPWSTR szStr, IN PCWSTR szStr,
IN va_list args) IN va_list args)
{ {
INT Len; INT Len;
@ -474,12 +474,12 @@ ConPrintfV(
// Len = vfwprintf(Stream->fStream, szStr, args); // vfprintf for direct ANSI // Len = vfwprintf(Stream->fStream, szStr, args); // vfprintf for direct ANSI
/* /*
* Reuse szStr as the pointer to end-of-string, to compute * Re-use szStr as the pointer to end-of-string, so as
* the string length instead of calling wcslen(). * to compute the string length instead of calling wcslen().
*/ */
// StringCchVPrintfW(bufSrc, ARRAYSIZE(bufSrc), szStr, args); // StringCchVPrintfW(bufSrc, ARRAYSIZE(bufSrc), szStr, args);
// Len = wcslen(bufSrc); // Len = wcslen(bufSrc);
StringCchVPrintfExW(bufSrc, ARRAYSIZE(bufSrc), &szStr, NULL, 0, szStr, args); StringCchVPrintfExW(bufSrc, ARRAYSIZE(bufSrc), (PWSTR*)&szStr, NULL, 0, szStr, args);
Len = szStr - bufSrc; Len = szStr - bufSrc;
CON_STREAM_WRITE2(Stream, bufSrc, Len, Len); CON_STREAM_WRITE2(Stream, bufSrc, Len, Len);
@ -519,7 +519,7 @@ INT
__cdecl __cdecl
ConPrintf( ConPrintf(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN LPWSTR szStr, IN PCWSTR szStr,
...) ...)
{ {
INT Len; INT Len;

View file

@ -39,40 +39,46 @@ extern "C" {
// Shadow type, implementation-specific // Shadow type, implementation-specific
typedef struct _CON_STREAM CON_STREAM, *PCON_STREAM; typedef struct _CON_STREAM CON_STREAM, *PCON_STREAM;
// typedef INT (__stdcall *CON_READ_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD); // typedef INT (__stdcall *CON_READ_FUNC)(
// Stream, szStr, len // IN PCON_STREAM Stream,
typedef INT (__stdcall *CON_WRITE_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD); // OUT PTCHAR szStr,
// IN OUT PDWORD len);
typedef INT (__stdcall *CON_WRITE_FUNC)(
IN PCON_STREAM Stream,
IN PCTCH szStr,
IN DWORD len);
INT INT
__stdcall __stdcall
ConWrite( ConWrite(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN PTCHAR szStr, IN PCTCH szStr,
IN DWORD len); IN DWORD len);
INT INT
ConStreamWrite( ConStreamWrite(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN PTCHAR szStr, IN PCTCH szStr,
IN DWORD len); IN DWORD len);
INT INT
ConPuts( ConPuts(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN LPWSTR szStr); IN PCWSTR szStr);
INT INT
ConPrintfV( ConPrintfV(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN LPWSTR szStr, IN PCWSTR szStr,
IN va_list args); IN va_list args);
INT INT
__cdecl __cdecl
ConPrintf( ConPrintf(
IN PCON_STREAM Stream, IN PCON_STREAM Stream,
IN LPWSTR szStr, IN PCWSTR szStr,
...); ...);
INT INT

View file

@ -39,7 +39,7 @@ ConWritePaging(
IN PCON_PAGER Pager, IN PCON_PAGER Pager,
IN PAGE_PROMPT PagePrompt, IN PAGE_PROMPT PagePrompt,
IN BOOL StartPaging, IN BOOL StartPaging,
IN PTCHAR szStr, IN PCTCH szStr,
IN DWORD len) IN DWORD len)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -121,7 +121,7 @@ ConPutsPaging(
IN PCON_PAGER Pager, IN PCON_PAGER Pager,
IN PAGE_PROMPT PagePrompt, IN PAGE_PROMPT PagePrompt,
IN BOOL StartPaging, IN BOOL StartPaging,
IN LPTSTR szStr) IN PCTSTR szStr)
{ {
DWORD len; DWORD len;
@ -142,7 +142,7 @@ ConResPagingEx(
IN UINT uID) IN UINT uID)
{ {
INT Len; INT Len;
PWCHAR szStr = NULL; PCWSTR szStr = NULL;
Len = K32LoadStringW(hInstance, uID, (PWSTR)&szStr, 0); Len = K32LoadStringW(hInstance, uID, (PWSTR)&szStr, 0);
if (szStr && Len) if (szStr && Len)

View file

@ -50,15 +50,17 @@ do { \
} while (0) } while (0)
// Pager, Done, Total typedef BOOL (__stdcall *PAGE_PROMPT)(
typedef BOOL (__stdcall *PAGE_PROMPT)(IN PCON_PAGER, IN DWORD, IN DWORD); IN PCON_PAGER Pager,
IN DWORD Done,
IN DWORD Total);
BOOL BOOL
ConWritePaging( ConWritePaging(
IN PCON_PAGER Pager, IN PCON_PAGER Pager,
IN PAGE_PROMPT PagePrompt, IN PAGE_PROMPT PagePrompt,
IN BOOL StartPaging, IN BOOL StartPaging,
IN PTCHAR szStr, IN PCTCH szStr,
IN DWORD len); IN DWORD len);
BOOL BOOL
@ -66,7 +68,7 @@ ConPutsPaging(
IN PCON_PAGER Pager, IN PCON_PAGER Pager,
IN PAGE_PROMPT PagePrompt, IN PAGE_PROMPT PagePrompt,
IN BOOL StartPaging, IN BOOL StartPaging,
IN LPTSTR szStr); IN PCTSTR szStr);
BOOL BOOL
ConResPagingEx( ConResPagingEx(

View file

@ -232,8 +232,8 @@ ConStreamSetCacheCodePage(
*/ */
Mode = Stream->Mode; Mode = Stream->Mode;
CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage); CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage);
return TRUE;
#endif #endif
return TRUE;
} }
HANDLE HANDLE
@ -277,7 +277,7 @@ ConStreamSetOSHandle(
if (!Stream->fStream) if (!Stream->fStream)
return FALSE; return FALSE;
int fdOut = _open_osfhandle(Handle, _O_TEXT /* FIXME! */); int fdOut = _open_osfhandle((intptr_t)Handle, _O_TEXT /* FIXME! */);
FILE* fpOut = _fdopen(fdOut, "w"); FILE* fpOut = _fdopen(fdOut, "w");
*Stream->fStream = *fpOut; *Stream->fStream = *fpOut;
/// setvbuf(Stream->fStream, NULL, _IONBF, 0); /// setvbuf(Stream->fStream, NULL, _IONBF, 0);

View file

@ -55,9 +55,15 @@ typedef enum _CON_STREAM_MODE
// Shadow type, implementation-specific // Shadow type, implementation-specific
typedef struct _CON_STREAM CON_STREAM, *PCON_STREAM; typedef struct _CON_STREAM CON_STREAM, *PCON_STREAM;
// typedef INT (__stdcall *CON_READ_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD); // typedef INT (__stdcall *CON_READ_FUNC)(
// Stream, szStr, len // IN PCON_STREAM Stream,
typedef INT (__stdcall *CON_WRITE_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD); // OUT PTCHAR szStr,
// IN OUT PDWORD len);
typedef INT (__stdcall *CON_WRITE_FUNC)(
IN PCON_STREAM Stream,
IN PCTCH szStr,
IN DWORD len);
/* /*
* Standard console streams, initialized by * Standard console streams, initialized by

View file

@ -133,7 +133,7 @@ K32LoadStringExW(
/* /*
* If nBufferMax == 0, then return a read-only pointer * If nBufferMax == 0, then return a read-only pointer
* to the resource itself in lpBuffer it is assumed that * to the resource itself in lpBuffer it is assumed that
* lpBuffer is actually a (LPWSTR *). * lpBuffer is actually a (LPWSTR*).
*/ */
if (nBufferMax == 0) if (nBufferMax == 0)
{ {