summaryrefslogtreecommitdiff
path: root/db-4.8.30/examples_c/csv/code.c
blob: a6a3878fe38363bbda672d14f190544b25d75c65 (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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2005-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

#include "csv.h"

typedef struct {
	char		*name;		/* Field name */
	char		*upper;		/* Field name in upper-case */
	datatype	 type;		/* Data type */
	int		 indx;		/* Index */
} FIELD;

int	 code_source(void);
int	 code_header(void);
int	 desc_dump(void);
int	 desc_load(void);
char	*type_to_string(datatype);
int	 usage(void);

/*
 * Globals
 */
FILE	*cfp;				/* C source file */
FILE	*hfp;				/* C source file */
char	*progname;			/* Program name */
int	 verbose;			/* Verbose flag */

u_int	 field_cnt;			/* Count of fields */
FIELD	*fields;			/* Field list */

int
main(int argc, char *argv[])
{
	int ch;
	char *cfile, *hfile;

	/* Initialize globals. */
	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		++progname;

	/* Initialize arguments. */
	cfile = "csv_local.c";		/* Default header/source files */
	hfile = "csv_local.h";

	/* Process arguments. */
	while ((ch = getopt(argc, argv, "c:f:h:v")) != EOF)
		switch (ch) {
		case 'c':
			cfile = optarg;
			break;
		case 'f':
			if (freopen(optarg, "r", stdin) == NULL) {
				fprintf(stderr,
				    "%s: %s\n", optarg, strerror(errno));
				return (EXIT_FAILURE);
			}
			break;
		case 'h':
			hfile = optarg;
			break;
		case 'v':
			++verbose;
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;

	if (*argv != NULL)
		return (usage());

	/* Load records from the input file. */
	if (desc_load())
		return (EXIT_FAILURE);

	/* Dump records for debugging. */
	if (verbose && desc_dump())
		return (EXIT_FAILURE);

	/* Open output files. */
	if ((cfp = fopen(cfile, "w")) == NULL) {
		fprintf(stderr,
		    "%s: %s: %s\n", progname, cfile, strerror(errno));
		return (EXIT_FAILURE);
	}
	if ((hfp = fopen(hfile, "w")) == NULL) {
		fprintf(stderr,
		    "%s: %s: %s\n", progname, hfile, strerror(errno));
		return (EXIT_FAILURE);
	}

	/* Build the source and header files. */
	if (code_header())
		return (EXIT_FAILURE);
	if (code_source())
		return (EXIT_FAILURE);

	return (EXIT_SUCCESS);
}

/*
 * desc_load --
 *	Load a description file.
 */
int
desc_load()
{
	u_int field_alloc;
	int version;
	char *p, *t, save_ch, buf[256];

	field_alloc = version = 0;
	while (fgets(buf, sizeof(buf), stdin) != NULL) {
		if ((p = strchr(buf, '\n')) == NULL) {
			fprintf(stderr, "%s: input line too long\n", progname);
			return (1);
		}
		*p = '\0';

		/* Skip leading whitespace. */
		for (p = buf; isspace(*p); ++p)
			;

		/* Skip empty lines or lines beginning with '#'. */
		if (*p == '\0' || *p == '#')
			continue;

		/* Get a version. */
		if (!version) {
			if (strncasecmp(
			    p, "version", sizeof("version") - 1) == 0) {
				version = 1;
				continue;
			}
			fprintf(stderr,
			    "%s: expected \"version\" line\n", progname);
			return (1);
		}

		/*
		 * Skip block close -- not currently useful, but when this
		 * code supports versioned descriptions, it will matter.
		 */
		if (*p == '}') {
			version = 0;
			continue;
		}

		/* Allocate a new field structure as necessary. */
		if (field_cnt == field_alloc &&
		    (fields = realloc(fields, field_alloc += 100)) == NULL) {
			fprintf(stderr, "%s: %s\n", progname, strerror(errno));
			return (1);
		}

		/* Find the end of the field name. */
		for (t = p; *t != '\0' && !isspace(*t); ++t)
			;
		save_ch = *t;
		*t = '\0';
		if ((fields[field_cnt].name = strdup(p)) == NULL ||
		    (fields[field_cnt].upper = strdup(p)) == NULL) {
			fprintf(stderr, "%s: %s\n", progname, strerror(errno));
			return (1);
		}
		*t = save_ch;
		p = t;

		fields[field_cnt].indx = 0;
		fields[field_cnt].type = NOTSET;
		for (;;) {
			/* Skip to the next field, if any. */
			for (; *p != '\0' && isspace(*p); ++p)
				;
			if (*p == '\0')
				break;

			/* Find the end of the field. */
			for (t = p; *t != '\0' && !isspace(*t); ++t)
				;
			save_ch = *t;
			*t = '\0';
			if (strcasecmp(p, "double") == 0)
				fields[field_cnt].type = DOUBLE;
			else if (strcasecmp(p, "index") == 0)
				fields[field_cnt].indx = 1;
			else if (strcasecmp(p, "string") == 0)
				fields[field_cnt].type = STRING;
			else if (strcasecmp(p, "unsigned_long") == 0)
				fields[field_cnt].type = UNSIGNED_LONG;
			else {
				fprintf(stderr,
				    "%s: unknown keyword: %s\n", progname, p);
				return (1);
			}
			*t = save_ch;
			p = t;
		}

		/* Create a copy of the field name that's upper-case. */
		for (p = fields[field_cnt].upper; *p != '\0'; ++p)
			if (islower(*p))
				*p = (char)toupper(*p);
		++field_cnt;
	}
	if (ferror(stdin)) {
		fprintf(stderr, "%s: stdin: %s\n", progname, strerror(errno));
		return (1);
	}
	return (0);
}

/*
 * desc_dump --
 *	Dump a set of FIELD structures.
 */
int
desc_dump()
{
	FIELD *f;
	u_int i;

	for (f = fields, i = 0; i < field_cnt; ++i, ++f) {
		fprintf(stderr, "field {%s}: (", f->name);
		switch (f->type) {
		case NOTSET:
			fprintf(stderr, "ignored");
			break;
		case DOUBLE:
			fprintf(stderr, "double");
			break;
		case STRING:
			fprintf(stderr, "string");
			break;
		case UNSIGNED_LONG:
			fprintf(stderr, "unsigned_long");
			break;
		}
		if (f->indx)
			fprintf(stderr, ", indexed");
		fprintf(stderr, ")\n");
	}
	return (0);
}

/*
 * code_header --
 *	Print out the C #include file.
 */
int
code_header()
{
	FIELD *f;
	u_int i;

	fprintf(hfp, "/*\n");
	fprintf(hfp, " *  DO NOT EDIT: automatically built by %s.\n", progname);
	fprintf(hfp, " *\n");
	fprintf(hfp, " * Record structure.\n");
	fprintf(hfp, " */\n");
	fprintf(hfp, "typedef struct __DbRecord {\n");
	fprintf(hfp, "\tu_int32_t\t recno;\t\t/* Record number */\n");
	fprintf(hfp, "\n");
	fprintf(hfp, "\t/*\n");
	fprintf(hfp, "\t * Management fields\n");
	fprintf(hfp, "\t */\n");
	fprintf(hfp, "\tvoid\t\t*raw;\t\t/* Memory returned by DB */\n");
	fprintf(hfp, "\tu_char\t\t*record;\t/* Raw record */\n");
	fprintf(hfp, "\tsize_t\t\t record_len;\t/* Raw record length */\n\n");
	fprintf(hfp, "\tu_int32_t\t field_count;\t/* Field count */\n");
	fprintf(hfp, "\tu_int32_t\t version;\t/* Record version */\n\n");
	fprintf(hfp, "\tu_int32_t\t*offset;\t/* Offset table */\n");
	fprintf(hfp, "\n");

	fprintf(hfp, "\t/*\n");
	fprintf(hfp, "\t * Indexed fields\n");
	fprintf(hfp, "\t */\n");
	for (f = fields, i = 0; i < field_cnt; ++i, ++f) {
		if (f->type == NOTSET)
			continue;
		if (i != 0)
			fprintf(hfp, "\n");
		fprintf(hfp, "#define	CSV_INDX_%s\t%d\n", f->upper, i + 1);
		switch (f->type) {
		case NOTSET:
			/* NOTREACHED */
			abort();
			break;
		case DOUBLE:
			fprintf(hfp, "\tdouble\t\t %s;\n", f->name);
			break;
		case STRING:
			fprintf(hfp, "\tchar\t\t*%s;\n", f->name);
			break;
		case UNSIGNED_LONG:
			fprintf(hfp, "\tu_long\t\t %s;\n", f->name);
			break;
		}
	}
	fprintf(hfp, "} DbRecord;\n");

	return (0);
}

/*
 * code_source --
 *	Print out the C structure initialization.
 */
int
code_source()
{
	FIELD *f;
	u_int i;

	fprintf(cfp, "/*\n");
	fprintf(cfp,
	   " *  DO NOT EDIT: automatically built by %s.\n", progname);
	fprintf(cfp, " *\n");
	fprintf(cfp, " * Initialized record structure.\n");
	fprintf(cfp, " */\n");
	fprintf(cfp, "\n");
	fprintf(cfp, "#include \"csv.h\"\n");
	fprintf(cfp, "#include \"csv_local.h\"\n");
	fprintf(cfp, "\n");
	fprintf(cfp, "DbRecord DbRecord_base = {\n");
	fprintf(cfp, "\t0,\t\t/* Record number */\n");
	fprintf(cfp, "\tNULL,\t\t/* Memory returned by DB */\n");
	fprintf(cfp, "\tNULL,\t\t/* Raw record */\n");
	fprintf(cfp, "\t0,\t\t/* Raw record length */\n");
	fprintf(cfp, "\t%d,\t\t/* Field count */\n", field_cnt);
	fprintf(cfp, "\t0,\t\t/* Record version */\n");
	fprintf(cfp, "\tNULL,\t\t/* Offset table */\n");
	fprintf(cfp, "\n");
	for (f = fields, i = 0; i < field_cnt; ++i, ++f) {
		if (f->type == NOTSET)
			continue;
		switch (f->type) {
		case NOTSET:
			abort();
			/* NOTREACHED */
			break;
		case DOUBLE:
		case UNSIGNED_LONG:
			fprintf(cfp, "\t0,\t\t/* %s */\n", f->name);
			break;
		case STRING:
			fprintf(cfp, "\tNULL,\t\t/* %s */\n", f->name);
			break;
		}
	}
	fprintf(cfp, "};\n");

	fprintf(cfp, "\n");
	fprintf(cfp, "DbField fieldlist[] = {\n");
	for (f = fields, i = 0; i < field_cnt; ++i, ++f) {
		if (f->type == NOTSET)
			continue;
		fprintf(cfp, "\t{ \"%s\",", f->name);
		fprintf(cfp, " CSV_INDX_%s,", f->upper);
		fprintf(cfp, "\n\t    %s,", type_to_string(f->type));
		fprintf(cfp, " %d,", f->indx ? 1 : 0);
		fprintf(cfp, " NULL,");
		fprintf(cfp, " FIELD_OFFSET(%s)},\n", f->name);
	}
	fprintf(cfp, "\t{NULL, 0, STRING, 0, NULL, 0}\n};\n");

	return (0);
}

char *
type_to_string(type)
	datatype type;
{
	switch (type) {
	case NOTSET:
		return ("NOTSET");
	case DOUBLE:
		return ("DOUBLE");
	case STRING:
		return ("STRING");
	case UNSIGNED_LONG:
		return ("UNSIGNED_LONG");
	}

	abort();
	/* NOTREACHED */
}

int
usage()
{
	(void)fprintf(stderr,
	    "usage: %s [-v] [-c source-file] [-f input] [-h header-file]\n",
	    progname);
	exit(1);
}