File: index.c

package info (click to toggle)
abc2mtex 1.6.1-4
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 392 kB
  • ctags: 410
  • sloc: ansic: 4,052; makefile: 86; sh: 9
file content (269 lines) | stat: -rw-r--r-- 5,619 bytes parent folder | download | duplicates (3)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/************************************************************************
*									*
*			index.c - V1.6.1 (Jan 97)			*
*									*
*		by    Chris Walshaw (C.Walshaw@gre.ac.uk)		*
*									*
*	Copyright Chris Walshaw. Permission is granted to use and	*
*	copy provided that this copyright notice remains attached.	*
*	This code may not be sold.					*
*									*
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "index.h"

void	g_error(char *fmt, ...)
{
	va_list	ap;
	va_start(ap,fmt);
	(void) fprintf(stderr,"ERROR: ");
	(void) vfprintf(stderr,fmt,ap);
	(void) fprintf(stderr,"\n");
	va_end(ap);
	exit(1);
}

void	get_index(char *fmt, char *fname)
{
	FILE	*fp;
	int	colon;
	char	c;
	int	f = 0;

	if ((fp = fopen(fname,"r")) == NULL)
		g_error("file %s does not exist",fname);

	fmt[0] = '\0';
	while ((fgets(&fmt[strlen(fmt)],99,fp)) != NULL);

	/* now check for errors */
	while (fmt[f]) {
		if (strchr("FXJORCTMLK|",fmt[f])) {
			c = fmt[f];
			if (fmt[f++] == '|') {
				if (fmt[f] == '0') ++f;
				if (strchr("12",fmt[f])) ++f;
			}
			if (fmt[f] == ':')
				colon = 1;
			else if (fmt[f] == '<' || fmt[f] == '>')
				colon = 0;
			else
			  g_error("fmt file - %c not followed by [><:]",c);
			if (fmt[++f] < '0' || fmt[f] > '9')
			  g_error("fmt file - %c not followed by length",c);
			while (fmt[f] >= '0' && fmt[f] <= '9') ++f;
			if (colon && fmt[f] != '\n')
			  g_error("fmt file - %c: not at end of line",c);
		} else if (fmt[f] == '\\')
			f += 2;
		else
			f += 1;
	}
	if (fmt[f-1] != '\n')
		g_error("fmt file - not terminated by newline character");
}

void	size_record(char *fmt, int *size, char *field)
{
	int	s = 0;
	int	f = 0;
	while (fmt[f]) {
		if (strchr("FXJORCTMLK|",fmt[f])) {
			field[s] = fmt[f++];
			if (field[s] == '|' && fmt[f] == '0') ++f;
			if (field[s] == '|' && strchr("12",fmt[f])) ++f;
			size[s++] = atoi(&fmt[++f]);
			while (fmt[f] >= '0' && fmt[f] <= '9')
				f++;
		} else if (fmt[f] == '\\')
			f += 2;
		else
			f += 1;
	}
	field[s] = '\0';
	size[s] = 0;
}

Record	*alloc_record(char *fmt, int *size)
{
	Record	*entry;
	int	f;
	ALLOC(entry,Record,1);
	for (f = 0; fmt[f]; ++f) {
		if (strchr("FXJORCTMLK",fmt[f])) {
			ALLOC(entry->fields[fmt[f]-'A'],char,size[f]+1);
		} else if (fmt[f] == '|') {
			ALLOC(entry->BARS,char,size[f]+1);
		} else
			g_error("in size format");
	}
	return(entry);
}

void	free_record(Record *entry, char *fmt)
{
	int	f;

	for (f = 0; fmt[f]; ++f) {
		if (strchr("FXJORCTMLK",fmt[f]))
			free(entry->fields[fmt[f]-'A']);
		else if (fmt[f] == '|')
			free(entry->BARS);
	}
	free(entry);
}

static	void	str_get(char *string, char *fmt, int *f, char *index, int *i)
{
	int	c;
	int	n;
	int	l;
	char	temp[99];
	*f += 2;
	n = atoi(&fmt[*f]);
	if (fmt[*f-1] == '<') {
		(void) strncpy(string,&index[*i],n);
		string[n] = '\0';
		/* remove trailing whitespace */
		l = strlen(string);
		for (c = l; c > 0 && string[c-1] == ' '; c--);
		string[c] = '\0';
		*i += n;
	} else if (fmt[*f-1] == '>') {
		(void) strncpy(temp,&index[*i],n);
		temp[n] = '\0';
		/* remove leading whitespace */
		for (c = 0; temp[c] == ' ' && c < n; c++);
		(void) strcpy(string,&temp[c]);
		*i += n;
	} else {
		(void) strncpy(string,&index[*i],n);
		l = strlen(string);
		if (string[l-1] == '\n')
			string[--l] = '\0';
		*i += strlen(string);
	}
	while (fmt[*f] >= '0' && fmt[*f] <= '9')
		(*f)++;
}

int	get_record(char *fmt, FILE *In, Record *entry)
{
	int	new_line = 1;
	int	f = 0;
	int	i = 0;
	static	char	index[999];

	while (fmt[f]) {
		if (new_line == 1) {
			if ((fgets(index,999,In)) == NULL)
				return(0);
			new_line = 0;
		}
		if (strchr("FXJORCTMLK",fmt[f]))
			str_get(entry->fields[fmt[f]-'A'],fmt,&f,index,&i);
		else
			switch (fmt[f]) {
			case '|':
				if (fmt[f+1] == '0') ++f;
				if (strchr("12",fmt[f+1])) ++f;
				str_get(entry->BARS,fmt,&f,index,&i);
				break;
			case '\n':
				new_line = 1;
				f += 1;
				i = 0;
				break;
			case '\\':
				f += 1;
				/*FALLTHROUGH*/
			default:
				f += 1;
				i += 1;
				break;
			}
	}
	return(1);
}

static	void	str_put(char *string, char *fmt, int *f, char *index, int *i)
{
	int	c;
	int	n;
	int	l;
	*f += 2;
	n = atoi(&fmt[*f]);
	if (fmt[*f-1] == '<') {
		(void) strncpy(&index[*i],string,n);
/* might need to pad with whitespace */
		for (c = strlen(string); c < n; c++)
			index[*i+c] = ' ';
		*i += n;
		index[*i] = '\0';
	} else if (fmt[*f-1] == '>') {
/* might need to pad with whitespace */
		l = strlen(string);
		if (l < n) {
			for (c = 0; c < n-l; c++)
				index[*i+c] = ' ';
			index[*i+c] = '\0';
			(void) strcpy(&index[*i+c],string);
		} else
			(void) strncpy(&index[*i],string,n);
		*i += n;
		index[*i] = '\0';
	} else {
		(void) strcpy(&index[*i],string);
		*i += strlen(string);
	}
	while (fmt[*f] >= '0' && fmt[*f] <= '9')
		(*f)++;
}

int	put_record(char *fmt, FILE *Out, Record *entry)
{
	int	f = 0;
	int	i = 0;
	static	char	index[999];

	while (fmt[f]) {
		if (i == 0)
			index[0] = '\0';
		if (strchr("FXJORCTMLK",fmt[f]))
			str_put(entry->fields[fmt[f]-'A'],fmt,&f,index,&i);
		else
			switch(fmt[f]) {
			case '|':
				if (fmt[f+1] == '0') ++f;
				if (strchr("12",fmt[f+1])) ++f;
				str_put(entry->BARS,fmt,&f,index,&i);
				break;
			case '\\':
				f += 1;
				/*FALLTHROUGH*/
			default:
				(void) strncpy(&index[i++],&fmt[f],1);
				index[i] = '\0';
				if (fmt[f++] == '\n') {
					(void) fputs(index,Out);
					i = 0;
				}
				break;
			}
	}
	return(1);
}

char	lower(char c)
{
	if (c >= 'A' && c <= 'Z')
		return(c-'A'+'a');
	else
		return(c);
}