This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathcore.cpp
More file actions
1317 lines (1073 loc) · 29.9 KB
/
core.cpp
File metadata and controls
1317 lines (1073 loc) · 29.9 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
/* Copyright (C) 2009-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
////////////////////////////////////////////////////////////////////////////////
//
// Private Source File:
// core.cpp
//
// Description:
// This file contains core methods abstracting away from the C runtime-
// library.
//
// Changes:
// 2009-07-04 MW Created.
// 2009-07-29 MW Moved from kernel to libcore.
//
////////////////////////////////////////////////////////////////////////////////
#include "core.h"
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE)
#include <CoreFoundation/CoreFoundation.h>
#endif
#if defined(_WINDOWS) || defined(TARGET_SUBPLATFORM_WINDOWS) || defined(_WINDOWS_SERVER)
#include <windows.h>
#endif
////////////////////////////////////////////////////////////////////////////////
int UTF8ToUnicode(const char * lpSrcStr, int cchSrc, uint16_t * lpDestStr, int cchDest);
int UnicodeToUTF8(const uint16_t *lpSrcStr, int cchSrc, char *lpDestStr, int cchDest);
////////////////////////////////////////////////////////////////////////////////
bool MCThrow(uint32_t p_error)
{
return false;
}
////////////////////////////////////////////////////////////////////////////////
#if defined(_DEBUG)
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
#include <windows.h>
#include <crtdbg.h>
#include <dbghelp.h>
void __MCAssert(const char *p_file, uint32_t p_line, const char *p_message)
{
_CrtDbgReport(_CRT_ASSERT, p_file, p_line, NULL, "%s", p_message);
}
void __MCLog(const char *p_file, uint32_t p_line, const char *p_format, ...)
{
char *t_string;
t_string = nil;
va_list t_args;
va_start(t_args, p_format);
MCCStringFormatV(t_string, p_format, t_args);
va_end(t_args);
_CrtDbgReport(_CRT_WARN, p_file, p_line, NULL, "[%u] %s\n", GetCurrentProcessId(), t_string);
MCCStringFree(t_string);
}
void __MCLogWithTrace(const char *p_file, uint32_t p_line, const char *p_format, ...)
{
typedef BOOL (WINAPI *SymFromAddrPtr)(HANDLE, DWORD64, PDWORD64, PSYMBOL_INFO);
typedef BOOL (WINAPI *SymGetLineFromAddr64Ptr)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_LINE64);
typedef BOOL (WINAPI *SymInitializePtr)(HANDLE, PCSTR, BOOL);
typedef DWORD (WINAPI *SymSetOptionsPtr)(DWORD);
static SymInitializePtr s_sym_initialize = nil;
static SymSetOptionsPtr s_sym_setoptions = nil;
static SymGetLineFromAddr64Ptr s_sym_get_line_from_addr_64 = nil;
static SymFromAddrPtr s_sym_from_addr = nil;
if (s_sym_from_addr == nil)
{
HMODULE t_dbg_help;
t_dbg_help = LoadLibraryA("dbghelp.dll");
if (t_dbg_help != nil)
{
s_sym_setoptions = (SymSetOptionsPtr)GetProcAddress(t_dbg_help, "SymSetOptions");
s_sym_initialize = (SymInitializePtr)GetProcAddress(t_dbg_help, "SymInitialize");
s_sym_from_addr = (SymFromAddrPtr)GetProcAddress(t_dbg_help, "SymFromAddr");
s_sym_get_line_from_addr_64 = (SymGetLineFromAddr64Ptr)GetProcAddress(t_dbg_help, "SymGetLineFromAddr64");
s_sym_initialize(GetCurrentProcess(), NULL, TRUE);
s_sym_setoptions(SYMOPT_LOAD_LINES);
}
}
char *t_string;
t_string = nil;
va_list t_args;
va_start(t_args, p_format);
MCCStringFormatV(t_string, p_format, t_args);
va_end(t_args);
_CrtDbgReport(_CRT_WARN, p_file, p_line, NULL, "[%u] %s\n", GetCurrentProcessId(), t_string);
MCCStringFree(t_string);
if (s_sym_from_addr != nil)
{
char t_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)];
PSYMBOL_INFO t_symbol = (PSYMBOL_INFO)t_buffer;
t_symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
t_symbol->MaxNameLen = MAX_SYM_NAME - 1;
void *t_backtrace[64];
USHORT t_count;
t_count = CaptureStackBackTrace(1, 62, t_backtrace, nil);
for(uint32_t i = 0; i < t_count; i++)
{
if (s_sym_from_addr(GetCurrentProcess(), (DWORD64)t_backtrace[i], nil, t_symbol))
{
IMAGEHLP_LINE64 t_line;
DWORD64 t_displacement;
t_line . SizeOfStruct = sizeof(IMAGEHLP_LINE64);
s_sym_get_line_from_addr_64(GetCurrentProcess(), (DWORD64)t_backtrace[i], &t_displacement, &t_line);
_CrtDbgReport(_CRT_WARN, p_file, p_line, NULL, "[%u] %s@0x%p, line %d\n", GetCurrentProcessId(), t_symbol -> Name, t_backtrace[i], t_line . LineNumber);
}
}
}
}
#elif defined(_MACOSX) || defined(_LINUX) || defined(TARGET_SUBPLATFORM_IPHONE)
#include <unistd.h>
void __MCAssert(const char *p_file, uint32_t p_line, const char *p_message)
{
}
void __MCLog(const char *p_file, uint32_t p_line, const char *p_format, ...)
{
fprintf(stderr, "[%d] ", getpid());
va_list t_args;
va_start(t_args, p_format);
vfprintf(stderr, p_format, t_args);
va_end(t_args);
fprintf(stderr, "\n");
}
#elif defined(TARGET_SUBPLATFORM_ANDROID)
#include <android/log.h>
void __MCAssert(const char *p_file, uint32_t p_line, const char *p_message)
{
}
void __MCLog(const char *p_file, uint32_t p_line, const char *p_format, ...)
{
va_list args;
va_start(args, p_format);
__android_log_vprint(ANDROID_LOG_INFO, "revandroid", p_format, args);
va_end(args);
}
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
bool MCMemoryAllocate(uindex_t p_size, void*& r_block)
{
void *t_block;
t_block = malloc(p_size);
if (t_block != nil)
{
r_block = t_block;
return true;
}
return false;
}
bool MCMemoryAllocateCopy(const void *p_block, uindex_t p_block_size, void*& r_block)
{
if (MCMemoryAllocate(p_block_size, r_block))
{
MCMemoryCopy(r_block, p_block, p_block_size);
return true;
}
return false;
}
bool MCMemoryReallocate(void *p_block, uindex_t p_new_size, void*& r_new_block)
{
void *t_new_block;
t_new_block = realloc(p_block, p_new_size);
if (t_new_block != nil)
{
r_new_block = t_new_block;
return true;
}
return false;
}
void MCMemoryDeallocate(void *p_block)
{
free(p_block);
}
bool MCMemoryNew(uindex_t p_size, void*& r_record)
{
if (MCMemoryAllocate(p_size, r_record))
{
MCMemoryClear(r_record, p_size);
return true;
}
return false;
}
void MCMemoryDelete(void *p_record)
{
MCMemoryDeallocate(p_record);
}
bool MCMemoryNewArray(uindex_t p_count, uindex_t p_size, void*& r_array)
{
if (MCMemoryAllocate(p_count * p_size, r_array))
{
MCMemoryClear(r_array, p_count * p_size);
return true;
}
return false;
}
bool MCMemoryResizeArray(uindex_t p_new_count, uindex_t p_size, void*& x_array, uindex_t& x_count)
{
if (MCMemoryReallocate(x_array, p_new_count * p_size, x_array))
{
if (p_new_count > x_count)
MCMemoryClear(static_cast<char *>(x_array) + x_count * p_size, (p_new_count - x_count) * p_size);
x_count = p_new_count;
return true;
}
return false;
}
void MCMemoryDeleteArray(void *p_array)
{
MCMemoryDeallocate(p_array);
}
void MCMemoryClear(void *p_bytes, uindex_t p_size)
{
memset(p_bytes, 0, p_size);
}
void MCMemoryCopy(void *p_dst, const void *p_src, uindex_t p_size)
{
memcpy(p_dst, p_src, p_size);
}
void MCMemoryMove(void *p_dst, const void *p_src, uindex_t p_size)
{
memmove(p_dst, p_src, p_size);
}
bool MCMemoryEqual(const void *p_left, const void *p_right, uindex_t p_size)
{
return memcmp(p_left, p_right, p_size) == 0;
}
compare_t MCMemoryCompare(const void *p_left, const void *p_right, uindex_t p_size)
{
return memcmp(p_left, p_right, p_size);
}
////////////////////////////////////////////////////////////////////////////////
uint32_t MCCStringLength(const char *p_string)
{
if (p_string == nil)
return 0;
return strlen(p_string);
}
bool MCCStringIsEmpty(const char *p_string)
{
return p_string == nil || *p_string == '\0';
}
bool MCCStringIsInteger(const char *p_string)
{
if (p_string == nil)
return false;
while(isdigit(*p_string++))
;
if (*p_string == '\0')
return true;
return false;
}
bool MCCStringTokenize(const char *p_string, char**& r_elements, uint32_t& r_element_count)
{
bool t_success;
t_success = true;
char **t_elements;
t_elements = nil;
uint32_t t_element_count;
t_element_count = 0;
if (p_string != nil)
{
const char *t_ptr;
t_ptr = p_string;
const char *t_token;
t_token = nil;
while(t_success)
{
// Skip spaces
while(*t_ptr == ' ')
t_ptr += 1;
t_token = t_ptr;
// If we find a quote, start a quoted token, else loop until whitespace
if (*t_ptr == '"')
{
t_ptr += 1;
while(*t_ptr != '\0' && *t_ptr != '"')
t_ptr += 1;
if (*t_ptr == '"')
t_ptr += 1;
}
else
{
while(*t_ptr != '\0' && *t_ptr != ' ')
t_ptr += 1;
}
// If ptr and token are the same, we have exhausted the string
if (t_ptr == t_token)
break;
// Add a new element to the array, and clone the substring
t_success = MCMemoryResizeArray(t_element_count + 1, t_elements, t_element_count);
if (t_success)
t_success = MCCStringCloneSubstring(t_token, t_ptr - t_token, t_elements[t_element_count - 1]);
}
}
if (t_success)
{
r_elements = t_elements;
r_element_count = t_element_count;
}
else
{
for(uint32_t i = 0; i < t_element_count; i++)
MCCStringFree(t_elements[i]);
MCMemoryDeleteArray(t_elements);
}
return t_success;
}
bool MCCStringSplit(const char *p_string, char p_split_char, char**& r_elements, uint32_t& r_element_count)
{
bool t_success;
t_success = true;
char **t_elements;
t_elements = nil;
uint32_t t_element_count;
t_element_count = 0;
if (p_string != nil)
{
const char *t_ptr;
t_ptr = p_string;
const char *t_next;
t_next = p_string;
while(t_success && (t_next != nil))
{
t_next = strchr(t_ptr, p_split_char);
// Add a new element to the array, and clone the substring
t_success = MCMemoryResizeArray(t_element_count + 1, t_elements, t_element_count);
if (t_success)
t_success = MCCStringCloneSubstring(t_ptr, (t_next == nil) ? MCCStringLength(t_ptr) : t_next - t_ptr, t_elements[t_element_count - 1]);
t_ptr = t_next + 1;
}
}
if (t_success)
{
r_elements = t_elements;
r_element_count = t_element_count;
}
else
{
for(uint32_t i = 0; i < t_element_count; i++)
MCCStringFree(t_elements[i]);
MCMemoryDeleteArray(t_elements);
}
return t_success;
}
bool MCCStringCombine(const char * const *p_elements, uint32_t p_element_count, char p_separator, char*& r_string)
{
uint32_t t_length;
t_length = 0;
for(uint32_t i = 0; i < p_element_count; i++)
t_length += MCCStringLength(p_elements[i]) + 1;
char *t_string;
if (!MCMemoryNewArray(t_length, t_string))
return false;
char *t_ptr;
t_ptr = t_string;
for(uint32_t i = 0; i < p_element_count; i++)
{
if (i > 0)
*t_ptr++ = p_separator;
uint32_t t_element_length;
t_element_length = MCCStringLength(p_elements[i]);
MCMemoryCopy(t_ptr, p_elements[i], t_element_length);
t_ptr += t_element_length;
}
*t_ptr = '\0';
r_string = t_string;
return true;
}
bool MCCStringFormat(char*& r_string, const char *p_format, ...)
{
va_list t_args;
int t_count;
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
va_start(t_args, p_format);
t_count = _vscprintf(p_format, t_args);
va_end(t_args);
#elif defined(_MACOSX) || defined(_LINUX) || defined(TARGET_SUBPLATFORM_IPHONE) || defined(TARGET_SUBPLATFORM_ANDROID) || defined(__EMSCRIPTEN__)
va_start(t_args, p_format);
t_count = vsnprintf(nil, 0, p_format, t_args);
va_end(t_args);
#else
#error "Implement MCCStringFormat"
#endif
char *t_new_string;
if (!MCMemoryAllocate(t_count + 1, t_new_string))
return false;
va_start(t_args, p_format);
vsprintf(t_new_string, p_format, t_args);
va_end(t_args);
r_string = t_new_string;
return true;
}
bool MCCStringFormatV(char*& r_string, const char *p_format, va_list p_args)
{
int t_count;
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
t_count = _vscprintf(p_format, p_args);
#elif defined(_MACOSX) || defined(_LINUX) || defined(TARGET_SUBPLATFORM_IPHONE) || defined(TARGET_SUBPLATFORM_ANDROID) || defined(__EMSCRIPTEN__)
t_count = vsnprintf(nil, 0, p_format, p_args);
#else
#error "Implement MCCStringFormat"
#endif
char *t_new_string;
if (!MCMemoryAllocate(t_count + 1, t_new_string))
return false;
vsprintf(t_new_string, p_format, p_args);
r_string = t_new_string;
return true;
}
bool MCCStringAppendFormat(char*& x_string, const char *p_format, ...)
{
va_list t_args;
int t_count;
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
va_start(t_args, p_format);
t_count = _vscprintf(p_format, t_args);
va_end(t_args);
#elif defined(_MACOSX) || defined(_LINUX) || defined(TARGET_SUBPLATFORM_IPHONE) || defined(TARGET_SUBPLATFORM_ANDROID) || defined(__EMSCRIPTEN__)
va_start(t_args, p_format);
t_count = vsnprintf(nil, 0, p_format, t_args);
va_end(t_args);
#else
#error "Implement MCCStringFormat"
#endif
uint32_t t_old_length;
t_old_length = MCCStringLength(x_string);
if (!MCMemoryReallocate(x_string, t_old_length + t_count + 1, x_string))
return false;
va_start(t_args, p_format);
vsprintf(x_string + t_old_length, p_format, t_args);
va_end(t_args);
return true;
}
bool MCCStringAppend(char*& x_string, const char *p_extra_string)
{
uint32_t t_old_length, t_new_length;
t_old_length = MCCStringLength(x_string);
t_new_length = MCCStringLength(p_extra_string);
if (!MCMemoryReallocate(x_string, t_old_length + t_new_length + 1, x_string))
return false;
MCMemoryCopy(x_string + t_old_length, p_extra_string, t_new_length + 1);
return true;
}
bool MCCStringClone(const char *p_string, char *& r_new_string)
{
if (p_string == nil)
{
r_new_string = nil;
return true;
}
if (!MCMemoryAllocate(strlen(p_string) + 1, r_new_string))
return false;
strcpy(r_new_string, p_string);
return true;
}
bool MCCStringCloneSubstring(const char *p_string, uint32_t p_length, char*& r_new_string)
{
if (!MCMemoryAllocate(p_length + 1, r_new_string))
return false;
MCMemoryCopy(r_new_string, p_string, p_length);
r_new_string[p_length] = '\0';
return true;
}
void MCCStringFree(char *p_string)
{
MCMemoryDeallocate(p_string);
}
bool MCCStringArrayClone(const char *const *p_strings, uint32_t p_count, char**& r_new_strings)
{
bool t_success;
t_success = true;
char **t_array;
t_array = nil;
if (t_success)
t_success = MCMemoryNewArray(p_count, t_array);
for(uint32_t i = 0; i < p_count && t_success; i++)
t_success = MCCStringClone(p_strings[i], t_array[i]);
if (t_success)
r_new_strings = t_array;
else
MCCStringArrayFree(t_array, p_count);
return t_success;
}
void MCCStringArrayFree(char **p_strings, uint32_t p_count)
{
if (p_strings == nil)
return;
for(uint32_t i = 0; i < p_count; i++)
MCCStringFree(p_strings[i]);
MCMemoryDeleteArray(p_strings);
}
bool MCCStringToUnicode(const char *p_string, unichar_t*& r_unicode_string)
{
uint32_t t_cstring_length;
t_cstring_length = MCCStringLength(p_string);
uint32_t t_length;
t_length = UTF8ToUnicode(p_string, t_cstring_length, nil, 0) / 2;
if (!MCMemoryNewArray(t_length + 1, r_unicode_string))
return false;
t_length = UTF8ToUnicode(p_string, t_cstring_length, (uint16_t *)r_unicode_string, t_length * 2) / 2;
r_unicode_string[t_length] = '\0';
return true;
}
bool MCCStringFromUnicode(const unichar_t *p_unicode_string, char*& r_string)
{
if (NULL == p_unicode_string)
return false;
uint32_t t_wstring_length;
t_wstring_length = 0;
while(p_unicode_string[t_wstring_length] != 0)
t_wstring_length++;
return MCCStringFromUnicodeSubstring(p_unicode_string, t_wstring_length, r_string);
}
bool MCCStringFromUnicodeSubstring(const unichar_t *p_chars, uint32_t p_char_count, char*& r_string)
{
uint32_t t_length;
t_length = UnicodeToUTF8((const uint16_t *)p_chars, p_char_count * 2, nil, 0);
if (!MCMemoryNewArray(t_length + 1, r_string))
return false;
t_length = UnicodeToUTF8((const uint16_t *)p_chars, p_char_count * 2, r_string, t_length);
r_string[t_length] = '\0';
return true;
}
#if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE)
bool MCCStringToNative(const char *p_string, char*& r_native_string)
{
CFStringRef t_cfstring;
if (!MCCStringToCFString(p_string, t_cfstring))
return false;
CFDataRef t_data;
t_data = CFStringCreateExternalRepresentation(kCFAllocatorDefault, t_cfstring, kCFStringEncodingMacRoman, '?');
uint32_t t_length;
t_length = CFDataGetLength(t_data);
const UInt8 *t_bytes;
t_bytes = CFDataGetBytePtr(t_data);
if (t_bytes[0] == 0xEF && t_bytes[1] == 0xBB && t_bytes[2] == 0xBF)
t_bytes += 3, t_length -= 3;
bool t_success;
t_success = MCCStringCloneSubstring((const char *)t_bytes, t_length, r_native_string);
CFRelease(t_data);
CFRelease(t_cfstring);
return t_success;
}
bool MCCStringFromNative(const char *p_native, char*& r_cstring)
{
bool t_success;
t_success = true;
CFStringRef t_cfstring;
t_cfstring = nil;
if (t_success)
{
t_cfstring = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, p_native, kCFStringEncodingMacRoman, kCFAllocatorNull);
if (t_cfstring == nil)
t_success = false;
}
CFIndex t_cstring_length;
char *t_buffer;
t_buffer = nil;
if (t_success)
{
CFStringGetBytes(t_cfstring, CFRangeMake(0, CFStringGetLength(t_cfstring)), kCFStringEncodingUTF8, 0, false, nil, 0, &t_cstring_length);
t_success = MCMemoryNewArray(t_cstring_length + 1, t_buffer);
}
if (t_success)
{
CFStringGetBytes(t_cfstring, CFRangeMake(0, CFStringGetLength(t_cfstring)), kCFStringEncodingUTF8, 0, false, (UInt8 *)t_buffer, t_cstring_length, nil);
t_buffer[t_cstring_length] = '\0';
r_cstring = t_buffer;
}
if (t_cfstring != nil)
CFRelease(t_cfstring);
return t_success;
}
#elif defined(_WINDOWS)
bool MCCStringToNative(const char *p_string, char *&r_native)
{
bool t_success = true;
unichar_t *t_unistring = nil;
char *t_native = nil;
uint32_t t_string_len = MCCStringLength(p_string);
if (t_success)
t_success = MCCStringToUnicode(p_string, t_unistring);
if (t_success)
t_success = MCMemoryNewArray(t_string_len + 1, t_native);
if (t_success)
WideCharToMultiByte(1252, WC_NO_BEST_FIT_CHARS, t_unistring, -1, t_native, t_string_len + 1, "?", NULL);
MCMemoryDeleteArray(t_unistring);
if (t_success)
r_native = t_native;
return t_success;
}
bool MCCStringFromNative(const char *p_native, char*& r_cstring)
{
bool t_success;
t_success = true;
// For us 'native' means Windows-1252 which has a 1-1 mapping to UTF-16
// so we know the length we need...
unichar_t *t_wstring;
t_wstring = nil;
if (t_success)
t_success = MCMemoryNewArray(MCCStringLength(p_native) + 1, t_wstring);
if (t_success)
{
MultiByteToWideChar(1252, 0, p_native, -1, t_wstring, MCCStringLength(p_native) + 1);
t_success = MCCStringFromUnicode(t_wstring, r_cstring);
}
MCMemoryDeleteArray(t_wstring);
return t_success;
}
#elif defined(_LINUX)
bool MCCStringFromNativeSubstring(const char *p_string, uint32_t p_length, char*& r_cstring)
{
char *t_native;
if (!MCMemoryNewArray(p_length * 2 + 1, t_native))
return false;
const uint8_t *t_string;
t_string = (const uint8_t *)p_string;
uint32_t j;
j = 0;
for(uint32_t i = 0; i < p_length; i++)
if (t_string[i] < 128)
t_native[j++] = t_string[i];
else
{
t_native[j++] = 0xc0 | (t_string[i] >> 6);
t_native[j++] = 0x80 | (t_string[i] & 0x3f);
}
t_native[j] = '\0';
r_cstring = t_native;
return true;
}
bool MCCStringFromNative(const char *p_string, char*& r_cstring)
{
return MCCStringFromNativeSubstring(p_string, MCCStringLength(p_string), r_cstring);
}
#endif
compare_t MCCStringCompare(const char *p_left, const char *p_right)
{
return strcmp(p_left, p_right);
}
compare_t MCCStringCompareCaseless(const char *p_left, const char *p_right)
{
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
return _stricmp(p_left, p_right);
#else
return strcasecmp(p_left, p_right);
#endif
}
bool MCCStringEqual(const char *p_left, const char *p_right)
{
return strcmp(p_left, p_right) == 0;
}
bool MCCStringEqualCaseless(const char *p_left, const char *p_right)
{
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
return _stricmp(p_left, p_right) == 0;
#else
return strcasecmp(p_left, p_right) == 0;
#endif
}
bool MCCStringEqualSubstring(const char *p_left, const char *p_right, index_t p_length)
{
return strncmp(p_left, p_right, p_length) == 0;
}
bool MCCStringEqualSubstringCaseless(const char *p_left, const char *p_right, index_t p_length)
{
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
return _strnicmp(p_left, p_right, p_length) == 0;
#else
return strncasecmp(p_left, p_right, p_length) == 0;
#endif
}
bool MCCStringBeginsWith(const char *p_string, const char *p_prefix)
{
return strlen(p_string) >= strlen(p_prefix) && strncmp(p_string, p_prefix, strlen(p_prefix)) == 0;
}
bool MCCStringBeginsWithCaseless(const char *p_string, const char *p_prefix)
{
return strlen(p_string) >= strlen(p_prefix) && MCCStringEqualSubstringCaseless(p_string, p_prefix, strlen(p_prefix));
}
bool MCCStringEndsWith(const char *p_string, const char *p_suffix)
{
return strlen(p_string) >= strlen(p_suffix) && strcmp(p_string + strlen(p_string) - strlen(p_suffix), p_suffix) == 0;
}
bool MCCStringEndsWithCaseless(const char *p_string, const char *p_suffix)
{
return strlen(p_string) >= strlen(p_suffix) && MCCStringEqualCaseless(p_string + strlen(p_string) - strlen(p_suffix), p_suffix);
}
bool MCCStringToCardinal(const char *p_string, uint32_t& r_value)
{
if (*p_string == 0)
return false;
uint32_t t_value;
t_value = 0;
while(*p_string != 0)
{
if (!isdigit(*p_string))
return false;
uint32_t t_new_value;
t_new_value = t_value * 10 + (*p_string - '0');
if (t_new_value < t_value)
return false;
t_value = t_new_value;
p_string++;
}
r_value = t_value;
return true;
}
bool MCCStringFirstIndexOf(const char *p_string, char p_search, uint32_t &r_index)
{
if (p_string == nil)
return false;
const char *t_position;
t_position = strchr(p_string, p_search);
if (t_position != nil)
{
r_index = t_position - p_string;
return true;
}
else
return false;
}
bool MCCStringFirstIndexOf(const char *p_string, const char *p_search, uint32_t &r_index)
{
if (p_string == nil)
return false;
const char *t_position;
t_position = strstr(p_string, p_search);
if (t_position != nil)
{
r_index = t_position - p_string;
return true;
}
else
return false;
}
bool MCCStringLastIndexOf(const char *p_string, char p_search, uint32_t &r_index)
{
if (p_string == nil)
return false;
const char *t_position;
t_position = strrchr(p_string, p_search);
if (t_position != nil)
{
r_index = t_position - p_string;
return true;
}
else
return false;
}
bool MCCStringLastIndexOf(const char *p_string, const char *p_search, uint32_t &r_index)
{
if (p_string == nil)
return false;
const char *t_position;
t_position = strstr(p_string, p_search);
if (t_position != nil)
{
const char *t_next;
while ((t_next = strstr(t_position + 1, p_search)) != nil)
t_position = t_next;
}
if (t_position != nil)
{
r_index = t_position - p_string;
return true;
}
else
return false;
}
bool MCCStringContains(const char *p_haystack, const char *p_needle)
{
return strstr(p_haystack, p_needle) != nil;
}
#if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE)
bool MCCStringToCFString(const char *p_cstring, CFStringRef& r_cfstring)
{
CFStringRef t_cfstring;
t_cfstring = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)p_cstring, MCCStringLength(p_cstring), kCFStringEncodingUTF8, false);
if (t_cfstring == nil)
return false;
r_cfstring = t_cfstring;
return true;
}
bool MCCStringFromCFString(CFStringRef p_cfstring, char *&r_cstring)
{
uint32_t t_buffer_size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(p_cfstring), kCFStringEncodingUTF8) + 1;
char *t_cstring = nil;
bool t_success = MCMemoryAllocate(t_buffer_size, t_cstring);
if (t_success)
t_success = CFStringGetCString(p_cfstring, t_cstring, t_buffer_size, kCFStringEncodingUTF8);
if (t_success)
r_cstring = t_cstring;
else
MCMemoryDeallocate(t_cstring);
return t_success;