summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-13 12:14:12 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-13 12:14:12 -0500
commit44792fd5369cfe2ad1478b72bf22b0d1829f291b (patch)
treeee991768c1632b50eaaba6e6b4f873eae420871a
parent2e442c894d50c42aea748d21a6f08add123e9a70 (diff)
downloadballs-44792fd5369cfe2ad1478b72bf22b0d1829f291b.zip
move readFile() out of balls.c
-rw-r--r--Makefile2
-rw-r--r--balls.c24
-rw-r--r--balls.h2
-rw-r--r--io.c32
4 files changed, 38 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index c21376d..8a2bc97 100644
--- a/Makefile
+++ b/Makefile
@@ -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}
diff --git a/balls.c b/balls.c
index 67eb0b2..4faa3f6 100644
--- a/balls.c
+++ b/balls.c
@@ -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) {
diff --git a/balls.h b/balls.h
index 21d8bc1..f17243b 100644
--- a/balls.h
+++ b/balls.h
@@ -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);
diff --git a/io.c b/io.c
new file mode 100644
index 0000000..dbd12f4
--- /dev/null
+++ b/io.c
@@ -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;
+}