pass in the display id to the compositor
[AROS.git] / tools / toollib / lineparser.c
blob1c9633123271ba70b628616a96094b489ab223ff
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 fread(line, 1, len, fd);
34 line[len] = 0;
35 len--;
37 while(isspace(line[len]) && len >= 0)
39 line[len] = 0;
40 len--;
43 return line;
46 char *keyword(char *line)
48 char *key = NULL;
49 int len;
51 if(line[0] == '#')
53 len = 1;
54 while(line[len] && !isspace(line[len]))
55 len++;
57 key = malloc(len * sizeof(char));
58 strncpy(key, &line[1], len - 1);
59 key[len - 1] = 0;
61 return key;
64 int get_words(char *line, char ***outarray)
66 char **array;
67 char *word;
68 int num, len;
70 /* Make sure that we have valid input */
71 if(!outarray || !line)
73 fprintf(stderr, "Passed NULL pointer to get_words()!\n");
74 exit(-1);
77 /* Free the old array */
78 array = *outarray;
79 if(array)
81 while(*array)
83 free(*array);
84 array++;
86 free(*outarray);
88 array = NULL;
90 /* Now scan the list of words */
91 num = 0;
92 word = line;
93 while(*word != 0)
95 /* Find the start of this word */
96 while(*word && isspace(*word) )
97 word++;
99 /* Find the end of this word */
100 len = 0;
101 while( word[len] && !isspace(word[len]) )
102 len++;
105 Copy this word into the array, making the array larger
106 in order to fit the pointer to the string.
108 if(len)
110 num++;
111 array = realloc(array, num * sizeof(char *));
112 array[num-1] = malloc((len+1) * sizeof(char));
113 strncpy( array[num-1], word, len);
114 array[num-1][len] = 0;
115 word = &word[len];
119 array = realloc(array, (num+1) * sizeof(char *) );
120 array[num] = NULL;
122 *outarray = array;
123 return num;