46#define KSTR(c_lit) kstr_new(c_lit, sizeof(c_lit) - 1)
47#define KSTR_NULL kstr_new(NULL, 0)
52#define Kstr_Fmt "%.*s"
56#define Kstr_Arg(kstr) (int) (kstr.len), (kstr.data)
65#define ONEGB_DEC_INT 1073741824
71#define GULP_MAX_FILE_SIZE ONEGB_DEC_INT
90#define Gulp_Res_Fmt "%s"
94#define Gulp_Res_Arg(gr) (string_from_Gulp_Res((gr)))
106#define KLS_GULP_FILE(kls, filepath) try_kls_gulp_file((kls),(filepath), GULP_MAX_FILE_SIZE)
110#define KLS_GULP_FILE_KSTR(kls, filepath) try_kls_gulp_file_to_kstr((kls),(filepath), GULP_MAX_FILE_SIZE, false)
114#ifdef KLS_GULP_IMPLEMENTATION
139 assert(g >= 0 && g <
TOT_GULP_RES &&
"Unexpected Gulp_Res value");
166 return kstr_new(c_lit, strlen(c_lit));
178 if (left.
len != right.
len) {
182 for (
size_t i=0; i < left.
len; i++) {
183 if (left.
data[i] != right.
data[i])
return false;
197 if (left.
len != right.
len) {
202 for (
size_t i=0; i < left.
len; i++) {
203 l =
'A' <= left.
data[i] &&
'Z' >= left.
data[i]
206 r =
'A' <= right.
data[i] &&
'Z' >= right.
data[i]
209 if (l != r)
return false;
260 while ( i < kstr.
len && isspace(kstr.
data[i])) {
275 while ( i < kstr.
len && isspace(kstr.
data[kstr.
len - i - 1])) {
309 if (k.
data[i] == c) {
331 while (i < k->len && k->
data[i] != delim) {
359 while (i < k->len && k->
data[i] != delim) {
385 while (i + delim.
len < k->
len &&
395 if (i + delim.
len == k->
len) {
406static char * kls_read_file(Koliseo* kls,
const char * f_name,
Gulp_Res * err,
size_t * f_size, ...)
414 FILE * f = fopen(f_name,
"rb");
418 fseek(f, 0, SEEK_END);
420 fseek(f, 0, SEEK_SET);
423 va_start(args, f_size);
424 size_t max_size = va_arg(args,
size_t);
425 if (length > max_size) {
432 buffer = KLS_PUSH_ARR_NAMED(kls,
char,length + 1,
"char*",
"Buffer for file gulp");
434 if (buffer == NULL) {
435 assert(0 &&
"KLS_PUSH_NAMED() failed\n");
439 read_length = fread(buffer, 1, length, f);
441 if (length != read_length) {
450 buffer[length] =
'\0';
458 if (strlen(buffer) == length) {
477 static_assert(
TOT_GULP_RES == 6,
"Number of Gulp_Res changed");
480 data = kls_read_file(kls, filepath, err, &f_size, max_size);
492 fprintf(stderr,
"[ERROR] %s(): Unexpected error {%i}.\n",__func__, *err);
498 assert(strlen(data) == f_size &&
"data len should be equal to f_size here!");
500 assert(0 &&
"kls_read_file() failed\n");
523 fprintf(stderr,
"%s(): kls_gulp_file_sized() failed with err {%s}.\n",__func__,
string_from_Gulp_Res(err));
529static Kstr * kls_read_file_to_kstr(Koliseo* kls,
const char * f_name,
Gulp_Res * err,
size_t * f_size, ...)
535 char * buffer = NULL;
537 FILE * f = fopen(f_name,
"rb");
539 bool allow_nullchar =
false;
542 fseek(f, 0, SEEK_END);
544 fseek(f, 0, SEEK_SET);
547 va_start(args, f_size);
548 size_t max_size = va_arg(args,
size_t);
549 if (length > max_size) {
554 bool allow_nulls = va_arg(args,
int);
555 allow_nullchar = allow_nulls;
558 buffer = KLS_PUSH_ARR_NAMED(kls,
char,length + 1,
"char*",
"Buffer for file gulp");
560 if (buffer == NULL) {
561 assert(0 &&
"KLS_PUSH_NAMED() failed\n");
565 read_length = fread(buffer, 1, length, f);
567 if (length != read_length) {
576 buffer[length] =
'\0';
584 if (strlen(buffer) == length) {
587 if (!allow_nullchar) {
591 Kstr * res = KLS_PUSH_NAMED(kls,
Kstr,
"Kstr",
"Kstr for file gulp");
593 assert(0 &&
"KLS_PUSH_NAMED() failed\n");
599 res->
len = strlen(buffer);
617 static_assert(
TOT_GULP_RES == 6,
"Number of Gulp_Res changed");
620 data = kls_read_file_to_kstr(kls, filepath, err, &f_size, max_size, allow_nullchar);
632 fprintf(stderr,
"[ERROR] %s(): Unexpected error {%i}.\n",__func__, *err);
638 assert(data->
len == f_size &&
"data len should be equal to f_size here!");
640 assert(0 &&
"kls_read_file_to_kstr() failed\n");
665 fprintf(stderr,
"%s(): kls_gulp_file_sized_to_kstr() failed with err {%s}.\n",__func__,
string_from_Gulp_Res(err));
const char * string_from_Gulp_Res(Gulp_Res g)
Return a constant string for the passed Gulp_Res.
Definition koliseo.c:3285
Kstr * kls_gulp_file_sized_to_kstr(Koliseo *kls, const char *filepath, Gulp_Res *err, size_t max_size, bool allow_nullchar)
Tries mapping the passed file on the Koliseo.
Definition koliseo.c:3763
bool kstr_try_token(Kstr *k, char delim, Kstr *part)
Scans the first passed Kstr and if the passed char is present, the old Kstr is set to second pointer ...
Definition koliseo.c:3476
Kstr kstr_trim_left(Kstr kstr)
Returns a new Kstr after removing heading spaces from the passed one.
Definition koliseo.c:3405
bool kstr_eq_ignorecase(Kstr left, Kstr right)
Checks if the two passed Kstr have equal data, ignoring case.
Definition koliseo.c:3343
char * kls_gulp_file_sized(Koliseo *kls, const char *filepath, Gulp_Res *err, size_t max_size)
Tries mapping the passed file on the Koliseo.
Definition koliseo.c:3623
Kstr kstr_trim_right(Kstr kstr)
Returns a new Kstr after removing trailing spaces from the passed one.
Definition koliseo.c:3420
Kstr * try_kls_gulp_file_to_kstr(Koliseo *kls, const char *filepath, size_t max_size, bool allow_nullchar)
Tries mapping the passed file on the Koliseo.
Definition koliseo.c:3805
char * try_kls_gulp_file(Koliseo *kls, const char *filepath, size_t max_size)
Tries mapping the passed file on the Koliseo.
Definition koliseo.c:3664
Kstr kstr_cut_r(Kstr *k, size_t n)
Cuts the passed Kstr by up to n chars, from the right.
Definition koliseo.c:3388
Kstr kstr_from_c_lit(const char *c_lit)
Returns a new Kstr from the passed null-terminated string.
Definition koliseo.c:3312
Kstr kstr_cut_l(Kstr *k, size_t n)
Cuts the passed Kstr by up to n chars, from the left.
Definition koliseo.c:3369
Kstr kstr_new(const char *str, size_t len)
Returns a new Kstr with the passed args set.
Definition koliseo.c:3298
#define Gulp_Res_Fmt
Format macro for a Gulp_Res.
Definition kls_gulp.h:90
#define Gulp_Res_Arg(gr)
Format matching macro for a Gulp_Res.
Definition kls_gulp.h:94
Kstr kstr_token(Kstr *k, char delim)
Scans the passed Kstr and cuts it up to the first occurrence of passed char, even if it is not presen...
Definition koliseo.c:3504
Kstr kstr_trim(Kstr kstr)
Returns a new Kstr after removing heading and trailing spaces from the passed one.
Definition koliseo.c:3437
Kstr kstr_token_kstr(Kstr *k, Kstr delim)
Definition koliseo.c:3524
Gulp_Res
Defines possible results for kls_gulp_file_sized().
Definition kls_gulp.h:77
@ TOT_GULP_RES
Definition kls_gulp.h:84
@ GULP_FILE_KLS_NULL
Definition kls_gulp.h:83
@ GULP_FILE_CONTAINS_NULLCHAR
Definition kls_gulp.h:82
@ GULP_FILE_NOT_EXIST
Definition kls_gulp.h:79
@ GULP_FILE_TOO_LARGE
Definition kls_gulp.h:80
@ GULP_FILE_OK
Definition kls_gulp.h:78
@ GULP_FILE_READ_ERROR
Definition kls_gulp.h:81
bool kstr_indexof(Kstr k, char c, int *idx)
Checks if passed Kstr contains the passed char, and if so, sets the value pointed by idx to the first...
Definition koliseo.c:3450
const char * gulp_res_names[TOT_GULP_RES+1]
String array for representations of Gulp_Res.
Definition koliseo.c:3268
bool kstr_eq(Kstr left, Kstr right)
Checks if the two passed Kstr have exactly equal data.
Definition koliseo.c:3324
size_t len
Definition kls_gulp.h:29
const char * data
Definition kls_gulp.h:28