aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/error.c6
-rw-r--r--src/common/error.h2
-rw-r--r--src/common/file.c67
-rw-r--r--src/common/file.h19
4 files changed, 90 insertions, 4 deletions
diff --git a/src/common/error.c b/src/common/error.c
index 1ea6e0d..a6a5f24 100644
--- a/src/common/error.c
+++ b/src/common/error.c
@@ -112,6 +112,9 @@ bc_error_print(bc_error_t *err)
case BC_ERROR_CONFIG_PARSER:
fprintf(stderr, "error: config-parser: %s\n", err->msg);
break;
+ case BC_ERROR_FILE:
+ fprintf(stderr, "error: file: %s\n", err->msg);
+ break;
case BLOGC_ERROR_SOURCE_PARSER:
fprintf(stderr, "error: source: %s\n", err->msg);
break;
@@ -121,9 +124,6 @@ bc_error_print(bc_error_t *err)
case BLOGC_ERROR_LOADER:
fprintf(stderr, "error: loader: %s\n", err->msg);
break;
- case BLOGC_ERROR_FILE:
- fprintf(stderr, "error: file: %s\n", err->msg);
- break;
case BLOGC_WARNING_DATETIME_PARSER:
fprintf(stderr, "warning: datetime: %s\n", err->msg);
break;
diff --git a/src/common/error.h b/src/common/error.h
index 2569538..d65a3e7 100644
--- a/src/common/error.h
+++ b/src/common/error.h
@@ -16,12 +16,12 @@ typedef enum {
// errors for src/common
BC_ERROR_CONFIG_PARSER = 1,
+ BC_ERROR_FILE,
// errors for src/blogc
BLOGC_ERROR_SOURCE_PARSER = 100,
BLOGC_ERROR_TEMPLATE_PARSER,
BLOGC_ERROR_LOADER,
- BLOGC_ERROR_FILE,
BLOGC_WARNING_DATETIME_PARSER,
} bc_error_type_t;
diff --git a/src/common/file.c b/src/common/file.c
new file mode 100644
index 0000000..70a5631
--- /dev/null
+++ b/src/common/file.c
@@ -0,0 +1,67 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include "file.h"
+#include "error.h"
+#include "utf8.h"
+#include "utils.h"
+
+
+char*
+bc_file_get_contents(const char *path, size_t *len, bc_error_t **err)
+{
+ if (path == NULL || err == NULL || *err != NULL)
+ return NULL;
+
+ *len = 0;
+ FILE *fp = fopen(path, "r");
+
+ if (fp == NULL) {
+ int tmp_errno = errno;
+ *err = bc_error_new_printf(BC_ERROR_FILE,
+ "Failed to open file (%s): %s", path, strerror(tmp_errno));
+ return NULL;
+ }
+
+ bc_string_t *str = bc_string_new();
+ char buffer[BC_FILE_CHUNK_SIZE];
+ char *tmp;
+
+ while (!feof(fp)) {
+ size_t read_len = fread(buffer, sizeof(char), BC_FILE_CHUNK_SIZE, fp);
+
+ tmp = buffer;
+
+ if (str->len == 0 && read_len > 0) {
+ // skipping BOM before validation, for performance. should be safe
+ // enough
+ size_t skip = bc_utf8_skip_bom((uint8_t*) buffer, read_len);
+ read_len -= skip;
+ tmp += skip;
+ }
+
+ *len += read_len;
+ bc_string_append_len(str, tmp, read_len);
+ }
+ fclose(fp);
+
+ if (!bc_utf8_validate_str(str)) {
+ *err = bc_error_new_printf(BC_ERROR_FILE,
+ "File content is not valid UTF-8: %s", path);
+ bc_string_free(str, true);
+ return NULL;
+ }
+
+ return bc_string_free(str, false);
+}
diff --git a/src/common/file.h b/src/common/file.h
new file mode 100644
index 0000000..e095de7
--- /dev/null
+++ b/src/common/file.h
@@ -0,0 +1,19 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#ifndef _FILE_H
+#define _FILE_H
+
+#include <stddef.h>
+#include "error.h"
+
+#define BC_FILE_CHUNK_SIZE 1024
+
+char* bc_file_get_contents(const char *path, size_t *len, bc_error_t **err);
+
+#endif /* _FILE_H */