-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsta_string.h
More file actions
2722 lines (1932 loc) · 57.7 KB
/
sta_string.h
File metadata and controls
2722 lines (1932 loc) · 57.7 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
#ifdef STA_PUBLIC_API
/*
#define STA_STRING_IMPLEMENTATION
#include "sta-main/sta_string.h"
*/
//strings array functions
a_bool a_string_array_get_index_of(const char ** array,int array_size,const char * s_to_be_found);
//strings functions
char * a_string_from_char_array_malloc(const char * ca,int cc);
char * a_string_copy_malloc(const char * s);
char * a_string_copy_2_malloc(const char * s1,const char * s2);
char * a_string_copy_3_malloc(const char * s1,const char * s2,const char * s3);
char * a_string_from_w_string_malloc(const wchar_t * wc_str);
wchar_t * w_string_from_a_string_malloc(const char * s);
char * a_string_from_int_malloc(int v);
char * a_string_copy_digit0to9_only_malloc(const char * s);
char * a_string_copy_fixed_size_malloc(const char * s,const char c,int cc);
char * a_string_get_after_malloc(const char * s,const char * ss);
char * a_string_get_between_malloc(const char * s,const char * after,const char * stop_on);
//
char * a_string_get_after_ptr(const char * s,const char * ss);
//string search and compare functions
a_bool a_string_are_same(const char * a,const char * b);
a_bool a_string_is_valid(const char * s);
int a_index_of_char(const char * s,int c);
int a_index_of_string(const char * s,const char * ss);
a_bool a_string_is_begin_with(const char * s, const char * s_begin);
a_bool a_string_is_end_with(const char * s, const char * s_end);
//http
char * a_url_from_http_GET_header_malloc( char * http_header);
char * a_url_from_http_HEAD_header_malloc( char * http_header);
//inplace
void a_string_replace_char(char * s,char c,char new_c);
void a_string_replace_chars(char * s,const char * c,char new_c);
void w_string_replace_chars(wchar_t * s,const wchar_t * c,wchar_t new_c);
void a_string_keep_alphanum_and_replace(char * s,const char * keep,int c_any_other);
//bytes
uint8_t* a_bytes_from_hex_string_malloc(const char * hs,int extra_bytes,int * out_size);
char * a_hex_string_from_bytes_malloc(const uint8_t * mem,int mem_size);
//char functions
int a_char_hex_to_int(int c);
//text file
a_bool a_text_read_line(char ** lines,char ** line,int * line_size);
//all lines reader
typedef struct
{
char * text_all;
a_bool text_all_is_malloc;
char * lines;
char * line;
int line_size;
int line_count;
} a_all_lines_t;
a_all_lines_t * a_all_lines_new_from_file(const char * filename);
a_all_lines_t * a_all_lines_new_from_string(char * s,a_bool do_malloc,a_bool is_malloc);
void a_all_lines_delete(a_all_lines_t * al);
a_bool a_all_lines_next(a_all_lines_t * al);
a_bool a_all_lines_next_peek(a_all_lines_t * al,char ** line,int * line_size);
a_bool a_all_lines_reset(a_all_lines_t * al);
// don't impact on reading lines
int a_all_lines_count(a_all_lines_t * al);
//var lines
#define for_lines_file(filename) a_all_lines_t * lines = a_all_lines_new_from_file(filename); if(lines){ \
while(all_lines_next(lines)){
#define for_lines(txt) a_all_lines_t * lines = a_all_lines_new_from_string(txt,0,0); if(lines){ \
while(all_lines_next(lines)){
#define next_lines } all_lines_delete(lines); }
//for_line malloc and free used auto
//var lines line_error empty_line line(likely used)
#define for_line_file(filename) int line_error = 0;char * empty_line = "\0\0"; char * line; for_lines_file(filename)\
if(lines->line_size) {line = a_string_from_char_array_malloc(lines->line,lines->line_size);\
if(!line){line= empty_line; line_error = 1;} }\
else line = empty_line;
#define for_line(text) int line_error = 0;char * empty_line = "\0\0"; char * line; for_lines(text)\
if(lines->line_size) {line = a_string_from_char_array_malloc(lines->line,lines->line_size);\
if(!line){line= empty_line; line_error = 1;} }\
else line = empty_line;
#define next_line if(line != empty_line) free(line); next_lines
// string to number
//2num (from string)
//int
#define a_num_i(i) atoi(i)
//uint
#define a_num_u(u) strtoul(u,0,10)
//float atof(d)
#define a_num_f(s) strtof(s,0)
//double
#define a_num_d(d) strtod(d,0)
//64
#define a_num_i64(s) strtoll (s,0,10)
// unsigned long long
#define a_num_u64(s) strtoull(s,0,10)
// scoped string
//copy
#define ssz__(expr) ssz__cdh(cdh,expr)
#define uuz__(expr) uuz__cdh(cdh,expr)
#define ssz_uuz(s) ssz__(a_string_from_w_string_malloc(s))
#define uuz_ssz(s) uuz__(w_string_from_a_string_malloc(s))
//char count
#define ssz_cc(s,cc) ssz__(a_string_from_char_array_malloc(s,cc))
#define ssz_1(psz) ssz__(a_string_copy_malloc(psz))
#define ssz_2(psz,psz2) ssz__(a_string_copy_2_malloc(psz,psz2))
#define ssz_3(psz,psz2,psz3) ssz__(a_string_copy_3_malloc(psz,psz2,psz3))
//#define ssz_4(psz,psz2,psz3,psz4) ssz__(a_string_copy_4_malloc(psz,psz2,psz3,psz4))
//load text file
#define ssz_file(filename) ssz__(a_string_from_file_malloc(filename))
//
#define ssz_s_cc(s,cc) ssz__(a_string_copy_fixed_size_malloc(s,'0',cc))
//todo
#define ssz_clipboard(expr) ssz__
// var ssz__empty
char * ssz__cdh(a_cdh,char * s);
wchar_t * uuz__cdh(a_cdh,wchar_t * s);
#define uuz_same(s1,s2) (wcscmp(s1,s2)==0)
#define uuz_not_same(s1,s2) (wcscmp(s1,s2)!=0)
// A Project By Arshad Latti
#endif
#ifdef STA_STRING_IMPLEMENTATION
#define STA_BYTES_IMPLEMENTATION
#endif
#ifdef STA_PUBLIC_API
/*
#define STA_BYTES_IMPLEMENTATION
#include "sta-main/sta_bytes.h"
*/
void a_bytes_read_at(void * bytes_dst,const void * bytes_src,int start,int len);
void a_bytes_write_at(void * bytes_dst,const void * bytes_src,int start,int len);
void * a_bytes_copy_malloc(const void * bytes,int bytes_size);
void * a_bytes_copy_2_malloc(const void * bytes_1,int bytes_size_1,const void * bytes_2,int bytes_size_2);
void * a_bytes_copy_3_malloc(const void * bytes_1,int bytes_size_1,const void * bytes_2,int bytes_size_2,const void * bytes_3,int bytes_size_3);
void * a_bytes_can_null_term_from_file_malloc(const char * file_name,a_bool be_null_term_output,long * out_size);
a_bool a_bytes_to_file(const char * file_name,const void * buffer,long buffer_size);
void * a_bytes_from_file_malloc(const char * file_name,long * out_size);
void * a_bytes_from_file_part_malloc(const char * file_name,int offset,int size);
void * a_bytes_pointer_at_offset(void * base,int offset);
void * a_bytes_malloc(void ** out_ptr_ptr,size_t size);
//void a_free(void * p);
#define a_free(p) {if(p) free(p);}
#define a_malloc(var,size) a_bytes_malloc((void**)&var,size)
// void * a_bytes_from_file_header_malloc(const char * file_name,int size);
#define a_bytes_from_file_header_malloc(file_name,size) a_bytes_from_file_part_malloc(file_name,0,size)
char * a_string_from_file_malloc(const char * file_name);
a_bool a_string_to_file(const char * file_name,const char * text);
//
#define a_array_to_file(file_name,array_name,array_size,name_of_array_type) a_bytes_to_file(file_name,array_name,array_size * sizeof(name_of_array_type))
// var uint8_t * data; long data_size;
#define a_data_bytes_from_file_malloc(filename) long data_size; uint8_t * data = a_bytes_from_file_malloc(filename,&data_size);
#define a_data_bytes_from_file_part_malloc(filename,offset,size) long data_size; uint8_t * data = a_bytes_from_file_part(filename,offset,size);
#define a_data_bytes_to_file(filename) a_bytes_to_file(filename,data,data_size);
// A Project By Arshad Latti
#endif
#ifndef STA_DEF_H_
#define STA_DEF_H_
#ifndef STA_NO_STDINT_H
#include <stdint.h>
#endif
/*
//#define STA_NO_STDINT_H
//#define STA_COMPILER_VC_6
#ifdef STA_COMPILER_VC_6
#include "stdint-vc6.h"
#endif
*/
#define a_bool int
#define a_true 1
#define a_false 0
#define a_null 0
// PACKED
/*
A_PACKED_BEGIN
struct disk_data
{
char x;
int y;
}A_PACKED;
A_PACKED_END
*/
#ifndef STA_PACKED_OPTIONS
//auto detect compiler
#define STA_PACKED_OPTIONS
#define STA_PACKED_COMPILER_GCC
//#define STA_PACKED_COMPILER_MSVC
//#define STA_PACKED_COMPILER_TCC
#endif
#ifdef STA_PACKED_COMPILER_GCC
#define A_PACKED_BEGIN
#define A_PACKED_END
#define A_PACKED __attribute__((packed))
#endif
#ifdef STA_PACKED_COMPILER_MSVC
#define A_PACKED_BEGIN #pragma pack( push,1 )
#define A_PACKED_END #pragma pack( pop )
#define A_PACKED
#endif
// Framebuffer Image
#define framebuffer_pixel_t uint32_t
struct s_framebuffer_image
{
framebuffer_pixel_t * pixels;
int32_t h,w,pitch;//all in pixels
};
typedef struct s_framebuffer_image framebuffer_image_t;
//AV_PIX_FMT_BGRA
struct s_framebuffer_color
{
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
};
typedef struct s_framebuffer_color framebuffer_color_t;
//
struct s_clip_rect
{
int32_t x,y,h,w;
};
typedef struct s_clip_rect clip_rect_t;
struct s_a_rect
{
int32_t x,y,x2,y2;
};
typedef struct s_a_rect a_rect_t;
struct s_a_rect_size
{
int h,w;
};
typedef struct s_a_rect_size a_rect_size_t;
struct s_a_point
{
int x,y;
};
typedef struct s_a_point a_point_t;
typedef struct s_a_date_time a_date_time_t;
struct s_a_date_time
{
uint16_t year;
uint8_t month;
uint8_t day;//date
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t flags;// bit 1.0=utc 2.0=24hr 3.0=date valid 4.0=time valid 5.0valid 6-8=resvered FF=not valid
};
//
#define A_MIN(a,b) ((a) < (b) ? (a) : (b))
#define A_MAX(a,b) ((a) < (b) ? (b) : (a))
#define A_KB(k) ((k) * 1024)
#define A_MB(mb) (A_KB((mb) * 1024))
#define A_GB(gb) (A_MB((gb) * 1024))
#define A_TB(tb) (A_GB((tb) * 1024))
//if.not if.index
#define if_not(expr) if(!(expr))
#define if_index(expr) if((expr) >= 0)
#define for_i(i_max) int i; for(i=0;i<i_max;i++)
#define for_i_(i_max) for(i=0;i<i_max;i++)
#define for_var(i,i_max) int i; for(i=0;i<i_max;i++)
#define for_var_(i,i_max) for(i=0;i<i_max;i++)
// for_j
#define for_j(x_max) for_var(j,x_max)
#define for_j_(x_max) for_var_(j,x_max)
// for_k
#define for_k(x_max) for_var(k,x_max)
#define for_k_(x_max) for_var_(k,x_max)
// for_x for_y
#define for_x(x_max) int x; for(x=0;x<x_max;x++)
#define for_x_(x_max) for(x=0;x<x_max;x++)
#define for_y(y_max) int y; for(y=0;y<y_max;y++)
#define for_y_(y_max) for(y=0;y<y_max;y++)
// #define for_i_xy(w,h) int i_x,i_y,i_xy,i_xy_max; i_xy_max = w*h; for(i_xy=0;i_xy < i_xy_max;i_xy++
// use }}
// #define for_xy_next }}
#define next_xy }}
#define for_xy(w,h) int i_x,i_y;\
for(i_y = 0; i_y < h ; i_y++){\
for(i_x = 0; i_x < w ; i_x++){
#define for_xy_(w,h) for(i_y = 0; i_y < h ; i_y++){\
for(i_x = 0; i_x < w ; i_x++){
#define for_fi(fi) for_xy(fi->w,fi->h)
#define for_fi_(fi) for_xy_(fi->w,fi->h)
#define next_fi }
#define for_i_cc(s) int cc = strlen(s); int i; for(i=0;i<cc;i++)
#define for_i_cc_(s) cc = strlen(s); for(i=0;i<cc;i++)
//#define for_cc(s) for_i_cc(s)
#define return_if_null(expr) if(!(expr)) return 0
#define return_if_zero(expr) if(!(expr)) return 0
#define return_if_fail(expr) if((expr) != 0) return 0
#define return_if_not_find(expr) if((expr) < 0) return 0
// ptr
//#define if
#define if_null(expr) if(!(expr))
//status
#define if_success(expr) if((expr) == 0)
#define if_fail(expr) if((expr) != 0)
//index
#define if_not_find(expr) if((expr) < 0)
#define if_find(expr) if((expr) >=0)
#define puts_i(i) printf("%i\n",i);
#define puts_s_i(s,i) printf("%s %i\n",s,i);
#define IFF(expr,on_true,on_false) ((expr)? on_true : on_false)
#define bitwise_and(a,b) ((a)&(b))
#define bitwise_or(a,b) ((a)|(b))
#define bitwise_xor(a,b) ((a)^(b))
#define bitwise_not(a) (~(a))
#define bitwise_shift_left(a,n) ((a) << (n))
#define bitwise_shift_right(a,n) ((a) >> (n))
#define logical_and(a,b) ((a)&&(b))
#define logical_or(a,b) ((a)||(b))
#define logical_not(a) (!(a))
//#define A_DEBUG_PUTS
#ifdef A_DEBUG_PUTS
#define dputs_i(i) printf("%i\n",i);
#define dputs_s_i(s,i) printf("%s %i\n",s,i);
#endif
#define argc_msg(num,msg) if(argc < num){puts(msg); return 0;}
//todo expr = 0;
#define free_safe(expr) {if(expr) free(expr);}
/* verbose_printf */
//#define A_VERBOSE_PRINTF
#ifdef A_VERBOSE_PRINTF
#define verbose_printf printf
#else
#define verbose_printf(expr,...)
#endif
#endif
#ifndef STA_BYTES_H
#define STA_BYTES_H
//#include "STA/sta_def.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
#define STA_BYTES_IMPLEMENTATION
#include "sta-main/sta_bytes.h"
*/
void a_bytes_read_at(void * bytes_dst,const void * bytes_src,int start,int len);
void a_bytes_write_at(void * bytes_dst,const void * bytes_src,int start,int len);
void * a_bytes_copy_malloc(const void * bytes,int bytes_size);
void * a_bytes_copy_2_malloc(const void * bytes_1,int bytes_size_1,const void * bytes_2,int bytes_size_2);
void * a_bytes_copy_3_malloc(const void * bytes_1,int bytes_size_1,const void * bytes_2,int bytes_size_2,const void * bytes_3,int bytes_size_3);
void * a_bytes_can_null_term_from_file_malloc(const char * file_name,a_bool be_null_term_output,long * out_size);
a_bool a_bytes_to_file(const char * file_name,const void * buffer,long buffer_size);
void * a_bytes_from_file_malloc(const char * file_name,long * out_size);
void * a_bytes_from_file_part_malloc(const char * file_name,int offset,int size);
void * a_bytes_pointer_at_offset(void * base,int offset);
void * a_bytes_malloc(void ** out_ptr_ptr,size_t size);
//void a_free(void * p);
#define a_free(p) {if(p) free(p);}
#define a_malloc(var,size) a_bytes_malloc((void**)&var,size)
// void * a_bytes_from_file_header_malloc(const char * file_name,int size);
#define a_bytes_from_file_header_malloc(file_name,size) a_bytes_from_file_part_malloc(file_name,0,size)
char * a_string_from_file_malloc(const char * file_name);
a_bool a_string_to_file(const char * file_name,const char * text);
//
#define a_array_to_file(file_name,array_name,array_size,name_of_array_type) a_bytes_to_file(file_name,array_name,array_size * sizeof(name_of_array_type))
// var uint8_t * data; long data_size;
#define a_data_bytes_from_file_malloc(filename) long data_size; uint8_t * data = a_bytes_from_file_malloc(filename,&data_size);
#define a_data_bytes_from_file_part_malloc(filename,offset,size) long data_size; uint8_t * data = a_bytes_from_file_part(filename,offset,size);
#define a_data_bytes_to_file(filename) a_bytes_to_file(filename,data,data_size);
// A Project By Arshad Latti
#ifdef __cplusplus
}
#endif
#endif
#ifdef STA_BYTES_IMPLEMENTATION
#ifndef STA_BYTES_IMPLEMENTATION_INCLUDED
#define STA_BYTES_IMPLEMENTATION_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
void * a_bytes_can_null_term_from_file_malloc(const char * file_name,a_bool be_null_term_output,long * out_size)
{
FILE * f = fopen(file_name,"rb");
if(!f)
return a_null;
if(fseek(f,0,SEEK_END))
{
fclose(f);
return a_null;
}
long size = ftell(f);
void* buffer=a_null;
char * text;
if(size > 0)
{
if(be_null_term_output)
size++;
if(out_size)
*out_size = size;
if(!fseek(f,0,SEEK_SET))
{
buffer = malloc(size);
if(buffer)
{
if(!fread(buffer,1,size,f))
{
free(buffer);
fclose(f);
return a_null;
}
if(be_null_term_output)
{
text = (char*)buffer;
text[size - 1]=0;
}
}
}
}
fclose(f);
return buffer;
}
void * a_bytes_copy_2_malloc(const void * bytes_1,int bytes_size_1,const void * bytes_2,int bytes_size_2)
{
if(!bytes_size_1 && !bytes_size_2) return 0;
char * r = (char*)malloc(bytes_size_1 + bytes_size_2);
if(r)
{
if(bytes_size_1)
memcpy(r,bytes_1,bytes_size_1);
if(bytes_size_2)
memcpy(&r[bytes_size_1],bytes_2,bytes_size_2);
}
return r;
}
void * a_bytes_copy_3_malloc(const void * bytes_1,int bytes_size_1,const void * bytes_2,int bytes_size_2,const void * bytes_3,int bytes_size_3)
{
if(!bytes_size_1 && !bytes_size_2) return 0;
char * r = (char*)malloc(bytes_size_1 + bytes_size_2 + bytes_size_3);
if(r)
{
if(bytes_size_1)
memcpy(r,bytes_1,bytes_size_1);
if(bytes_size_2)
memcpy(&r[bytes_size_1],bytes_2,bytes_size_2);
if(bytes_size_3)
memcpy(&r[bytes_size_1 + bytes_size_2],bytes_3,bytes_size_3);
}
return r;
}
void * a_bytes_copy_malloc(const void * bytes,int bytes_size)
{
if(!bytes_size) return 0;
void * r = malloc(bytes_size);
if(r)
memcpy(r,bytes,bytes_size);
return r;
}
void * a_bytes_from_file_malloc(const char * file_name,long * out_size)
{
return a_bytes_can_null_term_from_file_malloc(file_name,a_false,out_size);
}
void * a_bytes_from_file_part_malloc(const char * file_name,int offset,int size)
{
FILE * f = fopen(file_name,"rb");
if(!f)
return a_null;
if(fseek(f,0,SEEK_END))
{
fclose(f);
return a_null;
}
long file_size = ftell(f);
void* buffer=a_null;
char * text;
if(file_size >= size + offset)
{
/* if(be_null_term_output)
size++; */
if(!fseek(f,offset,SEEK_SET))
{
buffer = malloc(size);
if(buffer)
{
if(fread(buffer,1,size,f) != size)
{
free(buffer);
fclose(f);
return a_null;
}
}
}
}
fclose(f);
return buffer;
}
void * a_bytes_malloc(void ** out_ptr_ptr,size_t size)
{
*out_ptr_ptr = malloc(size);
return *out_ptr_ptr;
}
void * a_bytes_pointer_at_offset(void * base,int offset)
{
char * p = (char*)base;
p+=offset;
return p;
}
void a_bytes_read_at(void * bytes_dst,const void * bytes_src,int start,int len)
{
char * p = (char*) bytes_src;
p+=start;
if(len)
memcpy(bytes_dst,p,len);
}
a_bool a_bytes_to_file(const char * file_name,const void * buffer,long buffer_size)
{
FILE * f=fopen(file_name,"wb");
if(f)
{
if(fwrite(buffer,1,buffer_size,f) == buffer_size)
{
fclose(f);
return a_true;
}
fclose(f);
}
return a_false;
}
void a_bytes_write_at(void * bytes_dst,const void * bytes_src,int start,int len)
{
char * p = (char*) bytes_dst;
p+=start;
if(len)
memcpy(p,bytes_src,len);
}
char * a_string_from_file_malloc(const char * file_name)
{
return (char * ) a_bytes_can_null_term_from_file_malloc(file_name,a_true,a_null);
}
a_bool a_string_to_file(const char * file_name,const char * text)
{
return a_bytes_to_file(file_name,text,strlen(text));
}
#ifdef __cplusplus
}
#endif
#endif
#endif
#ifdef STA_STRING_IMPLEMENTATION
#define STA_CONTEXT_DYNAMIC_HANDLER_IMPLEMENTATION
#endif
#ifdef STA_PUBLIC_API
/*
#define STA_CONTEXT_DYNAMIC_HANDLER_IMPLEMENTATION
#include "sta-main/sta_context_dynamic_handler.h"
*/
typedef struct context_dynamic_handler_s context_dynamic_handler_t;
struct context_dynamic_handler_s
{
context_dynamic_handler_t * sub;
/* context_dynamic_handler_t * super;
int sub_is_error; */
int is_error;
void * r;//return value
gt_list_t * dh;
};
//note cdh parameter can null
context_dynamic_handler_t * context_dynamic_handler_new(void);
context_dynamic_handler_t * context_dynamic_handler_sub(context_dynamic_handler_t * cdh);
void context_dynamic_handler_delete(context_dynamic_handler_t * cdh);
void * context_dynamic_handler_add(context_dynamic_handler_t * cdh,void * ptr,void * func);
//return pointer of var
void * context_dynamic_handler_add_var(context_dynamic_handler_t * cdh,void ** var_ptr,void * func);
void * context_dynamic_handler_set_error(context_dynamic_handler_t * cdh,int is_error);
//var cdh
#define a_begin(expr) context_dynamic_handler_t * cdh = context_dynamic_handler_new();\
if(!cdh) return 0;
#define a_handle(ptr,func) if(ptr)\
{if(!context_dynamic_handler_add(cdh,ptr,func)) {(*func)(ptr); a_return(0)}}\
else a_return(0)
#define a_handle_var(var,func) if(!context_dynamic_handler_add_var(cdh,(void **)&var,func)){if(var)(*func)(var);a_return(0)}
#define a_handle_r(ptr,func) if(cdh){cdh->r = ptr; a_handle_var(cdh->r,func)}else{if(ptr)(*func)(ptr); a_return(0)}
#define a_return(expr) {context_dynamic_handler_delete(cdh);return expr;}
#define a_ok(expr) {if(!(expr)) a_return(0)}
#define a_ok_cdh(expr) {if(cdh){ if(cdh->is_error); a_return(0)}}
#define a_return_ok(expr) {if(cdh) cdh->r = 0;context_dynamic_handler_delete(cdh);return expr;}
#define a_sub context_dynamic_handler_sub(cdh)
#define a_cdh context_dynamic_handler_t * cdh
/*
//example a_cdh and a_sub( both can null)
int cdh_foo(a_cdh)
{
//use a_cdh
}
#define foo(expr) cdh_foo(a_sub)
//example a_begin and a_return
int fun(void)
{
a_begin()
cdh_foo(a_sub);
foo()
a_return(0)
}
*/
#define a_create_pod(name_of_type,name_of_variable) name_of_type * name_of_variable = (name_of_type *) malloc(sizeof(name_of_type)); a_handle(name_of_variable,free) \
memset(name_of_variable,0,sizeof(name_of_type));
// example: a_new_context_dynamic_handler_t a_delete_context_dynamic_handler_t
// format : {a_new_}name_of_type {a_delete_}name_of_type
#define a_create(name_of_type,name_of_variable) a_create_(name_of_type,(),name_of_variable)
//a_create_(a,(),v)
#define a_create_(name_of_type,parameters,name_of_variable) name_of_type * name_of_variable = a_new##name_of_type parameters;\
a_handle(name_of_variable,a_delete##name_of_type)
#define a_new(name_of_type,name_of_variable) a_new_(name_of_type,(),name_of_variable)
#define a_new_(name_of_type,parameters,name_of_variable)name_of_variable = a_new##name_of_type parameters;\
a_handle(name_of_variable,a_delete##name_of_type)
// A Project By Arshad Latti
#endif
#ifdef STA_CONTEXT_DYNAMIC_HANDLER_IMPLEMENTATION
#define STA_GT_LIST_IMPLEMENTATION
#endif
#ifdef STA_PUBLIC_API
//#define STA_GT_LIST_IMPLEMENTATION
//#include "sta-main/sta_gt_list.h"
struct s_gt_list__node
{
struct s_gt_list__node * next;
struct s_gt_list__node * pre;
gt_term_free_func data_term_func;
char data[sizeof(struct s_gt_list__node *)];
};
typedef struct s_gt_list__node gt_list__node_t;
struct s_gt_list
{
gt_list__node_t * node_first;
gt_list__node_t * node_last;
size_t item_size;
size_t items_count;
gt_init_with_func item_init_with_func;
gt_term_free_func item_term_free_func;
};
typedef struct s_gt_list gt_list_t;
gt_list_t * gt_list_new_ex(size_t item_size,gt_init_with_func item_init_with_func,gt_term_free_func item_term_free_func);
gt_list_t * gt_list_new(size_t item_size);
void gt_list_delete(gt_list_t * gtl);
a_bool gt_list_add_ex(gt_list_t * gtl,void * item,size_t item_size,gt_init_with_func item_init_with_func,gt_term_free_func item_term_free_func);
a_bool gt_list_add(gt_list_t * gtl,void * item);
a_bool gt_list_add_first(gt_list_t * gtl,void * item);
a_bool gt_list_add_first_ex(gt_list_t * gtl,void * item,size_t item_size,gt_init_with_func item_init_with_func,gt_term_free_func item_term_free_func);
gt_list__node_t * gt_list_add_at(gt_list_t * gtl,void * item,int index);
gt_list__node_t * gt_list_add_at_ex(gt_list_t * gtl,void * item,size_t item_size,gt_init_with_func item_init_with_func,gt_term_free_func item_term_free_func,int index);
void * gt_list_get_at(gt_list_t * gtl,int index);
//a_bool gt_list_add_first_ex
gt_list__node_t * gt_list__node_add(gt_list_t * gtl,size_t item_size,a_bool is_at_first);
gt_list__node_t * gt_list__node_remove(gt_list_t * gtl,a_bool is_at_first);
gt_list__node_t * gt_list__node_add_at(gt_list_t * gtl,size_t item_size,int index);
gt_list__node_t * gt_list__node_get_at(gt_list_t * gtl,int index);
#endif
#ifndef STA_DEF_H_
#define STA_DEF_H_
#ifndef STA_NO_STDINT_H
#include <stdint.h>
#endif
/*
//#define STA_NO_STDINT_H
//#define STA_COMPILER_VC_6
#ifdef STA_COMPILER_VC_6
#include "stdint-vc6.h"
#endif
*/
#define a_bool int
#define a_true 1
#define a_false 0
#define a_null 0
// PACKED
/*
A_PACKED_BEGIN
struct disk_data
{
char x;
int y;
}A_PACKED;
A_PACKED_END
*/
#ifndef STA_PACKED_OPTIONS
//auto detect compiler
#define STA_PACKED_OPTIONS
#define STA_PACKED_COMPILER_GCC
//#define STA_PACKED_COMPILER_MSVC
//#define STA_PACKED_COMPILER_TCC
#endif