From 12363f0ed6111943b0e71f0ada5b59db4734e1ad Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 10 Dec 2021 12:41:36 -0800 Subject: [PATCH] add input functions --- .gitignore | 3 ++ CMakeLists.txt | 2 ++ build.sh | 8 +++++ include/input.h | 13 ++++++++ src/input.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100755 build.sh create mode 100644 include/input.h create mode 100644 src/input.c diff --git a/.gitignore b/.gitignore index 374fe5c..1633e14 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ cmake* +.cache +build +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 85189d1..14eb77a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.17) project(flint C) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_C_STANDARD 99) include_directories(include) @@ -9,6 +10,7 @@ set(SOURCES src/set.c src/stack.c src/binarytree.c + src/input.c ) add_library(flint ${SOURCES}) diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..18423d2 --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh -e + +mkdir -p build +cd build +cmake .. +make +cp compile_commands.json .. +cd diff --git a/include/input.h b/include/input.h new file mode 100644 index 0000000..08fed0e --- /dev/null +++ b/include/input.h @@ -0,0 +1,13 @@ +#ifndef LIBFLINT_INPUT_H +#define LIBFLINT_INPUT_H + +#include + +char *get_input(const char *); +char **split(const char *, size_t *, const char *); +char **get_lines(const char *, size_t *); +int *get_ints(const char *, size_t *); +void del_split(char **); +void del_lines(char **); + +#endif // LIBFLINT_INPUT_H diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..ad439fe --- /dev/null +++ b/src/input.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +#ifdef __linux__ +#include +#endif + +#include "input.h" + +char *get_input(const char *path) { + FILE *fp = NULL; + fp = fopen(path, "r"); + if (fp == NULL) { + fprintf(stderr, "Failed to open %s. Returning NULL\n", path); + return NULL; + } + + fseek(fp, 0, SEEK_END); + size_t fsz = ftell(fp); + rewind(fp); + + char* buf = NULL; + buf = malloc(fsz + 1); + if (buf == NULL) { + fprintf(stderr, "Failed to malloc buf. Returning NULL\n"); + fclose(fp); + return NULL; + } + + fread(buf, 1, fsz, fp); + buf[fsz] = '\0'; + fclose(fp); + + return buf; +} + +char **split(const char *s, size_t *lsz, const char *delim) { + char **lines = NULL; + char *t = strtok(s, delim); + size_t n = 0; + + while (t != NULL) { + lines = realloc(lines, sizeof(char *) * ++n); + if (lines == NULL) { + fprintf(stderr, "Failed to realloc lines buffer. Returning NULL\n"); + free(s); + return NULL; + } + lines[n - 1] = t; + t = strtok(NULL, delim); + } + + *lsz = n; + return lines; +} + +char **get_lines(const char *path, size_t *lsz) { + return split(get_input(path), lsz, "\n"); +} + +int *get_ints(const char *path, size_t *sz) { + char **lines = get_lines(path, sz); + int *i = malloc(sizeof(int) * *sz); + + for (size_t idx = 0; idx < *sz; idx++) { + int n; + const char *errstr; + n = strtonum(lines[idx], INT_MIN, INT_MAX, &errstr); + if (errstr) { + printf("Failed to convert %s to int. Returning NULL\n", lines[idx]); + exit(1); + } + i[idx] = n; + } + + del_lines(lines); + return i; +} + +void del_split(char **sp) { + free(sp[0]); + free(sp); +} + +void del_lines(char **lines) { + del_split(lines); +}