16 #include <sys/types.h>
23 #include "../config.h"
25 #define PF_PREFIX "PROCFILE"
27 #define PFWORDS_INCREASE_STEP 200
28 #define PFLINES_INCREASE_STEP 10
29 #define PROCFILE_INCREMENT_BUFFER 512
31 int procfile_adaptive_initial_allocation = 0;
33 // if adaptive allocation is set, these store the
34 // max values we have seen so far
35 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
36 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
37 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
39 // ----------------------------------------------------------------------------
43 pfwords *pfwords_add(pfwords *fw, char *str) {
44 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
46 if(unlikely(fw->len == fw->size)) {
47 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
49 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
51 error(PF_PREFIX ": failed to expand words");
56 fw->size += PFWORDS_INCREASE_STEP;
59 fw->words[fw->len++] = str;
64 pfwords *pfwords_new(void) {
65 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
67 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
69 pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
70 if(unlikely(!new)) return NULL;
77 void pfwords_reset(pfwords *fw) {
78 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
82 void pfwords_free(pfwords *fw) {
83 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
89 // ----------------------------------------------------------------------------
92 pflines *pflines_add(pflines *fl, uint32_t first_word) {
93 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
95 if(unlikely(fl->len == fl->size)) {
96 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
98 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
100 error(PF_PREFIX ": failed to expand lines");
105 fl->size += PFLINES_INCREASE_STEP;
108 fl->lines[fl->len].words = 0;
109 fl->lines[fl->len++].first = first_word;
114 pflines *pflines_new(void) {
115 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
117 uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
119 pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
120 if(unlikely(!new)) return NULL;
127 void pflines_reset(pflines *fl) {
128 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
133 void pflines_free(pflines *fl) {
134 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
140 // ----------------------------------------------------------------------------
143 #define PF_CHAR_IS_SEPARATOR ' '
144 #define PF_CHAR_IS_NEWLINE 'N'
145 #define PF_CHAR_IS_WORD 'W'
146 #define PF_CHAR_IS_QUOTE 'Q'
147 #define PF_CHAR_IS_OPEN 'O'
148 #define PF_CHAR_IS_CLOSE 'C'
150 void procfile_close(procfile *ff) {
151 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
153 if(likely(ff->lines)) pflines_free(ff->lines);
154 if(likely(ff->words)) pfwords_free(ff->words);
156 if(likely(ff->fd != -1)) close(ff->fd);
160 procfile *procfile_parser(procfile *ff) {
161 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
163 char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data, quote = 0;
164 uint32_t l = 0, w = 0;
167 ff->lines = pflines_add(ff->lines, w);
168 if(unlikely(!ff->lines)) goto cleanup;
170 while(likely(s < e)) {
171 // we are not at the end
173 switch(ff->separators[(int)(*s)]) {
174 case PF_CHAR_IS_OPEN:
187 case PF_CHAR_IS_CLOSE:
193 ff->words = pfwords_add(ff->words, t);
194 if(unlikely(!ff->words)) goto cleanup;
196 ff->lines->lines[l].words++;
208 case PF_CHAR_IS_QUOTE:
209 if(unlikely(!quote && s == t)) {
210 // quote opened at the beginning
214 else if(unlikely(quote && quote == *s)) {
219 ff->words = pfwords_add(ff->words, t);
220 if(unlikely(!ff->words)) goto cleanup;
222 ff->lines->lines[l].words++;
231 case PF_CHAR_IS_SEPARATOR:
232 if(unlikely(quote || opened)) {
233 // we are inside a quote
238 if(unlikely(s == t)) {
239 // skip all leading white spaces
247 ff->words = pfwords_add(ff->words, t);
248 if(unlikely(!ff->words)) goto cleanup;
250 ff->lines->lines[l].words++;
256 case PF_CHAR_IS_NEWLINE:
260 ff->words = pfwords_add(ff->words, t);
261 if(unlikely(!ff->words)) goto cleanup;
263 ff->lines->lines[l].words++;
266 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
268 ff->lines = pflines_add(ff->lines, w);
269 if(unlikely(!ff->lines)) goto cleanup;
281 if(likely(s > t && t < e)) {
283 if(likely(ff->len < ff->size))
286 // we are going to loose the last byte
287 ff->data[ff->size - 1] = '\0';
290 ff->words = pfwords_add(ff->words, t);
291 if(unlikely(!ff->words)) goto cleanup;
293 ff->lines->lines[l].words++;
300 error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
305 procfile *procfile_readall(procfile *ff) {
306 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
311 while(likely(r > 0)) {
316 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
318 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
320 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
325 ff->size += PROCFILE_INCREMENT_BUFFER;
328 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
329 r = read(ff->fd, &ff->data[s], ff->size - s);
330 if(unlikely(r == -1)) {
331 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
339 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
340 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
341 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
346 pflines_reset(ff->lines);
347 pfwords_reset(ff->words);
349 ff = procfile_parser(ff);
351 if(unlikely(procfile_adaptive_initial_allocation)) {
352 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
353 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
354 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
357 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
361 static void procfile_set_separators(procfile *ff, const char *separators) {
362 static char def[256] = { [0 ... 255] = 0 };
365 if(unlikely(!def[255])) {
366 // this is thread safe
367 // we check that the last byte is non-zero
368 // if it is zero, multiple threads may be executing this at the same time
369 // setting in def[] the exact same values
370 for(i = 0; likely(i < 256) ;i++) {
371 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
372 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
373 else def[i] = PF_CHAR_IS_WORD;
378 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
379 while(likely(ffd != ffe)) *ffs++ = *ffd++;
381 // set the separators
382 if(unlikely(!separators))
383 separators = " \t=|";
385 ffs = ff->separators;
386 const char *s = separators;
388 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
391 void procfile_set_quotes(procfile *ff, const char *quotes) {
394 for(i = 0; i < 256 ; i++)
395 if(ff->separators[i] == PF_CHAR_IS_QUOTE)
396 ff->separators[i] = PF_CHAR_IS_WORD;
398 // if nothing given, return
399 if(unlikely(!quotes || !*quotes))
403 char *ffs = ff->separators;
404 const char *s = quotes;
406 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
409 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
410 // remove all open/close
412 for(i = 0; i < 256 ; i++)
413 if(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE)
414 ff->separators[i] = PF_CHAR_IS_WORD;
416 // if nothing given, return
417 if(unlikely(!open || !*open || !close || !*close))
421 char *ffs = ff->separators;
422 const char *s = open;
424 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
428 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
431 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
432 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
434 int fd = open(filename, O_RDONLY, 0666);
435 if(unlikely(fd == -1)) {
436 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
440 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
441 procfile *ff = malloc(sizeof(procfile) + size);
443 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
448 strncpy(ff->filename, filename, FILENAME_MAX);
449 ff->filename[FILENAME_MAX] = '\0';
456 ff->lines = pflines_new();
457 ff->words = pfwords_new();
459 if(unlikely(!ff->lines || !ff->words)) {
460 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
465 procfile_set_separators(ff, separators);
467 debug(D_PROCFILE, "File '%s' opened.", filename);
471 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
472 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
474 if(likely(ff->fd != -1)) close(ff->fd);
476 ff->fd = open(filename, O_RDONLY, 0666);
477 if(unlikely(ff->fd == -1)) {
482 strncpy(ff->filename, filename, FILENAME_MAX);
483 ff->filename[FILENAME_MAX] = '\0';
487 // do not do the separators again if NULL is given
488 if(likely(separators)) procfile_set_separators(ff, separators);
493 // ----------------------------------------------------------------------------
494 // example parsing of procfile data
496 void procfile_print(procfile *ff) {
497 uint32_t lines = procfile_lines(ff), l;
501 debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
503 for(l = 0; likely(l < lines) ;l++) {
504 words = procfile_linewords(ff, l);
506 debug(D_PROCFILE, " line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
508 for(w = 0; likely(w < words) ;w++) {
509 s = procfile_lineword(ff, l, w);
510 debug(D_PROCFILE, " [%d.%d] '%s'", l, w, s);