summaryrefslogtreecommitdiff
path: root/paragraph.c
blob: 9cba3dab6f851b6e2847cf4c1f6ede10dc9ae26e (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*  dctrl-tools - Debian control file inspection tools
    Copyright (C) 2003, 2004 Antti-Juhani Kaijanaho

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <gmp.h>
#include <string.h>
#include "msg.h"
#include "paragraph.h"
#include "strutil.h"

static
void parse_int(FSAF * fp, struct field_data * fd)
{
	const size_t len = fd->end - fd->start;
	struct fsaf_read_rv rd = fsaf_read(fp, fd->start, len);
	assert(rd.len == len);
	fd->int_valid = false;
	char * s = strndup(rd.b, len);
	if (s == 0) fatal_enomem(0);
	int r = mpz_set_str(fd->parsed, s, 10);
	free(s);
	fd->int_valid = (r == 0);
	if (!fd->int_valid) {
		message(L_INFORMATIONAL, _("parse of a numeric field failed"),
			0);
	}
}

void para_init(para_t * para, FSAF * fp, fieldtrie_t * trie)
{
	para->fp = fp;
	para->trie = trie;
	para->start = 0;
	para->end = 0;
	para->eof = false;
	for (size_t i = 0; i < MAX_FIELDS; i++) {
		mpz_init(para->fields[i].parsed);
	}
	para_parse_next(para);
}

void para_parse_next(para_t * para)
{
	debug_message("para_parse_next", 0);
	para->start = para->end;
	for (size_t i = 0; i < fieldtrie_count(para->trie); i++) {
		para->fields[i].start = 0;
		para->fields[i].end = 0;
		para->fields[i].int_valid = 0;
	}
	fsaf_invalidate(para->fp, para->start);
	register enum { START, FIELD_NAME, BODY, BODY_NEWLINE,
			BODY_SKIPBLANKS, END } state = START;
	register size_t pos = para->start;
	register FSAF * fp = para->fp;
	size_t field_start = 0;
	struct field_data * field_data = 0;
	while (state != END) {
#               ifndef TEST_NODEBUG
		static char * const stnm[] = { "START", "FIELD_NAME",
					       "BODY", "BODY_NEWLINE",
					       "BODY_SKIPBLANKS", "END" };
		if (do_msg(L_DEBUG)) fprintf(stderr, "State: %s\n", stnm[state]);
#               endif
		int c = fsaf_getc(fp, pos++);
		switch (state) {
		case START:
			switch (c) {
			case -1:
				para->eof = true;
				state = END;
				break;
			case '\n':
				para->start++;
				break;
			default:
				field_start = --pos;
				state = FIELD_NAME;
			}
			break;
		case FIELD_NAME:
			switch (c) {
			case -1:
				message(L_FATAL, _("unexpected end of file"), 0);
				fail();
			case ':': {
				size_t len = (pos-1) - field_start;
				struct fsaf_read_rv r = fsaf_read(fp, field_start, len);
				assert(r.len == len);
				struct field_attr attr =
					fieldtrie_lookup(para->trie, r.b, len);
				if (!attr.valid) {
					field_data = 0;
				} else {
					assert(attr.inx < MAX_FIELDS);
					field_data = &para->fields[attr.inx];
					field_data->start = pos;
				}
				state = BODY;
			}
				break;
			case '\n':
				message(L_FATAL, _("unexpected end of line"), 0);
				fail();
			}
			break;
		case BODY:
			switch (c) {
			case -1:
				message(L_FATAL, _("unexpected end of file"), 0);
				fail();
			case '\n':
				if (field_data != 0) {
					field_data->end = pos-1;
					while (field_data->start < field_data->end
					       && fsaf_getc(fp, field_data->start) == ' ') {
						++field_data->start;
					}
					parse_int(fp, field_data);
				}
				state = BODY_NEWLINE;
				break;
			}
			break;
		case BODY_NEWLINE:
			switch (c) {
			case -1:
				//para->eof = true;
				/* pass through */
			case '\n':
				state = END;
				break;
			case ' ': case '\t':
				state = BODY_SKIPBLANKS;
				break;
			default:
				field_start = --pos;
				state = FIELD_NAME;
			}
			break;
		case BODY_SKIPBLANKS:
			switch (c) {
			case -1:
				/* pass through */
			case '\n':
				state = END;
				break;
			case ' ': case '\t':
				break;
			default:
				state = BODY;
			}
			break;
		case END: assert(0);
		}
	}
	para->end = pos-1;
}