(empty log message)
@@ -10,22 +10,6 @@ | ||
10 | 10 | #include <vector> |
11 | 11 | #include <shlwapi.h> |
12 | 12 | |
13 | -/** textbuf - temporary text buffer | |
14 | - */ | |
15 | -namespace { | |
16 | - struct textbuf { | |
17 | - char* data; | |
18 | - textbuf() : data(NULL) {} | |
19 | - ~textbuf() { delete [] data; } | |
20 | - char* operator()(size_t n) | |
21 | - { | |
22 | - assert(n); | |
23 | - delete [] data, data = NULL; | |
24 | - return data = new char[n]; | |
25 | - } | |
26 | - }; | |
27 | -} | |
28 | - | |
29 | 13 | /* |
30 | 14 | * Functions of the class dialog |
31 | 15 | */ |
@@ -92,7 +76,7 @@ | ||
92 | 76 | string |
93 | 77 | dialog::gettext(int id) const |
94 | 78 | { |
95 | - textbuf buf; | |
79 | + win32::textbuf<char> buf; | |
96 | 80 | int size = 0; |
97 | 81 | for (int n = 0; n <= size + 1;) { |
98 | 82 | n += 256; |
@@ -134,7 +118,7 @@ | ||
134 | 118 | HWND h = item(id); |
135 | 119 | int i = ListBox_GetCurSel(h); |
136 | 120 | int n = ListBox_GetTextLen(h, i); |
137 | - textbuf buf; | |
121 | + win32::textbuf<char> buf; | |
138 | 122 | return n > 0 && ListBox_GetText(h, i, buf(n + 1)) == n ? |
139 | 123 | string(buf.data, n) : string(); |
140 | 124 | } |
@@ -28,25 +28,11 @@ | ||
28 | 28 | GetLastError() == ERROR_ALREADY_EXISTS)) throw error(); |
29 | 29 | } |
30 | 30 | |
31 | -namespace { | |
32 | - struct textbuf { | |
33 | - char* data; | |
34 | - textbuf() : data(NULL) {} | |
35 | - textbuf(size_t n) : data(new char[n]) {} | |
36 | - ~textbuf() { delete [] data; } | |
37 | - char* operator()(size_t n) | |
38 | - { | |
39 | - delete [] data, data = NULL; | |
40 | - return data = new char[n]; | |
41 | - } | |
42 | - }; | |
43 | -} | |
44 | - | |
45 | 31 | string |
46 | 32 | win32::profile(LPCSTR section, LPCSTR key, LPCSTR file) |
47 | 33 | { |
48 | 34 | if (!file) return string(); |
49 | - textbuf buf; | |
35 | + textbuf<char> buf; | |
50 | 36 | DWORD size = 0; |
51 | 37 | for (DWORD n = 0; n <= size + 2;) { |
52 | 38 | n += 256; |
@@ -65,9 +51,9 @@ | ||
65 | 51 | string |
66 | 52 | win32::xenv(const string& s) |
67 | 53 | { |
54 | + textbuf<char> buf; | |
68 | 55 | DWORD n = static_cast<DWORD>(s.size() + 1); |
69 | - textbuf buf(n); | |
70 | - n = ExpandEnvironmentStrings(s.c_str(), buf.data, n); | |
56 | + n = ExpandEnvironmentStrings(s.c_str(), buf(n), n); | |
71 | 57 | if (n > s.size() + 1) ExpandEnvironmentStrings(s.c_str(), buf(n), n); |
72 | 58 | return buf.data; |
73 | 59 | } |
@@ -82,12 +68,10 @@ | ||
82 | 68 | FileTimeToLocalFileTime(&ft, <); |
83 | 69 | FileTimeToSystemTime(<, &st); |
84 | 70 | } |
71 | + textbuf<char> buf; | |
85 | 72 | int n = fn(LOCALE_USER_DEFAULT, flags, &st, NULL, NULL, 0); |
86 | - textbuf buf(n); | |
87 | - if (n && fn(LOCALE_USER_DEFAULT, flags, &st, NULL, buf.data, n)) { | |
88 | - return buf.data; | |
89 | - } | |
90 | - return string(); | |
73 | + return n && fn(LOCALE_USER_DEFAULT, flags, &st, NULL, buf(n), n) ? | |
74 | + string(buf.data, n - 1) : string(); | |
91 | 75 | } |
92 | 76 | |
93 | 77 | HANDLE |
@@ -95,7 +79,7 @@ | ||
95 | 79 | { |
96 | 80 | assert(!(flags & ~(SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT | |
97 | 81 | SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS))); |
98 | - textbuf tmp(cmd.size() + 1); | |
82 | + textbuf<char> tmp(cmd.size() + 1); | |
99 | 83 | lstrcpyA(tmp.data, cmd.c_str()); |
100 | 84 | PathRemoveArgs(tmp.data); |
101 | 85 | string::size_type i = lstrlen(tmp.data); |
@@ -205,7 +189,7 @@ | ||
205 | 189 | string |
206 | 190 | win32::module::text(UINT id) const |
207 | 191 | { |
208 | - textbuf buf; | |
192 | + textbuf<char> buf; | |
209 | 193 | int size = 0; |
210 | 194 | for (int n = 0; n <= size + 1;) { |
211 | 195 | n += 256; |
@@ -218,7 +202,7 @@ | ||
218 | 202 | win32::module::textf(UINT id, ...) const |
219 | 203 | { |
220 | 204 | string s = text(id); |
221 | - textbuf buf; | |
205 | + textbuf<char> buf; | |
222 | 206 | int size = 0; |
223 | 207 | for (int n = 0; n <= size + 1;) { |
224 | 208 | n += 512; |
@@ -295,11 +279,10 @@ | ||
295 | 279 | win32::wstr::mbstr(LPCWSTR ws, UINT cp) |
296 | 280 | { |
297 | 281 | if (!ws) return string(); |
298 | - int size = WideCharToMultiByte(cp, 0, ws, -1, NULL, 0, NULL, NULL); | |
299 | - if (!size) return string(); | |
300 | - textbuf buf(size); | |
301 | - WideCharToMultiByte(cp, 0, ws, -1, buf.data, size, NULL, NULL); | |
302 | - return string(buf.data, size - 1); | |
282 | + textbuf<char> buf; | |
283 | + int n = WideCharToMultiByte(cp, 0, ws, -1, NULL, 0, NULL, NULL); | |
284 | + return n && WideCharToMultiByte(cp, 0, ws, -1, buf(n), n, NULL, NULL) ? | |
285 | + string(buf.data, n - 1) : string(); | |
303 | 286 | } |
304 | 287 | |
305 | 288 | /* |
@@ -89,6 +89,17 @@ | ||
89 | 89 | friend class trylock; |
90 | 90 | }; |
91 | 91 | |
92 | + // textbuf - text buffer template | |
93 | + template<typename _Ty> | |
94 | + struct textbuf { | |
95 | + _Ty* data; | |
96 | + textbuf() : data(NULL) {} | |
97 | + textbuf(size_t n) : data(new _Ty[n]) {} | |
98 | + ~textbuf() { delete [] data; } | |
99 | + _Ty* operator()(size_t n) | |
100 | + { delete [] data, data = NULL; return data = new _Ty[n]; } | |
101 | + }; | |
102 | + | |
92 | 103 | // wstr - wide character string |
93 | 104 | class wstr { |
94 | 105 | LPWSTR _data; |
@@ -259,12 +259,6 @@ | ||
259 | 259 | void put(const char* key, const char* value); |
260 | 260 | void erase(const char* key); |
261 | 261 | list<string> keys() const; |
262 | - public: | |
263 | - struct buf { | |
264 | - char* data; | |
265 | - buf(DWORD size) : data(new char[size]) {} | |
266 | - ~buf() { delete [] data; } | |
267 | - }; | |
268 | 262 | }; |
269 | 263 | } |
270 | 264 |
@@ -288,7 +282,7 @@ | ||
288 | 282 | if (_key && |
289 | 283 | RegQueryValueEx(_key, key, NULL, &type, NULL, &size) == ERROR_SUCCESS && |
290 | 284 | type == REG_SZ) { |
291 | - regkey::buf buf(size); | |
285 | + win32::textbuf<char> buf(size); | |
292 | 286 | if (RegQueryValueEx(_key, key, NULL, NULL, LPBYTE(buf.data), &size) == ERROR_SUCCESS) { |
293 | 287 | return buf.data; |
294 | 288 | } |
@@ -315,7 +309,7 @@ | ||
315 | 309 | DWORD size; |
316 | 310 | if (_key && RegQueryInfoKey(_key, NULL, NULL, NULL, NULL, NULL, |
317 | 311 | NULL, NULL, &size, NULL, NULL, NULL) == ERROR_SUCCESS) { |
318 | - regkey::buf buf(++size); | |
312 | + win32::textbuf<char> buf(++size); | |
319 | 313 | DWORD i = 0, n; |
320 | 314 | while (n = size, RegEnumValue(_key, i++, buf.data, &n, |
321 | 315 | NULL, NULL, NULL, NULL) == ERROR_SUCCESS) { |
@@ -353,7 +347,7 @@ | ||
353 | 347 | DWORD size; |
354 | 348 | if (_key && RegQueryInfoKey(HKEY(_key), NULL, NULL, NULL, NULL, &size, |
355 | 349 | NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) { |
356 | - regkey::buf buf(++size); | |
350 | + win32::textbuf<char> buf(++size); | |
357 | 351 | DWORD i = 0, n; |
358 | 352 | while (n = size, RegEnumKeyEx(HKEY(_key), i++, buf.data, &n, |
359 | 353 | NULL, NULL, NULL, NULL) == ERROR_SUCCESS) { |
@@ -145,11 +145,7 @@ | ||
145 | 145 | } |
146 | 146 | int n = 0; |
147 | 147 | if (_mb2u(&_mode, _codepage, text.c_str(), NULL, NULL, &n) != S_OK) throw text; |
148 | - struct buf { | |
149 | - LPWSTR data; | |
150 | - buf(int size) : data(new WCHAR[size]) {} | |
151 | - ~buf() { delete [] data; } | |
152 | - } buf(n + 1); | |
148 | + win32::textbuf<WCHAR> buf(n + 1); | |
153 | 149 | _mb2u(&_mode, _codepage, text.c_str(), NULL, buf.data, &n); |
154 | 150 | buf.data[n] = 0; |
155 | 151 | return win32::wstr::mbstr(buf.data, CP_UTF8); |
@@ -181,17 +181,7 @@ | ||
181 | 181 | } |
182 | 182 | } |
183 | 183 | |
184 | - struct textbuf { | |
185 | - LPWSTR data; | |
186 | - textbuf() : data(NULL) {} | |
187 | - ~textbuf() { delete [] data; } | |
188 | - LPWSTR operator()(size_t n) | |
189 | - { | |
190 | - assert(n); | |
191 | - delete [] data, data = NULL; | |
192 | - return data = new WCHAR[n]; | |
193 | - } | |
194 | - } tb[2]; | |
184 | + win32::textbuf<WCHAR> tb[2]; | |
195 | 185 | WPARAM si[] = { s1, s2 }; |
196 | 186 | for (int i = 0; i < 2; ++i) { |
197 | 187 | LVITEMW lv = { LVIF_TEXT }; |