diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-13 12:14:12 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-13 12:14:12 -0500 |
| commit | 44792fd5369cfe2ad1478b72bf22b0d1829f291b (patch) | |
| tree | ee991768c1632b50eaaba6e6b4f873eae420871a | |
| parent | 2e442c894d50c42aea748d21a6f08add123e9a70 (diff) | |
| download | balls-44792fd5369cfe2ad1478b72bf22b0d1829f291b.zip | |
move readFile() out of balls.c
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | balls.c | 24 | ||||
| -rw-r--r-- | balls.h | 2 | ||||
| -rw-r--r-- | io.c | 32 |
4 files changed, 38 insertions, 22 deletions
@@ -2,7 +2,7 @@ CC = gcc CFLAGS = -std=c99 -Wall -pedantic -Wno-deprecated-declarations LDFLAGS = -lGLEW -lGL -lX11 -lGLU -lOpenGL -lOpenCL -lglut -lGLX -SRC = balls.c sysfatal.c geo.c rand.c partition.c +SRC = balls.c sysfatal.c geo.c rand.c partition.c io.c OBJ = ${SRC:.c=.o} balls: ${OBJ} @@ -85,7 +85,6 @@ void keyboard(unsigned char key, int x, int y); void freeCL(void); void freeGL(void); void initShaders(void); -char *readFile(const char *filename, size_t *size); void compileShader(GLint shader); void frameCount(void); void drawString(const char *str); @@ -210,7 +209,9 @@ initCL(void) { free(platforms); /* Create program from file. */ - progBuf = readFile(PROG_FILE, &progSize); + err = readFile(PROG_FILE, &progBuf, &progSize); + if (err != 0) + sysfatal("Failed to read %s\n", PROG_FILE); cpuProg = clCreateProgramWithSource(cpuContext, 1, (const char **) &progBuf, &progSize, &err); if (err < 0) sysfatal("Failed to create CPU program.\n"); @@ -726,25 +727,6 @@ initShaders(void) { glUseProgram(prog); } -char * -readFile(const char *filename, size_t *size) { - FILE *f; - char *buf; - - if ((f = fopen(filename, "r")) == NULL) - sysfatal("Failed to open file '%s'\n", filename); - fseek(f, 0, SEEK_END); - *size = ftell(f); - if ((buf = malloc((*size + 1) * sizeof(char))) == NULL) { - fclose(f); - sysfatal("Failed to allocate file buffer for '%s'\n", filename); - } - rewind(f); - fread(buf, sizeof(char), *size, f); - buf[*size] = '\0'; - fclose(f); - return buf; -} void compileShader(GLint shader) { @@ -19,6 +19,8 @@ typedef struct { size_t size; /* Length of cell array. */ } Partition; +int readFile(const char *filename, char **contents, size_t *size); + Partition partitionCollisions(size_t nBalls); void freePartition(Partition part); void printPartition(Partition part); @@ -0,0 +1,32 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "balls.h" +#include "sysfatal.h" + +/* + * Read the file called filename. Sets contents to a malloc-allocated string containing + * the contents of the file and a terminal '\0'. Sets size to strlen(contents) (excludes + * '\0'). Returns non-zero on error. + */ +int +readFile(const char *filename, char **contents, size_t *size) { + FILE *f; + + if ((f = fopen(filename, "r")) == NULL) { + fprintf(stderr, "Failed to open file '%s'\n", filename); + return 1; + } + fseek(f, 0, SEEK_END); + *size = ftell(f); + if ((*contents = malloc((*size + 1) * sizeof(char))) == NULL) { + fclose(f); + fprintf(stderr, "Failed to allocate file buffer for '%s'\n", filename); + return 1; + } + rewind(f); + fread(*contents, sizeof(char), *size, f); + (*contents)[*size] = '\0'; + fclose(f); + return 0; +} |