-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopennurbs_defines.cpp
More file actions
2266 lines (1999 loc) · 61.3 KB
/
opennurbs_defines.cpp
File metadata and controls
2266 lines (1999 loc) · 61.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
*/
#include "opennurbs.h"
// {EA2EFFD2-C9A9-4cb1-BE15-D2F46290F1A1}
//const ON_UUID ON_MaterialRef::material_from_layer =
//{ 0xea2effd2, 0xc9a9, 0x4cb1, { 0xbe, 0x15, 0xd2, 0xf4, 0x62, 0x90, 0xf1, 0xa1 } };
// {86EDFDE4-8AAF-4bcd-AB7C-F7111978D7FE}
//const ON_UUID ON_MaterialRef::material_from_parent =
//{ 0x86edfde4, 0x8aaf, 0x4bcd, { 0xab, 0x7c, 0xf7, 0x11, 0x19, 0x78, 0xd7, 0xfe } };
// on_stricmp() is a wrapper for case insensitive string compare
// and calls one of _stricmp(), stricmp(), or strcasecmp()
// depending on OS.
int on_stricmp(const char * s1, const char * s2)
{
#if defined(ON_OS_WINDOWS)
//return stricmp(s1,s2);
return _stricmp(s1,s2);
#else
return strcasecmp(s1,s2);
#endif
}
// on_strupr() calls _strupr() or strupr() depending on OS
char* on_strupr(char* s)
{
#if defined(ON_OS_WINDOWS)
return _strupr(s); // ANSI name
#else
if (s) {
while (*s) {
*s = toupper(*s);
s++;
}
}
return s;
#endif
}
// on_strlwr() calls _strlwr() or strlwr() depending on OS
char* on_strlwr(char* s)
{
#if defined(ON_OS_WINDOWS)
return _strlwr(s); // ANSI name
#else
if (s) {
while (*s) {
*s = tolower(*s);
s++;
}
}
return s;
#endif
}
// on_strrev() calls _strrev() or strrev() depending on OS
char* on_strrev(char* s)
{
#if defined(ON_OS_WINDOWS)
return _strrev(s); // ANSI name
#else
int i, j;
char c;
for ( i = 0, j = ((int)strlen(s))-1; i < j; i++, j-- ) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
return s;
#endif
}
// Windows code page support
// Only ON_SetStringConversionWindowsLocaleID,
// ON_GetStringConversionWindowsLocaleID, and
// on_wcsicmp should look at g_s__windows_locale_id
// and g_s__windows_locale_os.
static unsigned int g_s__windows_locale_id = 0;
static unsigned int g_s__windows_locale_os = 0; // 0 = Win 95/98/ME...
// 1 = Win NT/2000/XP...
unsigned int ON_SetStringConversionWindowsLocaleID(unsigned int locale_id, ON_BOOL32 bWin9X)
{
unsigned int rc = g_s__windows_locale_id;
g_s__windows_locale_os = bWin9X ? 0 : 1;
g_s__windows_locale_id = locale_id?true:false;
return rc;
}
unsigned int ON_GetStringConversionWindowsLocaleID()
{
return g_s__windows_locale_id;
}
static int on__hack__tolower(int c)
{
// This tolower is a simple "hack" to provide something that
// sort of works when OpenNURBS is used with a compiler that
// fails to provide functional localization tools.
// TODO:
// Expand these switch statments as users provide support
// for symbols. This is not the correct way to solve this
// problem, but it will work in some simple cases.
// If you are using Microsoft Developer studio in Windows,
// then this code is never called.
//
// Before you get too carried away, study
//
// http://www.microsoft.com/globaldev/wrguide/WRG_sort.asp
//
// and make sure what you are attempting to do is worth the
// trouble. In short, this approach is doomed to fail
// and is not good enough for robust applications that want
// to work well with most languages.
//
// That said, please post your additions to the OpenNURBS
// newsgroup so they can be included in future
// distrubutions.
int i;
if ( c <= 0 )
{
i = c;
}
else if ( c <= 127 )
{
// ASCII character
i = tolower(c);
}
else if ( c <= 255 )
{
// "extended" ASCII character
switch(c)
{
case 192: // UNICODE Latin capital letter A with grave (A`)
i = 224; // UNICODE Latin small letter A with grave (a`)
break;
case 193: // UNICODE Latin capital letter A with acute (A')
i = 225; // UNICODE Latin small letter A with acute (a')
break;
case 194: // UNICODE Latin capital letter A with circumflex (A^)
i = 226; // UNICODE Latin small letter A with circumflex (a^)
break;
case 195: // UNICODE Latin capital letter A with tilde (A~)
i = 227; // UNICODE Latin small letter A with tilde (a~)
break;
case 196: // UNICODE Latin capital letter A with diaeresis (A")
i = 228; // UNICODE Latin small letter A with diaeresis (a")
break;
case 197: // UNICODE Latin capital letter A with ring above (A ring above)
i = 229; // UNICODE Latin small letter A with ring above (a ring above)
break;
case 198: // UNICODE Latin capital letter Ae
i = 230; // UNICODE Latin small letter Ae
break;
case 199: // UNICODE Latin capital letter C with cedilla (C,)
i = 231; // UNICODE Latin small letter C with cedilla (c,)
break;
case 200: // UNICODE Latin capital letter E with grave (E`)
i = 232; // UNICODE Latin small letter E with grave (e`)
break;
case 201: // UNICODE Latin capital letter E with acute (E')
i = 233; // UNICODE Latin small letter E with acute (e')
break;
case 202: // UNICODE Latin capital letter E with circumflex (E^)
i = 234; // UNICODE Latin small letter E with circumflex (e^)
break;
case 203: // UNICODE Latin capital letter E with diaeresis (E")
i = 235; // UNICODE Latin small letter E with diaeresis (e")
break;
case 204: // UNICODE Latin capital letter I with grave (I`)
i = 236; // UNICODE Latin small letter I with grave (i`)
break;
case 205: // UNICODE Latin capital letter I with acute (I')
i = 237; // UNICODE Latin small letter I with acute (i')
break;
case 206: // UNICODE Latin capital letter I with circumflex (I^)
i = 238; // UNICODE Latin small letter I with circumflex (i^)
break;
case 207: // UNICODE Latin capital letter I with diaeresis (I")
i = 239; // UNICODE Latin small letter I with diaeresis (i")
break;
case 208: // UNICODE Latin capital letter Eth (ED)
i = 240; // UNICODE Latin small letter Eth (ed)
break;
case 209: // UNICODE Latin capital letter N with tilde (N~)
i = 241; // UNICODE Latin small letter n with tilde (n~)
break;
case 210: // UNICODE Latin capital letter O with grave (O`)
i = 242; // UNICODE Latin small letter O with grave (o`)
break;
case 211: // UNICODE Latin capital letter O with acute (O')
i = 243; // UNICODE Latin small letter O with acute (o')
break;
case 212: // UNICODE Latin capital letter O with circumflex (O^)
i = 244; // UNICODE Latin small letter O with circumflex (o^)
break;
case 213: // UNICODE Latin capital letter O with tilde (O~)
i = 245; // UNICODE Latin small letter O with tilde (o~)
break;
case 214: // UNICODE Latin capital letter O with diaeresis (O")
i = 246; // UNICODE Latin small letter O with diaeresis (o")
break;
case 216: // UNICODE Latin capital letter O with stroke (O/)
i = 248; // UNICODE Latin small letter O with stroke (o/)
break;
case 217: // UNICODE Latin capital letter U with grave (U`)
i = 249; // UNICODE Latin small letter U with grave (u`)
break;
case 218: // UNICODE Latin capital letter U with acute (U')
i = 250; // UNICODE Latin small letter U with acute (u')
break;
case 219: // UNICODE Latin capital letter U with circumflex (U^)
i = 251; // UNICODE Latin small letter U with circumflex (u^)
break;
case 220: // UNICODE Latin capital letter U with diaeresis (U")
i = 252; // UNICODE Latin small letter U with diaeresis (u")
break;
case 221: // UNICODE Latin capital letter Y with acute (Y')
i = 253; // UNICODE Latin small letter Y with acute (y')
break;
case 222: // UNICODE Latin capital letter Thorn (P|)
i = 254; // UNICODE Latin small letter Thorn (p|)
break;
default:
i = c;
break;
}
}
else if ( c <= 0x0177 )
{
if ( 0 == (c % 2) )
i = c+1;
else
i = c;
}
else if ( c <= 0x0192 )
{
switch( c)
{
case 0x0178: // UNICODE Latin capital letter Y with diaeresis (Y")
i = 0x00FF; // UNICODE Latin small letter Y with diaeresis (y")
break;
case 0x0179: // UNICODE Latin capital letter Z with acute (Z')
i = 0x017A; // UNICODE Latin small letter Z with acute (z')
break;
case 0x017B: // UNICODE Latin capital letter Z with dot above
i = 0x017C; // UNICODE Latin small letter Z with dot above
break;
case 0x017D: // UNICODE Latin capital letter Z with caron
i = 0x017E; // UNICODE Latin small letter Z with caron
break;
case 0x018F: // UNICODE Latin capital letter Schwa
i = 0x0259; // UNICODE Latin small letter Schwa
break;
default:
i = c;
break;
}
}
else if ( c <= 0x01FF )
{
if ( 0 == (c % 2) )
i = c+1;
else
i = c;
}
else
{
// My apologies to those of you whose languages use these symbols.
// I am too ignorant to make good guesses at what to do here.
// Please fill in as needed and post your changes so I can include
// them in future versions of OpenNURBS.
switch (c)
{
// example
case 0x0391: // UNICODE Greek capital letter alpha
i = 0x03B1; // UNICODE Greek small letter alpha
break;
default:
i = c;
}
}
return i;
}
static
int on__hack__wcsicmp( const wchar_t* s1, const wchar_t* s2)
{
// This "hack" case insensitive wchar_t compare tool is used
// when OpenNURBS is compiled in a development environment
// that does not provide proper localization support.
// handle NULL strings consistently and without crashing.
if ( !s1 )
{
return s2 ? -1 : 0;
}
else if ( !s2 )
{
return 1;
}
int rc, c1, c2;
do
{
c1 = on__hack__tolower(*s1++);
c2 = on__hack__tolower(*s2++);
rc = c1-c2;
}
while( 0 == rc && c1 && c2 );
return rc;
}
#if defined(ON_COMPILER_MSC)
// Disable the MSC /W4 unreachable code warning for the call to on__hack__wcsicmp()
#pragma warning( push )
#pragma warning( disable : 4702 )
#endif
int on_wcsicmp( const wchar_t* s1, const wchar_t* s2)
{
// handle NULL strings consistently and without crashing.
if ( !s1 )
{
return s2 ? -1 : 0;
}
else if ( !s2 )
{
return 1;
}
#if defined(ON_OS_WINDOWS)
#if defined(ON_COMPILER_BORLAND)
// Borland's compiler / C library
return wcscmpi(s1,s2);
#else
// Microsoft compiler
if ( 0 != g_s__windows_locale_id )
{
if ( 0 == g_s__windows_locale_os )
{
// On Win 95/98/ME, CompareStringW() doesn't work
// and CompareStringA() is glacial. So we test
// strings and use wcsicmp() whenever it will return
// the right answer.
{
const wchar_t* c1 = s1;
const wchar_t* c2 = s2;
while ( *c1 > 0 && *c1 < 128 && *c2 > 0 && *c2 < 128 )
{
c1++;
c2++;
}
if ( 0 == *c1 || 0 == *c2 )
{
#if defined(ON_COMPILER_MSC1400)
return _wcsicmp(s1,s2);
#else
return wcsicmp(s1,s2);
#endif
}
}
// These convert UNICODE to wide character strings
ON_String a(s1);
ON_String b(s2);
// Wide char conversion
int rc = ::CompareStringA(g_s__windows_locale_id,
NORM_IGNORECASE | NORM_IGNOREWIDTH,
a.Array(),
-1,
b.Array(),
-1);
if (rc == CSTR_LESS_THAN)
return -1;
if (rc == CSTR_EQUAL)
return 0;
if (rc == CSTR_GREATER_THAN)
return 1;
}
else
{
// a version of Windows with working UNICODE support
int rc = ::CompareStringW(g_s__windows_locale_id,
NORM_IGNORECASE | NORM_IGNOREWIDTH,
s1,
-1,
s2,
-1);
if (rc == CSTR_LESS_THAN)
return -1;
if (rc == CSTR_EQUAL)
return 0;
if (rc == CSTR_GREATER_THAN)
return 1;
}
}
// Microsoft's wcsicmp() doesn't work right for
// upper/lower case accented latin characters,
// upper/lower case cyrillic, upper/lower case Greek,
// Asian characters, etc.
//
// Basically, if the character code >= 127 or you are
// using a language other than US english, then
// Microsoft's wcsicmp() blows it.
//
#if defined(ON_COMPILER_MSC1400)
return _wcsicmp(s1,s2); // Microsoft's compiler / C library
#else
return wcsicmp(s1,s2); // Microsoft's compiler / C library
#endif
#endif
#endif
// If your compiler does not have a way to perform
// a case insensitive compare of UNICODE strings,
// then use the "hack" version below.
return on__hack__wcsicmp(s1,s2);
}
#if defined(ON_COMPILER_MSC)
#pragma warning( pop )
#endif
wchar_t* on_wcsupr(wchar_t* s)
{
#if defined(ON_OS_WINDOWS)
#if defined(ON_COMPILER_BORLAND)
// Borland's compiler / C library
return _wcsupr(s);
#else
// Microsoft compiler
return _wcsupr(s);
#endif
#else
if (s)
{
wchar_t c;
while (*s)
{
if ( 0 != (c = toupper(*s)) )
*s = c;
s++;
}
}
return s;
#endif
}
// on_wcslwr() calls _wcslwr() or wcslwr() depending on OS
wchar_t* on_wcslwr(wchar_t* s)
{
#if defined(ON_OS_WINDOWS)
#if defined(ON_COMPILER_BORLAND)
// Borland's compiler / C library
return _wcslwr(s);
#else
// Microsoft compiler
return _wcslwr(s);
#endif
#else
if (s)
{
wchar_t c;
while (*s)
{
if ( 0 != (c = tolower(*s)) )
*s = c;
s++;
}
}
return s;
#endif
}
void ON_wString::MakeUpper()
{
if ( !IsEmpty() )
{
#if defined(ON_OS_WINDOWS)
if ( 0 != g_s__windows_locale_id )
{
if ( 0 == g_s__windows_locale_os )
{
// On Win 95/98/ME, LCMapStringW() doesn't work.
// (I hope you don't need the right answer in a hurry on Win9X.)
// These convert UNICODE to wide character strings
ON_String in(*this);
int len_in = in.Length();
int max_len_out = 2*len_in+16; // if 2x for wide char expansion
ON_String out;
out.ReserveArray(max_len_out+1);
out.SetLength(max_len_out+1);
// Wide char conversion
int rc = ::LCMapStringA(g_s__windows_locale_id,
LCMAP_UPPERCASE,
in.Array(),
len_in,
out.Array(),
max_len_out);
if (rc > 0 && rc <= max_len_out)
{
out.SetLength(rc);
operator=(out); // multi-byte to wchar conversion
return;
}
}
else
{
// a version of Windows with working UNICODE support
int len_in = Length();
int max_len_out = len_in+16;
ON_wString out;
out.ReserveArray(max_len_out+1);
out.SetLength(max_len_out+1);
// Wide char conversion
int rc = ::LCMapStringW(g_s__windows_locale_id,
LCMAP_UPPERCASE,
Array(),
len_in,
out.Array(),
max_len_out);
if (rc > 0 && rc <= max_len_out)
{
out.SetLength(rc);
operator=(out); // very fast - simply changes reference count
return;
}
}
}
#endif
// If ::LCMapStringA() or ::LCMapStringW() failed or we are
// running on a non-Windows OS, then we fall through to the
// wcslwr() function which will handle most most characters
// in most Western European languages but is not adequate for
// commercial quality software.
CopyArray();
on_wcsupr(m_s);
}
}
void ON_wString::MakeLower()
{
if ( !IsEmpty() )
{
#if defined(ON_OS_WINDOWS)
if ( 0 != g_s__windows_locale_id )
{
if ( 0 == g_s__windows_locale_os )
{
// On Win 95/98/ME, LCMapStringW() doesn't work.
// (I hope you don't need the right answer in a hurry on Win9X.)
// These convert UNICODE to wide character strings
ON_String in(*this);
int len_in = in.Length();
int max_len_out = 2*len_in+16; // if 2x for wide char expansion
ON_String out;
out.ReserveArray(max_len_out+1);
out.SetLength(max_len_out+1);
// Wide char conversion to multi-byte lower case string
int rc = ::LCMapStringA(g_s__windows_locale_id,
LCMAP_LOWERCASE,
in.Array(),
len_in,
out.Array(),
max_len_out);
if (rc > 0 && rc <= max_len_out)
{
out.SetLength(rc);
operator=(out); // multi-byte to wchar conversion
return;
}
}
else
{
// a version of Windows with working UNICODE support
int len_in = Length();
int max_len_out = len_in+16;
// ReserveArray(max_len_out+1) allocates max_len_out+2
// wchars (room for the NULL terminator is allocatcated).
// The +1 is just in case LCMapStringW() has a bug and
// writes an extra wchar or puts a NULL terminator
// in s[max_len_out]. This is a lot of paranoia, but
// the memory cost is negligable and it will prevent
// difficult to diagnose crashes if MS releases a buggy
// version of LCMapStringW().
ON_wString out;
out.ReserveArray(max_len_out+1);
out.SetLength(max_len_out+1);
// Wide char conversion to lower case.
// Note that changing to lower case in some languages
// can change the string length.
int rc = ::LCMapStringW(g_s__windows_locale_id,
LCMAP_LOWERCASE,
Array(),
len_in,
out.Array(),
max_len_out);
if (rc > 0 && rc <= max_len_out)
{
out.SetLength(rc);
operator=(out); // very fast - simply changes reference count
return;
}
}
}
#endif
// If ::LCMapStringA() or ::LCMapStringW() failed or we are
// running on a non-Windows OS, then we fall through to the
// wcslwr() function which will handle most most characters
// in most Western European languages but is not adequate for
// commercial quality software.
CopyArray();
on_wcslwr(m_s);
}
}
wchar_t* on_wcsrev(wchar_t* s)
{
if ( !s )
return 0;
int i, j;
wchar_t w;
for ( j = 0; 0 != s[j]; j++ )
{
// empty for body
}
for ( i = 0, j--; i < j; i++, j-- )
{
w = s[i];
if ( w >= 0xD800 && w <= 0xDBFF && s[i+1] >= 0xDC00 && s[i+1] <= 0xDFFF )
{
// UTF-16 surrogate pair
if ( i+1 < j-1 )
{
s[i] = s[j-1];
s[j-1] = w;
w = s[i+1];
s[i+1] = s[j];
s[j] = w;
}
i++;
j--;
}
else
{
s[i] = s[j];
s[j] = w;
}
}
return s;
}
int on_WideCharToMultiByte(
const wchar_t* lpWideCharStr,
int cchWideChar,
char* lpMultiByteStr,
int cchMultiByte
)
{
// 14 March 2011 Dale Lear
// It turns out that Windows WideCharToMultiByte does correctly
// convert UTF-16 to UTF-8 in Windows 7 when the code page
// is CP_ACP and calls with CP_UTF8 sometimes fail to do
// any conversion. So, I wrote ON_ConvertWideCharToUTF8()
// and opennurbs will use ON_ConvertWideCharToUTF8 to get
// consistent results on all platforms.
unsigned int error_status = 0;
unsigned int error_mask = 0xFFFFFFFF;
ON__UINT32 error_code_point = 0xFFFD;
const wchar_t* p1 = 0;
int count = ON_ConvertWideCharToUTF8(false,lpWideCharStr,cchWideChar,lpMultiByteStr,cchMultiByte,
&error_status,error_mask,error_code_point,&p1);
if ( 0 != error_status )
{
ON_ERROR("Error converting UTF-16 encoded wchar_t string to UTF-8 encoded char string.");
}
return count;
}
int on_MultiByteToWideChar(
const char* lpMultiByteStr,
int cchMultiByte,
wchar_t* lpWideCharStr,
int cchWideChar
)
{
// 14 March 2011 Dale Lear
// It turns out that Windows WideCharToMultiByte does correctly
// convert UTF-16 to UTF-8 in Windows 7 when the code page
// is CP_ACP and calls with CP_UTF8 sometimes fail to do
// any conversion. So, I wrote ON_ConvertUTF8ToWideChar()
// and opennurbs will use ON_ConvertUTF8ToWideChar to get
// consistent results on all platforms.
unsigned int error_status = 0;
unsigned int error_mask = 0xFFFFFFFF;
ON__UINT32 error_code_point = 0xFFFD;
const char* p1 = 0;
int count = ON_ConvertUTF8ToWideChar(lpMultiByteStr,cchMultiByte,lpWideCharStr,cchWideChar,
&error_status,error_mask,error_code_point,&p1);
if ( 0 != error_status )
{
ON_ERROR("Error converting UTF-8 encoded char string to UTF-16 encoded wchar_t string.");
}
return count;
}
int on_vsnprintf( char *buffer, size_t count, const char *format, va_list argptr )
{
#if defined(ON_OS_WINDOWS)
#if defined(ON_COMPILER_BORLAND)
return vsprintf( buffer, format, argptr );
#else
return _vsnprintf( buffer, count, format, argptr );
#endif
#else
return vsnprintf( buffer, count, format, argptr );
#endif
}
int on_vsnwprintf( wchar_t *buffer, size_t count, const wchar_t *format, va_list argptr )
{
#if defined(ON_OS_WINDOWS)
#if defined(ON_COMPILER_BORLAND)
return vswprintf( buffer, format, argptr );
#else
return _vsnwprintf( buffer, count, format, argptr );
#endif
#else
// if an OS doesn't support vsnwprintf(), use ASCII and hope for the best
ON_String aformat = format; // convert format from UNICODE to ASCII
// format an ASCII buffer
char* abuffer = (char*)onmalloc(4*count*sizeof(*abuffer));
int rc = on_vsnprintf( abuffer, 4*count, aformat.Array(), argptr );
// convert formatted ASCII buffer to UNICODE
on_MultiByteToWideChar( abuffer, (int)strlen(abuffer), buffer, (int)count );
onfree(abuffer);
return rc;
#endif
}
void on_splitpath(
const char* path,
const char** drive,
const char** dir,
const char** fname,
const char** ext
)
{
// The "const char* path" parameter is a UTF-8 encoded string.
// Since the unicode code point values for the characters we
// are searching for ( '/' '\' '.' ':' A-Z a-z) are all > 0
// and < 128, we can simply check for an array element having
// the character value and not have to worry about dealing
// with UTF-8 continuation values (>= 128).
const char slash1 = '/';
const char slash2 = '\\'; // do this even with the os is unix because
// we might be parsing a file name saved
// in Windows.
const char* f;
const char* e;
const char* s;
const char* s1;
if ( 0 != drive )
*drive = 0;
if ( 0 != dir )
*dir = 0;
if ( 0 != fname )
*fname = 0;
if ( 0 != ext )
*ext = 0;
if ( 0 != path && 0 != *path )
{
// deal with Windows' drive letter (even when the os is unix)
if ( ':' == path[1] )
{
if ( (path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z') )
{
if ( drive )
*drive = path;
path += 2;
if ( 0 == *path )
return;
}
}
}
if ( 0 != path && 0 != *path )
{
e = 0;
f = 0;
s1 = path;
while ( 0 != *s1 )
s1++;
s = (s1 > path) ? s1 - 1 : path;
while ( s > path && '.' != *s && slash1 != *s && slash2 != *s )
s--;
if ( '.' == *s && 0 != s[1] )
{
// extensions must have something after the dot.
e = s;
s1 = e;
s--;
}
while ( s > path && slash1 != *s && slash2 != *s )
s--;
if ( s >= path && s < s1 )
{
if (slash1 == *s || slash2 == *s )
{
if ( s+1 < s1 )
f = s+1;
}
else if ( s == path )
{
f = s;
}
}
if ( 0 == f )
{
// must have a non-empty filename in order to have and "extension"
f = e;
e = 0;
}
if ( 0 != dir && (0 == f || path < f) )
*dir = path;
if ( 0 != f && 0 != fname )
*fname = f;
if ( 0 != e && 0 != ext )
*ext = e;
}
}
void on_wsplitpath(
const wchar_t* path,
const wchar_t** drive,
const wchar_t** dir,
const wchar_t** fname,
const wchar_t** ext
)
{
// The "const wchar_t* path" parameter is a UTF-8, UTF-16 or UTF-32
// encoded string. Since the unicode code point values for the
// characters we are searching for ( '/' '\' '.' ':' A-Z a-z) are
// all > 0 and < 128, we can simply check for an array element
// having the character value and not have to worry about dealing
// with UTF-16 surrogate pair values (0xD800-0xDBFF and DC00-DFFF)
// and UTF-8 continuation values (>= 128).
const wchar_t slash1 = '/';
const wchar_t slash2 = '\\'; // do this even with the os is unix because
// we might be parsing a file name saved
// in Windows.
const wchar_t* f;
const wchar_t* e;
const wchar_t* s;
const wchar_t* s1;
if ( 0 != drive )
*drive = 0;
if ( 0 != dir )
*dir = 0;
if ( 0 != fname )
*fname = 0;
if ( 0 != ext )
*ext = 0;
if ( 0 != path && 0 != *path )
{
// deal with Windows' drive letter (even when the os is unix)
if ( ':' == path[1] )
{
if ( (path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z') )
{
if ( drive )
*drive = path;
path += 2;
if ( 0 == *path )
return;
}
}
}
if ( 0 != path && 0 != *path )
{
e = 0;
f = 0;
s1 = path;
while ( 0 != *s1 )
s1++;
s = (s1 > path) ? s1 - 1 : path;
while ( s > path && '.' != *s && slash1 != *s && slash2 != *s )
s--;
if ( '.' == *s && 0 != s[1] )
{
// extensions must have something after the dot.
e = s;