| ︙ | | | ︙ | |
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// core
#define Endians_Little 1
#define Endians_Big 2
// time
#define Scalpi_time_ms_per_s 1000
// Scalpi_Logger
#define Scalpi_Logger_writers_c_out_writer 0
#define Scalpi_Logger_writers_file_writer 1
#define Scalpi_Logger_writers_console_writer 2
#define Scalpi_Logger_writers_terminal_writer 3
// Scalpi_Console_ansi
#define Scalpi_Console_ansi_esc "\x1B"
|
|
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// core
#define Endians_Little 1
#define Endians_Big 2
// time
#define Scalpi_time_ms_per_s 1000
// Scalpi_Logger_writers
#define Scalpi_Logger_writers_c_out_writer 0
#define Scalpi_Logger_writers_file_writer 1
#define Scalpi_Logger_writers_console_writer 2
#define Scalpi_Logger_writers_terminal_writer 3
// Scalpi_Console_ansi
#define Scalpi_Console_ansi_esc "\x1B"
|
| ︙ | | | ︙ | |
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
|
// macroses
// c
#define or ||
#define and &&
#define OFFSETOF(type, field) ((size_t) &(((type *)0)->field))
#define FIELD_PARENT_PTR(type, field, instance) ((type *)((char *)(instance) - OFFSETOF(type, field)))
#define LEN(array) (sizeof(array) / sizeof(array[0]))
#define STR_LEN(str) (LENGTH(str) - 1)
// text
#define SLICE_TEXT(text) {.ptr=text, .len=STR_LEN(text)}
#define TEXT_PTR(text) (char(*)[STR_LEN(text)]) text
#define DEF_TEXT_PTR(name, text) char(*name)[STR_LEN(text)] = TEXT_PTR(text)
#define DEF_SLICE_TEXT(name, text) struct Slice name = SLICE_TEXT(text)
// types
// core
// memory
struct Slice {
char* ptr;
size_t len;
};
struct Finder {
struct Slice text;
|
|
|
|
|
>
>
|
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
|
// macroses
// c
#define or ||
#define and &&
#define OFFSETOF(type, field) ((size_t) &(((type *)0)->field))
#define FIELD_PARENT_PTR(type, field, instance) ((type *)((char *)(instance) - OFFSETOF(type, field)))
#define LEN(array) (sizeof(array) / sizeof(array[0]))
#define TEXT_LEN(str) (LEN(str) - 1)
// text
#define SLICE_TEXT(text) {.ptr=text, .len=TEXT_LEN(text)}
#define TEXT_PTR(text) (char(*)[TEXT_LEN(text)]) text
#define DEF_TEXT_PTR(name, text) char(*name)[TEXT_LEN(text)] = TEXT_PTR(text)
#define DEF_SLICE_TEXT(name, text) struct Slice name = SLICE_TEXT(text)
// types
// core
// memory
typedef size_t (*WriteFnPtr) (void* context, const char* bytes, size_t bytes_len);
struct Slice {
char* ptr;
size_t len;
};
struct Finder {
struct Slice text;
|
| ︙ | | | ︙ | |
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
|
DWORD output_mode;
#elif defined(__linux__)
struct termios termios;
#endif
};
// Os_Console_Input_Handle
#ifdef _WIN32
typedef void* Os_Console_Input_Handle;
#elif defined(__linux__)
typedef FILE* Os_Console_Input_Handle;
#endif
struct Os_Console_Input {
Os_Console_Input_Handle handle;
#ifdef _WIN32
#elif defined(__linux__)
int fileno;
#endif
};
typedef void* Os_Console_Output_Handle;
struct Os_Console_Output {
Os_Console_Output_Handle handle;
};
// Scalpi
// text
struct Scalpi_Text_Splitter {
struct Finder finder;
bool stop;
size_t last_pos;
};
// Scalpi
struct Scalpi_Writer {
void* context;
bool (*write) (void* context, size_t* writted, char* bytes, size_t bytes_len);
};
struct Scalpi_Logger {
// write to terminal, console, file or to all together, but no real check "bytes is writen"
struct Scalpi_Writer writers[4] // Scalpi_Logger_writer
};
struct Scalpi_Console {
void* input;
void* output;
};
// App
struct App_Os_Helper {
#ifdef _WIN32
// WinApi
HINSTANCE instance;
|
|
|
|
|
|
>
>
|
>
>
>
>
>
>
>
>
|
|
|
|
|
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
|
DWORD output_mode;
#elif defined(__linux__)
struct termios termios;
#endif
};
// Os_Console_Input_Handle
#ifdef _WIN32
typedef void* Os_Console_Input_Handle;
#elif defined(__linux__)
typedef FILE* Os_Console_Input_Handle;
#endif
struct Os_Console_Input {
Os_Console_Input_Handle handle;
#ifdef _WIN32
#elif defined(__linux__)
int fileno;
#endif
};
// Os_Console_Output_Handle
#ifdef _WIN32
typedef void* Os_Console_Output_Handle;
#elif defined(__linux__)
typedef FILE* Os_Console_Output_Handle;
#endif
struct Os_Console_Output {
Os_Console_Output_Handle handle;
#ifdef _WIN32
#elif defined(__linux__)
int fileno;
#endif
};
// Scalpi
// text
struct Scalpi_Text_Splitter {
struct Finder finder;
bool stop;
size_t last_pos;
};
// Scalpi
struct Scalpi_Writer {
void* context;
WriteFnPtr write;
};
struct Scalpi_Logger {
// write to terminal, console, file or to all together, but no real check "bytes is writen"
struct Scalpi_Writer writers[4] // Scalpi_Logger_writers
};
struct Scalpi_Console {
struct Os_Console_Input input;
struct Os_Console_Output output;
};
// App
struct App_Os_Helper {
#ifdef _WIN32
// WinApi
HINSTANCE instance;
|
| ︙ | | | ︙ | |
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// function prototypes
// globals
struct App global_app;
// functions
// functions
// core
// mem
// bits operations
uint8_t bits_rotl8(uint8_t value, unsigned int shift) {
shift %= 8;
return (value << shift) | (value >> (8 - shift));
}
|
<
|
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// function prototypes
// globals
struct App global_app;
// functions
// core
// mem
// bits operations
uint8_t bits_rotl8(uint8_t value, unsigned int shift) {
shift %= 8;
return (value << shift) | (value >> (8 - shift));
}
|
| ︙ | | | ︙ | |
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
|
uint64_t bits_rotl64(uint64_t value, unsigned int shift) {
shift %= 64;
return (value << shift) | (value >> (64 - shift));
}
// bytes operations
void Scalpi_mem_set(char* dest, char symbol, size_t len) {
for (size_t pos = 0; pos < len; pos++) {
dest[pos] = symbol;
}
}
void Scalpi_mem_copy(char* dest, char* src, size_t size) {
// i use this instead memcpy becouse clang-in-zig just replace memcpy to crash-zig-bloated-version code
for(size_t pos = 0; pos < size; pos++) {
dest[pos] = src[pos];
}
}
bool Scalpi_mem_isEql(char* a, char* b, size_t len) {
for(size_t pos = 0; pos < len; pos ++) {
char ac = a[pos];
char bc = b[pos];
if (ac != bc) return false;
}
return true;
}
void writeWithOffset(char* dest, size_t* writed, char* data, size_t data_len) {
Scalpi_mem_copy(&dest[*writed], data, data_len);
*writed += data_len;
}
// format_number
// copy non aligned data
void memcpy2(char* dest, char* src) {
dest[0] = src[0];
|
|
|
>
|
>
|
|
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
|
uint64_t bits_rotl64(uint64_t value, unsigned int shift) {
shift %= 64;
return (value << shift) | (value >> (64 - shift));
}
// bytes operations
size_t Scalpi_mem_fill(char* dest, char symbol, size_t len) {
for (size_t pos = 0; pos < len; pos++) {
dest[pos] = symbol;
}
}
size_t Scalpi_mem_write(char* dest, char* src, size_t size) {
// i use this instead memcpy becouse clang-in-zig just replace memcpy to crash-zig-bloated-version code
size_t pos;
for(pos = 0; pos < size; pos++) {
dest[pos] = src[pos];
}
return pos;
}
bool Scalpi_mem_isEql(char* a, char* b, size_t len) {
for(size_t pos = 0; pos < len; pos ++) {
char ac = a[pos];
char bc = b[pos];
if (ac != bc) return false;
}
return true;
}
void writeWithOffset(char* dest, size_t* writed, char* data, size_t data_len) {
Scalpi_mem_write(&dest[*writed], data, data_len);
*writed += data_len;
}
// format_number
// copy non aligned data
void memcpy2(char* dest, char* src) {
dest[0] = src[0];
|
| ︙ | | | ︙ | |
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
|
void Os_sleep(uint64_t ms) {
#ifdef _WIN32
Sleep(ms);
#endif
// TODO ME FOR LINUX
}
void os_print(uint8_t* bytes, uint64_t bytes_len) {
uint8_t* byte_ptr = bytes;
while (bytes_len > 0) {
putchar(*byte_ptr);
bytes_len -= 1;
byte_ptr += 1;
}
}
// void Os_Console_Output_init(&global_app.console.output, c.GetStdHandle(c.STD_OUTPUT_HANDLE));
// Scalpi lib
// Scalpi_logger
void Scalpi_logger_init (struct Scalpi_Logger* t) {
for (int i = 0; i < LEN(t->writers); i++) {
struct Scalpi_Writer* w = &t->writers[i];
w->context = 0;
w->write = 0;
}
}
void Scalpi_logger_tryWrite(struct Scalpi_Logger* t, char bytes[], uint64_t bytes_len) {
for (int i = 0; i < LEN(t->writers); i++) {
struct Scalpi_Writer* w = &t->writers[i];
if (w->write) {
size_t writed = 0;
w->write(w, &writed, bytes, bytes_len);
}
}
}
// App
// struct App_Os_Helper
bool App_Os_Helper_init(struct App_Os_Helper* os_helper) {
|
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
|
>
|
>
>
>
>
>
>
|
>
>
>
|
|
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
<
|
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
|
void Os_sleep(uint64_t ms) {
#ifdef _WIN32
Sleep(ms);
#endif
// TODO ME FOR LINUX
}
void Os_Console_Output_init(struct Os_Console_Output* output, Os_Console_Output_Handle handle) {
output->handle = handle;
#ifdef _WIN32
// output->handle = GetStdHandle(c.STD_OUTPUT_HANDLE);
#elif defined(__linux__)
// output->handle = stdout
output->fileno = fileno(output->handle);
#endif
}
void Os_Console_Input_init(struct Os_Console_Input* input, Os_Console_Input_Handle handle) {
input->handle = handle;
#ifdef _WIN32
// output->handle = GetStdHandle(c.STD_INPUT_HANDLE);
#elif defined(__linux__)
// output->handle = stdout
input->fileno = fileno(input->handle);
#endif
}
size_t Os_Console_Output_write(struct Os_Console_Output* output, const char* text, size_t len) {
#ifdef _WIN32
size_t writed = 0;
WriteConsoleA(output->handle, text, len, &writed, 0);
#elif defined(__linux__)
//write(output->fileno, text, len);
fwrite(text, sizeof(char), len, output->handle);
#endif
return len;
}
// Scalpi lib
// C_Text
size_t C_Text_getLen(const char text[]) {
size_t len = 0;
while(text[len] != 0) len++;
return len;
}
size_t C_Text_write(char buffer[], char data[]) {
const size_t len = C_Text_getLen(data);
return Scalpi_mem_write(buffer, data, len);
}
size_t C_Text_writeToWriteFn(WriteFnPtr write, void* context, const char text[]) {
const size_t len = C_Text_getLen(text);
return write(context, text, len);
}
// Scalpi_Text_writeU64AsText
#define Scalpi_Text_CharsDecFromU64_MAX_LEN TEXT_LEN("18446744073709551615")
size_t Scalpi_Text_writeU64AsText(char buffer[], uint64_t number, size_t min_width) { // utoa
size_t writed = 0;
// write number to tmp
char buffer_tmp[Scalpi_Text_CharsDecFromU64_MAX_LEN];
uintptr_t tmp_ptr = (uintptr_t) &buffer_tmp[0] + Scalpi_Text_CharsDecFromU64_MAX_LEN;
for (uint64_t num = number; num > 0; num = num / 10) {
uint8_t remainder = num % 10;
tmp_ptr--;
*(char*)tmp_ptr = '0' + (char) remainder;
}
const uintptr_t writed_tmp = (uintptr_t) &buffer_tmp[0] + Scalpi_Text_CharsDecFromU64_MAX_LEN - (uintptr_t) tmp_ptr;
// fill zeroes to min_width
size_t extra_fill = 0;
if (min_width > writed_tmp) {
extra_fill = min_width - writed_tmp;
writed += Scalpi_mem_fill(&buffer[0], '0', extra_fill);
}
if (writed_tmp > 0) writed += Scalpi_mem_write(&buffer[writed], (char*)tmp_ptr, writed_tmp);
return writed;
}
size_t Scalpi_Text_writeU64AsTextToWriteFn(WriteFnPtr write, void* context, uint64_t number, size_t min_width) {
char buffer_tmp[Scalpi_Text_CharsDecFromU64_MAX_LEN];
const size_t writed = Scalpi_Text_writeU64AsText(buffer_tmp, number, min_width);
return write(context, buffer_tmp, writed);
}
// Scalpi_Logger
void Scalpi_Logger_init (struct Scalpi_Logger* t) {
for (int i = 0; i < LEN(t->writers); i++) {
struct Scalpi_Writer* w = &t->writers[i];
w->context = 0;
w->write = 0;
}
}
size_t Scalpi_Logger_write(struct Scalpi_Logger* t, const char bytes[], size_t len) {
for (size_t i = 0; i < LEN(t->writers); i++) {
struct Scalpi_Writer* w = &t->writers[i];
if (w->write) {
size_t writed = w->write(w->context, bytes, len);
}
}
}
// App
// struct App_Os_Helper
bool App_Os_Helper_init(struct App_Os_Helper* os_helper) {
|
| ︙ | | | ︙ | |
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
DispatchMessageA(&msg);
#endif
// TODO ME FOR LINUX
}
// struct App
bool App_init(struct App* t) {
Scalpi_logger_init(&t->logger); // preinit console output for debug
if (App_Os_Helper_init(&t->os_helper)) {
t->tick = os_getTick();
return true;
// fallback
// App_Os_Helper_deinit(&t->os_helper);
|
|
|
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
|
DispatchMessageA(&msg);
#endif
// TODO ME FOR LINUX
}
// struct App
bool App_init(struct App* t) {
Scalpi_Logger_init(&t->logger); // preinit console output for debug
if (App_Os_Helper_init(&t->os_helper)) {
t->tick = os_getTick();
return true;
// fallback
// App_Os_Helper_deinit(&t->os_helper);
|
| ︙ | | | ︙ | |