-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdr.c
1327 lines (1207 loc) · 38.7 KB
/
stdr.c
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
#include "stdr.h"
/*------------*/
/* Prototypes */
/*------------*/
/* type conversion */
void stdr_i2str(int value, char* buffer); // int to string
void stdr_f2str(float value, char* buffer, int precision); // float to string. depends on stdr_strcpy, stdr_i2str, stdr_strcat
void stdr_l2hex(unsigned long value, char* buffer); // long to hex
void stdr_l2str(long value, char* buffer); // long to string
void stdr_ptr2str(void* ptr, char* buffer); // memory pointer to string
void stdr_sz_t2hex(stdr_size_t value, char* hex, stdr_size_t hex_size); // size_t to hex
void stdr_sz_t2str(stdr_size_t value, char* buffer); // size_t to string
void stdr_str_out(const char* message); // output string to console - depends on stdr_strlen
char* stdr_str2c(const char* str, int c); // string to char
long stdr_str2l(const char* str, char** endptr, int base); // string to long
void stdr_ui2hex(unsigned int value, char* buffer); // uint to hex
void stdr_ui2str(stdr_size_t value, char* buffer); // uint to string
void stdr_ul2hex(unsigned long value, char* buffer); // ulong to hex
void stdr_ul2str(unsigned long value, char* buffer); // ulong to string
void stdr_uc2str(unsigned char value, char* buffer); // uchar to string
void stdr_us2str(unsigned short value, char* buffer); // ushort to string
/* ctype.h */
int stdr_isdigit(int c);
int stdr_isspace(int c);
int stdr_isxdigit(int c);
/* math.h */
float stdr_asin(float x);
float stdr_atan2(float y, float x); // depends on stdr_fabs
float stdr_cos(float x);
float stdr_fabs(float x);
float stdr_pow(float base, int exp);
float stdr_sin(float x);
float stdr_sqrt(float x);
float stdr_tanf(float x);
/* stdio.h */
WIN_VOID stdr_fclose(WIN_FILE* stream); // depends on stdr_free
WIN_INT stdr_feof(WIN_FILE* stream);
WIN_INT stdr_fgetc(WIN_FILE* stream); // depends on stdr_read
WIN_PCHR stdr_fgets(WIN_PCHR str, WIN_INT n, WIN_FILE* stream); // depends on stdr_read
WIN_INT stdr_fprintf(WIN_HANDLE file, WIN_PCCHR format, ...); // depends on stdr_vsnprintf
WIN_INT stdr_fscanf(WIN_FILE* stream, WIN_PCCHR format, ...); // depends on stdr_vsnprintf
WIN_FILE* stdr_fopen(WIN_PCCHR filename, WIN_PCCHR mode); // depends on stdr_strcmp, stdr_malloc
WIN_SIZE_T stdr_fread(WIN_PVOID ptr, WIN_SIZE_T size, WIN_SIZE_T count, WIN_FILE* stream);
WIN_INT stdr_fseek(WIN_FILE* stream, long offset, int whence);
WIN_LONG stdr_ftell(WIN_FILE* stream); // depends on stdr_printf
WIN_SIZE_T stdr_fwrite(WIN_PCVOID ptr, WIN_SIZE_T size, WIN_SIZE_T count, WIN_FILE* stream); // depends on stdr_printf
WIN_VOID stdr_perror(WIN_PCCHR s); // depends on stdr_printf
int stdr_printf(const char* format, ...); // depends on stdr_ul2hex, stdr_strlen, stdr_strcpy, stdr_ul2str, stdr_sz_t2str, stdr_us2str, stdr_uc2str, stdr_i2str, stdr_f2str, stdr_ptr2str, stdr_ui2str, stdr_ui2hex, stdr_str_out
WIN_VOID stdr_rewind(WIN_FILE* stream);
int stdr_sprintf(char* buffer, const char* format, ...); // depends on stdr_ul2str, stdr_strlen, stdr_strcpy
int stdr_sscanf_hex(const char* str, unsigned long* value);
int stdr_vsnprintf(char* buffer, stdr_size_t count, const char* format, stdr_va_list args); // depends on stdr_i2str, stdr_strlen, stdr_strcpy, stdr_f2str
/* stdlib.h */
void* stdr_calloc(stdr_size_t num, stdr_size_t size); // depends on stdr_malloc
void stdr_exit(int status);
void stdr_free(void* ptr); // depends on stdr_offsetof
WIN_PVOID stdr_malloc(WIN_SIZE_T size);
stdr_size_t stdr_malloc_block_size(void* ptr); // depends on stdr_offsetof
void* stdr_realloc(void* ptr, stdr_size_t new_size); // depends on stdr_malloc, stdr_malloc_block_size, stdr_free, stdr_memcpy
/* string.h */
void* stdr_memcpy(void* dst, const void* src, stdr_size_t n);
int stdr_memcmp(const void* s1, const void* s2, stdr_size_t n);
void* stdr_memset(void* s, int c, stdr_size_t n);
char* stdr_strcat(char* dst, const char* src);
char* stdr_strchr(const char* str, int c);
int stdr_strcmp(const char* str1, const char* str2);
char* stdr_strcpy(char* dst, const char* src);
char* stdr_strdup(const char* str); // depends on stdr_strlen, stdr_malloc
stdr_size_t stdr_strlen(const char* str);
int stdr_strncmp(const char* s1, const char* s2, stdr_size_t n);
char* stdr_strrchr(const char* str, int c);
char* stdr_strstr(const char* haystack, const char* needle);
/* unistd.h */
WIN_SIZE_T stdr_read(WIN_PVOID ptr, WIN_SIZE_T count, WIN_FILE* stream);
/* wchar.h */
stdr_size_t stdr_wcslen(const stdr_wchar_t* s);
stdr_size_t stdr_wcstombs(char* dst, const stdr_wchar_t* src, stdr_size_t max);
/*-------------------*/
/* Library Functions */
/*-------------------*/
/* type conversion */
void stdr_i2str(int value, char* buffer) {
char temp[32];
int i = 0, j = 0;
int is_negative = (value < 0);
if (is_negative) {
value = -value;
}
do {
temp[i++] = (value % 10) + '0';
value /= 10;
} while (value > 0);
if (is_negative) {
temp[i++] = '-';
}
temp[i] = '\0';
for (j = 0, i--; i >= 0; i--, j++) {
buffer[j] = temp[i];
}
buffer[j] = '\0';
}
void stdr_f2str(float value, char* buffer, int precision) {
if (precision < 0) precision = 6;
if (value == 0.0f) {
stdr_strcpy(buffer, "0.0");
return;
}
int negative = (value < 0);
if (negative) value = -value;
int int_part = (int)value;
float frac_part = value - int_part;
char int_buffer[32];
stdr_i2str(int_part, int_buffer);
char frac_buffer[32];
frac_part *= stdr_pow(10, precision);
stdr_i2str((int)frac_part, frac_buffer);
if (negative) {
stdr_strcpy(buffer, "-");
stdr_strcat(buffer, int_buffer);
} else {
stdr_strcpy(buffer, int_buffer);
}
stdr_strcat(buffer, ".");
stdr_strcat(buffer, frac_buffer);
}
void stdr_l2hex(unsigned long value, char* buffer) {
const char* hex_digits = "0123456789ABCDEF";
int index = 0;
if (value == 0) {
buffer[index++] = '0';
}
int num_digits = 0;
char reverse_hex[50];
while (value != 0) {
reverse_hex[num_digits++] = hex_digits[value & 0xF];
value >>= 4;
}
for (int i = num_digits - 1; i >= 0; i--) {
buffer[index++] = reverse_hex[i];
}
buffer[index] = '\0';
}
void stdr_l2str(long value, char* buffer) {
if (value < 0) {
*buffer++ = '-';
value = -value;
}
char reverse[20];
int i = 0;
if (value == 0) {
reverse[i++] = '0';
}
while (value > 0) {
reverse[i++] = (value % 10) + '0';
value /= 10;
}
while (i > 0) {
*buffer++ = reverse[--i];
}
*buffer = '\0';
}
void stdr_ptr2str(void* ptr, char* buffer) {
// dep stdr_printf
const char* hex_digits = "0123456789ABCDEF";
stdr_uintptr_t value = (stdr_uintptr_t)ptr;
char* p = buffer;
*p++ = '0';
*p++ = 'x';
for (int i = (sizeof(void*) * 2) - 1; i >= 0; i--) {
*p++ = hex_digits[(value >> (i * 4)) & 0xF];
}
*p = '\0';
}
void stdr_sz_t2hex(stdr_size_t value, char* hex, stdr_size_t hex_size) {
const char* hex_digits = "0123456789ABCDEF";
hex[hex_size - 1] = '\0';
for (int i = hex_size - 2; i >= 0; --i) {
hex[i] = hex_digits[value & 0xF];
value >>= 4;
}
}
void stdr_sz_t2str(stdr_size_t value, char* buffer) {
char temp[32];
int i = 0, j = 0;
do {
temp[i++] = (value % 10) + '0';
value /= 10;
} while (value > 0);
temp[i] = '\0';
for (j = 0, i--; i >= 0; i--, j++) {
buffer[j] = temp[i];
}
buffer[j] = '\0';
}
void stdr_str_out(WIN_PCCHR message) {
WIN_HANDLE hConsole = __GetStdHandle(WIN_STD_OUTPUT_HANDLE);
if (hConsole != WIN_INVALID_HANDLE_VALUE) {
WIN_DWORD written;
__WriteConsoleA(hConsole, message, stdr_strlen(message), &written, STDR_NULL);
}
}
char* stdr_str2c(const char* str, int c) {
while (*str != '\0') {
if (*str == (char)c) {
return (char*)str;
}
str++;
}
return STDR_NULL;
}
long stdr_str2l(const char* str, char** endptr, int base) {
if (base < 0 || base == 1 || base > 36) {
if (endptr) {
*endptr = (char*)str;
}
return 0;
}
while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\f' || *str == '\v') {
str++;
}
int sign = 1;
if (*str == '-') {
sign = -1;
str++;
} else if (*str == '+') {
str++;
}
long result = 0;
while (*str) {
int digit;
if (*str >= '0' && *str <= '9') {
digit = *str - '0';
} else if (*str >= 'A' && *str <= 'Z') {
digit = *str - 'A' + 10;
} else if (*str >= 'a' && *str <= 'z') {
digit = *str - 'a' + 10;
} else {
break;
}
if (digit >= base) {
break;
}
result = result * base + digit;
str++;
}
if (endptr) {
*endptr = (char*)str;
}
return sign * result;
}
void stdr_ui2hex(unsigned int value, char* buffer) {
const char* hex_digits = "0123456789ABCDEF";
int index = 0;
if (value == 0) {
buffer[index++] = '0';
}
int num_digits = 0;
char reverse_hex[50];
while (value != 0) {
reverse_hex[num_digits++] = hex_digits[value & 0xF];
value >>= 4;
}
for (int i = num_digits - 1; i >= 0; i--) {
buffer[index++] = reverse_hex[i];
}
buffer[index] = '\0';
}
void stdr_ui2str(stdr_size_t value, char* buffer) {
char temp[32];
int i = 0, j = 0;
do {
temp[i++] = (value % 10) + '0';
value /= 10;
} while (value > 0);
temp[i] = '\0';
for (j = 0, i--; i >= 0; i--, j++) {
buffer[j] = temp[i];
}
buffer[j] = '\0';
}
void stdr_ul2hex(unsigned long value, char* buffer) {
const char* hex_digits = "0123456789ABCDEF";
char temp[17];
int i = 0;
if (value == 0) {
temp[i++] = '0';
}
while (value > 0) {
temp[i++] = hex_digits[value & 0xF];
value >>= 4;
}
temp[i] = '\0';
int j = 0;
while (i > 0) {
buffer[j++] = temp[--i];
}
buffer[j] = '\0';
}
void stdr_ul2str(unsigned long value, char* buffer) {
const char* hex_digits = "0123456789ABCDEF";
int index = 0;
if (value == 0) {
buffer[index++] = '0';
}
int num_digits = 0;
char reverse_hex[50];
while (value != 0) {
reverse_hex[num_digits++] = hex_digits[value & 0xF];
value >>= 4;
}
for (int i = num_digits - 1; i >= 0; i--) {
buffer[index++] = reverse_hex[i];
}
buffer[index] = '\0';
}
void stdr_uc2str(unsigned char value, char* buffer) {
char temp[4];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
temp[i] = '\0';
for (int j = 0; j < i; j++) {
buffer[j] = temp[i - j - 1];
}
buffer[i] = '\0';
}
void stdr_us2str(unsigned short value, char* buffer) {
char temp[6];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
temp[i] = '\0';
for (int j = 0; j < i; j++) {
buffer[j] = temp[i - j - 1];
}
buffer[i] = '\0';
}
/* ctype.h */
int stdr_isdigit(int c) {
return (c >= '0' && c <= '9');
}
int stdr_isspace(int c) {
return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v');
}
int stdr_isxdigit(int c) {
return ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'));
}
/* math.h */
float stdr_asin(float x) {
if (x > 1.0 || x < -1.0) return 0.0;
float result = x;
float term = x;
float x2 = x * x;
for (int n = 1; n < 10; ++n) {
term *= (x2 * (2 * n - 1) * (2 * n - 1)) / ((2 * n) * (2 * n + 1));
result += term;
}
return result;
}
float stdr_atan2(float y, float x) {
if (x == 0.0) {
if (y > 0.0) return STDR_HALF_PI;
if (y < 0.0) return -STDR_HALF_PI;
return 0.0;
}
float abs_y = stdr_fabs(y), abs_x = stdr_fabs(x);
float a = (abs_x - abs_y) / (abs_x + abs_y);
float angle = STDR_PI / 4.0 - STDR_PI / 4.0 * a;
if (x < 0.0) {
if (y < 0.0) return -STDR_PI + angle;
return STDR_PI - angle;
}
return y < 0.0 ? -angle : angle;
}
float stdr_cos(float x) {
while (x > STDR_PI) x -= STDR_TWO_PI;
while (x < -STDR_PI) x += STDR_TWO_PI;
float result = 1.0;
float term = 1.0;
float x2 = x * x;
int sign = -1;
for (int n = 2; n <= 20; n += 2) {
term *= x2 / ((n - 1) * n);
result += sign * term;
sign = -sign;
}
return result;
}
float stdr_fabs(float x) {
return x < 0 ? -x : x;
}
float stdr_pow(float base, int exp) {
float result = 1.0;
while (exp > 0) {
result *= base;
exp--;
}
return result;
}
float stdr_sin(float x) {
while (x > STDR_PI) x -= STDR_TWO_PI;
while (x < -STDR_PI) x += STDR_TWO_PI;
float result = 0.0;
float term = x;
float x2 = x * x;
int sign = 1;
for (int n = 1; n <= 19; n += 2) {
result += sign * term;
term *= x2 / ((n + 1) * (n + 2));
sign = -sign;
}
return result;
}
float stdr_sqrt(float x) {
float y = 1.0;
float epsilon = 0.000001; // Precision level
float x0 = x;
while (x - y > epsilon) {
x = (x + y) / 2;
y = x0 / x;
}
return x;
}
float stdr_tanf(float x) {
// Constants for a polynomial approximation
const float a = 0.3333314036f;
const float b = 0.1333923995f;
const float c = 0.0533740603f;
const float d = 0.0245650893f;
const float e = 0.0029671978f;
const float f = 0.0002729889f;
float x2 = x * x;
return x * (1 + x2 * (a + x2 * (b + x2 * (c + x2 * (d + x2 * (e + x2 * f))))));
}
/* stdio.h */
WIN_VOID stdr_fclose(WIN_FILE* stream) {
if (stream) {
__FlushFileBuffers((WIN_HANDLE)stream->hFile);
__CloseHandle((WIN_HANDLE)stream->hFile);
stdr_free(stream);
}
}
WIN_INT stdr_feof(WIN_FILE* stream) {
WIN_DWORD dwCurrentPos = __SetFilePointer((WIN_HANDLE)stream->hFile, 0, STDR_NULL, WIN_FILE_CURRENT);
WIN_DWORD dwFileSize = __GetFileSize((WIN_HANDLE)stream->hFile, STDR_NULL);
return dwCurrentPos >= dwFileSize;
}
WIN_INT stdr_fgetc(WIN_FILE* stream) {
WIN_CHR c;
if (stdr_read(&c, 1, stream->hFile) == 1) {
return (WIN_UCHR)c;
} else {
return WIN_EOF;
}
}
WIN_PCHR stdr_fgets(WIN_PCHR str, WIN_INT n, WIN_FILE* stream) {
WIN_INT i = 0;
WIN_CHR c;
while (i < n - 1) {
if (stdr_read(&c, 1, stream->hFile) != 1) {
break;
}
str[i++] = c;
if (c == '\n') {
break;
}
}
str[i] = '\0';
return i > 0 ? str : STDR_NULL;
}
WIN_INT stdr_fprintf(WIN_HANDLE file, WIN_PCCHR format, ...) {
WIN_CHR buffer[1024];
stdr_va_list args;
stdr_va_start(args, format);
WIN_INT ret = stdr_vsnprintf(buffer, 0, format, args);
stdr_va_end(args);
WIN_DWORD written;
__WriteFile(file, buffer, stdr_strlen(buffer), &written, STDR_NULL);
return ret;
}
WIN_INT stdr_fscanf(WIN_FILE* stream, WIN_PCCHR format, ...) {
WIN_CHR buffer[1024];
stdr_size_t i = 0;
WIN_INT c;
while (i < sizeof(buffer) - 1) {
c = stdr_read((WIN_HANDLE)&buffer[i], 1, stream->hFile);
if (c == 0 || buffer[i] == '\n') {
break;
}
i++;
}
buffer[i] = '\0';
stdr_va_list args;
stdr_va_start(args, format);
WIN_INT ret = stdr_vsnprintf(STDR_NULL, 0, format, args);
stdr_va_end(args);
return ret;
}
WIN_FILE* stdr_fopen(WIN_PCCHR filename, WIN_PCCHR mode) {
WIN_DWORD dwDesiredAccess = 0;
WIN_DWORD dwCreationDisposition = WIN_OPEN_EXISTING;
if (stdr_strcmp(mode, "rb") == 0) {
dwDesiredAccess = WIN_GENERIC_READ;
} else if (stdr_strcmp(mode, "wb") == 0) {
dwDesiredAccess = WIN_GENERIC_WRITE;
dwCreationDisposition = WIN_CREATE_ALWAYS;
} else if (stdr_strcmp(mode, "r+b") == 0) {
dwDesiredAccess = WIN_GENERIC_READ | WIN_GENERIC_WRITE;
dwCreationDisposition = WIN_OPEN_ALWAYS;
}
WIN_HANDLE hFile = __CreateFileA(filename, dwDesiredAccess, WIN_FILE_SHARE_READ, STDR_NULL, dwCreationDisposition, WIN_FILE_ATTRIBUTE_NORMAL, STDR_NULL);
if (hFile == WIN_INVALID_HANDLE_VALUE) {
return STDR_NULL;
}
WIN_FILE* file = (WIN_FILE*)stdr_malloc(sizeof(WIN_FILE));
if (!file) {
__CloseHandle((WIN_HANDLE)hFile);
return STDR_NULL;
}
file->hFile = hFile;
return file;
}
WIN_SIZE_T stdr_fread(WIN_PVOID ptr, WIN_SIZE_T size, WIN_SIZE_T count, WIN_FILE* stream) {
if (size == 0 || count == 0) return 0;
WIN_SIZE_T total_size = size * count;
WIN_DWORD bytesRead;
if (!__ReadFile((WIN_HANDLE)stream->hFile, ptr, (WIN_DWORD)total_size, &bytesRead, STDR_NULL)) {
return -1;
}
stdr_size_t elements_read = bytesRead / size;
return elements_read;
}
WIN_INT stdr_fseek(WIN_FILE* stream, WIN_LONG offset, WIN_INT whence) {
WIN_DWORD dwMoveMethod;
switch (whence) {
case WIN_SEEK_SET: dwMoveMethod = WIN_FILE_BEGIN; break;
case WIN_SEEK_CUR: dwMoveMethod = WIN_FILE_CURRENT; break;
case WIN_SEEK_END: dwMoveMethod = WIN_FILE_END; break;
default: return -1;
}
if (__SetFilePointer((WIN_HANDLE)stream->hFile, offset, STDR_NULL, dwMoveMethod) == WIN_INVALID_SET_FILE_POINTER && __GetLastError() != WIN_NO_ERROR) {
return -1;
}
return 0;
}
WIN_LONG stdr_ftell(WIN_FILE* stream) {
if (!stream || !stream->hFile) {
stdr_printf("Error: Invalid file stream or handle.\n");
return -1;
}
WIN_DWORD dwPos = __SetFilePointer((WIN_HANDLE)stream->hFile, 0, STDR_NULL, WIN_FILE_CURRENT);
if (dwPos == WIN_INVALID_SET_FILE_POINTER) {
WIN_DWORD dwError = __GetLastError();
stdr_printf("SetFilePointer failed with error %lu\n", dwError);
return -1;
}
return (WIN_LONG)dwPos;
}
WIN_SIZE_T stdr_fwrite(WIN_PCVOID ptr, WIN_SIZE_T size, WIN_SIZE_T count, WIN_FILE* stream) {
WIN_SIZE_T total_size = size * count;
WIN_DWORD bytesWritten;
WIN_BOOL result = __WriteFile((WIN_HANDLE)stream->hFile, ptr, (WIN_DWORD)total_size, &bytesWritten, STDR_NULL);
if (!result || bytesWritten < total_size) {
stdr_printf("Failed to write expected bytes: expected %zu, wrote %lu\n", total_size, bytesWritten);
return bytesWritten / size;
}
return count;
}
WIN_VOID stdr_perror(WIN_PCCHR s) {
WIN_DWORD errorMessageID = __GetLastError();
if (errorMessageID == 0) {
stdr_printf("%s: No error\n", s);
return;
}
WIN_PCHR messageBuffer = STDR_NULL;
__FormatMessageA(
WIN_FORMAT_MESSAGE_ALLOCATE_BUFFER | WIN_FORMAT_MESSAGE_FROM_SYSTEM | WIN_FORMAT_MESSAGE_IGNORE_INSERTS,
STDR_NULL, errorMessageID, __MAKELANGID(WIN_LANG_NEUTRAL, WIN_SUBLANG_DEFAULT), (WIN_PCHR)&messageBuffer, 0, STDR_NULL);
stdr_printf("%s: %s\n", s, messageBuffer);
__LocalFree(messageBuffer);
}
int stdr_printf(const char* format, ...) {
char buffer[1024];
char num_buffer[64];
const char* p = format;
char* b = buffer;
stdr_va_list args;
stdr_va_start(args, format);
while (*p) {
if (*p == '%') {
++p;
int width = 0;
int precision = 0;
int zero_pad = 0;
if (*p == '0') {
zero_pad = 1;
++p;
}
while (*p >= '0' && *p <= '9') {
width = width * 10 + (*p - '0');
++p;
}
if (*p == '.') {
++p;
while (*p >= '0' && *p <= '9') {
precision = precision * 10 + (*p - '0');
++p;
}
}
if (*p == 'l') {
++p;
if (*p == 'X') {
unsigned long value = stdr_va_arg(args, unsigned long);
stdr_ul2hex(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
} else if (*p == 'u') {
unsigned long value = stdr_va_arg(args, unsigned long);
stdr_ul2str(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
}
} else if (*p == 'z') {
++p;
if (*p == 'u') {
stdr_size_t value = stdr_va_arg(args, stdr_size_t);
stdr_sz_t2str(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
}
} else if (*p == 'h') {
++p;
if (*p == 'u') {
unsigned short value = (unsigned short)stdr_va_arg(args, unsigned int);
stdr_us2str(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
} else if (*p == 'h') {
++p;
if (*p == 'u') {
unsigned char value = (unsigned char)stdr_va_arg(args, unsigned int);
stdr_uc2str(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
}
}
} else {
switch (*p) {
case 'i':
case 'd': {
int value = stdr_va_arg(args, int);
stdr_i2str(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
break;
}
case 'f': {
float value = stdr_va_arg(args, float);
stdr_f2str((float)value, num_buffer, precision);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
break;
}
case 's': {
const char* str = stdr_va_arg(args, const char*);
int len = stdr_strlen(str);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, str);
b += len;
break;
}
case 'c': {
int char_value = stdr_va_arg(args, int);
*b++ = (char)char_value;
break;
}
case 'p': {
void* ptr = stdr_va_arg(args, void*);
stdr_ptr2str(ptr, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
break;
}
case 'u': {
unsigned int value = stdr_va_arg(args, unsigned int);
stdr_ui2str(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
break;
}
case 'X': {
unsigned int value = stdr_va_arg(args, unsigned int);
stdr_ui2hex(value, num_buffer);
int len = stdr_strlen(num_buffer);
int pad = (width > len) ? width - len : 0;
if (zero_pad) {
while (pad-- > 0) *b++ = '0';
}
stdr_strcpy(b, num_buffer);
b += len;
break;
}
default:
*b++ = '%'; *b++ = *p;
break;
}
}
} else {
*b++ = *p;
}
p++;
}
*b = '\0';
stdr_va_end(args);
stdr_str_out(buffer);
return stdr_strlen(buffer);
}
WIN_VOID stdr_rewind(WIN_FILE* stream) {
__SetFilePointer((WIN_HANDLE)stream->hFile, 0, STDR_NULL, WIN_FILE_BEGIN);
}
int stdr_sprintf(char* buffer, const char* format, ...) {
char num_buffer[64];
const char* p = format;
char* b = buffer;
stdr_va_list args;
stdr_va_start(args, format);
while (*p) {
if (*p == '%') {
++p;
int width = 0;
while (*p >= '0' && *p <= '9') {
width = width * 10 + (*p - '0');
++p;
}
if (*p == 'l') {
++p;
if (*p == 'X' || *p == 'x') {
unsigned long value = stdr_va_arg(args, unsigned long);
stdr_ul2str(value, num_buffer);
int num_len = stdr_strlen(num_buffer);
stdr_strcpy(b, num_buffer);
b += num_len;
}
} else {
*b++ = '%';
*b++ = *p;
}
} else {
*b++ = *p;
}
++p;
}
*b = '\0';
stdr_va_end(args);
return stdr_strlen(buffer);
}
int stdr_sscanf_hex(const char* str, unsigned long* value) {
unsigned long result = 0;
int count = 0;
while (*str) {
char c = *str++;
int digit = (c >= '0' && c <= '9') ? c - '0' :
(c >= 'a' && c <= 'f') ? c - 'a' + 10 :
(c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1;
if (digit == -1) break;
result = result * 16 + digit;
count++;
}
*value = result;
return count ? 1 : 0;
}
int stdr_vsnprintf(char* buffer, stdr_size_t count, const char* format, stdr_va_list args) {
char num_buffer[64];
const char* p = format;
char* b = buffer;
stdr_size_t remaining = count;
stdr_size_t required = 0;
while (*p) {
if (*p == '%') {
p++;
switch (*p) {
case 'i':
case 'd': {
int value = stdr_va_arg(args, int);
stdr_i2str(value, num_buffer);
stdr_size_t len = stdr_strlen(num_buffer);
required += len;
if (buffer && remaining > len) {
stdr_strcpy(b, num_buffer);
b += len;
remaining -= len;
} else {
if (buffer) remaining = 1;
}
break;
}
case 'f': {
float value = stdr_va_arg(args, float);
stdr_f2str((float)value, num_buffer, 6);
stdr_size_t len = stdr_strlen(num_buffer);
required += len;
if (buffer && remaining > len) {
stdr_strcpy(b, num_buffer);
b += len;
remaining -= len;
} else {
if (buffer) remaining = 1;
}
break;
}
case 's': {
const char* str = stdr_va_arg(args, const char*);
stdr_size_t len = stdr_strlen(str);
required += len;
if (buffer && remaining > len) {
stdr_strcpy(b, str);
b += len;
remaining -= len;
} else {
if (buffer) remaining = 1;
}
break;
}
default:
required += 2;
if (buffer && remaining > 2) {
*b++ = '%';
*b++ = *p;
remaining -= 2;
} else {
if (buffer) remaining = 1;
}
break;
}
} else {
required++;
if (buffer && remaining > 1) {
*b++ = *p;
remaining--;
} else {
if (buffer) remaining = 1;
}
}
p++;
}
if (buffer && remaining > 0) {
*b = '\0';
}
return required;
}
/* stdlib.h */
void* stdr_calloc(stdr_size_t num, stdr_size_t size) {
stdr_size_t total_size = num * size;
void* ptr = stdr_malloc(total_size);
if (!ptr) {
return STDR_NULL;
}
unsigned char* byte_ptr = (unsigned char*)ptr;
for (stdr_size_t i = 0; i < total_size; i++) {
byte_ptr[i] = 0;
}
return ptr;
}
WIN_VOID stdr_exit(WIN_INT status) {
__ExitProcess((WIN_UINT)status);
}