From 050d32ae204b64f7098b12c77cdb92a33ebc7562 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 29 May 2020 01:23:04 +0200 Subject: blogc: added filelist parser --- src/blogc/filelist-parser.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ src/blogc/filelist-parser.h | 16 +++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/blogc/filelist-parser.c create mode 100644 src/blogc/filelist-parser.h (limited to 'src') diff --git a/src/blogc/filelist-parser.c b/src/blogc/filelist-parser.c new file mode 100644 index 0000000..ac9c50a --- /dev/null +++ b/src/blogc/filelist-parser.c @@ -0,0 +1,69 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2019 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include "../common/utils.h" + + +typedef enum { + LINE_START = 1, + LINE, +} blogc_filelist_parser_state_t; + + +bc_slist_t* +blogc_filelist_parse(const char *src, size_t src_len) +{ + size_t current = 0; + size_t start = 0; + bc_slist_t *rv = NULL; + bc_string_t *line = bc_string_new(); + blogc_filelist_parser_state_t state = LINE_START; + + while (current < src_len) { + char c = src[current]; + bool is_last = current == src_len - 1; + + switch (state) { + + case LINE_START: + if (c == '#') { + while (current < src_len) { + if (src[current] == '\r' || src[current] == '\n') + break; + current++; + } + break; + } + if (c == ' ' || c == '\t' || c == '\r' || c == '\n') + break; + start = current; + state = LINE; + continue; + + case LINE: + if (c == '\r' || c == '\n' || is_last) { + if (is_last && c != '\r' && c != '\n') + bc_string_append_c(line, c); + rv = bc_slist_append(rv, bc_str_strip(line->str)); + bc_string_free(line, false); + line = bc_string_new(); + state = LINE_START; + break; + } + bc_string_append_c(line, c); + break; + + } + + current++; + } + + bc_string_free(line, true); + + return rv; +} diff --git a/src/blogc/filelist-parser.h b/src/blogc/filelist-parser.h new file mode 100644 index 0000000..5c65ee0 --- /dev/null +++ b/src/blogc/filelist-parser.h @@ -0,0 +1,16 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2019 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _FILELIST_PARSER_H +#define _FILELIST_PARSER_H + +#include "../common/utils.h" + +bc_slist_t* blogc_filelist_parse(const char *src, size_t src_len); + +#endif /* _FILELIST_PARSER_H */ -- cgit v1.2.3-18-g5258