source: trunk/src/kernel32/heapstring.cpp@ 5478

Last change on this file since 5478 was 5478, checked in by sandervl, 24 years ago

Removed WideCharToLocal & LocalToWideChar

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