aboutsummaryrefslogtreecommitdiff
path: root/parse.h
blob: 9d2ab2e56881e8596fab6f51c8935105a06c74ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef PARSE_H
#define PARSE_H

#include <string>

#include <stdio.h>
#include <stdlib.h>

// #include "strbuf.h"
#include "vcal.h"

// #define TYPE vcomponent
#include "linked_list.h"
// #undef TYPE

/*
 * The standard says that no line should be longer than 75 octets.
 * This sets the default amount of memory to allocate for each string,
 * but strings are reallocated when needed.
 */
#define SEGSIZE 75

typedef enum {
	p_key, p_value, p_param_name, p_param_value, p_escape
} part_context;

/*
 * Struct holding most state information needed while parsing.
 * Kept together for simplicity.
 */
struct parse_ctx {
	char* filename;
	llist<std::string> key_stack;
	llist<vcomponent> comp_stack;

	/* Number for unfolded lines */
	int line;
	int column;

	/* Actuall lines and columns from file */
	int pline;
	int pcolumn;

	std::string str;

	parse_ctx (const char* filename);

	~parse_ctx ();
};

int handle_kv(
		std::string* key,
		// content_line*  cline,
		parse_ctx*     ctx
		);

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 */