aboutsummaryrefslogtreecommitdiff
path: root/src/parse.h
blob: ed235e887ff7396568a958d215986425655104ea (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef PARSE_H
#define PARSE_H

#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

/*
 * Transfers a strbuf from src to target. 
 * Does this first copying the contents, followed by capping the
 * target and reseting the src.
 */
#define TRANSFER(target, src) do { \
	DEEP_COPY(strbuf)((target), (src)); \
	strbuf_cap(target); \
	strbuf_soft_reset(src); \
} while (0)

/*
 * Current context for the character consumer (parse_file).
 */
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.
 */
typedef struct {
	/* Which file we are parsing, copied to all components to allow
	 * writebacks later */
	char* filename;

	FILE* f;

   /*
	 * context stacks used since ICS files form a tree. key_stack is
	 * only for sequrity purposes.
	 */
	// LLIST(strbuf) key_stack;
	// LLIST(vcomponent) comp_stack;
	SCM key_stack;
	SCM comp_stack;

	/* Number for unfolded lines
	 * TODO remove this
	 * */
	int line;
	int column;

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

	/*
	 * String which we write everything read into.
	 * Later copied to appropiate places.
	 */
	strbuf str;
} parse_ctx;

INIT_F(parse_ctx, FILE* f, char* filename);
FREE_F(parse_ctx);


/*
 * Character consumer. Reads characters from stdin until end of file.
 * Whenever it finds a token with a special value (such as ':', ';',
 * ...) it saves it away.
 * Once It has parsed a full line it calls handel_kv. Which build my
 * actuall datastructure.
 */
int parse_file(char* filename, FILE* f, vcomponent* cal);

/*
 * Called whenever parse_file finishes a line. Copies the contents of
 * ctx and the current content_line into the object stack, stored in
 * ctx. 
 */
int handle_kv(
		strbuf* key,
		strbuf* val,
		SCM* content_pair,
		parse_ctx*     ctx
		);

/*
 * 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(parse_ctx* ctx, char c);

int handle_escape (parse_ctx* ctx);

#endif /* PARSE_H */