-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenscript.c
More file actions
executable file
·3292 lines (2770 loc) · 108 KB
/
Copy pathgenscript.c
File metadata and controls
executable file
·3292 lines (2770 loc) · 108 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
//#!gcc ./genscript.c -o ./gsb.exe
/*************************************************************************
* Generates build.ninja files for multiple platforms
* build: gcc genscript.c -ggdb3 -o gsb.exe
* clang genscript.c -ggdb3 -o gsb.exe
* run: gsb.exe [-help]
*
*
* Modules
* If first line is $static or $shared it will be build as such
*************************************************************************/
///TODO: Fix Complier selection
///TODO: Check for overrun in elix_cstring_inreplace
#define GENSCRIPT_VERSION 20260706 // Changes: Some work on Compiler selection, and cross compiling. Need more testing on that.
#define SOURCE_DIRECTORY "src"
//#define SOURCE_DIRECTORY "source"
#define DEFAULT_BUILD_MODE "debug" //"release"
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#ifndef SSIZE_MAX
#ifdef _WIN64
#define SSIZE_MAX INT64_MAX
#define SSIZE_MIN INT64_MIN
#else
#define SSIZE_MAX INT32_MAX
#define SSIZE_MIN INT32_MIN
#endif
#endif
#define __USE_LARGEFILE64
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#if __WIN32__
#include <process.h>
#else
#include <spawn.h>
#endif
#ifndef nullptr
#define nullptr NULL
#endif
/*** Elix ***/
#define allocate_mem(TYPE, COUNT) (TYPE *)calloc(COUNT, sizeof(TYPE))
#define free_if(M) if (M != nullptr) { free(M); M = nullptr;}
#define LOG_LINE printf("%s:%d \n", __FILE__, __LINE__)
#define LOG_MESSAGE(M, ...) printf("%18s:%04d | " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define LOG_INFO(M, ...) printf( M "\n", ##__VA_ARGS__)
#define PRINT(M, ...) printf( M , ##__VA_ARGS__)
#define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
#define FH(q) q
#define UNUSEDARG __attribute__((unused))
#ifndef ELIX_FILE_PATH_LENGTH
#define ELIX_FILE_PATH_LENGTH 768
#endif
#ifndef ELIX_FILE_NAME_LENGTH
#define ELIX_FILE_NAME_LENGTH 256
#endif
#ifndef ftello64
#define ftello64 ftello
#endif
#ifndef fseeko64
#define fseeko64 fseeko
#endif
#define ELIX_CHAR_LOWER 1
#define ELIX_CHAR_UPPER 2
#define ELIX_CHAR_CAPITALISE 3
#if defined(PLATFORM_WINDOWS) && defined(COMPILER_MSCV)
#define pZD "%Id"
#define pZX "%Ix"
#define pZU "%Iu"
#else
#define pZD "%zd"
#define pZX "%zx"
#define pZU "%zu"
#endif
typedef void * data_pointer;
typedef FILE * file_pointer;
typedef uint64_t file_size;
typedef int64_t file_offset;
typedef enum {
EFF_STATUS_UNKNOWN = 0x001,
EFF_STATUS_WRITABLE = 0x002,
EFF_STATUS_READABLE = 0x004,
EFF_FILE_READ = 0x010,
EFF_FILE_WRITE = 0x020, // Append
EFF_FILE_TRUNCATE = 0x040, // Reset
EFF_FILE_WRITING = 0x060,
EFF_FILE_NEW = 0x080, //new
EFF_FILE_CREATING = 0x0C0,
EFF_FILE_ERROR = 0x100,
EFF_FILE_READ_ERROR = 0x300,
EFF_FILE_WRITE_ERROR = 0x500,
EFF_FILE_CREATE_ERROR = 0x700,
} ElixFileFlag;
struct ElixFile {
FILE * handle;
file_size length;
file_size pos;
uint32_t flag;
};
typedef struct ElixFile elix_file;
// > String
uint32_t elix_hash( const char * str, size_t len ) {
//Jenkins One-at-a-time hash
uint32_t hash = 0;
size_t i;
for (i = 0; i < len; i++) {
hash += str[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
size_t elix_cstring_length(const char * string, uint8_t include_terminator ) {
if (string) {
size_t c = 0;
while(*string++) {
++c;
}
return c + include_terminator;
}
return 0;
}
bool elix_cstring_has_suffix( const char * str, const char * suffix) {
size_t str_length = elix_cstring_length(str, 0);
size_t suffix_length = elix_cstring_length(suffix, 0);
size_t offset = 0;
if ( str_length < suffix_length )
return false;
offset = str_length - suffix_length;
for (size_t c = 0; c < suffix_length; c++ ) {
if ( str[offset + c] != suffix[c] )
return false;
}
return true;
}
bool elix_cstring_has_prefix( const char * str, const char * prefix) {
size_t str_length = elix_cstring_length(str, 0);
size_t prefix_length = elix_cstring_length(prefix, 0);
if ( str_length < prefix_length )
return false;
for (size_t c = 0; c < prefix_length; c++ ) {
if ( str[c] != prefix[c] )
return false;
}
return true;
}
size_t elix_cstring_append( char * str, const size_t len, const char * text, const size_t text_len) {
size_t length = elix_cstring_length(str, 0);
// TODO: Switch to memcpy
for (size_t c = 0;length < len && c < text_len; length++, c++) {
str[length] = text[c];
}
if ( length >= len ){
LOG_INFO("elix_cstring_append reached the end of the buffer.");
}
str[length] = 0;
return length;
}
void elix_cstring_copy( const char * source_init, char * dest_init) {
char * source = (char *)source_init;
char * dest = dest_init;
do {
*dest++ = *source++;
} while( *source != 0);
*dest = '\0';
}
void elix_cstring_copy2( const char * source_init, char * dest_init, size_t dest_size) {
char * source = (char *)source_init;
char * dest = dest_init;
do {
*dest++ = *source++;
dest_size--;
} while( dest_size && *source != 0);
*dest = '\0';
}
char * elix_cstring_from( const char * source, const char * default_str, size_t max_length ) {
//ASSERT( default_str != nullptr )
const char * ptr = source != nullptr ? source : default_str;
size_t length = (max_length == SIZE_MAX) ? elix_cstring_length(ptr, 1) : max_length;
if ( length <= 1 ) {
if ( default_str != nullptr ) {
ptr = default_str;
length = elix_cstring_length(ptr, 1);
} else {
//LOG_ERROR("elix_cstring_from failed, source was empty");
return nullptr;
}
}
char * dest = calloc(length, 1); //new char[length]();
elix_cstring_copy2(ptr, dest, length-1);
//dest[length] = 0;
return dest;
}
#define ELIX_IS_WHITESPACE(x) (x == ' ' || x == '\n' || x == '\r')
size_t elix_cstring_trim( char * string ) {
size_t length = elix_cstring_length(string, 0);
while( length && ELIX_IS_WHITESPACE(string[0]) ) {
memmove(string, string + 1, length);
length--;
}
size_t pos = length ? length-1 : 0;
while( pos && ELIX_IS_WHITESPACE(string[pos]) ) {
string[pos] = 0;
length--;
pos--;
}
return length;
}
void elix_cstring_transform( char * string, uint8_t mode ) {
size_t length = elix_cstring_length(string, 0);
for (size_t var = 0; var < length; ++var) {
switch (mode) {
case ELIX_CHAR_LOWER:
if (string[var] >= 'A' && string[var] <= 'Z') {
string[var] += 32;
}
break;
case ELIX_CHAR_UPPER:
if (string[var] >= 'a' && string[var] <= 'z') {
string[var] -= 32;
}
break;
case ELIX_CHAR_CAPITALISE:
if (string[var] >= 'a' && string[var] <= 'z') {
string[var] -= 32;
}
mode = 0;
break;
default:
break;
}
}
}
size_t elix_cstring_find_of(const char * str, const char * search, size_t offset) {
size_t length = elix_cstring_length(str,0);
size_t sl = elix_cstring_length(search,0);
for (size_t c = offset; c < length - (sl-1) && str[c] != 0; c++) {
bool found = false;
for (size_t sc = 0; sc < sl; sc++) {
if ( str[c+sc] == search[sc]) {
found = (sc == sl-1);
} else {
break;
}
}
if ( found )
return c;
}
return SIZE_MAX;
}
size_t elix_cstring_find_last_of(const char * str, const char * search, size_t offset) {
size_t length = elix_cstring_length(str, 0);
size_t sl = elix_cstring_length(search, 0);
for (size_t c = length - sl; c > offset; c--) {
bool found = false;
for (size_t sc = 0; sc < sl; sc++) {
if ( str[c+sc] == search[sc]) {
found = (sc == sl-1);
} else {
break;
}
}
if ( found ) {
return c;
}
}
return SIZE_MAX;
}
size_t elix_cstring_inreplace( char * source_text, size_t buffer_size, const char *search, const char *replace, uint8_t repeat) {
///NOTE: This is slow, and not a good implemenation
///TODO: Check for overflow
size_t search_len = 0, replace_len = 0;
ssize_t diff_len = 0;
size_t source_len = elix_cstring_length(source_text, 1);
do {
size_t pos = elix_cstring_find_of(source_text, search, 0);
if ( pos != SIZE_MAX ) {
search_len = elix_cstring_length(search, 0);
replace_len = elix_cstring_length(replace, 0);
diff_len = search_len - replace_len;
size_t l = 0;
if ( diff_len > 0 ) {
for (size_t i = pos; i < source_len; l++,i++) {
if ( l < replace_len ) {
source_text[i] = replace[l];
} else {
source_text[i] = source_text[i+diff_len];
}
}
} else {
if ( pos+search_len >= buffer_size) {
return 0;
}
if ( diff_len < 0 ) {
size_t j = source_len-diff_len;
for (size_t i = source_len; i >= pos+search_len; i--) {
j = i-diff_len;
source_text[j] = source_text[i];
}
}
for (size_t i = pos; i < buffer_size && l < replace_len; l++,i++) {
source_text[i] = replace[l];
}
}
} else {
repeat = 0;
}
} while (repeat);
return diff_len;
}
uint32_t elix_cstring_hash( const char * str ) {
return elix_hash(str, elix_cstring_length(str, 0));
}
uint8_t elix_cstring_match( const char * p1, const char * p2,size_t max_length) {
uint8_t * a = (uint8_t *)p1, * b = (uint8_t *)p2;
for (size_t i = 0; i < max_length; ++i) {
if ( a[i] != b[i] ) {
return 0;
}
if ( a[i] == 0 ) {
return 1;
}
}
return 1;
}
char * elix_cstring_merge( char ** source_array, char token) {
size_t str_size = 0;
size_t str_position = 0;
char * string = nullptr;
uint8_t c = 0;
uint8_t max = 0;
if (!source_array) {
return string;
}
while ( source_array[c]) {
str_size += elix_cstring_length(source_array[c], 0 );
c++;
}
max = c;
if ( str_size > 0x1FF ) {
return string;
}
str_size += c;
string = calloc(str_size, sizeof(char));
for (uint8_t i = 0; i < max; i++) {
str_position = elix_cstring_append(string, str_size, source_array[i], elix_cstring_length(source_array[i], 1) );
if ( str_position < str_size ) {
string[str_position-1] = token;
}
}
return string;
}
char ** elix_cstring_split( const char * source, char token, char string_bracket) {
#define TOKEN_CACHE 32
size_t source_len = elix_cstring_length(source, 0);
size_t tokens = 0;
size_t position[TOKEN_CACHE] = {0};
size_t maxlength = 0, last_token = 0;
uint8_t outside_string = 1;
for (size_t i = 0; i < source_len; i++) {
if ( source[i] == token ) {
if ( outside_string ) {
maxlength = (i - last_token) > maxlength ? (i - last_token) : maxlength;
last_token = i;
if (tokens < TOKEN_CACHE ) {
position[tokens] = last_token + 1;
}
tokens++;
}
} else if ( source[i] == string_bracket ) {
outside_string = !outside_string;
}
}
position[tokens] = source_len+1;
tokens++;
char ** output = nullptr;
if (tokens > TOKEN_CACHE ) {
//TODO
LOG_INFO("Split found %zd tokens, only has room for %d", tokens, TOKEN_CACHE);
} else {
output = malloc((tokens+1) * sizeof(char *));
if (!output) {
LOG_INFO("malloc failed in elix_cstring_split");
}
last_token = 0;
for (size_t i = 0; i < tokens; i++) {
output[i] = elix_cstring_from(source + last_token, "", position[i] - last_token);
//LOG_INFO("%d > %d - '%s'", last_token, position[i], output[i]);
last_token = position[i];
}
output[tokens] = nullptr;
}
return output;
#undef TOKEN_CACHE
}
size_t elix_cstring_dequote( char * string ) {
size_t length = elix_cstring_length(string, 0);
while( length && string[0] == '"' ) {
memmove(string, string + 1, length);
length--;
}
size_t pos = length ? length-1 : 0;
while( pos && string[pos] == '"' ) {
string[pos] = 0;
length--;
pos--;
}
return length;
}
// < String
// > File
file_offset elix_file_offset(elix_file * file) {
// Note: due to pointless warnings, using ftello64 instead of ftello
file_offset q = ftello64( FH(file->handle) );
if ( q < 0 ) {
return 0;
}
return q;
}
uint32_t elix_file_at_end(elix_file * file) {
if ( file->handle ) {
if ( file->pos >= file->length )
return true;
return !(feof( FH(file->handle) ) == 0);
}
return true;
}
uint32_t elix_file_seek(elix_file * file, file_offset pos ) {
if ( fseeko64( FH(file->handle), pos, SEEK_SET ) == 0) {
file->pos = pos;
return true;
}
return false;
}
size_t elix_file_read(elix_file * file, data_pointer buffer, size_t data_size, size_t amount) {
if ( !file || !file->handle || file->length < data_size || !buffer ) {
return 0;
}
size_t q = fread(buffer, data_size, amount, FH(file->handle));
if ( q != 0 ) {
file_offset p = elix_file_offset( file );
if ( p >= 0 ) {
file->pos = p;
}
}
return q;
}
file_size elix_file_tell(elix_file * file) {
return (file_size)(elix_file_offset(file));
}
file_size elix_file__update_length(elix_file * file) {
file_offset pos = elix_file_offset(file);
fseeko64( FH(file->handle), 0, SEEK_END );
file->length = elix_file_tell(file);
fseeko64( FH(file->handle), pos, SEEK_SET );
return file->length;
}
uint32_t elix_file_open(elix_file * file, const char * filename, ElixFileFlag mode) {
file->pos = 0;
file->length = 0;
file->handle = 0;
if ( mode & EFF_FILE_NEW ) {
struct stat buffer = {0};
if ( stat(filename, &buffer) == 0 ) {
file->flag = EFF_FILE_CREATE_ERROR;
return (uint32_t) EFF_FILE_CREATE_ERROR;
}
}
//
char cmode[4] = "rb";
if ( mode & EFF_FILE_CREATING ) {
cmode[0] = 'w';
} else if ( mode & EFF_FILE_WRITING ) {
cmode[0] = 'a';
}
file->handle = fopen(filename, cmode);
//fopen_s( &file->handle, filename, (mode & EFF_FILE_WRITE ? "wb" : "rb") );
if ( file->handle ) {
file->flag = mode;
elix_file__update_length(file);
return 0;
} else {
file->flag = EFF_FILE_ERROR;
//LOG_MESSAGE("File not found %s", filename);
}
return (uint32_t)(EFF_FILE_ERROR | (mode & EFF_FILE_WRITING ? EFF_FILE_WRITE_ERROR : EFF_FILE_READ_ERROR));
}
uint32_t elix_file_close(elix_file * file) {
if ( file->handle ) {
return fclose( FH(file->handle) ) == 0;
}
return false;
}
size_t elix_file_read_line(elix_file * file, data_pointer data, size_t data_size) {
if ( !file || !file->handle || !data ) {
return 0;
}
uint8_t char_buffer;
uint8_t * data_buffer = data;
size_t index = 0;
size_t q;// = elix_file_read(file, &char_buffer, 1,1);
do {
q = elix_file_read(file, &char_buffer, 1,1);
if (char_buffer > 13 )
data_buffer[index++] = char_buffer;
} while ( char_buffer > 10 && index < data_size && !elix_file_at_end(file));
data_buffer[index] = 0;
return index;
}
size_t elix_file_write_string( elix_file * file, const char * string, size_t data_size) {
if ( !file || !file->handle ) {
return 0;
}
if ( data_size == 0 ) {
data_size = elix_cstring_length(string, 0);
}
return fwrite(string, data_size, 1, FH(file->handle));
}
// < File
// > Path
struct ElixPath {
char * uri;
char * path;
char * filename;
char * filetype;
};
typedef struct ElixPath elix_path;
struct ElixDirectory {
uint16_t count;
elix_path * files;
};
typedef struct ElixDirectory elix_directory;
void elix_path_update(elix_path * path, const char * string) {
size_t length = elix_cstring_length(string, 0);
size_t split = SIZE_MAX;
size_t extension_split = SIZE_MAX;
for (split = length-1; split > 0; split--) {
if ( string[split] == '\\' || string[split] == '/') {
split++;
break;
}
if ( extension_split == SIZE_MAX && string[split] == '.') {
extension_split = split;
}
}
path->uri = calloc(length+1, 1);
elix_cstring_copy(string, path->uri);
path->path = elix_cstring_from( string, "/", split );
#ifdef PLATFORM_WINDOWS
elix_cstring_char_replace(path->path, '\\', '/');
#endif
if ( extension_split != SIZE_MAX && extension_split != split ) {
path->filename = elix_cstring_from(string + split, "/", extension_split - split + 1);
path->filetype = elix_cstring_from(string + extension_split, "/",SIZE_MAX);
} else {
path->filename = elix_cstring_from(string + split, "/", length - split+1);
path->filetype = nullptr;
}
}
elix_path elix_path_create(const char * string) {
elix_path uri = {0,0,0,0};
size_t length = elix_cstring_length(string, 0);
size_t split = SIZE_MAX;
size_t extension_split = SIZE_MAX;
for (split = length-1; split > 0; split--) {
if ( string[split] == '\\' || string[split] == '/') {
split++;
break;
}
if ( extension_split == SIZE_MAX && string[split] == '.') {
extension_split = split;
}
}
uri.uri = malloc(length+1);
memset(uri.uri,0, length+1);
elix_cstring_copy(string, uri.uri);
uri.path = elix_cstring_from( string, "/", split );
#ifdef PLATFORM_WINDOWS
elix_cstring_char_replace(uri.path, '\\', '/');
#endif
if ( extension_split != SIZE_MAX && extension_split != split ) {
uri.filename = elix_cstring_from(string + split, "/", extension_split - split + 1);
uri.filetype = elix_cstring_from(string + extension_split, "/",SIZE_MAX);
} else {
uri.filename = elix_cstring_from(string + split, "/", length - split+1);
uri.filetype = nullptr;
}
return uri;
}
// < Path
// > OS
void elix_os_directory_list_destroy(elix_directory ** directory) {
for (int f = 0; f < (*directory)->count; ++f) {
free_if((*directory)->files[f].uri)
free_if((*directory)->files[f].path)
free_if((*directory)->files[f].filename)
free_if((*directory)->files[f].filetype)
}
(*directory)->count = 0;
free_if((*directory)->files)
//free_if(*directory)
}
elix_directory * elix_os_directory_list_files(const char * path, const char * suffix) {
uint16_t index = 0;
elix_directory * directory = nullptr;
DIR * current_directory = nullptr;
struct dirent * entity = nullptr;
char buffer[ELIX_FILE_PATH_LENGTH] = {0};
size_t path_len = elix_cstring_length(path, 0);
current_directory = opendir(path);
if ( !current_directory ) {
return directory;
}
directory = allocate_mem(elix_directory,1);
while ((entity = readdir(current_directory)) ) {
if ( entity->d_name[0] == '.' && (entity->d_name[1] == '.'|| entity->d_name[1]== 0)) {
} else if (suffix) {
if ( elix_cstring_has_suffix(entity->d_name, suffix ) ) {
directory->count++;
}
} else {
directory->count++;
}
}
directory->files = allocate_mem(elix_path, directory->count);
rewinddir(current_directory);
elix_cstring_copy(path, buffer);
if ( path[path_len-1] != '/' ) {
elix_cstring_append(buffer, ELIX_FILE_PATH_LENGTH, "/", 1);
path_len++;
}
char * filename_buffer = buffer + path_len;
size_t path_ubffer = ELIX_FILE_PATH_LENGTH - path_len;
while ( (entity = readdir(current_directory)) ) {
size_t filename_size = elix_cstring_length(entity->d_name, 0);
if ( entity->d_name[0] == '.' && (entity->d_name[1] == '.'|| entity->d_name[1]== 0)){
} else if (suffix) {
if ( elix_cstring_has_suffix(entity->d_name, suffix ) ) {
elix_cstring_append(buffer, ELIX_FILE_PATH_LENGTH, entity->d_name, filename_size);
elix_path_update(&(directory->files[index]), buffer);
index++;
memset(filename_buffer, 0, filename_size);
}
} else {
elix_cstring_append(buffer, ELIX_FILE_PATH_LENGTH, entity->d_name, filename_size);
directory->files[index] = elix_path_create(buffer);
index++;
memset(filename_buffer, 0, filename_size);
}
}
closedir(current_directory);
return directory;
}
bool elix_os_directory_make(const char * path, uint32_t mode, UNUSEDARG bool parent) {
#if __WIN32__
if ( ! mkdir(path) ) {
return true;
}
errno_t err;
_get_errno( &err );
#else
int32_t err = mkdir(path, S_IRWXU| S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if ( !err ) {
return true;
}
#endif
if ( err == EEXIST ) {
//LOG_MESSAGE("Directory was not created because dirname is the name of an existing file, directory, or device. %s", path);
} else if ( err == ENOENT ) {
//LOG_MESSAGE("Path was not found. %s", path);
}
return false;
}
uint8_t elix_os_directory_is( const char * path){
#ifdef __WIN32__
struct _stat64 directory;
if ( !_stat64( path, &directory ) ) {
return !!(directory.st_mode & _S_IFDIR);
}
errno_t err;
_get_errno( &err );
#else
struct stat64 directory;
int32_t err = stat64( path, &directory );
if ( !err ) {
return S_ISDIR(directory.st_mode);
}
#endif
if ( err == ENOENT ) {
//LOG_MESSAGE("Path was not found. %s", path);
} else if ( err == EINVAL ) {
LOG_MESSAGE("Invalid parameter; . %s", path);
}
return false;
}
void elix_os_command_capture( char * program, char * buffer, size_t buffer_size, char ** env, ...) {
int err = 0;
size_t count = 0;
size_t str_size = 0;
size_t str_position = 0;
char * command_string = nullptr;
va_list passed_arguments, count_arguments;
va_start(passed_arguments, env);
va_copy(count_arguments, passed_arguments);
while ( true ) {
char * carg = va_arg(count_arguments, char*);
if (!carg) break;
str_size += elix_cstring_length(carg, 1);
count++;
}
va_end(count_arguments);
str_size += elix_cstring_length(program, 1) + count;
if ( str_size > 0x1FF ) {
LOG_INFO("Command lenght is to much: %s", program);
return;
}
command_string = calloc(str_size, sizeof(char));
str_position = elix_cstring_append(command_string, str_size, program, elix_cstring_length(program, 1) );
if ( count ) {
for ( size_t i = 1; i <= count; i++) {
if ( str_position < str_size ) {
command_string[str_position-1] = ' ';
}
char * current_arg = va_arg(passed_arguments, char*);
str_position = elix_cstring_append(command_string, str_size, current_arg, elix_cstring_length(current_arg, 1) );
}
}
FILE * command_pipe = popen(command_string, "r");
if ( !command_pipe ) {
LOG_INFO("Error running command: %s", program);
} else {
size_t q = fread(buffer, buffer_size, 1, command_pipe);
pclose(command_pipe);
}
va_end(passed_arguments);
free_if(command_string);
}
void elix_os_command( char * program, char ** env, ...) {
//
int err = 0;
size_t count = 0;
size_t str_size = 0;
char ** arguments = nullptr;
va_list passed_arguments, count_arguments;
va_start(passed_arguments, env);
va_copy(count_arguments, passed_arguments);
while ( true ) {
char * carg = va_arg(count_arguments, char*);
if (!carg) break;
count++;
}
va_end(count_arguments);
if ( count ) {
arguments = calloc((count + 2) , sizeof(char *));
arguments[0] = program;
for ( size_t i = 1; i <= count; i++) {
arguments[i] = va_arg(passed_arguments, char*);
}
}
#ifdef __WIN32__
err = _spawnvp( _P_WAIT, arguments[0], (const char *const *) arguments );
#else
id_t pid;
err = posix_spawnp(&pid, arguments[0], nullptr, nullptr, arguments, nullptr );
#endif
if ( err != 0 ) {
LOG_INFO("Error running command: %s", program);
}
va_end(passed_arguments);
free_if(arguments);
}
// < OS
/*** Genscript ***/
typedef struct {
uint32_t id;
char note[16];
char text[1024];
} PlatformConfig;
typedef struct {
char stage[16];
char oext[16];
char content[256];
} ExtensionOutput;
typedef struct {
char name[16];
char command[64];
char flag[512];
char libflag[512];
char define[256];
} CompilerConfigOption;
#define PACK_ID(A, B, C, D) (A | B << 8 | C << 16 | D << 24 )
#define UNPACK_ID(ID) { ID & 0xFF, (ID & 0xFF00) >> 8, (ID & 0xFF0000) >> 16, (ID & 0xFF000000) >> 24, 0 }
typedef struct {
uint32_t id; // Use PACK_ID macro
char filename[64];
char content[1024];
} EditorFile;
/* Genscript Default Settings */
static char config_arch_text[512] = "[defines]\n\
PLATFORM_BITS=$bits\n\
[options]\n\
compiler=$compiler\n\
linker=$compiler\n\
static_linker=ar -rc\n\
";
static char config_default_text[2048] = "[options]\n\
program_suffix=\n\
shared_suffix=.so\n\
static_suffix=.a\n\
cppStandard=c++17\n\
cStandard=c17\n\
[includes]\n\
./include/\n\
[options-windows]\n\
program_suffix=.exe\n\
shared_suffix=.dll\n\
[options-3ds]\n\
finaliser=3dsxtool\n\
program_suffix=.elf\n\
finalise_suffix=.3dsx\n\
[commands]\n\
compile_cpp=${compiler} ${compiler_includes} ${compller_defines} ${compiler_cpp_flags} ${compiler_flags} -o $out -c $in\n\
compile_c=${compiler} ${compiler_includes} ${compller_defines} ${compiler_c_flags} ${compiler_flags} -o $out -c $in\n\
link_shared=${linker} -shared ${compiler_lib_flags} $in -o ${binary_prefix}$out ${compiler_lib}\n\
link_static=${static_linker} ${object_dir}/$out $in \n\
link=${linker} ${compiler_lib_flags} $in -o ${binary_prefix}$out ${compiler_linker_flags} ${compiler_lib} \n\
finalise=${finaliser} ${finalise_flags} $in -o ${binary_prefix}$out\n\
build_resources=echo\n\
clean=rm -rf ${object_dir}\n\
";
static CompilerConfigOption compiler_debug_options[8] = {
{"gcc", "$triplet-gcc", "-ggdb3", "-ggdb3 ", "_DEBUG"},
{"gcc-full", "$triplet-gcc","-ggdb3 -fsanitize=address,undefined -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fasynchronous-unwind-tables",
"-ggdb3 -fsanitize=address,undefined -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fasynchronous-unwind-tables", "_DEBUG"},
{"clang", "clang --target=$triplet", "-ggdb3", "-ggdb3 ", "_DEBUG"},
};