aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-09-09 03:33:14 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-09-09 03:33:14 +0200
commit1c8174674d522a2c55b4f143f7e0dac848e34281 (patch)
tree5cb1d9c51036fb7993bb1bd43ad99089fed77721 /src/blogc
parent9dd0fcbeaed19c362ed2d1071d2ea967572b67f4 (diff)
downloadblogc-1c8174674d522a2c55b4f143f7e0dac848e34281.tar.gz
blogc-1c8174674d522a2c55b4f143f7e0dac848e34281.tar.bz2
blogc-1c8174674d522a2c55b4f143f7e0dac848e34281.zip
blogc: common: moved "file" to common
Diffstat (limited to 'src/blogc')
-rw-r--r--src/blogc/file.c70
-rw-r--r--src/blogc/file.h19
-rw-r--r--src/blogc/loader.c6
3 files changed, 3 insertions, 92 deletions
diff --git a/src/blogc/file.c b/src/blogc/file.c
deleted file mode 100644
index 518c665..0000000
--- a/src/blogc/file.c
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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 "../common/error.h"
-#include "../common/utf8.h"
-#include "../common/utils.h"
-
-// this would belong to loader.c, but we need it in a separated file to be
-// able to mock it when unit testing the loader functions.
-
-
-char*
-blogc_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(BLOGC_ERROR_FILE,
- "Failed to open file (%s): %s", path, strerror(tmp_errno));
- return NULL;
- }
-
- bc_string_t *str = bc_string_new();
- char buffer[BLOGC_FILE_CHUNK_SIZE];
- char *tmp;
-
- while (!feof(fp)) {
- size_t read_len = fread(buffer, sizeof(char), BLOGC_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(BLOGC_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/blogc/file.h b/src/blogc/file.h
deleted file mode 100644
index 6a0e094..0000000
--- a/src/blogc/file.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * 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 "../common/error.h"
-
-#define BLOGC_FILE_CHUNK_SIZE 1024
-
-char* blogc_file_get_contents(const char *path, size_t *len, bc_error_t **err);
-
-#endif /* _FILE_H */
diff --git a/src/blogc/loader.c b/src/blogc/loader.c
index 754723b..b182e0b 100644
--- a/src/blogc/loader.c
+++ b/src/blogc/loader.c
@@ -12,11 +12,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "file.h"
#include "source-parser.h"
#include "template-parser.h"
#include "loader.h"
#include "../common/error.h"
+#include "../common/file.h"
#include "../common/utils.h"
@@ -63,7 +63,7 @@ blogc_template_parse_from_file(const char *f, bc_error_t **err)
if (err == NULL || *err != NULL)
return NULL;
size_t len;
- char *s = blogc_file_get_contents(f, &len, err);
+ char *s = bc_file_get_contents(f, &len, err);
if (s == NULL)
return NULL;
bc_slist_t *rv = blogc_template_parse(s, len, err);
@@ -78,7 +78,7 @@ blogc_source_parse_from_file(const char *f, bc_error_t **err)
if (err == NULL || *err != NULL)
return NULL;
size_t len;
- char *s = blogc_file_get_contents(f, &len, err);
+ char *s = bc_file_get_contents(f, &len, err);
if (s == NULL)
return NULL;
bc_trie_t *rv = blogc_source_parse(s, len, err);