1 | /* $Id: heapstring.cpp,v 1.55 2004-03-18 11:13:41 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
4 | *
|
---|
5 | * Win32 compatibility string functions for OS/2
|
---|
6 | *
|
---|
7 | * NOTE: lstrcpyn* always appends a terminating 0 (unlike strncpy)!
|
---|
8 | *
|
---|
9 | * Copyright 1999 Patrick Haller
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*****************************************************************************
|
---|
13 | * Includes *
|
---|
14 | *****************************************************************************/
|
---|
15 |
|
---|
16 | #include <odin.h>
|
---|
17 | #include <odinwrap.h>
|
---|
18 | #include <os2sel.h>
|
---|
19 |
|
---|
20 | #include <os2win.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <string.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <winnls.h>
|
---|
25 | #include <unicode.h>
|
---|
26 | #include <ctype.h>
|
---|
27 | #include <wcstr.h>
|
---|
28 | #include "heap.h"
|
---|
29 | #include <wine\unicode.h>
|
---|
30 | #include "misc.h"
|
---|
31 | #include "codepage.h"
|
---|
32 |
|
---|
33 | #define DBG_LOCALLOG DBG_heapstring
|
---|
34 | #include "dbglocal.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /*****************************************************************************
|
---|
38 | * Defines *
|
---|
39 | *****************************************************************************/
|
---|
40 |
|
---|
41 | ODINDEBUGCHANNEL(KERNEL32-HEAPSTRING)
|
---|
42 |
|
---|
43 |
|
---|
44 | /*****************************************************************************
|
---|
45 | * Imported variables *
|
---|
46 | *****************************************************************************/
|
---|
47 |
|
---|
48 | // This macro could be mapped to GetProcessHeap() if there
|
---|
49 | // is any change in functionality
|
---|
50 | #define GetProcessHeap() Heap_ProcessHeap
|
---|
51 |
|
---|
52 | extern HANDLE Heap_ProcessHeap;
|
---|
53 |
|
---|
54 |
|
---|
55 | /*****************************************************************************
|
---|
56 | * Name :
|
---|
57 | * Purpose :
|
---|
58 | * Parameters:
|
---|
59 | * Variables :
|
---|
60 | * Result :
|
---|
61 | * Remark :
|
---|
62 | * Status :
|
---|
63 | *
|
---|
64 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
65 | *****************************************************************************/
|
---|
66 |
|
---|
67 | int WIN32API lstrlenA(LPCSTR arg1)
|
---|
68 | {
|
---|
69 | dprintf2(("KERNEL32: lstrlenA(%s)\n",
|
---|
70 | arg1));
|
---|
71 |
|
---|
72 | if(arg1 == NULL) {
|
---|
73 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 | return strlen(arg1);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /*****************************************************************************
|
---|
81 | * Name :
|
---|
82 | * Purpose :
|
---|
83 | * Parameters:
|
---|
84 | * Variables :
|
---|
85 | * Result :
|
---|
86 | * Remark :
|
---|
87 | * Status :
|
---|
88 | *
|
---|
89 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
90 | *****************************************************************************/
|
---|
91 |
|
---|
92 | int WIN32API lstrlenW(LPCWSTR str)
|
---|
93 | {
|
---|
94 | if(str == NULL) {
|
---|
95 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | const WCHAR *s = str;
|
---|
100 | while (*s) s++;
|
---|
101 | dprintf2(("KERNEL32: lstrlenW(%08xh) returns %d\n",
|
---|
102 | str, s - str));
|
---|
103 |
|
---|
104 | return s - str;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /*****************************************************************************
|
---|
109 | * Name :
|
---|
110 | * Purpose :
|
---|
111 | * Parameters:
|
---|
112 | * Variables :
|
---|
113 | * Result :
|
---|
114 | * Remark :
|
---|
115 | * Status :
|
---|
116 | *
|
---|
117 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
118 | *****************************************************************************/
|
---|
119 |
|
---|
120 | LPSTR WIN32API lstrcatA(LPSTR arg1, LPCSTR arg2)
|
---|
121 | {
|
---|
122 | dprintf2(("KERNEL32: lstrcat(%s,%s)\n",
|
---|
123 | arg1,
|
---|
124 | arg2));
|
---|
125 |
|
---|
126 | if(arg2 == NULL)
|
---|
127 | return arg1;
|
---|
128 | strcat(arg1, arg2);
|
---|
129 | return arg1;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /*****************************************************************************
|
---|
134 | * Name :
|
---|
135 | * Purpose :
|
---|
136 | * Parameters:
|
---|
137 | * Variables :
|
---|
138 | * Result :
|
---|
139 | * Remark :
|
---|
140 | * Status :
|
---|
141 | *
|
---|
142 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
143 | *****************************************************************************/
|
---|
144 |
|
---|
145 | LPWSTR WIN32API lstrcatW(LPWSTR dst, LPCWSTR src)
|
---|
146 | {
|
---|
147 | dprintf2(("KERNEL32: lstrcatW(%08xh,%08xh)\n",
|
---|
148 | dst, src));
|
---|
149 |
|
---|
150 | if(src == NULL)
|
---|
151 | return dst;
|
---|
152 |
|
---|
153 | strcpyW( dst + strlenW(dst), src );
|
---|
154 | return dst;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /*****************************************************************************
|
---|
159 | * Name :
|
---|
160 | * Purpose :
|
---|
161 | * Parameters:
|
---|
162 | * Variables :
|
---|
163 | * Result :
|
---|
164 | * Remark :
|
---|
165 | * Status :
|
---|
166 | *
|
---|
167 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
168 | *****************************************************************************/
|
---|
169 |
|
---|
170 | int WIN32API lstrcmpA(LPCSTR arg1, LPCSTR arg2)
|
---|
171 | {
|
---|
172 | dprintf2(("KERNEL32: lstrcmpA(%s,%s)\n",
|
---|
173 | arg1,
|
---|
174 | arg2));
|
---|
175 |
|
---|
176 | if(arg1 == NULL)
|
---|
177 | return -1;
|
---|
178 | if(arg2 == NULL)
|
---|
179 | return 1;
|
---|
180 |
|
---|
181 | return strcmp(arg1, arg2);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /*****************************************************************************
|
---|
186 | * Name :
|
---|
187 | * Purpose :
|
---|
188 | * Parameters:
|
---|
189 | * Variables :
|
---|
190 | * Result :
|
---|
191 | * Remark :
|
---|
192 | * Status :
|
---|
193 | *
|
---|
194 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
195 | *****************************************************************************/
|
---|
196 |
|
---|
197 | int WIN32API lstrncmpA(LPCSTR arg1, LPCSTR arg2, int l)
|
---|
198 | {
|
---|
199 | dprintf2(("KERNEL32: lstrncmpA(%s,%s,%d)\n",
|
---|
200 | arg1,
|
---|
201 | arg2,
|
---|
202 | l));
|
---|
203 |
|
---|
204 | return strncmp(arg1, arg2, l);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /*****************************************************************************
|
---|
208 | * Name : lstrncmpiA
|
---|
209 | * Purpose :
|
---|
210 | * Parameters:
|
---|
211 | * Variables :
|
---|
212 | * Result :
|
---|
213 | * Remark :
|
---|
214 | * Status :
|
---|
215 | *
|
---|
216 | * Author : Przemyslaw Dobrowolski
|
---|
217 | *****************************************************************************/
|
---|
218 | int WIN32API lstrncmpiA(LPCSTR str1, LPCSTR str2, INT n )
|
---|
219 | {
|
---|
220 | INT firstch,lastch;
|
---|
221 | INT result = 0;
|
---|
222 |
|
---|
223 | if (n)
|
---|
224 | {
|
---|
225 | do
|
---|
226 | {
|
---|
227 | firstch = toupper(*str1);
|
---|
228 | lastch = toupper(*str2);
|
---|
229 | str1++;
|
---|
230 | str2++;
|
---|
231 | } while (--n && *str1 && *str2 && firstch == lastch);
|
---|
232 |
|
---|
233 | result = firstch - lastch;
|
---|
234 | }
|
---|
235 |
|
---|
236 | return(result);
|
---|
237 | }
|
---|
238 | //TODO: Don't know if this is completely correct
|
---|
239 | int WIN32API lstrncmpiW(LPCWSTR str1, LPCWSTR str2, int n)
|
---|
240 | {
|
---|
241 | INT firstch,lastch;
|
---|
242 | INT result = 0;
|
---|
243 |
|
---|
244 | if (n)
|
---|
245 | {
|
---|
246 | do
|
---|
247 | {
|
---|
248 | firstch = toupperW(*str1);
|
---|
249 | lastch = toupperW(*str2);
|
---|
250 | str1++;
|
---|
251 | str2++;
|
---|
252 | } while (--n && *str1 && *str2 && firstch == lastch);
|
---|
253 |
|
---|
254 | result = firstch - lastch;
|
---|
255 | }
|
---|
256 |
|
---|
257 | return(result);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /*****************************************************************************
|
---|
261 | * Name :
|
---|
262 | * Purpose :
|
---|
263 | * Parameters:
|
---|
264 | * Variables :
|
---|
265 | * Result :
|
---|
266 | * Remark :
|
---|
267 | * Status :
|
---|
268 | *
|
---|
269 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
270 | *****************************************************************************/
|
---|
271 |
|
---|
272 | int WIN32API lstrcmpW(LPCWSTR arg1, LPCWSTR arg2)
|
---|
273 | {
|
---|
274 | dprintf2(("KERNEL32: lstrcmpW (%08xh, %08xh)\n",
|
---|
275 | arg1,
|
---|
276 | arg2));
|
---|
277 |
|
---|
278 | if(arg1 == NULL)
|
---|
279 | return -1;
|
---|
280 | if(arg2 == NULL)
|
---|
281 | return 1;
|
---|
282 |
|
---|
283 | return strcmpW( arg1, arg2 );
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | /*****************************************************************************
|
---|
288 | * Name :
|
---|
289 | * Purpose :
|
---|
290 | * Parameters:
|
---|
291 | * Variables :
|
---|
292 | * Result :
|
---|
293 | * Remark :
|
---|
294 | * Status :
|
---|
295 | *
|
---|
296 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
297 | *****************************************************************************/
|
---|
298 |
|
---|
299 | int WIN32API lstrncmpW(LPCWSTR arg1, LPCWSTR arg2, int l)
|
---|
300 | {
|
---|
301 | dprintf2(("KERNEL32: lstrncmpW(%08xh,%08xh,%d)\n",
|
---|
302 | arg1,
|
---|
303 | arg2,
|
---|
304 | l));
|
---|
305 |
|
---|
306 | return wcsncmp((wchar_t*)arg1,
|
---|
307 | (wchar_t*)arg2,
|
---|
308 | l);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*****************************************************************************
|
---|
312 | * Name :
|
---|
313 | * Purpose :
|
---|
314 | * Parameters:
|
---|
315 | * Variables :
|
---|
316 | * Result :
|
---|
317 | * Remark :
|
---|
318 | * Status :
|
---|
319 | *
|
---|
320 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
321 | *****************************************************************************/
|
---|
322 |
|
---|
323 | LPSTR WIN32API lstrcpyA(LPSTR dest, LPCSTR src)
|
---|
324 | {
|
---|
325 | if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
|
---|
326 | return NULL;
|
---|
327 |
|
---|
328 | return strcpy(dest, src);
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | /*****************************************************************************
|
---|
333 | * Name :
|
---|
334 | * Purpose :
|
---|
335 | * Parameters:
|
---|
336 | * Variables :
|
---|
337 | * Result :
|
---|
338 | * Remark :
|
---|
339 | * Status :
|
---|
340 | *
|
---|
341 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
342 | *****************************************************************************/
|
---|
343 |
|
---|
344 | LPWSTR WIN32API lstrcpyW(LPWSTR dest, LPCWSTR src)
|
---|
345 | {
|
---|
346 | if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
|
---|
347 | return NULL;
|
---|
348 |
|
---|
349 | WCHAR *p = dest;
|
---|
350 | while ((*p++ = *src++));
|
---|
351 |
|
---|
352 | return dest;
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | /*****************************************************************************
|
---|
357 | * Name :
|
---|
358 | * Purpose :
|
---|
359 | * Parameters:
|
---|
360 | * Variables :
|
---|
361 | * Result :
|
---|
362 | * Remark :
|
---|
363 | * Status :
|
---|
364 | *
|
---|
365 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
366 | *****************************************************************************/
|
---|
367 |
|
---|
368 | LPSTR WIN32API lstrcpynA(LPSTR arg1, LPCSTR arg2, int arg3)
|
---|
369 | {
|
---|
370 | register LPSTR p1 = arg1;
|
---|
371 | register LPSTR p2 = (LPSTR)arg2;
|
---|
372 |
|
---|
373 | if(arg3 == 0) {
|
---|
374 | return NULL;
|
---|
375 | }
|
---|
376 |
|
---|
377 | //PH: looks like either \0 or arg3 terminate the copy
|
---|
378 | //return strncpy(arg1, arg2, arg3);
|
---|
379 | arg3--; // pre-decrement to avoid exceeding buffer length
|
---|
380 | // results in better code than (arg1 > 1)
|
---|
381 |
|
---|
382 | for (;*p2 && arg3; arg3--)
|
---|
383 | *p1++ = *p2++;
|
---|
384 |
|
---|
385 | *p1 = 0; //CB: copy arg-1, set end 0
|
---|
386 |
|
---|
387 | return arg1;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /*****************************************************************************
|
---|
392 | * Name :
|
---|
393 | * Purpose :
|
---|
394 | * Parameters:
|
---|
395 | * Variables :
|
---|
396 | * Result :
|
---|
397 | * Remark :
|
---|
398 | * Status :
|
---|
399 | *
|
---|
400 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
401 | *****************************************************************************/
|
---|
402 |
|
---|
403 | LPWSTR WIN32API lstrcpynW(LPWSTR dst, LPCWSTR src, int n)
|
---|
404 | {
|
---|
405 | LPWSTR p = dst;
|
---|
406 |
|
---|
407 | /* In real windows the whole function is protected by an exception handler
|
---|
408 | * that returns ERROR_INVALID_PARAMETER on faulty parameters
|
---|
409 | * We currently just check for NULL.
|
---|
410 | */
|
---|
411 | if (!dst || !src) {
|
---|
412 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
413 | return 0;
|
---|
414 | }
|
---|
415 | while ((n-- > 1) && *src) *p++ = *src++;
|
---|
416 | if (n >= 0) *p = 0;
|
---|
417 | return dst;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | /*****************************************************************************
|
---|
422 | * Name :
|
---|
423 | * Purpose :
|
---|
424 | * Parameters:
|
---|
425 | * Variables :
|
---|
426 | * Result :
|
---|
427 | * Remark :
|
---|
428 | * Status :
|
---|
429 | *
|
---|
430 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
431 | *****************************************************************************/
|
---|
432 |
|
---|
433 | int WIN32API lstrcmpiA(LPCSTR arg1, LPCSTR arg2)
|
---|
434 | {
|
---|
435 | dprintf2(("KERNEL32: lstrcmpiA(%s,%s)\n",
|
---|
436 | arg1,
|
---|
437 | arg2));
|
---|
438 |
|
---|
439 | if(arg1 == NULL)
|
---|
440 | return -1;
|
---|
441 |
|
---|
442 | if(arg2 == NULL)
|
---|
443 | return 1;
|
---|
444 |
|
---|
445 | return strcmpi(arg1, arg2);
|
---|
446 | }
|
---|
447 |
|
---|
448 |
|
---|
449 | /*****************************************************************************
|
---|
450 | * Name :
|
---|
451 | * Purpose :
|
---|
452 | * Parameters:
|
---|
453 | * Variables :
|
---|
454 | * Result :
|
---|
455 | * Remark :
|
---|
456 | * Status :
|
---|
457 | *
|
---|
458 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
459 | *****************************************************************************/
|
---|
460 |
|
---|
461 | int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
|
---|
462 | {
|
---|
463 | if (!str1 || !str2) {
|
---|
464 |
|
---|
465 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
466 | return 0;
|
---|
467 | }
|
---|
468 | return strcmpiW( str1, str2 );
|
---|
469 | }
|
---|
470 |
|
---|
471 | //*****************************************************************************
|
---|
472 | //lstrcpynWtoA and lstrcpynAtoW must zero-terminate the string
|
---|
473 | //because Wine code depends on this behaviour (i.e. comdlg32)
|
---|
474 | //*****************************************************************************
|
---|
475 | int WIN32API lstrcpynWtoA(LPSTR astring, LPCWSTR ustring, int length)
|
---|
476 | {
|
---|
477 | int ret;
|
---|
478 |
|
---|
479 | ret = WideCharToMultiByte(CP_ACP, 0, ustring, -1, astring, length, 0, NULL);
|
---|
480 | if(ret == 0) {
|
---|
481 | SetLastError(ERROR_SUCCESS); //WideCharToMultiByte sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
482 | ret = length;
|
---|
483 | }
|
---|
484 | //Must not always set the last character to 0; some apps send the wrong
|
---|
485 | //string size to apis that use this function (i.e. GetMenuStringW (Notes))
|
---|
486 | //-> overwrites stack
|
---|
487 | if(ret == length) {
|
---|
488 | #if 1
|
---|
489 | lstrtrunc( astring, length );
|
---|
490 | #else
|
---|
491 | astring[length-1] = 0;
|
---|
492 | #endif
|
---|
493 | }
|
---|
494 | else astring[ret] = 0;
|
---|
495 |
|
---|
496 | return ret;
|
---|
497 | }
|
---|
498 |
|
---|
499 | //lstrcpynWtoA and lstrcpynAtoW must zero-terminate the string
|
---|
500 | //because Wine code depends on this behaviour (i.e. comdlg32)
|
---|
501 | int WIN32API lstrcpynAtoW(LPWSTR unicode, LPCSTR ascii, int unilen)
|
---|
502 | {
|
---|
503 | int ret;
|
---|
504 |
|
---|
505 | ret = MultiByteToWideChar(CP_ACP, 0, ascii, -1, unicode, unilen );
|
---|
506 | if(ret == 0) {
|
---|
507 | SetLastError(ERROR_SUCCESS); //MultiByteToWideChar sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
508 | ret = unilen;
|
---|
509 | }
|
---|
510 |
|
---|
511 | //Must not always set the last character to 0; some apps send the wrong
|
---|
512 | //string size to apis that use this function (i.e. GetMenuStringW (Notes))
|
---|
513 | //-> overwrites stack
|
---|
514 | if(ret == unilen) {
|
---|
515 | unicode[unilen-1] = 0;
|
---|
516 | }
|
---|
517 | else unicode[ret] = 0;
|
---|
518 | return ret;
|
---|
519 | }
|
---|
520 |
|
---|
521 | /*****************************************************************************
|
---|
522 | * Name :
|
---|
523 | * Purpose : Converts unicode string to ascii string
|
---|
524 | * Parameters:
|
---|
525 | * Variables :
|
---|
526 | * Result : returns length of ascii string
|
---|
527 | * Remark :
|
---|
528 | * Status :
|
---|
529 | *
|
---|
530 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
531 | *****************************************************************************/
|
---|
532 |
|
---|
533 | LPSTR WIN32API lstrcpyWtoA(LPSTR ascii, LPCWSTR unicode)
|
---|
534 | {
|
---|
535 | //@@@PH huh? wuz dat?
|
---|
536 | if (unicode == NULL)
|
---|
537 | {
|
---|
538 | DebugInt3();
|
---|
539 | if (ascii != NULL) ((LPWSTR)ascii)[0] = 0; //CB: set at least end
|
---|
540 | return NULL;
|
---|
541 | }
|
---|
542 |
|
---|
543 | if (ascii == NULL) {
|
---|
544 | DebugInt3();
|
---|
545 | return NULL; /* garbage in, garbage out ! */
|
---|
546 | }
|
---|
547 |
|
---|
548 | /* forward to function with len parameter */
|
---|
549 | lstrcpynWtoA(ascii,
|
---|
550 | unicode,
|
---|
551 | WideCharToMultiByte( CP_ACP, 0, unicode, -1, 0, 0, 0, 0 )); //end included
|
---|
552 |
|
---|
553 | return ascii;
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 | /*****************************************************************************
|
---|
558 | * Name :
|
---|
559 | * Purpose : Copies the full string from ascii to unicode
|
---|
560 | * Parameters:
|
---|
561 | * Variables :
|
---|
562 | * Result :
|
---|
563 | * Remark :
|
---|
564 | * Status :
|
---|
565 | *
|
---|
566 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
567 | *****************************************************************************/
|
---|
568 |
|
---|
569 | LPWSTR WIN32API lstrcpyAtoW(LPWSTR unicode, LPCSTR ascii)
|
---|
570 | {
|
---|
571 | /* achimha for security, strlen might trap if garbage in */
|
---|
572 | /* @@@PH 98/06/07 */
|
---|
573 | if (ascii == NULL)
|
---|
574 | {
|
---|
575 | DebugInt3();
|
---|
576 | if (unicode != NULL) unicode[0] = 0; //CB: set at least end
|
---|
577 | return NULL;
|
---|
578 | }
|
---|
579 |
|
---|
580 | if (unicode == NULL) {
|
---|
581 | DebugInt3();
|
---|
582 | return NULL; /* garbage in, garbage out ! */
|
---|
583 | }
|
---|
584 |
|
---|
585 | /* forward to call with length parameter */
|
---|
586 | lstrcpynAtoW(unicode, ascii, MultiByteToWideChar( CP_ACP, 0, ascii, -1, 0, 0 )); //end included
|
---|
587 | return (unicode);
|
---|
588 | }
|
---|
589 |
|
---|
590 | /*****************************************************************************
|
---|
591 | * NAME
|
---|
592 | * lstrlenWtoA
|
---|
593 | *
|
---|
594 | * PURPOSE
|
---|
595 | * calculate the length of string when unicode string was converted to
|
---|
596 | * ansi string.
|
---|
597 | *
|
---|
598 | * PARAMETERS
|
---|
599 | * ustring - unicode string
|
---|
600 | * ulen - length of ustring
|
---|
601 | *
|
---|
602 | * RESULT
|
---|
603 | * Success
|
---|
604 | * if ulen < 0, length of null-terminated unicode string NOT including
|
---|
605 | * null-terminator.
|
---|
606 | * otherwise, length of string when first ulen characters of ustring
|
---|
607 | * converted to ansi string.
|
---|
608 | *
|
---|
609 | * Failure
|
---|
610 | * return 0.
|
---|
611 | *
|
---|
612 | * REMARK
|
---|
613 | * this function is not Win32 API but helper for convenient.
|
---|
614 | *
|
---|
615 | * AUTHOR : KO Myung-Hun
|
---|
616 | *****************************************************************************/
|
---|
617 |
|
---|
618 | int WIN32API lstrlenWtoA( LPCWSTR ustring, int ulen )
|
---|
619 | {
|
---|
620 | int ret;
|
---|
621 |
|
---|
622 | if( ulen < 0 )
|
---|
623 | ulen = lstrlenW( ustring );
|
---|
624 |
|
---|
625 | if( !IsDBCSEnv() ) return ulen;
|
---|
626 |
|
---|
627 | ret = WideCharToMultiByte( CP_ACP, 0, ustring, ulen, 0, 0, 0, 0 );
|
---|
628 | if(ret == 0) {
|
---|
629 | SetLastError(ERROR_SUCCESS); //WideCharToMultiByte sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
630 | return 0;
|
---|
631 | }
|
---|
632 |
|
---|
633 | return ret;
|
---|
634 | }
|
---|
635 |
|
---|
636 | /*****************************************************************************
|
---|
637 | * NAME
|
---|
638 | * lstrlenAtoW
|
---|
639 | *
|
---|
640 | * PURPOSE
|
---|
641 | * return the length of string when ansi string was converted to
|
---|
642 | * unicode string.
|
---|
643 | *
|
---|
644 | * PARAMETERS
|
---|
645 | * astring - ansi string
|
---|
646 | * alen - length of astring
|
---|
647 | *
|
---|
648 | * RESULT
|
---|
649 | * Success
|
---|
650 | * if alen < 0, length of null-terminated ansi string NOT including
|
---|
651 | * null-terminator.
|
---|
652 | * otherwise, length of string when first alen characters of astring
|
---|
653 | * converted to unicode string.
|
---|
654 | *
|
---|
655 | * Failure
|
---|
656 | * return 0.
|
---|
657 | *
|
---|
658 | * REMARK
|
---|
659 | * this function is not Win32 API but helper for convenient.
|
---|
660 | *
|
---|
661 | * AUTHOR : KO Myung-Hun
|
---|
662 | *****************************************************************************/
|
---|
663 |
|
---|
664 | int WIN32API lstrlenAtoW( LPCSTR astring, int alen )
|
---|
665 | {
|
---|
666 | int ret;
|
---|
667 |
|
---|
668 | if( alen < 0 )
|
---|
669 | alen = strlen( astring );
|
---|
670 |
|
---|
671 | if( !IsDBCSEnv() ) return alen;
|
---|
672 |
|
---|
673 | ret = MultiByteToWideChar( CP_ACP, 0, astring, alen, 0, 0 );
|
---|
674 | if(ret == 0) {
|
---|
675 | SetLastError(ERROR_SUCCESS); //MultiByteToWideChar sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
676 | return 0;
|
---|
677 | }
|
---|
678 |
|
---|
679 | return ret;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /*****************************************************************************
|
---|
683 | * NAME
|
---|
684 | * lstrtrunc
|
---|
685 | *
|
---|
686 | * PURPOSE
|
---|
687 | * truncate ansi string
|
---|
688 | *
|
---|
689 | * PARAMETERS
|
---|
690 | * max - max length of truncated string including null-terminator
|
---|
691 | *
|
---|
692 | * RESULT
|
---|
693 | * Success
|
---|
694 | * return the length of truncated string not including null-terminator
|
---|
695 | *
|
---|
696 | * Failure
|
---|
697 | * return 0.
|
---|
698 | *
|
---|
699 | * REMARK
|
---|
700 | * this function is not Win32 API but helper for convenient.
|
---|
701 | *
|
---|
702 | * AUTHOR : KO Myung-Hun
|
---|
703 | *****************************************************************************/
|
---|
704 |
|
---|
705 | int WIN32API lstrtrunc( LPSTR astring, int max )
|
---|
706 | {
|
---|
707 | int i;
|
---|
708 |
|
---|
709 | if( !astring || ( max < 1 ))
|
---|
710 | return 0;
|
---|
711 |
|
---|
712 | astring[ max - 1 ] = 0;
|
---|
713 |
|
---|
714 | if( !IsDBCSEnv() ) return max;
|
---|
715 |
|
---|
716 | max = strlen( astring );
|
---|
717 | for( i = 0; i < max; i++ )
|
---|
718 | if( IsDBCSLeadByte( astring[ i ]))
|
---|
719 | i++;
|
---|
720 |
|
---|
721 | if( i > max ) // broken DBCS lead byte ?
|
---|
722 | astring[ --max ] = 0; // then remove DBCS lead byte
|
---|
723 |
|
---|
724 | return max;
|
---|
725 | }
|
---|
726 |
|
---|
727 | /*****************************************************************************
|
---|
728 | * Name :
|
---|
729 | * Purpose :
|
---|
730 | * Parameters:
|
---|
731 | * Variables :
|
---|
732 | * Result :
|
---|
733 | * Remark :
|
---|
734 | * Status :
|
---|
735 | *
|
---|
736 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
737 | *****************************************************************************/
|
---|
738 |
|
---|
739 | LPVOID WIN32API HEAP_xalloc(HANDLE heap, DWORD flags, DWORD size )
|
---|
740 | {
|
---|
741 | LPVOID p = HeapAlloc( heap, flags, size );
|
---|
742 | if (!p)
|
---|
743 | {
|
---|
744 | dprintf2(("KERNEL32: HEAP_xalloc(%08xh,%08xh,%08xh) out of memory.\n",
|
---|
745 | heap,
|
---|
746 | flags,
|
---|
747 | size));
|
---|
748 | }
|
---|
749 | return p;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | /*****************************************************************************
|
---|
754 | * Name :
|
---|
755 | * Purpose :
|
---|
756 | * Parameters:
|
---|
757 | * Variables :
|
---|
758 | * Result :
|
---|
759 | * Remark :
|
---|
760 | * Status :
|
---|
761 | *
|
---|
762 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
763 | *****************************************************************************/
|
---|
764 |
|
---|
765 | LPVOID WIN32API HEAP_xrealloc(HANDLE heap, DWORD flags, LPVOID lpMem,
|
---|
766 | DWORD size )
|
---|
767 | {
|
---|
768 | LPVOID p = HeapReAlloc( heap, flags, lpMem, size );
|
---|
769 | if (!p)
|
---|
770 | {
|
---|
771 | dprintf2(("KERNEL32: HEAP_xrealloc(%08xh,%08xh,%08xh,%08xh) out of memory.\n",
|
---|
772 | heap,
|
---|
773 | flags,
|
---|
774 | lpMem,
|
---|
775 | size));
|
---|
776 | }
|
---|
777 | return p;
|
---|
778 | }
|
---|
779 |
|
---|
780 |
|
---|
781 | /*****************************************************************************
|
---|
782 | * Name :
|
---|
783 | * Purpose :
|
---|
784 | * Parameters:
|
---|
785 | * Variables :
|
---|
786 | * Result :
|
---|
787 | * Remark :
|
---|
788 | * Status :
|
---|
789 | *
|
---|
790 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
791 | *****************************************************************************/
|
---|
792 |
|
---|
793 | LPVOID WIN32API HEAP_malloc(DWORD size )
|
---|
794 | {
|
---|
795 | LPVOID p = HeapAlloc( GetProcessHeap(), 0, size );
|
---|
796 | if (!p)
|
---|
797 | {
|
---|
798 | dprintf2(("KERNEL32: HEAP_malloc(%08xh) out of memory.\n",
|
---|
799 | size));
|
---|
800 | }
|
---|
801 | return p;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | /*****************************************************************************
|
---|
806 | * Name :
|
---|
807 | * Purpose :
|
---|
808 | * Parameters:
|
---|
809 | * Variables :
|
---|
810 | * Result :
|
---|
811 | * Remark :
|
---|
812 | * Status :
|
---|
813 | *
|
---|
814 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
815 | *****************************************************************************/
|
---|
816 |
|
---|
817 | DWORD WIN32API HEAP_size(LPVOID lpMem)
|
---|
818 | {
|
---|
819 | return HeapSize( GetProcessHeap(), 0, lpMem );
|
---|
820 | }
|
---|
821 |
|
---|
822 |
|
---|
823 | /*****************************************************************************
|
---|
824 | * Name :
|
---|
825 | * Purpose :
|
---|
826 | * Parameters:
|
---|
827 | * Variables :
|
---|
828 | * Result :
|
---|
829 | * Remark :
|
---|
830 | * Status :
|
---|
831 | *
|
---|
832 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
833 | *****************************************************************************/
|
---|
834 |
|
---|
835 | LPVOID WIN32API HEAP_realloc(LPVOID lpMem, DWORD size )
|
---|
836 | {
|
---|
837 | LPVOID p = HeapReAlloc( GetProcessHeap(), 0, lpMem, size );
|
---|
838 | if (!p)
|
---|
839 | {
|
---|
840 | dprintf2(("KERNEL32: HEAP_realloc(%08xh,%08xh) out of memory.\n",
|
---|
841 | lpMem,
|
---|
842 | size));
|
---|
843 | }
|
---|
844 | return p;
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 | /*****************************************************************************
|
---|
849 | * Name :
|
---|
850 | * Purpose :
|
---|
851 | * Parameters:
|
---|
852 | * Variables :
|
---|
853 | * Result :
|
---|
854 | * Remark :
|
---|
855 | * Status :
|
---|
856 | *
|
---|
857 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
858 | *****************************************************************************/
|
---|
859 |
|
---|
860 | BOOL WIN32API HEAP_free(LPVOID lpMem)
|
---|
861 | {
|
---|
862 | return HeapFree( GetProcessHeap(), 0, lpMem);
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 | /*****************************************************************************
|
---|
867 | * Name :
|
---|
868 | * Purpose :
|
---|
869 | * Parameters:
|
---|
870 | * Variables :
|
---|
871 | * Result :
|
---|
872 | * Remark :
|
---|
873 | * Status :
|
---|
874 | *
|
---|
875 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
876 | *****************************************************************************/
|
---|
877 |
|
---|
878 | LPSTR WIN32API HEAP_strdupA(HANDLE heap, DWORD flags, LPCSTR str )
|
---|
879 | {
|
---|
880 | int iLength = lstrlenA(str) + 1;
|
---|
881 | LPSTR p = (LPSTR)HEAP_xalloc( heap, flags, iLength );
|
---|
882 | memcpy( p, str, iLength );
|
---|
883 | return p;
|
---|
884 | }
|
---|
885 |
|
---|
886 |
|
---|
887 | /*****************************************************************************
|
---|
888 | * Name :
|
---|
889 | * Purpose :
|
---|
890 | * Parameters:
|
---|
891 | * Variables :
|
---|
892 | * Result :
|
---|
893 | * Remark :
|
---|
894 | * Status :
|
---|
895 | *
|
---|
896 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
897 | *****************************************************************************/
|
---|
898 |
|
---|
899 | LPWSTR WIN32API HEAP_strdupW(HANDLE heap, DWORD flags, LPCWSTR str )
|
---|
900 | {
|
---|
901 | INT len = lstrlenW(str) + 1;
|
---|
902 | LPWSTR p = (LPWSTR)HEAP_xalloc( heap, flags, len * sizeof(WCHAR) );
|
---|
903 | memcpy( p, str, len * sizeof(WCHAR) );
|
---|
904 | return p;
|
---|
905 | }
|
---|
906 |
|
---|
907 |
|
---|
908 | /*****************************************************************************
|
---|
909 | * Name :
|
---|
910 | * Purpose :
|
---|
911 | * Parameters:
|
---|
912 | * Variables :
|
---|
913 | * Result :
|
---|
914 | * Remark :
|
---|
915 | * Status :
|
---|
916 | *
|
---|
917 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
918 | *****************************************************************************/
|
---|
919 |
|
---|
920 | LPWSTR WIN32API HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR str )
|
---|
921 | {
|
---|
922 | LPWSTR ret;
|
---|
923 | int len;
|
---|
924 |
|
---|
925 | if (!str) return NULL;
|
---|
926 |
|
---|
927 | len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0);
|
---|
928 | ret = (LPWSTR)HEAP_xalloc( heap, flags, len*sizeof(WCHAR));
|
---|
929 | MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
|
---|
930 |
|
---|
931 | return ret;
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | /*****************************************************************************
|
---|
936 | * Name :
|
---|
937 | * Purpose :
|
---|
938 | * Parameters:
|
---|
939 | * Variables :
|
---|
940 | * Result :
|
---|
941 | * Remark :
|
---|
942 | * Status :
|
---|
943 | *
|
---|
944 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
945 | *****************************************************************************/
|
---|
946 |
|
---|
947 | LPSTR WIN32API HEAP_strdupWtoA(HANDLE heap, DWORD flags, LPCWSTR str )
|
---|
948 | {
|
---|
949 | LPSTR ret;
|
---|
950 | int len;
|
---|
951 |
|
---|
952 | if (!str) return NULL;
|
---|
953 |
|
---|
954 | len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, 0, NULL);
|
---|
955 | ret = (LPSTR)HEAP_xalloc( heap, flags, len);
|
---|
956 | WideCharToMultiByte(CP_ACP, 0, str, -1, ret, len, 0, NULL );
|
---|
957 | return ret;
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 |
|
---|
962 |
|
---|