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 fw = reallocz(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
28 fw->size += PFWORDS_INCREASE_STEP;
31 fw->words[fw->len++] = str;
36 pfwords *pfwords_new(void) {
37 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
39 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
41 pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
47 void pfwords_reset(pfwords *fw) {
48 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
52 void pfwords_free(pfwords *fw) {
53 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
59 // ----------------------------------------------------------------------------
62 pflines *pflines_add(pflines *fl, uint32_t first_word) {
63 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
65 if(unlikely(fl->len == fl->size)) {
66 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
68 fl = reallocz(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
69 fl->size += PFLINES_INCREASE_STEP;
72 fl->lines[fl->len].words = 0;
73 fl->lines[fl->len++].first = first_word;
78 pflines *pflines_new(void) {
79 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
81 uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
83 pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
89 void pflines_reset(pflines *fl) {
90 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
95 void pflines_free(pflines *fl) {
96 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
102 // ----------------------------------------------------------------------------
105 #define PF_CHAR_IS_SEPARATOR ' '
106 #define PF_CHAR_IS_NEWLINE 'N'
107 #define PF_CHAR_IS_WORD 'W'
108 #define PF_CHAR_IS_QUOTE 'Q'
109 #define PF_CHAR_IS_OPEN 'O'
110 #define PF_CHAR_IS_CLOSE 'C'
112 void procfile_close(procfile *ff) {
113 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
115 if(likely(ff->lines)) pflines_free(ff->lines);
116 if(likely(ff->words)) pfwords_free(ff->words);
118 if(likely(ff->fd != -1)) close(ff->fd);
122 procfile *procfile_parser(procfile *ff) {
123 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
125 char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data, quote = 0;
126 uint32_t l = 0, w = 0;
129 ff->lines = pflines_add(ff->lines, w);
130 if(unlikely(!ff->lines)) goto cleanup;
132 while(likely(s < e)) {
133 // we are not at the end
135 switch(ff->separators[(uint8_t)(*s)]) {
136 case PF_CHAR_IS_OPEN:
149 case PF_CHAR_IS_CLOSE:
155 ff->words = pfwords_add(ff->words, t);
156 if(unlikely(!ff->words)) goto cleanup;
158 ff->lines->lines[l].words++;
170 case PF_CHAR_IS_QUOTE:
171 if(unlikely(!quote && s == t)) {
172 // quote opened at the beginning
176 else if(unlikely(quote && quote == *s)) {
181 ff->words = pfwords_add(ff->words, t);
182 if(unlikely(!ff->words)) goto cleanup;
184 ff->lines->lines[l].words++;
193 case PF_CHAR_IS_SEPARATOR:
194 if(unlikely(quote || opened)) {
195 // we are inside a quote
200 if(unlikely(s == t)) {
201 // skip all leading white spaces
209 ff->words = pfwords_add(ff->words, t);
210 if(unlikely(!ff->words)) goto cleanup;
212 ff->lines->lines[l].words++;
218 case PF_CHAR_IS_NEWLINE:
222 ff->words = pfwords_add(ff->words, t);
223 if(unlikely(!ff->words)) goto cleanup;
225 ff->lines->lines[l].words++;
228 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
230 ff->lines = pflines_add(ff->lines, w);
231 if(unlikely(!ff->lines)) goto cleanup;
243 if(likely(s > t && t < e)) {
245 if(likely(ff->len < ff->size))
248 // we are going to loose the last byte
249 ff->data[ff->size - 1] = '\0';
252 ff->words = pfwords_add(ff->words, t);
253 if(unlikely(!ff->words)) goto cleanup;
255 ff->lines->lines[l].words++;
262 error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
267 procfile *procfile_readall(procfile *ff) {
268 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
273 while(likely(r > 0)) {
275 ssize_t x = ff->size - s;
278 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
280 ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
281 ff->size += PROCFILE_INCREMENT_BUFFER;
284 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %lu", ff->filename, s, ff->size - s);
285 r = read(ff->fd, &ff->data[s], ff->size - s);
286 if(unlikely(r == -1)) {
287 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
295 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
296 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
297 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
302 pflines_reset(ff->lines);
303 pfwords_reset(ff->words);
305 ff = procfile_parser(ff);
307 if(unlikely(procfile_adaptive_initial_allocation)) {
308 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
309 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
310 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
313 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
317 static void procfile_set_separators(procfile *ff, const char *separators) {
318 static char def[256] = { [0 ... 255] = 0 };
320 if(unlikely(!def[255])) {
321 // this is thread safe
322 // we check that the last byte is non-zero
323 // if it is zero, multiple threads may be executing this at the same time
324 // setting in def[] the exact same values
326 for(i = 0; likely(i < 256) ;i++) {
327 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
328 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
329 else def[i] = PF_CHAR_IS_WORD;
334 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
335 while(likely(ffd != ffe)) *ffs++ = *ffd++;
337 // set the separators
338 if(unlikely(!separators))
339 separators = " \t=|";
341 ffs = ff->separators;
342 const char *s = separators;
344 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
347 void procfile_set_quotes(procfile *ff, const char *quotes) {
350 for(i = 0; i < 256 ; i++)
351 if(unlikely(ff->separators[i] == PF_CHAR_IS_QUOTE))
352 ff->separators[i] = PF_CHAR_IS_WORD;
354 // if nothing given, return
355 if(unlikely(!quotes || !*quotes))
359 char *ffs = ff->separators;
360 const char *s = quotes;
362 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
365 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
366 // remove all open/close
368 for(i = 0; i < 256 ; i++)
369 if(unlikely(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE))
370 ff->separators[i] = PF_CHAR_IS_WORD;
372 // if nothing given, return
373 if(unlikely(!open || !*open || !close || !*close))
377 char *ffs = ff->separators;
378 const char *s = open;
380 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
384 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
387 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
388 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
390 int fd = open(filename, O_RDONLY, 0666);
391 if(unlikely(fd == -1)) {
392 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
396 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
397 procfile *ff = mallocz(sizeof(procfile) + size);
398 strncpyz(ff->filename, filename, FILENAME_MAX);
405 ff->lines = pflines_new();
406 ff->words = pfwords_new();
408 if(unlikely(!ff->lines || !ff->words)) {
409 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
414 procfile_set_separators(ff, separators);
416 debug(D_PROCFILE, "File '%s' opened.", filename);
420 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
421 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
423 if(likely(ff->fd != -1)) close(ff->fd);
425 ff->fd = open(filename, O_RDONLY, 0666);
426 if(unlikely(ff->fd == -1)) {
431 strncpyz(ff->filename, filename, FILENAME_MAX);
435 // do not do the separators again if NULL is given
436 if(likely(separators)) procfile_set_separators(ff, separators);
441 // ----------------------------------------------------------------------------
442 // example parsing of procfile data
444 void procfile_print(procfile *ff) {
445 uint32_t lines = procfile_lines(ff), l;
448 debug(D_PROCFILE, "File '%s' with %u lines and %u words", ff->filename, ff->lines->len, ff->words->len);
450 for(l = 0; likely(l < lines) ;l++) {
451 uint32_t words = procfile_linewords(ff, l);
453 debug(D_PROCFILE, " line %u starts at word %u and has %u words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
456 for(w = 0; likely(w < words) ;w++) {
457 s = procfile_lineword(ff, l, w);
458 debug(D_PROCFILE, " [%u.%u] '%s'", l, w, s);