-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.c
More file actions
133 lines (97 loc) · 2.51 KB
/
Copy pathcompile.c
File metadata and controls
133 lines (97 loc) · 2.51 KB
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
#include "compile.h"
#include "longline.h"
/*A program for building GDBM files from space-delimited text files.
Copyright (C) 2000,2001,2002 Brian Langenberger
Released under the terms of the GNU Public License.
See the file "COPYING" for full details.*/
int findSpace(char *s) {
int i;
for (i = 0 ; ((s[i] != ' ') && (s[i] != '\0')) ; i++)
/*Do nothing*/;
if (s[i] == '\0')
return -1;
else
return i;
}
int findNonSpace(char *s, int start) {
int i;
for (i = start ; ((s[i] == ' ') && (s[i] != '\0')) ; i++)
/*Do nothng*/;
if (s[i] == '\0')
return -1;
else
return i;
}
int main(int argc, char *argv[]) {
FILE *input;
GDBM_FILE output;
int i;
char *line;
int linelength;
int lines = 0;
int space,nonspace;
datum keyd,valued;
if (argc != 3) {
fprintf(stderr,
"*** Usage: compile <text input file> <gdbm output file>\n");
exit(1);
}
/*if the first argument is "-", read from stdin*/
if (strcmp(argv[1], "-") == 0) {
input = stdin;
} else {
input = fopen(argv[1], "r");
}
if (input == NULL) {
fprintf(stderr,
"error opening %s for reading\n", argv[1]);
exit(1);
}
output = gdbm_open(argv[2], 0, GDBM_NEWDB, 0644, 0);
if (output == NULL) {
fprintf(stderr,
"error opening %s for writing\n", argv[2]);
if (strcmp(argv[1], "-") != 0) fclose(input);
exit(1);
}
for (line = readLongLine(input) ; line != NULL ;
free(line),line = readLongLine(input)) {
linelength = strlen(line);
space = findSpace(line);
if (space == -1) {
fprintf(stderr, "Line %d has no space!\n", lines);
exit(1);
}
nonspace = findNonSpace(line, space);
keyd.dptr = line;
keyd.dsize = space;
valued.dptr = line + nonspace;
valued.dsize = linelength - nonspace;
#ifdef DEBUG
for (i = 0 ; i < keyd.dsize ; i++) {
fputc(keyd.dptr[i], stderr);
}
fprintf(stderr, "<>");
for (i = 0 ; i < valued.dsize ; i++) {
fputc(valued.dptr[i], stderr);
}
fputc('\n', stderr);
#endif
if (gdbm_store(output, keyd, valued, GDBM_INSERT) == 1) {
fprintf(stderr, "Key ");
for (i = 0 ; i < keyd.dsize ; i++)
fputc(keyd.dptr[i], stderr);
fprintf(stderr, " is already present in database\n");
exit(1);
}
lines++;
}
gdbm_sync(output);
/*only close the file if it is not stdin*/
if (strcmp(argv[1], "-") != 0) {
fclose(input);
}
gdbm_close(output);
printf("Inserted %d lines into %s\n", lines, argv[2]);
return 0;
}