Merge branch 'main' of github.com:roodletoof/cig
This commit is contained in:
@@ -333,7 +333,7 @@ typedef struct string_builder {
|
|||||||
} string_builder_t;
|
} string_builder_t;
|
||||||
|
|
||||||
string_builder_t sb_create(allocator_t builder_allocator);
|
string_builder_t sb_create(allocator_t builder_allocator);
|
||||||
// assumes the provided string will live until sb_build is called.
|
// assumes the provided string will live until sb_build_and_clear is called.
|
||||||
void sb_add_string(string_builder_t *this, const char *string);
|
void sb_add_string(string_builder_t *this, const char *string);
|
||||||
void sb_add_i64(string_builder_t *this, int64_t i64);
|
void sb_add_i64(string_builder_t *this, int64_t i64);
|
||||||
void sb_add_i32(string_builder_t *this, int32_t i32);
|
void sb_add_i32(string_builder_t *this, int32_t i32);
|
||||||
@@ -344,8 +344,12 @@ void sb_add_u32(string_builder_t *this, uint32_t u32);
|
|||||||
void sb_add_u16(string_builder_t *this, uint16_t u16);
|
void sb_add_u16(string_builder_t *this, uint16_t u16);
|
||||||
void sb_add_u8(string_builder_t *this, uint8_t u8);
|
void sb_add_u8(string_builder_t *this, uint8_t u8);
|
||||||
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length);
|
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length);
|
||||||
const char *sb_build(string_builder_t *this, allocator_t output_allocator);
|
const char *sb_build_and_clear(string_builder_t *this, allocator_t output_allocator);
|
||||||
void sb_fprint(string_builder_t *this, FILE *dest);
|
void sb_fprint_and_clear(string_builder_t *this, FILE *dest);
|
||||||
|
|
||||||
|
// file_io /////////////////////////////////////////////////////////////////////
|
||||||
|
char *read_entire_file(const char *path, allocator_t allocator);
|
||||||
|
char **read_all_file_lines(const char *path, allocator_t allocator);
|
||||||
|
|
||||||
#ifdef CIG_IMPL
|
#ifdef CIG_IMPL
|
||||||
|
|
||||||
@@ -377,6 +381,7 @@ void allocator_reset(allocator_t this) {
|
|||||||
#include "cli.c"
|
#include "cli.c"
|
||||||
#include "scanner.c"
|
#include "scanner.c"
|
||||||
#include "string_builder.c"
|
#include "string_builder.c"
|
||||||
|
#include "file_io.c"
|
||||||
|
|
||||||
#endif // CIG_IMPL
|
#endif // CIG_IMPL
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include "cig.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
char *read_entire_file(const char *path, allocator_t allocator) {
|
||||||
|
FILE *fp = fopen(path, "rb");
|
||||||
|
assert(fp != NULL);
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
long size = ftell(fp);
|
||||||
|
rewind(fp);
|
||||||
|
char *buf = allocator_alloc(allocator, size+1);
|
||||||
|
assert(buf != NULL);
|
||||||
|
size_t read = fread(buf, 1, size, fp);
|
||||||
|
assert(read == (size_t)size);
|
||||||
|
fclose(fp);
|
||||||
|
buf[size] = '\0';
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns dynamic array, of fixed char strings.
|
||||||
|
char **read_all_file_lines(const char *path, allocator_t allocator) {
|
||||||
|
char *contents = read_entire_file(path, allocator);
|
||||||
|
char **lines = make_arr(allocator, char*);
|
||||||
|
arr_append(lines, contents);
|
||||||
|
bool just_split = false;
|
||||||
|
for (char *c = contents; (*c)!='\0'; c++) {
|
||||||
|
if ((*c) == '\n') {
|
||||||
|
*c = '\0';
|
||||||
|
just_split = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (just_split) {
|
||||||
|
just_split = false;
|
||||||
|
arr_append(lines, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
+4
-2
@@ -91,7 +91,7 @@ void sb_add_substring(string_builder_t *this, const char *string, size_t substri
|
|||||||
sb_add_string(this, buffer);
|
sb_add_string(this, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *sb_build(
|
const char *sb_build_and_clear(
|
||||||
string_builder_t *this,
|
string_builder_t *this,
|
||||||
allocator_t output_allocator
|
allocator_t output_allocator
|
||||||
) {
|
) {
|
||||||
@@ -123,7 +123,7 @@ const char *sb_build(
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sb_fprint(string_builder_t *this, FILE *dest) {
|
void sb_fprint_and_clear(string_builder_t *this, FILE *dest) {
|
||||||
for (
|
for (
|
||||||
string_builder_node_t *node = this->head;
|
string_builder_node_t *node = this->head;
|
||||||
node != NULL;
|
node != NULL;
|
||||||
@@ -131,4 +131,6 @@ void sb_fprint(string_builder_t *this, FILE *dest) {
|
|||||||
) {
|
) {
|
||||||
fprintf(dest, "%s", node->string);
|
fprintf(dest, "%s", node->string);
|
||||||
}
|
}
|
||||||
|
this->head = NULL;
|
||||||
|
this->tail = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user