3 #define PF_PREFIX "PROCFILE"
5 #define PFWORDS_INCREASE_STEP 200
6 #define PFLINES_INCREASE_STEP 10
7 #define PROCFILE_INCREMENT_BUFFER 512
9 int procfile_adaptive_initial_allocation = 0;
11 // if adaptive allocation is set, these store the
12 // max values we have seen so far
13 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
14 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
15 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
17 // ----------------------------------------------------------------------------
21 pfwords *pfwords_add(pfwords *fw, char *str) {
22 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
24 if(unlikely(fw->len == fw->size)) {
25 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
27 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
29 error(PF_PREFIX ": failed to expand words");
34 fw->size += PFWORDS_INCREASE_STEP;
37 fw->words[fw->len++] = str;
42 pfwords *pfwords_new(void) {
43 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
45 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
47 pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
48 if(unlikely(!new)) return NULL;
55 void pfwords_reset(pfwords *fw) {
56 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
60 void pfwords_free(pfwords *fw) {
61 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
67 // ----------------------------------------------------------------------------
70 pflines *pflines_add(pflines *fl, uint32_t first_word) {
71 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
73 if(unlikely(fl->len == fl->size)) {
74 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
76 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
78 error(PF_PREFIX ": failed to expand lines");
83 fl->size += PFLINES_INCREASE_STEP;
86 fl->lines[fl->len].words = 0;
87 fl->lines[fl->len++].first = first_word;
92 pflines *pflines_new(void) {
93 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
95 uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
97 pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
98 if(unlikely(!new)) return NULL;
105 void pflines_reset(pflines *fl) {
106 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
111 void pflines_free(pflines *fl) {
112 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
118 // ----------------------------------------------------------------------------
121 #define PF_CHAR_IS_SEPARATOR ' '
122 #define PF_CHAR_IS_NEWLINE 'N'
123 #define PF_CHAR_IS_WORD 'W'
124 #define PF_CHAR_IS_QUOTE 'Q'
125 #define PF_CHAR_IS_OPEN 'O'
126 #define PF_CHAR_IS_CLOSE 'C'
128 void procfile_close(procfile *ff) {
129 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
131 if(likely(ff->lines)) pflines_free(ff->lines);
132 if(likely(ff->words)) pfwords_free(ff->words);
134 if(likely(ff->fd != -1)) close(ff->fd);
138 procfile *procfile_parser(procfile *ff) {
139 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
141 char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data, quote = 0;
142 uint32_t l = 0, w = 0;
145 ff->lines = pflines_add(ff->lines, w);
146 if(unlikely(!ff->lines)) goto cleanup;
148 while(likely(s < e)) {
149 // we are not at the end
151 switch(ff->separators[(uint8_t)(*s)]) {
152 case PF_CHAR_IS_OPEN:
165 case PF_CHAR_IS_CLOSE:
171 ff->words = pfwords_add(ff->words, t);
172 if(unlikely(!ff->words)) goto cleanup;
174 ff->lines->lines[l].words++;
186 case PF_CHAR_IS_QUOTE:
187 if(unlikely(!quote && s == t)) {
188 // quote opened at the beginning
192 else if(unlikely(quote && quote == *s)) {
197 ff->words = pfwords_add(ff->words, t);
198 if(unlikely(!ff->words)) goto cleanup;
200 ff->lines->lines[l].words++;
209 case PF_CHAR_IS_SEPARATOR:
210 if(unlikely(quote || opened)) {
211 // we are inside a quote
216 if(unlikely(s == t)) {
217 // skip all leading white spaces
225 ff->words = pfwords_add(ff->words, t);
226 if(unlikely(!ff->words)) goto cleanup;
228 ff->lines->lines[l].words++;
234 case PF_CHAR_IS_NEWLINE:
238 ff->words = pfwords_add(ff->words, t);
239 if(unlikely(!ff->words)) goto cleanup;
241 ff->lines->lines[l].words++;
244 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
246 ff->lines = pflines_add(ff->lines, w);
247 if(unlikely(!ff->lines)) goto cleanup;
259 if(likely(s > t && t < e)) {
261 if(likely(ff->len < ff->size))
264 // we are going to loose the last byte
265 ff->data[ff->size - 1] = '\0';
268 ff->words = pfwords_add(ff->words, t);
269 if(unlikely(!ff->words)) goto cleanup;
271 ff->lines->lines[l].words++;
278 error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
283 procfile *procfile_readall(procfile *ff) {
284 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
289 while(likely(r > 0)) {
294 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
296 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
298 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
303 ff->size += PROCFILE_INCREMENT_BUFFER;
306 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %lu", ff->filename, s, ff->size - s);
307 r = read(ff->fd, &ff->data[s], ff->size - s);
308 if(unlikely(r == -1)) {
309 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
317 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
318 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
319 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
324 pflines_reset(ff->lines);
325 pfwords_reset(ff->words);
327 ff = procfile_parser(ff);
329 if(unlikely(procfile_adaptive_initial_allocation)) {
330 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
331 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
332 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
335 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
339 static void procfile_set_separators(procfile *ff, const char *separators) {
340 static char def[256] = { [0 ... 255] = 0 };
343 if(unlikely(!def[255])) {
344 // this is thread safe
345 // we check that the last byte is non-zero
346 // if it is zero, multiple threads may be executing this at the same time
347 // setting in def[] the exact same values
348 for(i = 0; likely(i < 256) ;i++) {
349 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
350 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
351 else def[i] = PF_CHAR_IS_WORD;
356 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
357 while(likely(ffd != ffe)) *ffs++ = *ffd++;
359 // set the separators
360 if(unlikely(!separators))
361 separators = " \t=|";
363 ffs = ff->separators;
364 const char *s = separators;
366 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
369 void procfile_set_quotes(procfile *ff, const char *quotes) {
372 for(i = 0; i < 256 ; i++)
373 if(ff->separators[i] == PF_CHAR_IS_QUOTE)
374 ff->separators[i] = PF_CHAR_IS_WORD;
376 // if nothing given, return
377 if(unlikely(!quotes || !*quotes))
381 char *ffs = ff->separators;
382 const char *s = quotes;
384 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
387 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
388 // remove all open/close
390 for(i = 0; i < 256 ; i++)
391 if(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE)
392 ff->separators[i] = PF_CHAR_IS_WORD;
394 // if nothing given, return
395 if(unlikely(!open || !*open || !close || !*close))
399 char *ffs = ff->separators;
400 const char *s = open;
402 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
406 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
409 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
410 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
412 int fd = open(filename, O_RDONLY, 0666);
413 if(unlikely(fd == -1)) {
414 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
418 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
419 procfile *ff = malloc(sizeof(procfile) + size);
421 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
426 strncpyz(ff->filename, filename, FILENAME_MAX);
433 ff->lines = pflines_new();
434 ff->words = pfwords_new();
436 if(unlikely(!ff->lines || !ff->words)) {
437 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
442 procfile_set_separators(ff, separators);
444 debug(D_PROCFILE, "File '%s' opened.", filename);
448 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
449 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
451 if(likely(ff->fd != -1)) close(ff->fd);
453 ff->fd = open(filename, O_RDONLY, 0666);
454 if(unlikely(ff->fd == -1)) {
459 strncpyz(ff->filename, filename, FILENAME_MAX);
463 // do not do the separators again if NULL is given
464 if(likely(separators)) procfile_set_separators(ff, separators);
469 // ----------------------------------------------------------------------------
470 // example parsing of procfile data
472 void procfile_print(procfile *ff) {
473 uint32_t lines = procfile_lines(ff), l;
477 debug(D_PROCFILE, "File '%s' with %u lines and %u words", ff->filename, ff->lines->len, ff->words->len);
479 for(l = 0; likely(l < lines) ;l++) {
480 words = procfile_linewords(ff, l);
482 debug(D_PROCFILE, " line %u starts at word %u and has %u words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
484 for(w = 0; likely(w < words) ;w++) {
485 s = procfile_lineword(ff, l, w);
486 debug(D_PROCFILE, " [%u.%u] '%s'", l, w, s);