diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-09-02 16:10:59 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-09-02 16:14:43 +0100 |
commit | c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd (patch) | |
tree | b15cb3c1d407bee7b8dfdd330f46b8cb084328de | |
parent | f1891b0c2e85262a8f2dc0a0d13eea192fe00c26 (diff) | |
download | obf-c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd.tar.gz obf-c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd.tar.bz2 obf-c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd.zip |
New module for functions I no longer need to work on
lib.(h|c) basically has structures and functions I no longer want to
stare at in main.c
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | lib.c | 57 | ||||
-rw-r--r-- | lib.h | 23 | ||||
-rw-r--r-- | main.c | 131 |
4 files changed, 119 insertions, 94 deletions
@@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -Wextra -Wpedantic -ggdb -fsanitize=address -std=c11 LIBS= -OBJECTS=main.o +OBJECTS=lib.o main.o OUT=obf.out ARGS= @@ -0,0 +1,57 @@ +/* lib.c + * Created: 2023-09-02 + * Author: Aryadev Chavali + * Description: General functions used throughout + */ + +#include <string.h> + +#include "./lib.h" + +buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size) +{ + buffer_t *buf = malloc(sizeof(*buf) + str_size + 1); + buf->name = name; + buf->size = str_size; + memcpy(buf->data, str, str_size); + buf->data[str_size] = '\0'; + return buf; +} + +void print_error(const char *handle, size_t row, size_t column, + const char *reason) +{ + fprintf(stderr, "%s:%lu:%lu:%s\n", handle, row, column, reason); +} + +char *fread_all(FILE *fp) +{ + const size_t CHUNK_SIZE = 1024, MULT = 2; + struct + { + char *data; + size_t used, available; + } buffer = {calloc(CHUNK_SIZE, sizeof(*buffer.data)), 0, CHUNK_SIZE}; + + size_t acc = 0, bytes_read = 0; + while ((bytes_read = fread(buffer.data + acc, sizeof(*buffer.data), + CHUNK_SIZE, fp)) != 0) + { + buffer.used += bytes_read; + acc += bytes_read; + if (buffer.used + CHUNK_SIZE >= buffer.available) + { + buffer.available = MAX(buffer.available * MULT, buffer.used + CHUNK_SIZE); + buffer.data = realloc(buffer.data, buffer.available); + } + } + buffer.data = realloc(buffer.data, buffer.used + 1); + buffer.data[buffer.used] = '\0'; + return buffer.data; +} + +bool usable_character(char c) +{ + return c == '>' || c == '<' || c == '+' || c == '-' || c == '-' || c == '.' || + c == ',' || c == '[' || c == ']'; +} @@ -0,0 +1,23 @@ +#ifndef LIB_H +#define LIB_H + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#define MAX(a, b) (a > b ? a : b) + +bool usable_character(char c); +char *fread_all(FILE *fp); +void print_error(const char *handle, size_t row, size_t column, + const char *reason); + +typedef struct Buffer +{ + const char *name; + size_t size; + char data[]; +} buffer_t; + +buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size); +#endif @@ -10,6 +10,8 @@ #include <stdlib.h> #include <string.h> +#include "./lib.h" + #define MEMORY_DEFAULT 30000 typedef struct @@ -18,12 +20,6 @@ typedef struct uint8_t memory[MEMORY_DEFAULT]; } machine_t; -bool usable_character(char c) -{ - return c == '>' || c == '<' || c == '+' || c == '-' || c == '-' || c == '.' || - c == ',' || c == '[' || c == ']'; -} - typedef struct AST { size_t col, row; @@ -41,34 +37,48 @@ typedef struct AST int loop_ref; } node_t; -typedef struct Buffer -{ - const char *name; - size_t size; - char data[]; -} buffer_t; - -buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size) -{ - buffer_t *buf = malloc(sizeof(*buf) + str_size + 1); - buf->name = name; - buf->size = str_size; - memcpy(buf->data, str, str_size); - buf->data[str_size] = '\0'; - return buf; -} - -void print_error(const char *handle, size_t row, size_t column, - const char *reason) +char *ast_to_str(node_t *ast, size_t size) { - fprintf(stderr, "%s:%lu:%lu:%s\n", handle, row, column, reason); + char *out = calloc(size + 1, 1); + for (size_t i = 0; i < size; ++i) + { + switch (ast[i].type) + { + case NEXT: + out[i] = '>'; + break; + case PREV: + out[i] = '<'; + break; + case INC: + out[i] = '+'; + break; + case DEC: + out[i] = '-'; + break; + case OUT: + out[i] = '.'; + break; + case READ: + out[i] = ','; + break; + case LIN: + out[i] = '['; + break; + case LOUT: + out[i] = ']'; + break; + } + } + out[size] = '\0'; + return out; } struct PResult { node_t *nodes; size_t size; -} parse_input(buffer_t *buffer) +} parse_buffer(buffer_t *buffer) { node_t *nodes = NULL; size_t usable = 0, loops = 0; @@ -166,71 +176,6 @@ error: return (struct PResult){0}; } -char *ast_to_str(node_t *ast, size_t size) -{ - char *out = calloc(size + 1, 1); - for (size_t i = 0; i < size; ++i) - { - switch (ast[i].type) - { - case NEXT: - out[i] = '>'; - break; - case PREV: - out[i] = '<'; - break; - case INC: - out[i] = '+'; - break; - case DEC: - out[i] = '-'; - break; - case OUT: - out[i] = '.'; - break; - case READ: - out[i] = ','; - break; - case LIN: - out[i] = '['; - break; - case LOUT: - out[i] = ']'; - break; - } - } - out[size] = '\0'; - return out; -} - -#define MAX(a, b) (a > b ? a : b) - -char *fread_all(FILE *fp) -{ - const size_t CHUNK_SIZE = 1024, MULT = 2; - struct - { - char *data; - size_t used, available; - } buffer = {calloc(CHUNK_SIZE, sizeof(*buffer.data)), 0, CHUNK_SIZE}; - - size_t acc = 0, bytes_read = 0; - while ((bytes_read = fread(buffer.data + acc, sizeof(*buffer.data), - CHUNK_SIZE, fp)) != 0) - { - buffer.used += bytes_read; - acc += bytes_read; - if (buffer.used + CHUNK_SIZE >= buffer.available) - { - buffer.available = MAX(buffer.available * MULT, buffer.used + CHUNK_SIZE); - buffer.data = realloc(buffer.data, buffer.available); - } - } - buffer.data = realloc(buffer.data, buffer.used + 1); - buffer.data[buffer.used] = '\0'; - return buffer.data; -} - int main(int argc, char *argv[]) { if (argc == 1) @@ -260,7 +205,7 @@ int main(int argc, char *argv[]) fclose(handle); buffer = buffer_init_str(filepath, file_data, strlen(file_data)); - res = parse_input(buffer); + res = parse_buffer(buffer); if (res.nodes == NULL) { fputs("Exiting early...\n", stderr); |