-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetline.c
176 lines (156 loc) · 2.81 KB
/
getline.c
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
#define READ_TO_EOL { \
char k; \
do { k = fgetc(fp); } while(k!=LF && k!=-1); \
}
char read_to_eol_and_check_ignore(FILE *fp)
{
const char *match = " MG:IGNORE";
const char *matchptr = match;
for(;;)
{
char ch = fgetc(fp);
if (ch == LF || ch == -1)
return 0;
if (ch == *matchptr)
{
matchptr++;
if (!*matchptr)
{
READ_TO_EOL;
return 1;
}
}
else
{
matchptr = match;
if (ch == *matchptr) matchptr++;
READ_TO_EOL;
return 0;
}
}
}
int mgetline(FILE *fp, uchar *line, int maxlen)
// read a line from file 'fp', trim whitespace on both sides,
// and remove comments.
// returns the length of the fully-processed line.
{
char ch = -1;
int i = 0;
char in_cmt = 0;
char inquote = 0;
int lastch = -1;
readnextline: ;
// do ltrim
do
{
ch = fgetc(fp);
if (!IS_WHITESPACE(ch)) break;
} while(!feof(fp));
// read in the line
for(;;)
{
if (ch==-1) // end of file
{
line[i] = 0;
if (in_cmt)
{
printf("warning: '/*' without '*/': '%s'\n", line);
}
break;
}
if (!in_cmt)
{
// handle special chars...
if (!inquote)
{
if (ch==34)
{
inquote = 1;
}
else if (ch=='/')
{
if (i && line[i-1]=='/') // C99 '//' cmt
{ // drop rest of line
line[--i] = 0;
if (read_to_eol_and_check_ignore(fp))
goto readnextline;
break;
}
}
else if (ch=='*') // multiline '/*' cmt pair
{
if (i && line[i-1]=='/')
{
line[--i] = 0; // erase the '/'
in_cmt = 1;
lastch = -1;
goto nextchar;
}
}
}
else // inside a string quote
{
if (ch==34) inquote = 0;
}
// add text to buffer...
if (ch != CR) // ignore CR, we only care about LF
{
if (ch==LF)
{ // reached end of line
if (line[i-1]=='\\') // concat C line extensions
{
--i; // remove the '\'
// read next line
goto readnextline;
}
else
{
line[i] = 0;
break;
}
}
else
{
if (i < maxlen)
{
line[i++] = ch;
}
else
{
line[i-1] = 0;
printf("mgetline: warning: max length of %d exceeded on line:\n", maxlen);
printf("'%s'\n", line);
// drop chars to end of line and abort
READ_TO_EOL;
return maxlen-1;
}
}
}
}
else // inside /* */ comments
{
if (lastch=='*' && ch=='/') // '*/' cmt pair
{
in_cmt = 0;
goto readnextline;
}
else lastch = ch;
}
nextchar: ;
ch = fgetc(fp);
} // end of main read loop
// rtrim whitespace
while(i > 0)
{
--i;
if (!IS_WHITESPACE(line[i]))
{
line[++i] = 0;
return i;
}
}
// if we reach here, then whole line was whitespace
// normally we'd set line[0] = 0 but we don't have to
// because we know it was already ltrim'd.
return 0;
}