summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2021-06-04 20:52:16 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2021-06-04 20:52:16 -0400
commitfb609a2d7857049ba1eda73d055add640c8ff91f (patch)
tree7b1fbf416391adbf669a7dcfbe5d196e539a3cd7 /config.c
parent496ed94812366c9e31b242b4e01c1d7b19f9b758 (diff)
Started work on the config file parser.
Diffstat (limited to 'config.c')
-rw-r--r--config.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/config.c b/config.c
new file mode 100644
index 0000000..d1bdb10
--- /dev/null
+++ b/config.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *read_file(const char *filename, long *size) {
+ /* Open the file. */
+ FILE *fp = fopen(filename, "r");
+ /* Size of the file, in bytes. */
+ long filesize = 0;
+
+ /* Return NULL, if we couldn't open the file. */
+ if (fp == NULL) {
+ fclose(fp);
+ return NULL;
+ }
+
+ /* Return NULL, if we couldn't seek to the end of the file. */
+ if (fseek(fp, 0L, SEEK_END)) {
+ fclose(fp);
+ return NULL;
+ }
+
+ /* Get the size of the file, in bytes. */
+ filesize = ftell(fp);
+
+ /* Return NULL, if the returned size is negative. */
+ if (*filesize < 0) {
+ fclose(fp);
+ return NULL;
+ }
+
+ /* Allocate enough space for the entire file, plus one. */
+ buf = malloc(filesize+1);
+
+ /* Return NULL, if the buffer wasn't allocated. */
+ if (buf == NULL) {
+ fclose(fp);
+ free(cfg);
+ return NULL;
+ }
+
+ /* Zero out the buffer. */
+ memset(buf, 0, filesize+1);
+
+ /* Seek back to the start of the file. */
+ rewind(fp);
+ /* Read the entire file contents into the buffer. */
+ fread(buf, sizeof(char), filesize, fp);
+ /* Close the file. */
+ fclose(fp);
+
+ /* Return the filesize, in bytes. */
+ *size = filesize;
+ /* Return the buffer. */
+ return buf;
+}
+
+config *parse_config(const char *filename) {
+ /* Size of the file, in bytes. */
+ long filesize = 0;
+ /* Config settings. */
+ config *cfg;
+ /* Buffer of the file contents. */
+ char *buf = read_file(filename, &filesize);
+
+ /* Return NULL, if the buffer wasn't allocated */
+ if (buf == NULL) {
+ return NULL;
+ }
+
+ cfg = malloc(sizeof(config));
+
+ /* Return NULL, if cfg wasn't allocated. */
+ if (cfg == NULL) {
+ free(buf);
+ return NULL;
+ }
+
+ for (; *buf != '\0'; buf++) {
+ }
+}