mesa: simplify dependencies in mmakefiles
[AROS.git] / tools / toollib / lineparser.c
blobd34a394d9795442a79247399c48ffbf2c1abd9a8
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: A very simple line parser. Basically from archtool.c
6 */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <ctype.h>
13 #include <toollib/lineparser.h>
15 char *get_line(FILE *fd)
17 int count, len;
18 char *line;
19 char buffer;
21 len = 0;
24 count = fread(&buffer, 1, 1, fd);
25 len += count;
26 } while(count != 0 && buffer != '\n');
28 if(len == 0 && count == 0)
29 return NULL;
31 fseek(fd, -len, SEEK_CUR);
32 line = malloc( (len+1) * sizeof(char) );
33 count = fread(line, 1, len, fd);
34 if (count != len) {
35 free(line);
36 return NULL;
38 line[len] = 0;
39 len--;
41 while(isspace(line[len]) && len >= 0)
43 line[len] = 0;
44 len--;
47 return line;
50 char *keyword(char *line)
52 char *key = NULL;
53 int len;
55 if(line[0] == '#')
57 len = 1;
58 while(line[len] && !isspace(line[len]))
59 len++;
61 key = malloc(len * sizeof(char));
62 strncpy(key, &line[1], len - 1);
63 key[len - 1] = 0;
65 return key;
68 int get_words(char *line, char ***outarray)
70 char **array;
71 char *word;
72 int num, len;
74 /* Make sure that we have valid input */
75 if(!outarray || !line)
77 fprintf(stderr, "Passed NULL pointer to get_words()!\n");
78 exit(-1);
81 /* Free the old array */
82 array = *outarray;
83 if(array)
85 while(*array)
87 free(*array);
88 array++;
90 free(*outarray);
92 array = NULL;
94 /* Now scan the list of words */
95 num = 0;
96 word = line;
97 while(*word != 0)
99 /* Find the start of this word */
100 while(*word && isspace(*word) )
101 word++;
103 /* Find the end of this word */
104 len = 0;
105 while( word[len] && !isspace(word[len]) )
106 len++;
109 Copy this word into the array, making the array larger
110 in order to fit the pointer to the string.
112 if(len)
114 num++;
115 array = realloc(array, num * sizeof(char *));
116 array[num-1] = malloc((len+1) * sizeof(char));
117 strncpy( array[num-1], word, len);
118 array[num-1][len] = 0;
119 word = &word[len];
123 array = realloc(array, (num+1) * sizeof(char *) );
124 array[num] = NULL;
126 *outarray = array;
127 return num;