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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
|
// dear imgui test engine
// (helpers/utilities. do NOT use this as a general purpose library)
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "imgui_te_utils.h"
#include "imgui.h"
#include "imgui_internal.h"
#define STR_IMPLEMENTATION
#include "thirdparty/Str/Str.h"
#if defined(_WIN32)
#if !defined(_WINDOWS_)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <shellapi.h> // ShellExecuteA()
#include <stdio.h>
#else
#include <errno.h>
#include <unistd.h>
#endif
#ifndef _MSC_VER
#include <sys/types.h>
#include <sys/stat.h> // stat()
#endif
#if defined(__linux) || defined(__linux__) || defined(__MACH__) || defined(__MSL__) || defined(__MINGW32__)
#include <pthread.h> // pthread_setname_np()
#endif
#include <chrono> // high_resolution_clock::now()
#include <thread> // this_thread::sleep_for()
//-----------------------------------------------------------------------------
// Hashing Helpers
//-----------------------------------------------------------------------------
// - ImHashDecoratedPathParseLiteral() [internal]
// - ImHashDecoratedPath()
// - ImFindNextDecoratedPartInPath()
//-----------------------------------------------------------------------------
// - Parse literals encoded as "$$xxxx/" and incorporate into our hash based on type.
// - $$ not passed by caller.
static ImGuiID ImHashDecoratedPathParseLiteral(ImGuiID crc, const unsigned char* str, const unsigned char* str_end, const unsigned char** out_str_remaining)
{
// Parse type (default to int)
ImGuiDataType type = ImGuiDataType_S32;
if (*str == '(')
{
// "$$(int)????" where ???? is s32 or u32
if (str + 5 < str_end && memcmp(str, "(int)", 5) == 0)
{
type = ImGuiDataType_S32;
str += 5;
}
// "$$(ptr)0x????" where ???? is ptr size
else if (str + 7 < str_end && memcmp(str, "(ptr)0x", 7) == 0)
{
type = ImGuiDataType_Pointer;
str += 7;
}
}
// Parse value
switch (type)
{
case ImGuiDataType_S32:
{
// e.g. "$$(int)123" for s32/u32/ImGuiID, same as PushID(int)
int v = 0;
{
int negative = 0;
if (str < str_end && *str == '-') { negative = 1; str++; }
if (str < str_end && *str == '+') { str++; }
for (char c = *str; str < str_end; c = *(++str))
{
if (c >= '0' && c <= '9') { v = (v * 10) + (c - '0'); }
else break;
}
if (negative)
v = -v;
}
crc = ~ImHashData(&v, sizeof(int), ~crc);
break;
}
case ImGuiDataType_Pointer:
{
// e.g. "$$(ptr)0x1234FFFF" for pointers, same as PushID(void*)
intptr_t v = 0;
{
for (char c = *str; str < str_end; c = *(++str))
{
if (c >= '0' && c <= '9') { v = (v << 4) + (c - '0'); }
else if (c >= 'A' && c <= 'F') { v = (v << 4) + 10 + (c - 'A'); }
else if (c >= 'a' && c <= 'f') { v = (v << 4) + 10 + (c - 'a'); }
else break;
}
}
crc = ~ImHashData(&v, sizeof(void*), ~crc);
break;
}
}
// "$$xxxx" must always be either end of string, either leading to a next section e.g. "$$xxxx/"
IM_ASSERT(str == str_end || *str == '/');
*out_str_remaining = str;
return crc;
}
// Hash "hello/world" as if it was "helloworld"
// To hash a forward slash we need to use "hello\\/world"
// IM_ASSERT(ImHashDecoratedPath("Hello/world") == ImHashStr("Helloworld", 0));
// IM_ASSERT(ImHashDecoratedPath("Hello\\/world") == ImHashStr("Hello/world", 0));
// IM_ASSERT(ImHashDecoratedPath("$$1") == (n = 1, ImHashData(&n, sizeof(int))));
// Adapted from ImHash(). Not particularly fast!
ImGuiID ImHashDecoratedPath(const char* str, const char* str_end, ImGuiID seed)
{
static ImU32 crc32_lut[256] = { 0 };
if (!crc32_lut[1])
{
const ImU32 polynomial = 0xEDB88320;
for (ImU32 i = 0; i < 256; i++)
{
ImU32 crc = i;
for (ImU32 j = 0; j < 8; j++)
crc = (crc >> 1) ^ (ImU32(-int(crc & 1)) & polynomial);
crc32_lut[i] = crc;
}
}
// Prefixing the string with / ignore the seed
if (str != str_end && str[0] == '/')
seed = 0;
seed = ~seed;
ImU32 crc = seed;
// Focus for non-zero terminated string for consistency
if (str_end == NULL)
str_end = str + strlen(str);
bool inhibit_one = false;
bool new_section = true;
const unsigned char* current = (const unsigned char*)str;
while (current < (const unsigned char*)str_end)
{
const unsigned char c = *current++;
// Backslash to inhibit special behavior of following character
if (c == '\\' && !inhibit_one)
{
inhibit_one = true;
continue;
}
// Forward slashes are ignored unless prefixed with a backward slash
if (c == '/' && !inhibit_one)
{
inhibit_one = false;
new_section = true;
seed = crc; // Set seed to the new path
continue;
}
// $$ at the beginning of a section to encode literals.
// - Currently: "$$????" = hash of 1 as int
// - May add pointers and other types.
if (c == '$' && current[0] == '$' && !inhibit_one && new_section)
{
crc = ImHashDecoratedPathParseLiteral(crc, current + 1, (const unsigned char*)str_end, ¤t);
continue;
}
// Reset the hash when encountering ###
if (c == '#' && current[0] == '#' && current[1] == '#')
crc = seed;
// Hash byte
crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c];
inhibit_one = new_section = false;
}
return ~crc;
}
// Returns a next element of decorated hash path.
// "//hello/world/child" --> "world/child"
// "world/child" --> "child"
// This is a helper for code needing to do some parsing of individual nodes in a path.
// Note: we need the (unsigned char*) stuff in order to keep code similar to ImHashDecoratedPath(). They are not really necessary in this function tho.
const char* ImFindNextDecoratedPartInPath(const char* str, const char* str_end)
{
const unsigned char* current = (const unsigned char*)str;
while (*current == '/')
current++;
bool inhibit_one = false;
while (true)
{
if (str_end != NULL && current == (const unsigned char*)str_end)
break;
const unsigned char c = *current++;
if (c == 0)
break;
if (c == '\\' && !inhibit_one)
{
inhibit_one = true;
continue;
}
// Forward slashes are ignored unless prefixed with a backward slash
if (c == '/' && !inhibit_one)
return (const char*)current;
inhibit_one = false;
}
return NULL;
}
//-----------------------------------------------------------------------------
// File/Directory Helpers
//-----------------------------------------------------------------------------
// - ImFileExist()
// - ImFileCreateDirectoryChain()
// - ImFileFindInParents()
// - ImFileLoadSourceBlurb()
//-----------------------------------------------------------------------------
#if _WIN32
static const char IM_DIR_SEPARATOR = '\\';
static void ImUtf8ToWideChar(const char* multi_byte, ImVector<wchar_t>* buf)
{
const int wsize = ::MultiByteToWideChar(CP_UTF8, 0, multi_byte, -1, NULL, 0);
buf->resize(wsize);
::MultiByteToWideChar(CP_UTF8, 0, multi_byte, -1, (wchar_t*)buf->Data, wsize);
}
#else
static const char IM_DIR_SEPARATOR = '/';
#endif
bool ImFileExist(const char* filename)
{
struct stat dir_stat;
int ret = stat(filename, &dir_stat);
return (ret == 0);
}
bool ImFileDelete(const char* filename)
{
#if _WIN32
ImVector<wchar_t> buf;
ImUtf8ToWideChar(filename, &buf);
return ::DeleteFileW(&buf[0]) == TRUE;
#else
unlink(filename);
#endif
return false;
}
// Create directories for specified path. Slashes will be replaced with platform directory separators.
// e.g. ImFileCreateDirectoryChain("aaaa/bbbb/cccc.png")
// will try to create "aaaa/" then "aaaa/bbbb/".
bool ImFileCreateDirectoryChain(const char* path, const char* path_end)
{
IM_ASSERT(path != NULL);
IM_ASSERT(path[0] != 0);
if (path_end == NULL)
path_end = path + strlen(path);
// Copy in a local, zero-terminated buffer
size_t path_len = (size_t)(path_end - path);
char* path_local = (char*)IM_ALLOC(path_len + 1);
memcpy(path_local, path, path_len);
path_local[path_len] = 0;
#if defined(_WIN32)
ImVector<ImWchar> buf;
#endif
// Modification of passed file_name allows us to avoid extra temporary memory allocation.
// strtok() pokes \0 into places where slashes are, we create a directory using directory_name and restore slash.
for (char* token = strtok(path_local, "\\/"); token != NULL; token = strtok(NULL, "\\/"))
{
// strtok() replaces slashes with NULLs. Overwrite removed slashes here with the type of slashes the OS needs (win32 functions need backslashes).
if (token != path_local)
*(token - 1) = IM_DIR_SEPARATOR;
#if defined(_WIN32)
// Use ::CreateDirectoryW() because ::CreateDirectoryA() treat filenames in the local code-page instead of UTF-8.
const int filename_wsize = ImTextCountCharsFromUtf8(path_local, NULL) + 1;
buf.resize(filename_wsize);
ImTextStrFromUtf8(&buf[0], filename_wsize, path_local, NULL);
if (!::CreateDirectoryW((wchar_t*)&buf[0], NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
#else
if (mkdir(path_local, S_IRWXU) != 0 && errno != EEXIST)
#endif
{
IM_FREE(path_local);
return false;
}
}
IM_FREE(path_local);
return true;
}
bool ImFileFindInParents(const char* sub_path, int max_parent_count, Str* output)
{
IM_ASSERT(sub_path != NULL);
IM_ASSERT(output != NULL);
for (int parent_level = 0; parent_level < max_parent_count; parent_level++)
{
output->clear();
for (int j = 0; j < parent_level; j++)
output->append("../");
output->append(sub_path);
if (ImFileExist(output->c_str()))
return true;
}
output->clear();
return false;
}
bool ImFileLoadSourceBlurb(const char* file_name, int line_no_start, int line_no_end, ImGuiTextBuffer* out_buf)
{
size_t file_size = 0;
char* file_begin = (char*)ImFileLoadToMemory(file_name, "rb", &file_size, 1);
if (file_begin == NULL)
return false;
char* file_end = file_begin + file_size;
int line_no = 0;
const char* test_src_begin = NULL;
const char* test_src_end = NULL;
for (const char* p = file_begin; p < file_end; )
{
line_no++;
const char* line_begin = p;
const char* line_end = ImStrchrRange(line_begin + 1, file_end, '\n');
if (line_end == NULL)
line_end = file_end;
if (line_no >= line_no_start && line_no <= line_no_end)
{
if (test_src_begin == NULL)
test_src_begin = line_begin;
test_src_end = ImMax(test_src_end, line_end);
}
p = line_end + 1;
}
if (test_src_begin != NULL)
out_buf->append(test_src_begin, test_src_end);
else
out_buf->clear();
ImGui::MemFree(file_begin);
return true;
}
//-----------------------------------------------------------------------------
// Path Helpers
//-----------------------------------------------------------------------------
// - ImPathFindFilename()
// - ImPathFindFileExt()
// - ImPathFixSeparatorsForCurrentOS()
//-----------------------------------------------------------------------------
const char* ImPathFindFilename(const char* path, const char* path_end)
{
IM_ASSERT(path != NULL);
if (!path_end)
path_end = path + strlen(path);
const char* p = path_end;
while (p > path)
{
if (p[-1] == '/' || p[-1] == '\\')
break;
p--;
}
return p;
}
// "folder/filename" -> return pointer to "" (end of string)
// "folder/filename.png" -> return pointer to ".png"
// "folder/filename.png.bak" -> return pointer to ".png.bak"
const char* ImPathFindExtension(const char* path, const char* path_end)
{
if (!path_end)
path_end = path + strlen(path);
const char* filename = ImPathFindFilename(path, path_end);
const char* p = filename;
while (p < path_end)
{
if (p[0] == '.')
break;
p++;
}
return p;
}
void ImPathFixSeparatorsForCurrentOS(char* buf)
{
#ifdef _WIN32
for (char* p = buf; *p != 0; p++)
if (*p == '/')
*p = '\\';
#else
for (char* p = buf; *p != 0; p++)
if (*p == '\\')
*p = '/';
#endif
}
//-----------------------------------------------------------------------------
// String Helpers
//-----------------------------------------------------------------------------
static const char* ImStrStr(const char* haystack, size_t hlen, const char* needle, int nlen)
{
const char* end = haystack + hlen;
const char* p = haystack;
while ((p = (const char*)memchr(p, *needle, end - p)) != NULL)
{
if (end - p < nlen)
return NULL;
if (memcmp(p, needle, nlen) == 0)
return p;
p++;
}
return NULL;
}
void ImStrReplace(Str* s, const char* find, const char* repl)
{
IM_ASSERT(find != NULL && *find);
IM_ASSERT(repl != NULL);
int find_len = (int)strlen(find);
int repl_len = (int)strlen(repl);
int repl_diff = repl_len - find_len;
// Estimate required length of new buffer if string size increases.
int need_capacity = s->capacity();
int num_matches = INT_MAX;
if (repl_diff > 0)
{
num_matches = 0;
need_capacity = s->length() + 1;
for (char* p = s->c_str(), *end = s->c_str() + s->length(); p != NULL && p < end;)
{
p = (char*)ImStrStr(p, end - p, find, find_len);
if (p)
{
need_capacity += repl_diff;
p += find_len;
num_matches++;
}
}
}
if (num_matches == 0)
return;
const char* not_owned_data = s->owned() ? NULL : s->c_str();
if (!s->owned() || need_capacity > s->capacity())
s->reserve(need_capacity);
if (not_owned_data != NULL)
s->set(not_owned_data);
// Replace data.
for (char* p = s->c_str(), *end = s->c_str() + s->length(); p != NULL && p < end && num_matches--;)
{
p = (char*)ImStrStr(p, end - p, find, find_len);
if (p)
{
memmove(p + repl_len, p + find_len, end - p - find_len + 1);
memcpy(p, repl, repl_len);
p += repl_len;
end += repl_diff;
}
}
}
const char* ImStrchrRangeWithEscaping(const char* str, const char* str_end, char find_c)
{
while (str < str_end)
{
const char c = *str;
if (c == '\\')
{
str += 2;
continue;
}
if (c == find_c)
return str;
str++;
}
return NULL;
}
// Suboptimal but ok for the data size we are dealing with (see commit on 2022/08/22 for a faster and more complicated version)
void ImStrXmlEscape(Str* s)
{
ImStrReplace(s, "&", "&");
ImStrReplace(s, "<", "<");
ImStrReplace(s, ">", ">");
ImStrReplace(s, "\"", """);
ImStrReplace(s, "\'", "'");
}
// Based on code from https://github.com/EddieBreeg/C_b64 by @EddieBreeg.
int ImStrBase64Encode(const unsigned char* src, char* dst, int length)
{
static const char* b64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
int i, j, k, l, encoded_len = 0;
while (length > 0)
{
switch (length)
{
case 1:
i = src[0] >> 2;
j = (src[0] & 3) << 4;
k = 64;
l = 64;
break;
case 2:
i = src[0] >> 2;
j = ((src[0] & 3) << 4) | (src[1] >> 4);
k = (src[1] & 15) << 2;
l = 64;
break;
default:
i = src[0] >> 2;
j = ((src[0] & 3) << 4) | (src[1] >> 4);
k = ((src[1] & 0xf) << 2) | (src[2] >> 6 & 3);
l = src[2] & 0x3f;
break;
}
dst[0] = b64Table[i];
dst[1] = b64Table[j];
dst[2] = b64Table[k];
dst[3] = b64Table[l];
src += 3;
dst += 4;
length -= 3;
encoded_len += 4;
}
return encoded_len;
}
//-----------------------------------------------------------------------------
// Parsing Helpers
//-----------------------------------------------------------------------------
// - ImParseSplitCommandLine()
// - ImParseFindIniSection()
//-----------------------------------------------------------------------------
void ImParseExtractArgcArgvFromCommandLine(int* out_argc, char const*** out_argv, const char* cmd_line)
{
size_t cmd_line_len = strlen(cmd_line);
int n = 1;
{
const char* p = cmd_line;
while (*p != 0)
{
const char* arg = p;
while (*arg == ' ')
arg++;
const char* arg_end = strchr(arg, ' ');
if (arg_end == NULL)
p = arg_end = cmd_line + cmd_line_len;
else
p = arg_end + 1;
n++;
}
}
int argc = n;
char const** argv = (char const**)malloc(sizeof(char*) * ((size_t)argc + 1) + (cmd_line_len + 1));
IM_ASSERT(argv != NULL);
char* cmd_line_dup = (char*)argv + sizeof(char*) * ((size_t)argc + 1);
strcpy(cmd_line_dup, cmd_line);
{
argv[0] = "main.exe";
argv[argc] = NULL;
char* p = cmd_line_dup;
for (n = 1; n < argc; n++)
{
char* arg = p;
char* arg_end = strchr(arg, ' ');
if (arg_end == NULL)
p = arg_end = cmd_line_dup + cmd_line_len;
else
p = arg_end + 1;
argv[n] = arg;
arg_end[0] = 0;
}
}
*out_argc = argc;
*out_argv = argv;
}
bool ImParseFindIniSection(const char* ini_config, const char* header, ImVector<char>* result)
{
IM_ASSERT(ini_config != NULL);
IM_ASSERT(header != NULL);
IM_ASSERT(result != NULL);
size_t ini_len = strlen(ini_config);
size_t header_len = strlen(header);
IM_ASSERT(header_len > 0);
if (ini_len == 0)
return false;
const char* section_start = strstr(ini_config, header);
if (section_start == NULL)
return false;
const char* section_end = strstr(section_start + header_len, "\n[");
if (section_end == NULL)
section_end = section_start + ini_len;
// "\n[" matches next header start on all platforms, but it cuts new line marker in half on windows.
if (*(section_end - 1) == '\r')
--section_end;
size_t section_len = (size_t)(section_end - section_start);
result->resize((int)section_len + 1);
ImStrncpy(result->Data, section_start, section_len);
return true;
}
//-----------------------------------------------------------------------------
// Time Helpers
//-----------------------------------------------------------------------------
// - ImTimeGetInMicroseconds()
// - ImTimestampToISO8601()
//-----------------------------------------------------------------------------
uint64_t ImTimeGetInMicroseconds()
{
// Trying std::chrono out of unfettered optimism that it may actually work..
using namespace std;
chrono::microseconds ms = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now().time_since_epoch());
return (uint64_t)ms.count();
}
void ImTimestampToISO8601(uint64_t timestamp, Str* out_date)
{
time_t unix_time = (time_t)(timestamp / 1000000); // Convert to seconds.
tm* time = gmtime(&unix_time);
const char* time_format = "%Y-%m-%dT%H:%M:%S";
size_t size_req = strftime(out_date->c_str(), out_date->capacity(), time_format, time);
if (size_req >= (size_t)out_date->capacity())
{
out_date->reserve((int)size_req);
strftime(out_date->c_str(), out_date->capacity(), time_format, time);
}
}
//-----------------------------------------------------------------------------
// Threading Helpers
//-----------------------------------------------------------------------------
// - ImThreadSleepInMilliseconds()
// - ImThreadSetCurrentThreadDescription()
//-----------------------------------------------------------------------------
void ImThreadSleepInMilliseconds(int ms)
{
using namespace std;
this_thread::sleep_for(chrono::milliseconds(ms));
}
#if defined(_MSC_VER)
// Helper function for setting thread name on Win32
// This is a separate function because __try cannot coexist with local objects that need destructors called on stack unwind
static void ImThreadSetCurrentThreadDescriptionWin32OldStyle(const char* description)
{
// Old-style Win32 thread name setting method
// See https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = description;
info.dwThreadID = (DWORD)-1;
info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
#pragma warning(pop)
}
#endif // #ifdef _WIN32
// Set the description (name) of the current thread for debugging purposes
void ImThreadSetCurrentThreadDescription(const char* description)
{
#if defined(_MSC_VER) // Windows + Visual Studio
// New-style thread name setting
// Only supported from Win 10 version 1607/Server 2016 onwards, hence the need for dynamic linking
typedef HRESULT(WINAPI* SetThreadDescriptionFunc)(HANDLE hThread, PCWSTR lpThreadDescription);
SetThreadDescriptionFunc set_thread_description = (SetThreadDescriptionFunc)::GetProcAddress(GetModuleHandleA("Kernel32.dll"), "SetThreadDescription");
if (set_thread_description)
{
ImVector<ImWchar> buf;
const int description_wsize = ImTextCountCharsFromUtf8(description, NULL) + 1;
buf.resize(description_wsize);
ImTextStrFromUtf8(&buf[0], description_wsize, description, NULL);
set_thread_description(::GetCurrentThread(), (wchar_t*)&buf[0]);
}
// Also do the old-style method too even if the new-style one worked, as the two work in slightly different sets of circumstances
ImThreadSetCurrentThreadDescriptionWin32OldStyle(description);
#elif defined(__linux) || defined(__linux__) || defined(__MINGW32__) // Linux or MingW
pthread_setname_np(pthread_self(), description);
#elif defined(__MACH__) || defined(__MSL__) // OSX
pthread_setname_np(description);
#else
// This is a nice-to-have rather than critical functionality, so fail silently if we don't support this platform
#endif
}
//-----------------------------------------------------------------------------
// Build info helpers
//-----------------------------------------------------------------------------
// - ImBuildGetCompilationInfo()
// - ImBuildGetGitBranchName()
//-----------------------------------------------------------------------------
// Turn __DATE__ "Jan 10 2019" into "2019-01-10"
static void ImBuildParseDateFromCompilerIntoYMD(const char* in_date, char* out_buf, size_t out_buf_size)
{
char month_str[5];
int year, month, day;
sscanf(in_date, "%3s %d %d", month_str, &day, &year);
const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
const char* p = strstr(month_names, month_str);
month = p ? (int)(1 + (p - month_names) / 3) : 0;
ImFormatString(out_buf, out_buf_size, "%04d-%02d-%02d", year, month, day);
}
// Those strings are used to output easily identifiable markers in compare logs. We only need to support what we use for testing.
// We can probably grab info in eaplatform.h/eacompiler.h etc. in EASTL
const ImBuildInfo* ImBuildGetCompilationInfo()
{
static ImBuildInfo build_info;
if (build_info.Type[0] == '\0')
{
// Build Type
#if defined(DEBUG) || defined(_DEBUG)
build_info.Type = "Debug";
#else
build_info.Type = "Release";
#endif
// CPU
#if defined(_M_X86) || defined(_M_IX86) || defined(__i386) || defined(__i386__) || defined(_X86_) || defined(_M_AMD64) || defined(_AMD64_) || defined(__x86_64__)
build_info.Cpu = (sizeof(size_t) == 4) ? "X86" : "X64";
#elif defined(__aarch64__)
build_info.Cpu = "ARM64";
#else
#error
build_info.Cpu = (sizeof(size_t) == 4) ? "Unknown32" : "Unknown64";
#endif
// Platform/OS
#if defined(_WIN32)
build_info.OS = "Windows";
#elif defined(__linux) || defined(__linux__)
build_info.OS = "Linux";
#elif defined(__MACH__) || defined(__MSL__)
build_info.OS = "OSX";
#elif defined(__ORBIS__)
build_info.OS = "PS4";
#elif defined(_DURANGO)
build_info.OS = "XboxOne";
#else
build_info.OS = "Unknown";
#endif
// Compiler
#if defined(_MSC_VER)
build_info.Compiler = "MSVC";
#elif defined(__clang__)
build_info.Compiler = "Clang";
#elif defined(__GNUC__)
build_info.Compiler = "GCC";
#else
build_info.Compiler = "Unknown";
#endif
// Date/Time
ImBuildParseDateFromCompilerIntoYMD(__DATE__, build_info.Date, IM_ARRAYSIZE(build_info.Date));
build_info.Time = __TIME__;
}
return &build_info;
}
bool ImBuildFindGitBranchName(const char* git_repo_path, Str* branch_name)
{
IM_ASSERT(git_repo_path != NULL);
IM_ASSERT(branch_name != NULL);
Str256f head_path("%s/.git/HEAD", git_repo_path);
size_t head_size = 0;
bool result = false;
if (char* git_head = (char*)ImFileLoadToMemory(head_path.c_str(), "r", &head_size, 1))
{
const char prefix[] = "ref: refs/heads/"; // Branch name is prefixed with this in HEAD file.
const int prefix_length = IM_ARRAYSIZE(prefix) - 1;
strtok(git_head, "\r\n"); // Trim new line
if (head_size > prefix_length && strncmp(git_head, prefix, prefix_length) == 0)
{
// "ref: refs/heads/master" -> "master"
branch_name->set(git_head + prefix_length);
}
else
{
// Should be git hash, keep first 8 characters (see #42)
branch_name->setf("%.8s", git_head);
}
result = true;
IM_FREE(git_head);
}
return result;
}
//-----------------------------------------------------------------------------
// Operating System Helpers
//-----------------------------------------------------------------------------
// - ImOsCreateProcess()
// - ImOsPOpen()
// - ImOsPClose()
// - ImOsOpenInShell()
// - ImOsConsoleSetTextColor()
// - ImOsIsDebuggerPresent()
//-----------------------------------------------------------------------------
bool ImOsCreateProcess(const char* cmd_line)
{
#ifdef _WIN32
STARTUPINFOA siStartInfo;
PROCESS_INFORMATION piProcInfo;
ZeroMemory(&siStartInfo, sizeof(STARTUPINFOA));
char* cmd_line_copy = ImStrdup(cmd_line);
BOOL ret = ::CreateProcessA(NULL, cmd_line_copy, NULL, NULL, FALSE, 0, NULL, NULL, &siStartInfo, &piProcInfo);
free(cmd_line_copy);
::CloseHandle(siStartInfo.hStdInput);
::CloseHandle(siStartInfo.hStdOutput);
::CloseHandle(siStartInfo.hStdError);
::CloseHandle(piProcInfo.hProcess);
::CloseHandle(piProcInfo.hThread);
return ret != 0;
#else
IM_UNUSED(cmd_line);
return false;
#endif
}
FILE* ImOsPOpen(const char* cmd_line, const char* mode)
{
IM_ASSERT(cmd_line != NULL && *cmd_line);
IM_ASSERT(mode != NULL && *mode);
#if _WIN32
ImVector<wchar_t> w_cmd_line;
ImVector<wchar_t> w_mode;
ImUtf8ToWideChar(cmd_line, &w_cmd_line);
ImUtf8ToWideChar(mode, &w_mode);
w_mode.resize(w_mode.Size + 1);
wcscat(w_mode.Data, L"b"); // Windows requires 'b' mode while unixes do not support it and default to binary.
return _wpopen(w_cmd_line.Data, w_mode.Data);
#else
return popen(cmd_line, mode);
#endif
}
void ImOsPClose(FILE* fp)
{
IM_ASSERT(fp != NULL);
#if _WIN32
_pclose(fp);
#else
pclose(fp);
#endif
}
void ImOsOpenInShell(const char* path)
{
Str256 command(path);
#ifdef _WIN32
ImPathFixSeparatorsForCurrentOS(command.c_str());
::ShellExecuteA(NULL, "open", command.c_str(), NULL, NULL, SW_SHOWDEFAULT);
#else
#if __APPLE__
const char* open_executable = "open";
#else
const char* open_executable = "xdg-open";
#endif
command.setf("%s \"%s\"", open_executable, path);
ImPathFixSeparatorsForCurrentOS(command.c_str());
system(command.c_str());
#endif
}
void ImOsConsoleSetTextColor(ImOsConsoleStream stream, ImOsConsoleTextColor color)
{
#ifdef _WIN32
HANDLE hConsole = 0;
switch (stream)
{
case ImOsConsoleStream_StandardOutput: hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE); break;
case ImOsConsoleStream_StandardError: hConsole = ::GetStdHandle(STD_ERROR_HANDLE); break;
}
WORD wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
switch (color)
{
case ImOsConsoleTextColor_Black: wAttributes = 0x00; break;
case ImOsConsoleTextColor_White: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case ImOsConsoleTextColor_BrightWhite: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case ImOsConsoleTextColor_BrightRed: wAttributes = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
case ImOsConsoleTextColor_BrightGreen: wAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case ImOsConsoleTextColor_BrightBlue: wAttributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case ImOsConsoleTextColor_BrightYellow: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
default: IM_ASSERT(0);
}
::SetConsoleTextAttribute(hConsole, wAttributes);
#elif defined(__linux) || defined(__linux__) || defined(__MACH__) || defined(__MSL__)
// FIXME: check system capabilities (with environment variable TERM)
FILE* handle = 0;
switch (stream)
{
case ImOsConsoleStream_StandardOutput: handle = stdout; break;
case ImOsConsoleStream_StandardError: handle = stderr; break;
}
const char* modifier = "";
switch (color)
{
case ImOsConsoleTextColor_Black: modifier = "\033[30m"; break;
case ImOsConsoleTextColor_White: modifier = "\033[0m"; break;
case ImOsConsoleTextColor_BrightWhite: modifier = "\033[1;37m"; break;
case ImOsConsoleTextColor_BrightRed: modifier = "\033[1;31m"; break;
case ImOsConsoleTextColor_BrightGreen: modifier = "\033[1;32m"; break;
case ImOsConsoleTextColor_BrightBlue: modifier = "\033[1;34m"; break;
case ImOsConsoleTextColor_BrightYellow: modifier = "\033[1;33m"; break;
default: IM_ASSERT(0);
}
fprintf(handle, "%s", modifier);
#endif
}
bool ImOsIsDebuggerPresent()
{
#ifdef _WIN32
return ::IsDebuggerPresent() != 0;
#elif defined(__linux__)
int debugger_pid = 0;
char buf[2048]; // TracerPid is located near the start of the file. If end of the buffer gets cut off thats fine.
FILE* fp = fopen("/proc/self/status", "rb"); // Can not use ImFileLoadToMemory because size detection of /proc/self/status would fail.
if (fp == NULL)
return false;
fread(buf, 1, IM_ARRAYSIZE(buf), fp);
fclose(fp);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
if (char* tracer_pid = strstr(buf, "TracerPid:"))
{
tracer_pid += 10; // Skip label
while (isspace(*tracer_pid))
tracer_pid++;
debugger_pid = atoi(tracer_pid);
}
return debugger_pid != 0;
#else
// FIXME
return false;
#endif
}
void ImOsOutputDebugString(const char* message)
{
#ifdef _WIN32
OutputDebugStringA(message);
#else
IM_UNUSED(message);
#endif
}
//-----------------------------------------------------------------------------
// Str.h + InputText bindings
//-----------------------------------------------------------------------------
struct InputTextCallbackStr_UserData
{
Str* StrObj;
ImGuiInputTextCallback ChainCallback;
void* ChainCallbackUserData;
};
static int InputTextCallbackStr(ImGuiInputTextCallbackData* data)
{
InputTextCallbackStr_UserData* user_data = (InputTextCallbackStr_UserData*)data->UserData;
if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
{
// Resize string callback
// If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want.
Str* str = user_data->StrObj;
IM_ASSERT(data->Buf == str->c_str());
str->reserve(data->BufTextLen + 1);
data->Buf = (char*)str->c_str();
}
else if (user_data->ChainCallback)
{
// Forward to user callback, if any
data->UserData = user_data->ChainCallbackUserData;
return user_data->ChainCallback(data);
}
return 0;
}
// Draw an extra colored frame over the previous item
// Similar to DebugDrawItemRect() but use Max(1.0f, FrameBorderSize)
void ImGui::ItemErrorFrame(ImU32 col)
{
ImGuiContext& g = *GetCurrentContext();
ImDrawList* drawlist = GetWindowDrawList();
ImGuiStyle& style = GetStyle();
// FIXME: GetItemRectMin() / GetItemRectMax() will include label. NavRect is not probably defined :(
drawlist->AddRect(g.LastItemData.NavRect.Min, g.LastItemData.NavRect.Max, GetColorU32(col), style.FrameRounding, ImDrawFlags_None, ImMax(1.0f, style.FrameBorderSize));
}
bool ImGui::InputText(const char* label, Str* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallbackStr_UserData cb_user_data;
cb_user_data.StrObj = str;
cb_user_data.ChainCallback = callback;
cb_user_data.ChainCallbackUserData = user_data;
return InputText(label, (char*)str->c_str(), (size_t)str->capacity() + 1, flags, InputTextCallbackStr, &cb_user_data);
}
bool ImGui::InputTextWithHint(const char* label, const char* hint, Str* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallbackStr_UserData cb_user_data;
cb_user_data.StrObj = str;
cb_user_data.ChainCallback = callback;
cb_user_data.ChainCallbackUserData = user_data;
return InputTextWithHint(label, hint, (char*)str->c_str(), (size_t)str->capacity() + 1, flags, InputTextCallbackStr, &cb_user_data);
}
bool ImGui::InputTextMultiline(const char* label, Str* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallbackStr_UserData cb_user_data;
cb_user_data.StrObj = str;
cb_user_data.ChainCallback = callback;
cb_user_data.ChainCallbackUserData = user_data;
return InputTextMultiline(label, (char*)str->c_str(), (size_t)str->capacity() + 1, size, flags, InputTextCallbackStr, &cb_user_data);
}
// anchor parameter indicates which split would retain it's constant size.
// anchor = 0 - both splits resize when parent container size changes. Both value_1 and value_2 should be persistent.
// anchor = -1 - top/left split would have a constant size. bottom/right split would resize when parent container size changes. value_1 should be persistent, value_2 will always be recalculated from value_1.
// anchor = +1 - bottom/right split would have a constant size. top/left split would resize when parent container size changes. value_2 should be persistent, value_1 will always be recalculated from value_2.
bool ImGui::Splitter(const char* id, float* value_1, float* value_2, int axis, int anchor, float min_size_0, float min_size_1)
{
// FIXME-DOGFOODING: This needs further refining.
// FIXME-SCROLL: When resizing either we'd like to keep scroll focus on something (e.g. last clicked item for list, bottom for log)
// See https://github.com/ocornut/imgui/issues/319
ImGuiContext& g = *GImGui;
ImGuiStyle& style = g.Style;
ImGuiWindow* window = ImGui::GetCurrentWindow();
if (min_size_0 < 0)
min_size_0 = ImGui::GetFrameHeight();
if (min_size_1)
min_size_1 = ImGui::GetFrameHeight();
IM_ASSERT(axis == ImGuiAxis_X || axis == ImGuiAxis_Y);
float& v_1 = *value_1;
float& v_2 = *value_2;
ImRect splitter_bb;
const float avail = axis == ImGuiAxis_X ? ImGui::GetContentRegionAvail().x - style.ItemSpacing.x : ImGui::GetContentRegionAvail().y - style.ItemSpacing.y;
if (anchor < 0)
{
v_2 = ImMax(avail - v_1, min_size_1); // First split is constant size.
}
else if (anchor > 0)
{
v_1 = ImMax(avail - v_2, min_size_0); // Second split is constant size.
}
else
{
float r = v_1 / (v_1 + v_2); // Both splits maintain same relative size to parent.
v_1 = IM_ROUND(avail * r) - 1;
v_2 = IM_ROUND(avail * (1.0f - r)) - 1;
}
if (axis == ImGuiAxis_X)
{
float x = window->DC.CursorPos.x + v_1 + IM_ROUND(style.ItemSpacing.x * 0.5f);
splitter_bb = ImRect(x - 1, window->WorkRect.Min.y, x + 1, window->WorkRect.Max.y);
}
else if (axis == ImGuiAxis_Y)
{
float y = window->DC.CursorPos.y + v_1 + IM_ROUND(style.ItemSpacing.y * 0.5f);
splitter_bb = ImRect(window->WorkRect.Min.x, y - 1, window->WorkRect.Max.x, y + 1);
}
return ImGui::SplitterBehavior(splitter_bb, ImGui::GetID(id), (ImGuiAxis)axis, &v_1, &v_2, min_size_0, min_size_1, 3.0f);
}
// FIXME-TESTS: Should eventually remove.
ImFont* ImGui::FindFontByPrefix(const char* prefix)
{
ImGuiContext& g = *GImGui;
for (ImFont* font : g.IO.Fonts->Fonts)
if (strncmp(font->ConfigData->Name, prefix, strlen(prefix)) == 0)
return font;
return NULL;
}
// Legacy version support
#if IMGUI_VERSION_NUM < 18924
const char* ImGui::TabBarGetTabName(ImGuiTabBar* tab_bar, ImGuiTabItem* tab)
{
return tab_bar->GetTabName(tab);
}
#endif
#if IMGUI_VERSION_NUM < 18927
ImGuiID ImGui::TableGetInstanceID(ImGuiTable* table, int instance_no)
{
// Changed in #6140
return table->ID + instance_no;
}
#endif
ImGuiID TableGetHeaderID(ImGuiTable* table, const char* column, int instance_no)
{
IM_ASSERT(table != NULL);
int column_n = -1;
for (int n = 0; n < table->Columns.size() && column_n < 0; n++)
if (strcmp(ImGui::TableGetColumnName(table, n), column) == 0)
column_n = n;
IM_ASSERT(column_n != -1);
return TableGetHeaderID(table, column_n, instance_no);
}
ImGuiID TableGetHeaderID(ImGuiTable* table, int column_n, int instance_no)
{
IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount);
const ImGuiID table_instance_id = ImGui::TableGetInstanceID(table, instance_no);
const char* column_name = ImGui::TableGetColumnName(table, column_n);
#if IMGUI_VERSION_NUM >= 18927
const int column_id_differencier = column_n;
#else
const int column_id_differencier = instance_no * table->ColumnsCount + column_n;
#endif
const int column_id = ImHashData(&column_id_differencier, sizeof(column_id_differencier), table_instance_id);
return ImHashData(column_name, strlen(column_name), column_id);
}
// FIXME: Could be moved to core as an internal function?
void TableDiscardInstanceAndSettings(ImGuiID table_id)
{
ImGuiContext& g = *GImGui;
IM_ASSERT(g.CurrentTable == NULL);
if (ImGuiTableSettings* settings = ImGui::TableSettingsFindByID(table_id))
settings->ID = 0;
if (ImGuiTable* table = ImGui::TableFindByID(table_id))
ImGui::TableRemove(table);
// FIXME-TABLE: We should be able to use TableResetSettings() instead of TableRemove()! Maybe less of a clean slate but would be good to check that it does the job
//ImGui::TableResetSettings(table);
}
// Helper to verify ImDrawData integrity of buffer count (broke before e.g. #6716)
void DrawDataVerifyMatchingBufferCount(ImDrawData* draw_data)
{
int total_vtx_count = 0;
int total_idx_count = 0;
for (ImDrawList* draw_list : draw_data->CmdLists)
{
total_vtx_count += draw_list->VtxBuffer.Size;
total_idx_count += draw_list->IdxBuffer.Size;
}
IM_UNUSED(total_vtx_count);
IM_UNUSED(total_idx_count);
IM_ASSERT(total_vtx_count == draw_data->TotalVtxCount);
IM_ASSERT(total_idx_count == draw_data->TotalIdxCount);
}
//-----------------------------------------------------------------------------
// Simple CSV parser
//-----------------------------------------------------------------------------
void ImGuiCsvParser::Clear()
{
Rows = Columns = 0;
if (_Data != NULL)
IM_FREE(_Data);
_Data = NULL;
_Index.clear();
}
bool ImGuiCsvParser::Load(const char* filename)
{
size_t len = 0;
_Data = (char*)ImFileLoadToMemory(filename, "rb", &len, 1);
if (_Data == NULL)
return false;
int columns = 1;
if (Columns > 0)
{
columns = Columns; // User-provided expected column count.
}
else
{
for (const char* c = _Data; *c != '\n' && *c != '\0'; c++) // Count columns. Quoted columns with commas are not supported.
if (*c == ',')
columns++;
}
// Count rows. Extra new lines anywhere in the file are ignored.
int max_rows = 0;
for (const char* c = _Data, *end = c + len; c < end; c++)
if ((*c == '\n' && c[1] != '\r' && c[1] != '\n') || *c == '\0')
max_rows++;
if (columns == 0 || max_rows == 0)
return false;
// Create index
_Index.resize(columns * max_rows);
int col = 0;
char* col_data = _Data;
for (char* c = _Data; *c != '\0'; c++)
{
const bool is_comma = (*c == ',');
const bool is_eol = (*c == '\n' || *c == '\r');
const bool is_eof = (*c == '\0');
if (is_comma || is_eol || is_eof)
{
_Index[Rows * columns + col] = col_data;
col_data = c + 1;
if (is_comma)
{
col++;
}
else
{
if (col + 1 == columns)
Rows++;
else
fprintf(stderr, "%s: Unexpected number of columns on line %d, ignoring.\n", filename, Rows + 1); // FIXME
col = 0;
}
*c = 0;
if (is_eol)
while (c[1] == '\r' || c[1] == '\n')
c++;
}
}
Columns = columns;
return true;
}
//-----------------------------------------------------------------------------
|