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

Last change on this file since 3976 was 3976, checked in by phaller, 25 years ago

.

File size: 37.8 KB
Line 
1/* $Id: heapstring.cpp,v 1.31 2000-08-10 02:19:57 phaller 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 <heapstring.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 = tolower((char)*str1);
230 lastch = tolower((char)*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 wcscmp( (wchar_t*)arg1,
265 (wchar_t*)arg2 );
266}
267
268
269/*****************************************************************************
270 * Name :
271 * Purpose :
272 * Parameters:
273 * Variables :
274 * Result :
275 * Remark :
276 * Status :
277 *
278 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
279 *****************************************************************************/
280
281int WIN32API lstrncmpW(LPCWSTR arg1, LPCWSTR arg2, int l)
282{
283 dprintf2(("KERNEL32: OS2lstrncmpW(%08xh,%08xh,%d)\n",
284 arg1,
285 arg2,
286 l));
287
288 return wcsncmp((wchar_t*)arg1,
289 (wchar_t*)arg2,
290 l);
291}
292
293/*****************************************************************************
294 * Name :
295 * Purpose :
296 * Parameters:
297 * Variables :
298 * Result :
299 * Remark :
300 * Status :
301 *
302 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
303 *****************************************************************************/
304
305LPSTR WIN32API lstrcpyA(LPSTR dest, LPCSTR src)
306{
307 if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
308 return NULL;
309
310 return O32_lstrcpy(dest, src);
311}
312
313
314/*****************************************************************************
315 * Name :
316 * Purpose :
317 * Parameters:
318 * Variables :
319 * Result :
320 * Remark :
321 * Status :
322 *
323 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
324 *****************************************************************************/
325
326LPWSTR WIN32API lstrcpyW(LPWSTR dest, LPCWSTR src)
327{
328 if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
329 return NULL;
330
331 UniStrcpy( (UniChar*)dest,
332 (UniChar*)src );
333 return dest;
334}
335
336
337/*****************************************************************************
338 * Name :
339 * Purpose :
340 * Parameters:
341 * Variables :
342 * Result :
343 * Remark :
344 * Status :
345 *
346 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
347 *****************************************************************************/
348
349LPSTR WIN32API lstrcpynA(LPSTR arg1, LPCSTR arg2, int arg3)
350{
351 register LPSTR p1 = arg1;
352 register LPSTR p2 = (LPSTR)arg2;
353
354 dprintf2(("KERNEL32: OS2lstrcpyA(%08xh, %08xh, %08xh)\n",
355 arg1,
356 arg2,
357 arg3));
358
359 //PH: looks like either \0 or arg3 terminate the copy
360 //return strncpy(arg1, arg2, arg3);
361 arg3--; // pre-decrement to avoid exceeding buffer length
362 // results in better code than (arg1 > 1)
363
364 for (;*p2 && arg3; arg3--)
365 *p1++ = *p2++;
366
367 *p1 = 0; //CB: copy arg-1, set end 0
368
369 return arg1;
370}
371
372
373/*****************************************************************************
374 * Name :
375 * Purpose :
376 * Parameters:
377 * Variables :
378 * Result :
379 * Remark :
380 * Status :
381 *
382 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
383 *****************************************************************************/
384
385LPWSTR WIN32API lstrcpynW(LPWSTR dest, LPCWSTR src, int arg3)
386{
387 dprintf2(("KERNEL32: lstrcpynW(%08xh,%08xh,%08xh)",
388 dest,
389 src,
390 arg3));
391
392 if (arg3 == 0)
393 return NULL;
394
395 UniStrncpy( (UniChar*)dest,
396 (UniChar*)src,
397 arg3-1); //CB: copy arg3-1 characters
398 dest[arg3-1] = 0; //CB: set end
399 return dest;
400}
401
402
403/*****************************************************************************
404 * Name :
405 * Purpose :
406 * Parameters:
407 * Variables :
408 * Result :
409 * Remark :
410 * Status :
411 *
412 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
413 *****************************************************************************/
414
415int WIN32API lstrcmpiA(LPCSTR arg1, LPCSTR arg2)
416{
417 dprintf2(("KERNEL32: lstrcmpiA(%s,%s)\n",
418 arg1,
419 arg2));
420
421 if(arg1 == NULL)
422 return -1;
423
424 if(arg2 == NULL)
425 return 1;
426
427 return O32_lstrcmpi(arg1, arg2);
428}
429
430
431/*****************************************************************************
432 * Name :
433 * Purpose :
434 * Parameters:
435 * Variables :
436 * Result :
437 * Remark :
438 * Status :
439 *
440 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
441 *****************************************************************************/
442
443int WIN32API lstrcmpiW(LPCWSTR arg1, LPCWSTR arg2)
444{
445 char *astr1, *astr2;
446 int rc;
447
448 dprintf2(("KERNEL32: lstrcmpiW(%08xh,%08xh)\n",
449 arg1,
450 arg2));
451
452 if(arg1 == NULL)
453 return -1;
454
455 if(arg2 == NULL)
456 return 1;
457
458 // NOTE: This function has no equivalent in uunidef.h
459 astr1 = UnicodeToAsciiString((LPWSTR)arg1);
460 astr2 = UnicodeToAsciiString((LPWSTR)arg2);
461 rc = lstrcmpiA(astr1, astr2);
462 FreeAsciiString(astr2);
463 FreeAsciiString(astr1);
464 return(rc);
465}
466
467
468/*****************************************************************************
469 * Name :
470 * Purpose :
471 * Parameters:
472 * Variables :
473 * Result :
474 * Remark :
475 * Status :
476 *
477 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
478 *****************************************************************************/
479
480// unilen: length of astring buffer (including 0 terminator)
481// returns string length
482
483int WIN32API lstrcpynCtoA(LPSTR astring,
484 LPCWSTR ustring,
485 int unilen,
486 UconvObject uconv_object)
487{
488 int i;
489 int rc;
490 size_t uni_chars_left;
491 size_t out_bytes_left;
492 size_t num_subs;
493 UniChar* in_buf;
494 char* out_buf;
495
496 if (ustring == NULL)
497 {
498 if (astring != NULL && unilen > 0)
499 astring[0] = 0;
500 return 0;
501 }
502
503 if (astring == NULL || unilen <= 0)
504 return 0;
505
506 if (uconv_object)
507 {
508 if (unilen == 1)
509 {
510 astring[0] = 0;
511 return 0; //no data
512 }
513
514 //SvL: Determine length of unicode string
515 uni_chars_left = UniStrlen((UniChar*)ustring)+1;
516 uni_chars_left = min(uni_chars_left, unilen);
517 unilen = uni_chars_left;
518
519 out_bytes_left = uni_chars_left; //size in bytes == elements
520 in_buf = (UniChar*)ustring;
521 out_buf = astring;
522 rc = UniUconvFromUcs(uconv_object,
523 &in_buf, &uni_chars_left,
524 (void**)&out_buf, &out_bytes_left,
525 &num_subs);
526
527 /* @@@PH 2000/08/10
528 * this causes the last character in the converted
529 * target string to be chopped off. I.e. causes CMD.EXE
530 * to loose the CRLF functionality.
531 */
532 /*
533 unilen -= 1+out_bytes_left; //end + left bytes
534 astring[unilen] = 0; //terminate
535 */
536
537 return unilen; //length of string (excluding terminator)
538 }
539 else
540 {
541 /* idiots unicode conversion :) */
542 for (i = 0; i < unilen-1; i++)
543 {
544 astring[i] = (ustring[i] > 255) ? (char)20 : (char)ustring[i]; //CB: handle invalid characters as space
545 if (ustring[i] == 0) return i; //asta la vista, baby
546 }
547
548 astring[unilen-1] = 0; // @@@PH: 1999/06/09 fix - always terminate string
549
550 return(unilen-1);
551 }
552}
553
554int WIN32API lstrcpynWtoA(LPSTR astring,
555 LPCWSTR ustring,
556 int unilen)
557{
558 return lstrcpynCtoA(astring, ustring, unilen, GetWindowsUconvObject());
559}
560
561
562/*****************************************************************************
563 * Name :
564 * Purpose :
565 * Parameters:
566 * Variables :
567 * Result :
568 * Remark :
569 * Status :
570 *
571 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
572 *****************************************************************************/
573
574// asciilen: max length of unicode buffer (including end 0)
575// @@@PH 0 termination is NOT necessarily included !
576int lstrcpynAtoC(LPWSTR unicode,
577 LPCSTR ascii,
578 int asciilen,
579 UconvObject uconv_object)
580{
581 int rc;
582 int i;
583 size_t uni_chars_left;
584 size_t in_bytes_left;
585 size_t num_subs;
586 UniChar* out_buf;
587 char* in_buf;
588
589 dprintf2(("KERNEL32: HeapString: lstrcpynAtoW(%s,%08xh,%d)\n",
590 ascii,
591 unicode,
592 asciilen));
593
594 //CB: no input, set at least terminator
595 if (ascii == NULL)
596 {
597 if (unicode != NULL && asciilen > 0) unicode[0] = 0;
598 return 0;
599 }
600
601 if (unicode == NULL || asciilen <= 0)
602 return 0; //nothing to do
603
604 if (uconv_object)
605 {
606 //@@@PH what's this?
607 if ((asciilen == 1) && (*ascii == '\0') )
608 {
609 unicode[0] = 0;
610 return 0;
611 }
612
613 in_buf = (LPSTR)ascii;
614
615 //@@@PH what's this?
616 //in_bytes_left = asciilen-1; //buffer size in bytes
617
618 //SvL: Determine length of ascii string
619 in_bytes_left = strlen(in_buf)+1;
620 in_bytes_left = asciilen = min(in_bytes_left, asciilen); //buffer size in bytes
621
622 out_buf = (UniChar*)unicode;
623
624 uni_chars_left = in_bytes_left; //elements
625
626 rc = UniUconvToUcs( uconv_object,
627 (void**)&in_buf, &in_bytes_left,
628 &out_buf, &uni_chars_left,
629 &num_subs );
630
631 /* @@@PH 2000/08/10
632 * this causes the last character in the converted
633 * target string to be chopped off. I.e. causes CMD.EXE
634 * to enter command correctly.
635 */
636 /*
637 asciilen -= 1+uni_chars_left; //end + left bytes
638
639 unicode[asciilen] = 0; // always terminate string
640 */
641
642 return asciilen; //length of string (excluding terminator)
643 }
644 else
645 { //poor man's conversion
646
647// for(i = 0;i < asciilen-1;i++)
648 for(i = 0;i < asciilen;i++)
649 {
650 unicode[i] = ascii[i];
651 if (ascii[i] == 0)
652 //return i-1; //work done
653 return i; //work done
654 }
655
656// unicode[asciilen-1] = 0;
657// return asciilen-1;
658 return asciilen;
659 }
660}
661
662int WIN32API lstrcpynAtoW(LPWSTR unicode,
663 LPCSTR ascii,
664 int asciilen)
665{
666 return lstrcpynAtoC(unicode, ascii, asciilen, GetWindowsUconvObject());
667}
668
669/*****************************************************************************
670 * Name :
671 * Purpose : Converts unicode string to ascii string
672 * Parameters:
673 * Variables :
674 * Result : returns length of ascii string
675 * Remark :
676 * Status :
677 *
678 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
679 *****************************************************************************/
680
681LPSTR WIN32API lstrcpyWtoA(LPSTR ascii, LPCWSTR unicode)
682{
683 //@@@PH huh? wuz dat?
684 if (unicode == NULL)
685 {
686 if (unicode != NULL) ((LPWSTR)unicode)[0] = 0; //CB: set at least end
687 return NULL;
688 }
689
690 if (unicode == NULL)
691 return NULL; /* garbage in, garbage out ! */
692
693 /* forward to function with len parameter */
694 lstrcpynWtoA(ascii,
695 unicode,
696 UniStrlen((UniChar*)unicode)+1); //end included
697
698 return ascii;
699}
700
701
702/*****************************************************************************
703 * Name :
704 * Purpose : Copies the full string from ascii to unicode
705 * Parameters:
706 * Variables :
707 * Result :
708 * Remark :
709 * Status :
710 *
711 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
712 *****************************************************************************/
713
714LPWSTR WIN32API lstrcpyAtoW(LPWSTR unicode, LPCSTR ascii)
715{
716 /* achimha for security, strlen might trap if garbage in */
717 /* @@@PH 98/06/07 */
718 if (ascii == NULL)
719 {
720 if (unicode != NULL) unicode[0] = 0; //CB: set at least end
721 return NULL;
722 }
723
724 if (unicode == NULL)
725 return NULL; /* garbage in, garbage out ! */
726
727 /* forward to call with length parameter */
728 lstrcpynAtoW(unicode, ascii, strlen(ascii)+1); //end included
729 return (unicode);
730}
731
732
733
734
735/*****************************************************************************
736 * Name :
737 * Purpose :
738 * Parameters:
739 * Variables :
740 * Result :
741 * Remark :
742 * Status :
743 *
744 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
745 *****************************************************************************/
746
747LPVOID WIN32API HEAP_xalloc( HANDLE heap, DWORD flags, DWORD size )
748{
749 LPVOID p = HeapAlloc( heap, flags, size );
750 if (!p)
751 {
752 dprintf2(("KERNEL32: HEAP_xalloc(%08xh,%08xh,%08xh) out of memory.\n",
753 heap,
754 flags,
755 size));
756 }
757 return p;
758}
759
760
761/*****************************************************************************
762 * Name :
763 * Purpose :
764 * Parameters:
765 * Variables :
766 * Result :
767 * Remark :
768 * Status :
769 *
770 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
771 *****************************************************************************/
772
773LPVOID WIN32API HEAP_xrealloc( HANDLE heap, DWORD flags, LPVOID lpMem, DWORD size )
774{
775 LPVOID p = HeapReAlloc( heap, flags, lpMem, size );
776 if (!p)
777 {
778 dprintf2(("KERNEL32: HEAP_xrealloc(%08xh,%08xh,%08xh,%08xh) out of memory.\n",
779 heap,
780 flags,
781 lpMem,
782 size));
783 }
784 return p;
785}
786
787
788/*****************************************************************************
789 * Name :
790 * Purpose :
791 * Parameters:
792 * Variables :
793 * Result :
794 * Remark :
795 * Status :
796 *
797 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
798 *****************************************************************************/
799
800LPVOID WIN32API HEAP_malloc(DWORD size )
801{
802 LPVOID p = HeapAlloc( GetProcessHeap(), 0, size );
803 if (!p)
804 {
805 dprintf2(("KERNEL32: HEAP_malloc(%08xh) out of memory.\n",
806 size));
807 }
808 return p;
809}
810
811
812/*****************************************************************************
813 * Name :
814 * Purpose :
815 * Parameters:
816 * Variables :
817 * Result :
818 * Remark :
819 * Status :
820 *
821 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
822 *****************************************************************************/
823
824LPVOID WIN32API HEAP_realloc(LPVOID lpMem, DWORD size )
825{
826 LPVOID p = HeapReAlloc( GetProcessHeap(), 0, lpMem, size );
827 if (!p)
828 {
829 dprintf2(("KERNEL32: HEAP_realloc(%08xh,%08xh) out of memory.\n",
830 lpMem,
831 size));
832 }
833 return p;
834}
835
836
837/*****************************************************************************
838 * Name :
839 * Purpose :
840 * Parameters:
841 * Variables :
842 * Result :
843 * Remark :
844 * Status :
845 *
846 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
847 *****************************************************************************/
848
849VOID WIN32API HEAP_free(LPVOID lpMem)
850{
851 dprintf2(("KERNEL32: HEAP_free(%08xh)\n",
852 lpMem));
853
854 HeapFree( GetProcessHeap(), 0, lpMem);
855}
856
857
858/*****************************************************************************
859 * Name :
860 * Purpose :
861 * Parameters:
862 * Variables :
863 * Result :
864 * Remark :
865 * Status :
866 *
867 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
868 *****************************************************************************/
869
870LPSTR WIN32API HEAP_strdupA( HANDLE heap, DWORD flags, LPCSTR str )
871{
872 LPSTR p = (LPSTR)HEAP_xalloc( heap, flags, strlen(str) + 1 );
873 strcpy( p, str );
874 return p;
875}
876
877
878/*****************************************************************************
879 * Name :
880 * Purpose :
881 * Parameters:
882 * Variables :
883 * Result :
884 * Remark :
885 * Status :
886 *
887 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
888 *****************************************************************************/
889
890LPWSTR WIN32API HEAP_strdupW( HANDLE heap, DWORD flags, LPCWSTR str )
891{
892 INT len = lstrlenW(str) + 1;
893 LPWSTR p = (LPWSTR)HEAP_xalloc( heap, flags, len * sizeof(WCHAR) );
894 lstrcpyW( p, str );
895 return p;
896}
897
898
899/*****************************************************************************
900 * Name :
901 * Purpose :
902 * Parameters:
903 * Variables :
904 * Result :
905 * Remark :
906 * Status :
907 *
908 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
909 *****************************************************************************/
910
911LPWSTR WIN32API HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str )
912{
913 LPWSTR ret;
914
915 if (!str) return NULL;
916 ret = (LPWSTR)HEAP_xalloc( heap, flags, (strlen(str)+1) * sizeof(WCHAR) );
917 lstrcpyAtoW( ret, (LPSTR)str );
918 return ret;
919}
920
921
922/*****************************************************************************
923 * Name :
924 * Purpose :
925 * Parameters:
926 * Variables :
927 * Result :
928 * Remark :
929 * Status :
930 *
931 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
932 *****************************************************************************/
933
934LPSTR WIN32API HEAP_strdupWtoA( HANDLE heap, DWORD flags, LPCWSTR str )
935{
936 LPSTR ret;
937
938 if (!str) return NULL;
939 ret = (LPSTR)HEAP_xalloc( heap, flags, lstrlenW(str) + 1 );
940 lstrcpyWtoA( ret, (LPWSTR)str );
941 return ret;
942}
943
944
945/*****************************************************************************
946 * Name : WideCharToLocal
947 * Purpose : similar lstrcpyWtoA, should handle codepages properly
948 * Parameters:
949 * Variables :
950 * Result : strlen of the destination string
951 * Remark :
952 * Status :
953 *
954 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
955 *****************************************************************************/
956
957INT WIN32API WideCharToLocal(LPSTR pLocal, LPWSTR pWide, INT dwChars)
958{
959 dprintf2(("KERNEL32: WideCharToLocal(%08xh,%08xh,%08xh)\n",
960 pLocal,
961 pWide,
962 dwChars));
963
964 *pLocal = 0;
965 WideCharToMultiByte(CP_ACP,
966 0,
967 pWide,
968 -1,
969 pLocal,
970 dwChars,
971 NULL,
972 NULL);
973
974 return strlen(pLocal);
975}
976
977
978/*****************************************************************************
979 * Name : LocalToWideChar
980 * Purpose : similar lstrcpyAtoW, should handle codepages properly
981 * Parameters:
982 * Variables :
983 * Result : strlen of the destination string
984 * Remark :
985 * Status :
986 *
987 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
988 *****************************************************************************/
989
990INT WIN32API LocalToWideChar(LPWSTR pWide, LPSTR pLocal, INT dwChars)
991{
992 *pWide = 0;
993
994 dprintf2(("KERNEL32: LocalToWideChar(%08xh,%08xh,%08xh)\n",
995 pLocal,
996 pWide,
997 dwChars));
998
999 MultiByteToWideChar(CP_ACP,
1000 0,
1001 pLocal,
1002 -1,
1003 pWide,
1004 dwChars);
1005
1006 return lstrlenW(pWide);
1007}
1008
1009
1010
1011
1012
1013
1014#if 0
1015
1016
1017/*****************************************************************************
1018 * Name :
1019 * Purpose :
1020 * Parameters:
1021 * Variables :
1022 * Result :
1023 * Remark :
1024 * Status :
1025 *
1026 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
1027 *****************************************************************************/
1028
1029// Converts unicode string to ascii string
1030// returns pointer to ascii string
1031char * WIN32API UnicodeToAsciiString(WCHAR *ustring)
1032{
1033 char *astring;
1034
1035 if(ustring == NULL) return(NULL);
1036
1037 astring = (char *)malloc( 1 + UniStrlen((UniChar*)ustring) );
1038 UnicodeToAscii( ustring, astring );
1039 return(astring);
1040}
1041
1042
1043/*****************************************************************************
1044 * Name :
1045 * Purpose :
1046 * Parameters:
1047 * Variables :
1048 * Result :
1049 * Remark :
1050 * Status :
1051 *
1052 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
1053 *****************************************************************************/
1054
1055// Converts ascii string to unicode string
1056// returns pointer to unicode string
1057WCHAR * WIN32API AsciiToUnicodeString(char *astring)
1058{
1059 WCHAR *ustring;
1060
1061 if(astring == NULL)
1062 return(NULL);
1063
1064 ustring = (WCHAR *)malloc( 1 + strlen(astring) << 1 );
1065 AsciiToUnicode( astring, ustring );
1066 return(ustring);
1067}
1068
1069#endif
1070
1071
1072
1073/**************************************************************************
1074 * This function is used just locally !
1075 * Description: Inverts a string.
1076 */
1077static void OLE_InvertString(char* string)
1078{
1079 char sTmpArray[128];
1080 INT counter, i = 0;
1081
1082 for (counter = strlen(string); counter > 0; counter--)
1083 {
1084 memcpy(sTmpArray + i, string + counter-1, 1);
1085 i++;
1086 }
1087 memcpy(sTmpArray + i, "\0", 1);
1088 strcpy(string, sTmpArray);
1089}
1090
1091/***************************************************************************************
1092 * This function is used just locally !
1093 * Description: Test if the given string (psNumber) is valid or not.
1094 * The valid characters are the following:
1095 * - Characters '0' through '9'.
1096 * - One decimal point (dot) if the number is a floating-point value.
1097 * - A minus sign in the first character position if the number is
1098 * a negative value.
1099 * If the function succeeds, psBefore/psAfter will point to the string
1100 * on the right/left of the decimal symbol. pbNegative indicates if the
1101 * number is negative.
1102 */
1103static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
1104{
1105char sNumberSet[] = "0123456789";
1106BOOL bInDecimal = FALSE;
1107
1108 /* Test if we do have a minus sign */
1109 if ( *pInput == '-' )
1110 {
1111 *pbNegative = TRUE;
1112 pInput++; /* Jump to the next character. */
1113 }
1114
1115 while(*pInput != '\0')
1116 {
1117 /* Do we have a valid numeric character */
1118 if ( strchr(sNumberSet, *pInput) != NULL )
1119 {
1120 if (bInDecimal == TRUE)
1121 *psAfter++ = *pInput;
1122 else
1123 *psBefore++ = *pInput;
1124 }
1125 else
1126 {
1127 /* Is this a decimal point (dot) */
1128 if ( *pInput == '.' )
1129 {
1130 /* Is it the first time we find it */
1131 if ((bInDecimal == FALSE))
1132 bInDecimal = TRUE;
1133 else
1134 return -1; /* ERROR: Invalid parameter */
1135 }
1136 else
1137 {
1138 /* It's neither a numeric character, nor a decimal point.
1139 * Thus, return an error.
1140 */
1141 return -1;
1142 }
1143 }
1144 pInput++;
1145 }
1146
1147 /* Add an End of Line character to the output buffers */
1148 *psBefore = '\0';
1149 *psAfter = '\0';
1150
1151 return 0;
1152}
1153
1154/**************************************************************************
1155 * This function is used just locally !
1156 * Description: A number could be formatted using different numbers
1157 * of "digits in group" (example: 4;3;2;0).
1158 * The first parameter of this function is an array
1159 * containing the rule to be used. It's format is the following:
1160 * |NDG|DG1|DG2|...|0|
1161 * where NDG is the number of used "digits in group" and DG1, DG2,
1162 * are the corresponding "digits in group".
1163 * Thus, this function returns the grouping value in the array
1164 * pointed by the second parameter.
1165 */
1166static INT OLE_GetGrouping(char* sRule, INT index)
1167{
1168 char sData[2], sRuleSize[2];
1169 INT nData, nRuleSize;
1170
1171 memcpy(sRuleSize, sRule, 1);
1172 memcpy(sRuleSize+1, "\0", 1);
1173 nRuleSize = atoi(sRuleSize);
1174
1175 if (index > 0 && index < nRuleSize)
1176 {
1177 memcpy(sData, sRule+index, 1);
1178 memcpy(sData+1, "\0", 1);
1179 nData = atoi(sData);
1180 }
1181
1182 else
1183 {
1184 memcpy(sData, sRule+nRuleSize-1, 1);
1185 memcpy(sData+1, "\0", 1);
1186 nData = atoi(sData);
1187 }
1188
1189 return nData;
1190}
1191
1192
1193/*****************************************************************************
1194 * Name : GetNumberFormat
1195 * Purpose : format a given number string according to local settings
1196 * Parameters:
1197 * Variables :
1198 * Result :
1199 * Remark : KERNEL32.355
1200 * Status :
1201 *
1202 * Author : Patrick Haller [Sun, 2000/06/12 12:46]
1203 *****************************************************************************/
1204
1205
1206ODINFUNCTION6(INT, GetNumberFormatA,
1207 LCID, locale,
1208 DWORD, dwflags,
1209 LPCSTR, lpvalue,
1210 CONST NUMBERFMTA *,lpFormat,
1211 LPSTR, lpNumberStr,
1212 int, cchNumber)
1213{
1214 dprintf(("not properly implemented.\n"));
1215
1216 char sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
1217 INT nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
1218 char sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
1219 char sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];
1220 char *pStr = NULL, *pTmpStr = NULL;
1221 LCID systemDefaultLCID;
1222 BOOL bNegative = FALSE;
1223 enum Operations
1224 {
1225 USE_PARAMETER,
1226 USE_LOCALEINFO,
1227 USE_SYSTEMDEFAULT,
1228 RETURN_ERROR
1229 } used_operation;
1230
1231 strncpy(sNumber, lpvalue, 128);
1232 sNumber[127] = '\0';
1233
1234 /* Make sure we have a valid input string, get the number
1235 * of digits before and after the decimal symbol, and check
1236 * if this is a negative number.
1237 */
1238 if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
1239 {
1240 nNumberDecimal = strlen(sDigitsBeforeDecimal);
1241 nDigits = strlen(sDigitsAfterDecimal);
1242 }
1243 else
1244 {
1245 SetLastError(ERROR_INVALID_PARAMETER);
1246 return 0;
1247 }
1248
1249 /* Which source will we use to format the string */
1250 used_operation = RETURN_ERROR;
1251 if (lpFormat != NULL)
1252 {
1253 if (dwflags == 0)
1254 used_operation = USE_PARAMETER;
1255 }
1256 else
1257 {
1258 if (dwflags & LOCALE_NOUSEROVERRIDE)
1259 used_operation = USE_LOCALEINFO;
1260 else
1261 used_operation = USE_SYSTEMDEFAULT;
1262 }
1263
1264 /* Load the fields we need */
1265 switch(used_operation)
1266 {
1267 case USE_LOCALEINFO:
1268 GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
1269 GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
1270 GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
1271 GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
1272 GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
1273 GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
1274 break;
1275 case USE_PARAMETER:
1276 sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
1277 strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
1278 sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
1279 strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
1280 sprintf(sILZero, "%d",lpFormat->LeadingZero);
1281 sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
1282 break;
1283 case USE_SYSTEMDEFAULT:
1284 systemDefaultLCID = GetSystemDefaultLCID();
1285 GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
1286 GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
1287 GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
1288 GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
1289 GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
1290 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
1291 break;
1292 default:
1293 SetLastError(ERROR_INVALID_PARAMETER);
1294 return 0;
1295 }
1296
1297 nNumberDigits = atoi(sNumberDigits);
1298
1299 /* Remove the ";" */
1300 i=0;
1301 j = 1;
1302 for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
1303 {
1304 if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
1305 {
1306 memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
1307 i++;
1308 j++;
1309 }
1310 }
1311 sprintf(sBuffer, "%d", i);
1312 memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
1313 memcpy(sRule + j, "\0", 1);
1314
1315 /* First, format the digits before the decimal. */
1316 if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0))
1317 {
1318 /* Working on an inverted string is easier ! */
1319 OLE_InvertString(sDigitsBeforeDecimal);
1320
1321 nStep = nCounter = i = j = 0;
1322 nRuleIndex = 1;
1323 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
1324
1325 /* Here, we will loop until we reach the end of the string.
1326 * An internal counter (j) is used in order to know when to
1327 * insert the "digit group symbol".
1328 */
1329 while (nNumberDecimal > 0)
1330 {
1331 i = nCounter + nStep;
1332 memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
1333 nCounter++;
1334 j++;
1335 if (j >= nGrouping)
1336 {
1337 j = 0;
1338 if (nRuleIndex < sRule[0])
1339 nRuleIndex++;
1340 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
1341 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
1342 nStep+= strlen(sDigitGroupSymbol);
1343 }
1344
1345 nNumberDecimal--;
1346 }
1347
1348 memcpy(sDestination + i+1, "\0", 1);
1349 /* Get the string in the right order ! */
1350 OLE_InvertString(sDestination);
1351 }
1352 else
1353 {
1354 nLZ = atoi(sILZero);
1355 if (nLZ != 0)
1356 {
1357 /* Use 0.xxx instead of .xxx */
1358 memcpy(sDestination, "0", 1);
1359 memcpy(sDestination+1, "\0", 1);
1360 }
1361 else
1362 memcpy(sDestination, "\0", 1);
1363
1364 }
1365
1366 /* Second, format the digits after the decimal. */
1367 j = 0;
1368 nCounter = nNumberDigits;
1369 if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
1370 {
1371 i = strlen(sNumber) - strlen(pStr) + 1;
1372 strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
1373 j = strlen(sDigitsAfterDecimal);
1374 if (j < nNumberDigits)
1375 nCounter = nNumberDigits-j;
1376 }
1377 for (i=0;i<nCounter;i++)
1378 memcpy(sDigitsAfterDecimal+i+j, "0", 1);
1379 memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);
1380
1381 i = strlen(sDestination);
1382 j = strlen(sDigitsAfterDecimal);
1383 /* Finally, construct the resulting formatted string. */
1384
1385 for (nCounter=0; nCounter<i; nCounter++)
1386 memcpy(sNumber + nCounter, sDestination + nCounter, 1);
1387
1388 memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
1389
1390 for (i=0; i<j; i++)
1391 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
1392 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
1393
1394 /* Is it a negative number */
1395 if (bNegative == TRUE)
1396 {
1397 i = atoi(sNegNumber);
1398 pStr = sDestination;
1399 pTmpStr = sNumber;
1400 switch (i)
1401 {
1402 case 0:
1403 *pStr++ = '(';
1404 while (*sNumber != '\0')
1405 *pStr++ = *pTmpStr++;
1406 *pStr++ = ')';
1407 break;
1408 case 1:
1409 *pStr++ = '-';
1410 while (*pTmpStr != '\0')
1411 *pStr++ = *pTmpStr++;
1412 break;
1413 case 2:
1414 *pStr++ = '-';
1415 *pStr++ = ' ';
1416 while (*pTmpStr != '\0')
1417 *pStr++ = *pTmpStr++;
1418 break;
1419 case 3:
1420 while (*pTmpStr != '\0')
1421 *pStr++ = *pTmpStr++;
1422 *pStr++ = '-';
1423 break;
1424 case 4:
1425 while (*pTmpStr != '\0')
1426 *pStr++ = *pTmpStr++;
1427 *pStr++ = ' ';
1428 *pStr++ = '-';
1429 break;
1430 default:
1431 while (*pTmpStr != '\0')
1432 *pStr++ = *pTmpStr++;
1433 break;
1434 }
1435 }
1436 else
1437 strcpy(sDestination, sNumber);
1438
1439 /* If cchNumber is zero, then returns the number of bytes or characters
1440 * required to hold the formatted number string
1441 */
1442 if (cchNumber==0)
1443 retVal = strlen(sDestination) + 1;
1444 else
1445 {
1446 strncpy (lpNumberStr, sDestination, cchNumber);
1447 lpNumberStr[cchNumber-1] = '\0'; /* ensure we got a NULL at the end */
1448 retVal = strlen(lpNumberStr);
1449 }
1450
1451 return retVal;
1452}
1453
1454
1455
1456
1457/*****************************************************************************
1458 * Name : GetNumberFormat
1459 * Purpose : format a given number string according to local settings
1460 * Parameters:
1461 * Variables :
1462 * Result :
1463 * Remark :
1464 * Status :
1465 *
1466 * Author : Patrick Haller [Sun, 2000/06/12 12:46]
1467 *****************************************************************************/
1468
1469int WIN32API GetNumberFormatW(LCID Locale,
1470 DWORD dwFlags,
1471 LPCWSTR lpValue,
1472 CONST NUMBERFMTW *lpFormat,
1473 LPWSTR lpNumberStr,
1474 int cchNumber)
1475{
1476 dprintf(("GetNumberFormatW(%08x,%08x,%s,%08x,%s,%08x) not properly implemented.\n",
1477 Locale,
1478 dwFlags,
1479 lpValue,
1480 lpFormat,
1481 lpNumberStr,
1482 cchNumber));
1483
1484 // @@@PH cheap ass emulation
1485 lstrcpynW(lpNumberStr,
1486 lpValue,
1487 cchNumber);
1488
1489 return lstrlenW(lpNumberStr);
1490}
1491
Note: See TracBrowser for help on using the repository browser.