From 1c8174674d522a2c55b4f143f7e0dac848e34281 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 9 Sep 2016 03:33:14 +0200 Subject: blogc: common: moved "file" to common --- src/blogc/file.c | 70 ------------------------------------------------------ src/blogc/file.h | 19 --------------- src/blogc/loader.c | 6 ++--- src/common/error.c | 6 ++--- src/common/error.h | 2 +- src/common/file.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/file.h | 19 +++++++++++++++ 7 files changed, 93 insertions(+), 96 deletions(-) delete mode 100644 src/blogc/file.c delete mode 100644 src/blogc/file.h create mode 100644 src/common/file.c create mode 100644 src/common/file.h (limited to 'src') 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 - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#include -#include -#include -#include -#include -#include -#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 - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifndef _FILE_H -#define _FILE_H - -#include -#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 #include #include -#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); 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 + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include +#include +#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 + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _FILE_H +#define _FILE_H + +#include +#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 */ -- cgit v1.2.3-18-g5258