}
/* the file is opened. it's line based - this will be a bit messy */
- while (readword(word, fp, MAXLINE_LEN) != -1) {
+ while (readword(word, fp, "\n\t ", MAXLINE_LEN) != -1) {
/* do something */
switch(expect) {
case RESOLV_KEYWORD:
/**
* read a word from a stream. Return the number
* of character read or -1 on failure or EOF
+ * All the chars in *del are used to stop when reading.
+ * It defaults to '\n\t ' (newline, tab, space);
*/
int
-readword(char *line, FILE *from, size_t lim)
+readword(char *word, FILE *from, char *del, size_t lim)
{
int c;
char *l;
+ char *d;
+ char *delim;
int i;
- l = line; i = 0;
+ l = word; i = 0;
+ if (!del) {
+ delim = "\n\t ";
+ } else {
+ delim = del;
+ }
+
while ((c = getc(from)) != EOF) {
- if (c != '\n' && c != ' ' && c != '\t') {
- *l++ = c; lim--; i++;
- } else {
- *l = '\0';
- return i;
+
+ for (d = del; *d; d++) {
+ if (c == *d) {
+ *l = '\0';
+ return i;
+ }
}
+ *l++ = c; lim--; i++;
+
if ((size_t)i > lim) {
return -1;
}
/**
* return a word from a stream
*/
-int readword(char *, FILE *, size_t);
+int readword(char *, FILE *, char *, size_t);
#endif /* !_UTIL_H */