16 #include <sys/types.h>
22 #define PF_PREFIX "PROCFILE"
25 // ----------------------------------------------------------------------------
28 #define PFWORDS_INCREASE_STEP 200
30 pfwords *pfwords_add(pfwords *fw, char *str) {
31 if(pfdebug) fprintf(stderr, PF_PREFIX ": adding word No %d: '%s'\n", fw->len, str);
33 if(fw->len == fw->size) {
34 if(pfdebug) fprintf(stderr, PF_PREFIX ": expanding words\n");
36 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
38 fprintf(stderr, PF_PREFIX ": failed to expand words\n");
43 fw->size += PFWORDS_INCREASE_STEP;
46 fw->words[fw->len++] = str;
51 pfwords *pfwords_new(void) {
52 if(pfdebug) fprintf(stderr, PF_PREFIX ": initializing words\n");
54 pfwords *new = malloc(sizeof(pfwords) + PFWORDS_INCREASE_STEP * sizeof(char *));
58 new->size = PFWORDS_INCREASE_STEP;
62 void pfwords_reset(pfwords *fw) {
63 if(pfdebug) fprintf(stderr, PF_PREFIX ": reseting words\n");
67 void pfwords_free(pfwords *fw) {
68 if(pfdebug) fprintf(stderr, PF_PREFIX ": freeing words\n");
74 // ----------------------------------------------------------------------------
77 #define PFLINES_INCREASE_STEP 10
79 pflines *pflines_add(pflines *fl, uint32_t first_word) {
80 if(pfdebug) fprintf(stderr, PF_PREFIX ": adding line %d at word %d\n", fl->len, first_word);
82 if(fl->len == fl->size) {
83 if(pfdebug) fprintf(stderr, PF_PREFIX ": expanding lines\n");
85 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
87 fprintf(stderr, PF_PREFIX ": failed to expand lines\n");
92 fl->size += PFLINES_INCREASE_STEP;
95 fl->lines[fl->len].words = 0;
96 fl->lines[fl->len++].first = first_word;
101 pflines *pflines_new(void) {
102 if(pfdebug) fprintf(stderr, PF_PREFIX ": initializing lines\n");
104 pflines *new = malloc(sizeof(pflines) + PFLINES_INCREASE_STEP * sizeof(ffline));
105 if(!new) return NULL;
108 new->size = PFLINES_INCREASE_STEP;
112 void pflines_reset(pflines *fl) {
113 if(pfdebug) fprintf(stderr, PF_PREFIX ": reseting lines\n");
118 void pflines_free(pflines *fl) {
119 if(pfdebug) fprintf(stderr, PF_PREFIX ": freeing lines\n");
125 // ----------------------------------------------------------------------------
128 #define PROCFILE_INITIAL_BUFFER 256
129 #define PROCFILE_INCREMENT_BUFFER 512
131 #define PF_CHAR_IS_SEPARATOR 0
132 #define PF_CHAR_IS_NEWLINE 1
133 #define PF_CHAR_IS_WORD 2
135 void procfile_close(procfile *ff) {
136 if(pfdebug) fprintf(stderr, PF_PREFIX ": Closing file '%s'. Reason: %s\n", ff->filename, strerror(errno));
138 if(ff->lines) pflines_free(ff->lines);
139 if(ff->words) pfwords_free(ff->words);
141 if(ff->fd != -1) close(ff->fd);
145 procfile *procfile_parser(procfile *ff) {
146 if(pfdebug) fprintf(stderr, PF_PREFIX ": Parsing file '%s'\n", ff->filename);
148 char *s = ff->data, *e = ff->data, *t = ff->data;
149 uint32_t l = 0, w = 0;
152 ff->lines = pflines_add(ff->lines, w);
153 if(!ff->lines) goto cleanup;
156 switch(ff->separators[(int)(*s)]) {
157 case PF_CHAR_IS_SEPARATOR:
159 // skip all leading white spaces
167 ff->words = pfwords_add(ff->words, t);
168 if(!ff->words) goto cleanup;
170 ff->lines->lines[l].words++;
176 case PF_CHAR_IS_NEWLINE:
180 ff->words = pfwords_add(ff->words, t);
181 if(!ff->words) goto cleanup;
183 ff->lines->lines[l].words++;
186 if(pfdebug) fprintf(stderr, PF_PREFIX ": ended line %d with %d words\n", l, ff->lines->lines[l].words);
188 ff->lines = pflines_add(ff->lines, w);
189 if(!ff->lines) goto cleanup;
203 if(ff->len < ff->size) *s = '\0';
205 // we are going to loose the last byte
206 ff->data[ff->size - 1] = '\0';
209 ff->words = pfwords_add(ff->words, t);
210 if(!ff->words) goto cleanup;
212 ff->lines->lines[l].words++;
219 fprintf(stderr, PF_PREFIX ": Failed to parse file '%s'. Reason: %s\n", ff->filename, strerror(errno));
224 procfile *procfile_readall(procfile *ff) {
225 if(pfdebug) fprintf(stderr, PF_PREFIX ": Reading file '%s'.\n", ff->filename);
227 ssize_t s = 0, r = ff->size, x = ff->size;
232 if(pfdebug) fprintf(stderr, PF_PREFIX ": Expanding data buffer for file '%s'.\n", ff->filename);
234 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
236 fprintf(stderr, PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
241 ff->size += PROCFILE_INCREMENT_BUFFER;
242 x = PROCFILE_INCREMENT_BUFFER;
245 r = read(ff->fd, &ff->data[s], ff->size - s);
247 fprintf(stderr, PF_PREFIX ": Cannot read from file '%s'. Reason: %s\n", ff->filename, strerror(errno));
256 if(lseek(ff->fd, 0, SEEK_SET) == -1) {
257 fprintf(stderr, PF_PREFIX ": Cannot rewind on file '%s'.\n", ff->filename);
262 pflines_reset(ff->lines);
263 pfwords_reset(ff->words);
265 ff = procfile_parser(ff);
270 procfile *procfile_open(const char *filename, const char *separators) {
271 if(pfdebug) fprintf(stderr, PF_PREFIX ": Opening file '%s'\n", filename);
273 int fd = open(filename, O_RDONLY|O_NOATIME, 0666);
275 fprintf(stderr, PF_PREFIX ": Cannot open file '%s'. Reason: %s\n", filename, strerror(errno));
279 procfile *ff = malloc(sizeof(procfile) + PROCFILE_INITIAL_BUFFER);
281 fprintf(stderr, PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
286 strncpy(ff->filename, filename, FILENAME_MAX);
287 ff->filename[FILENAME_MAX] = '\0';
290 ff->size = PROCFILE_INITIAL_BUFFER;
293 ff->lines = pflines_new();
294 ff->words = pfwords_new();
296 if(!ff->lines || !ff->words) {
297 fprintf(stderr, PF_PREFIX ": Cannot initialize parser for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
303 for(i = 0; i < 256 ;i++) {
304 if(i == '\n' || i == '\r') ff->separators[i] = PF_CHAR_IS_NEWLINE;
305 else if(isspace(i) || !isprint(i)) ff->separators[i] = PF_CHAR_IS_SEPARATOR;
306 else ff->separators[i] = PF_CHAR_IS_WORD;
309 if(!separators) separators = " \t=|";
310 const char *s = separators;
311 while(*s) ff->separators[(int)*s++] = PF_CHAR_IS_SEPARATOR;
317 // ----------------------------------------------------------------------------
318 // example parsing of procfile data
320 void procfile_print(procfile *ff) {
321 uint32_t lines = procfile_lines(ff), l;
325 fprintf(stderr, "File '%s' with %d lines and %d words\n", ff->filename, ff->lines->len, ff->words->len);
327 for(l = 0; l < lines ;l++) {
328 words = procfile_linewords(ff, l);
330 fprintf(stderr, " line %d starts at word %d and has %d words\n", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
332 for(w = 0; w < words ;w++) {
333 s = procfile_lineword(ff, l, w);
334 fprintf(stderr, " [%d.%d] '%s'\n", l, w, s);