aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-15 21:54:37 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-15 21:54:41 +0100
commit7c61575432904666ce169b3f01158d69d1693d74 (patch)
treeabb7239d10780d1889d6b111f948f8aefae899f1
parentAdd character escaping. (diff)
downloadcalp-7c61575432904666ce169b3f01158d69d1693d74.tar.gz
calp-7c61575432904666ce169b3f01158d69d1693d74.tar.xz
Move fold function.
-rw-r--r--parse.c87
-rw-r--r--parse.h22
2 files changed, 56 insertions, 53 deletions
diff --git a/parse.c b/parse.c
index b1a05221..98d847a1 100644
--- a/parse.c
+++ b/parse.c
@@ -21,59 +21,6 @@
#undef V
/*
- * Input
- * f: file to get characters from
- * ctx: current parse context
- * c: last read character
- * output:
- * 0: line folded
- * 1: line ended
- *
- * A carrige return means that the current line is at an
- * end. The following character should always be \n.
- * However, if the first character on the next line is a
- * whitespace then the two lines should be concatenated.
- *
- * NOTE
- * The above is true according to the standard. But I have
- * found files with only NL. The code below ends line on the
- * first of NL or CR, and then ensures that the program thinks
- * it got the expected CRNL.
- */
-int fold(FILE* f, parse_ctx* ctx, char c) {
- int retval;
-
- char buf[2] = {
- (c == '\n' ? '\n' : fgetc(f)),
- fgetc(f)
- };
-
- ctx->pcolumn = 1;
-
- if (buf[0] != '\n') {
- ERR_P(ctx, "expected newline after CR");
- retval = -1;
-
- } else if (buf[1] == ' ' || buf[1] == '\t') {
- retval = 0;
- ctx->pcolumn++;
-
- } else if (ungetc(buf[1], f) != buf[1]) {
- ERR_P(ctx, "Failed to put character back on FILE");
- retval = -2;
-
- } else {
- retval = 1;
- ++ctx->line;
- ctx->column = 0;
- }
-
- ++ctx->pline;
-
- return retval;
-}
-
-/*
* name *(";" param) ":" value CRLF
*/
int parse_file(char* filename, FILE* f, vcomponent* root) {
@@ -290,6 +237,40 @@ int handle_kv (
return 0;
}
+int fold(FILE* f, parse_ctx* ctx, char c) {
+ int retval;
+
+ char buf[2] = {
+ (c == '\n' ? '\n' : fgetc(f)),
+ fgetc(f)
+ };
+
+ ctx->pcolumn = 1;
+
+ if (buf[0] != '\n') {
+ ERR_P(ctx, "expected newline after CR");
+ retval = -1;
+
+ } else if (buf[1] == ' ' || buf[1] == '\t') {
+ retval = 0;
+ ctx->pcolumn++;
+
+ } else if (ungetc(buf[1], f) != buf[1]) {
+ ERR_P(ctx, "Failed to put character back on FILE");
+ retval = -2;
+
+ } else {
+ retval = 1;
+ ++ctx->line;
+ ctx->column = 0;
+ }
+
+ ++ctx->pline;
+
+ return retval;
+}
+
+
INIT_F(parse_ctx, char* filename) {
INIT(LLIST(strbuf), &this->key_stack);
INIT(LLIST(vcomponent), &this->comp_stack);
diff --git a/parse.h b/parse.h
index ae48a351..b69bcfa5 100644
--- a/parse.h
+++ b/parse.h
@@ -52,4 +52,26 @@ int handle_kv(
int parse_file(char* filename, FILE* f, vcomponent* cal);
+/*
+ * Input
+ * f: file to get characters from
+ * ctx: current parse context
+ * c: last read character
+ * output:
+ * 0: line folded
+ * 1: line ended
+ *
+ * A carrige return means that the current line is at an
+ * end. The following character should always be \n.
+ * However, if the first character on the next line is a
+ * whitespace then the two lines should be concatenated.
+ *
+ * NOTE
+ * The above is true according to the standard. But I have
+ * found files with only NL. The code below ends line on the
+ * first of NL or CR, and then ensures that the program thinks
+ * it got the expected CRNL.
+ */
+int fold(FILE* f, parse_ctx* ctx, char c);
+
#endif /* PARSE_H */