| 1 | #pragma once |
| 2 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 3 | |
| 4 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
| 9 | #define ConsoleExitOnFailureSource(d, x, c, f, ...) if (FAILED(x)) { ConsoleWriteError(x, c, f, __VA_ARGS__); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } |
| 10 | #define ConsoleExitOnLastErrorSource(d, x, c, f, ...) { x = ::GetLastError(); x = HRESULT_FROM_WIN32(x); if (FAILED(x)) { ConsoleWriteError(x, c, f, __VA_ARGS__); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } } |
| 11 | #define ConsoleExitOnNullSource(d, p, x, e, c, f, ...) if (NULL == p) { x = e; ConsoleWriteError(x, c, f, __VA_ARGS__); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } |
| 12 | #define ConsoleExitOnNullWithLastErrorSource(d, p, x, c, f, ...) if (NULL == p) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (!FAILED(x)) { x = E_FAIL; } ConsoleWriteError(x, c, f, __VA_ARGS__); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } |
| 13 | #define ConsoleExitWithLastErrorSource(d, x, c, f, ...) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (!FAILED(x)) { x = E_FAIL; } ConsoleWriteError(x, c, f, __VA_ARGS__); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } |
| 14 | |
| 15 | |
| 16 | #define ConsoleExitOnFailure(x, c, f, ...) ConsoleExitOnFailureSource(DUTIL_SOURCE_DEFAULT, x, c, f, __VA_ARGS__) |
| 17 | #define ConsoleExitOnLastError(x, c, f, ...) ConsoleExitOnLastErrorSource(DUTIL_SOURCE_DEFAULT, x, c, f, __VA_ARGS__) |
| 18 | #define ConsoleExitOnNull(p, x, e, c, f, ...) ConsoleExitOnNullSource(DUTIL_SOURCE_DEFAULT, p, x, e, c, f, __VA_ARGS__) |
| 19 | #define ConsoleExitOnNullWithLastError(p, x, c, f, ...) ConsoleExitOnNullWithLastErrorSource(DUTIL_SOURCE_DEFAULT, p, x, c, f, __VA_ARGS__) |
| 20 | #define ConsoleExitWithLastError(x, c, f, ...) ConsoleExitWithLastErrorSource(DUTIL_SOURCE_DEFAULT, x, c, f, __VA_ARGS__) |
| 21 | |
| 22 | // enums |
| 23 | typedef enum CONSOLE_COLOR { CONSOLE_COLOR_NORMAL, CONSOLE_COLOR_RED, CONSOLE_COLOR_YELLOW, CONSOLE_COLOR_GREEN } CONSOLE_COLOR; |
| 24 | |
| 25 | // structs |
| 26 | |
| 27 | // functions |
| 28 | /******************************************************************** |
| 29 | ConsoleInitialize - initialize console for UTF-8 |
| 30 | |
| 31 | *********************************************************************/ |
| 32 | HRESULT DAPI ConsoleInitialize(); |
| 33 | void DAPI ConsoleUninitialize(); |
| 34 | |
| 35 | void DAPI ConsoleGreen(); |
| 36 | void DAPI ConsoleRed(); |
| 37 | void DAPI ConsoleYellow(); |
| 38 | void DAPI ConsoleNormal(); |
| 39 | |
| 40 | /******************************************************************** |
| 41 | ConsoleWrite - full color printfA without libc |
| 42 | |
| 43 | NOTE: only supports ANSI characters |
| 44 | assumes already in normal color and resets the screen to normal color |
| 45 | ********************************************************************/ |
| 46 | HRESULT DAPI ConsoleWrite( |
| 47 | CONSOLE_COLOR cc, |
| 48 | __in_z __format_string LPCSTR szFormat, |
| 49 | ... |
| 50 | ); |
| 51 | |
| 52 | /******************************************************************** |
| 53 | ConsoleWriteW - sends UTF-8 characters to console out in color. |
| 54 | |
| 55 | NOTE: assumes already in normal color and resets the screen to normal color |
| 56 | ********************************************************************/ |
| 57 | HRESULT DAPI ConsoleWriteW( |
| 58 | __in CONSOLE_COLOR cc, |
| 59 | __in_z LPCWSTR wzData |
| 60 | ); |
| 61 | |
| 62 | /******************************************************************** |
| 63 | ConsoleWriteLine - full color printfA plus newline without libc |
| 64 | |
| 65 | NOTE: only supports ANSI characters |
| 66 | assumes already in normal color and resets the screen to normal color |
| 67 | ********************************************************************/ |
| 68 | HRESULT DAPI ConsoleWriteLine( |
| 69 | CONSOLE_COLOR cc, |
| 70 | __in_z __format_string LPCSTR szFormat, |
| 71 | ... |
| 72 | ); |
| 73 | |
| 74 | /******************************************************************** |
| 75 | ConsoleWriteError - display an error to the console out |
| 76 | |
| 77 | NOTE: only supports ANSI characters |
| 78 | does not write to stderr |
| 79 | ********************************************************************/ |
| 80 | HRESULT DAPI ConsoleWriteError( |
| 81 | HRESULT hrError, |
| 82 | CONSOLE_COLOR cc, |
| 83 | __in_z __format_string LPCSTR szFormat, |
| 84 | ... |
| 85 | ); |
| 86 | |
| 87 | /******************************************************************** |
| 88 | ConsoleReadW - reads a line from console in as UTF-8 to populate Unicode buffer |
| 89 | |
| 90 | ********************************************************************/ |
| 91 | HRESULT DAPI ConsoleReadW( |
| 92 | __deref_out_z LPWSTR* ppwzBuffer |
| 93 | ); |
| 94 | |
| 95 | /******************************************************************** |
| 96 | ConsoleReadNonBlockingW - Read from the console without blocking |
| 97 | Won't work for redirected files (exe < txtfile), but will work for stdin redirected to |
| 98 | an anonymous or named pipe |
| 99 | |
| 100 | if (fReadLine), stop reading immediately when \r\n is found |
| 101 | *********************************************************************/ |
| 102 | HRESULT DAPI ConsoleReadNonBlockingW( |
| 103 | __deref_out_ecount_opt(*pcchSize) LPWSTR* ppwzBuffer, |
| 104 | __out DWORD* pcchSize, |
| 105 | BOOL fReadLine |
| 106 | ); |
| 107 | |
| 108 | /******************************************************************** |
| 109 | ConsoleReadStringA - get console input without libc |
| 110 | |
| 111 | NOTE: only supports ANSI characters |
| 112 | *********************************************************************/ |
| 113 | HRESULT DAPI ConsoleReadStringA( |
| 114 | __deref_inout_ecount_part(cchCharBuffer,*pcchNumCharReturn) LPSTR* szCharBuffer, |
| 115 | CONST DWORD cchCharBuffer, |
| 116 | __out DWORD* pcchNumCharReturn |
| 117 | ); |
| 118 | |
| 119 | /******************************************************************** |
| 120 | ConsoleReadStringW - get console input without libc |
| 121 | |
| 122 | *********************************************************************/ |
| 123 | HRESULT DAPI ConsoleReadStringW( |
| 124 | __deref_inout_ecount_part(cchCharBuffer,*pcchNumCharReturn) LPWSTR* szCharBuffer, |
| 125 | CONST DWORD cchCharBuffer, |
| 126 | __out DWORD* pcchNumCharReturn |
| 127 | ); |
| 128 | |
| 129 | /******************************************************************** |
| 130 | ConsoleSetReadHidden - set console input no echo |
| 131 | |
| 132 | *********************************************************************/ |
| 133 | HRESULT DAPI ConsoleSetReadHidden(void); |
| 134 | |
| 135 | /******************************************************************** |
| 136 | ConsoleSetReadNormal - reset to echo |
| 137 | |
| 138 | *********************************************************************/ |
| 139 | HRESULT DAPI ConsoleSetReadNormal(void); |
| 140 | |
| 141 | #ifdef __cplusplus |
| 142 | } |
| 143 | #endif |
| 144 |