ReactOS 0.4.16-dev-1946-g52006dd
wine2ros.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Wine-To-ReactOS
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Reducing dependency on Wine
5 * COPYRIGHT: Copyright 2025 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6 */
7
8#if DBG
9
10#include <windows.h>
11#include <stdlib.h>
12#include <stdio.h>
13#include <string.h>
14#include "wine2ros.h"
15
16BOOL
18{
19 CHAR szValue[MAX_PATH];
20 PCHAR pch0, pch1;
21 BOOL ret;
23
25 ret = GetEnvironmentVariableA("DEBUGCHANNEL", szValue, _countof(szValue));
27
28 if (!ret)
29 return FALSE;
30
31 for (pch0 = szValue;; pch0 = pch1 + 1)
32 {
33 pch1 = strchr(pch0, ',');
34 if (pch1)
35 *pch1 = ANSI_NULL;
36 if (_stricmp(pch0, channel) == 0)
37 return TRUE;
38 if (!pch1)
39 return FALSE;
40 }
41}
42
43#define DEBUGSTR_HEX "0123456789ABCDEF"
44#define DEBUGSTR_QUOTE_TAIL_LEN 8
45
46static PSTR
47debugstr_quote_a(
48 _Out_ PSTR pszBuf,
50 _In_opt_ PCSTR pszSrc)
51{
52 PCH pch = pszBuf;
53 PCCH pchSrc = pszSrc;
54
55 if (!pszSrc)
56 return "(null)";
57
58 if (!((ULONG_PTR)pszSrc >> 16))
59 {
60 snprintf(pszBuf, cchBuf, "%p", pszSrc);
61 return pszBuf;
62 }
63
64 *pch++ = '"';
65 --cchBuf;
66
67 for (; cchBuf > DEBUGSTR_QUOTE_TAIL_LEN; ++pchSrc)
68 {
69 switch (*pchSrc)
70 {
71 case '\'': case '\"': case '\\': case '\t': case '\r': case '\n':
72 *pch++ = '\\';
73 if (*pchSrc == '\t')
74 *pch++ = 't';
75 else if (*pchSrc == '\r')
76 *pch++ = 'r';
77 else if (*pchSrc == '\n')
78 *pch++ = 'n';
79 else
80 *pch++ = *pchSrc;
81 cchBuf -= 2;
82 break;
83 default:
84 {
85 if (*pchSrc >= ' ')
86 {
87 *pch++ = *pchSrc;
88 --cchBuf;
89 }
90 else
91 {
92 *pch++ = '\\';
93 *pch++ = 'x';
94 *pch++ = DEBUGSTR_HEX[(*pchSrc >> 4) & 0xF];
95 *pch++ = DEBUGSTR_HEX[*pchSrc & 0xF];
96 cchBuf -= 4;
97 }
98 break;
99 }
100 }
101 }
102
103 if (cchBuf <= DEBUGSTR_QUOTE_TAIL_LEN)
104 {
105 *pch++ = '.';
106 *pch++ = '.';
107 *pch++ = '.';
108 }
109 *pch++ = '"';
110 *pch = ANSI_NULL;
111 return pszBuf;
112}
113
114static PSTR
115debugstr_quote_w(
116 _Out_ PSTR pszBuf,
118 _In_opt_ PCWSTR pszSrc)
119{
120 PCH pch = pszBuf;
121 PCWCH pchSrc = pszSrc;
122
123 if (!pszSrc)
124 return "(null)";
125
126 if (!((ULONG_PTR)pszSrc >> 16))
127 {
128 snprintf(pszBuf, cchBuf, "%p", pszSrc);
129 return pszBuf;
130 }
131
132 *pch++ = '"';
133 --cchBuf;
134
135 for (; cchBuf > DEBUGSTR_QUOTE_TAIL_LEN; ++pchSrc)
136 {
137 switch (*pchSrc)
138 {
139 case L'\'': case L'\"': case L'\\': case L'\t': case L'\r': case L'\n':
140 *pch++ = '\\';
141 if (*pchSrc == L'\t')
142 *pch++ = 't';
143 else if (*pchSrc == L'\r')
144 *pch++ = 'r';
145 else if (*pchSrc == L'\n')
146 *pch++ = 'n';
147 else
148 *pch++ = *pchSrc;
149 cchBuf -= 2;
150 break;
151 default:
152 {
153 if (*pchSrc >= L' ' && *pchSrc < 0x100)
154 {
155 *pch++ = (CHAR)*pchSrc;
156 --cchBuf;
157 }
158 else
159 {
160 *pch++ = '\\';
161 *pch++ = 'x';
162 *pch++ = DEBUGSTR_HEX[(*pchSrc >> 12) & 0xF];
163 *pch++ = DEBUGSTR_HEX[(*pchSrc >> 8) & 0xF];
164 *pch++ = DEBUGSTR_HEX[(*pchSrc >> 4) & 0xF];
165 *pch++ = DEBUGSTR_HEX[*pchSrc & 0xF];
166 cchBuf -= 6;
167 }
168 break;
169 }
170 }
171 }
172
173 if (cchBuf <= DEBUGSTR_QUOTE_TAIL_LEN)
174 {
175 *pch++ = '.';
176 *pch++ = '.';
177 *pch++ = '.';
178 }
179 *pch++ = '"';
180 *pch = ANSI_NULL;
181 return pszBuf;
182}
183
184#define DEBUGSTR_NUM_BUFFS 5
185#define DEBUGSTR_BUFF_SIZE MAX_PATH
186
187static LPSTR
188debugstr_next_buff(void)
189{
190 static CHAR s_bufs[DEBUGSTR_NUM_BUFFS][DEBUGSTR_BUFF_SIZE];
191 static SIZE_T s_index = 0;
192 PCHAR ptr = s_bufs[s_index];
193 s_index = (s_index + 1) % _countof(s_bufs);
194 return ptr;
195}
196
197PCSTR
199{
200 return debugstr_quote_a(debugstr_next_buff(), DEBUGSTR_BUFF_SIZE, pszA);
201}
202
203PCSTR
205{
206 return debugstr_quote_w(debugstr_next_buff(), DEBUGSTR_BUFF_SIZE, pszW);
207}
208
209PCSTR
210debugstr_guid(_In_opt_ const GUID *id)
211{
212 PCHAR ptr;
213
214 if (!id)
215 return "(null)";
216
217 ptr = debugstr_next_buff();
218
219 if (!((ULONG_PTR)id >> 16))
220 {
221 snprintf(ptr, DEBUGSTR_BUFF_SIZE, "%p", id);
222 }
223 else
224 {
225 snprintf(ptr, DEBUGSTR_BUFF_SIZE,
226 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
227 id->Data1, id->Data2, id->Data3,
228 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
229 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7]);
230 }
231
232 return ptr;
233}
234
235#endif /* DBG */
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define CHAR(Char)
#define _stricmp
Definition: cat.c:22
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define SetLastError(x)
Definition: compat.h:752
#define MAX_PATH
Definition: compat.h:34
#define GetEnvironmentVariableA(x, y, z)
Definition: compat.h:754
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint id
Definition: glext.h:5910
#define debugstr_guid
Definition: kernel32.h:35
#define debugstr_a
Definition: kernel32.h:31
#define debugstr_w
Definition: kernel32.h:32
#define error(str)
Definition: mkdosfs.c:1605
#define pch(ap)
Definition: match.c:418
static PVOID ptr
Definition: dispmode.c:27
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
CHAR * PCH
Definition: ntbasedef.h:403
CONST WCHAR * PCWCH
Definition: ntbasedef.h:423
#define ANSI_NULL
CONST CHAR * PCCH
Definition: ntbasedef.h:404
_In_ UINT cchBuf
Definition: shlwapi.h:378
#define _countof(array)
Definition: sndvol32.h:70
char * PSTR
Definition: typedefs.h:51
const uint16_t * PCWSTR
Definition: typedefs.h:57
ULONG_PTR SIZE_T
Definition: typedefs.h:80
const char * PCSTR
Definition: typedefs.h:52
uint32_t ULONG_PTR
Definition: typedefs.h:65
char * PCHAR
Definition: typedefs.h:51
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define IntIsDebugChannelEnabled(channel)
Definition: wine2ros.h:40
#define snprintf
Definition: wintirpc.h:48
char * LPSTR
Definition: xmlstorage.h:182
char CHAR
Definition: xmlstorage.h:175