4 #define PF_PREFIX "PROCFILE"
6 #define PFWORDS_INCREASE_STEP 200
7 #define PFLINES_INCREASE_STEP 10
8 #define PROCFILE_INCREMENT_BUFFER 512
10 int procfile_adaptive_initial_allocation = 0;
12 // if adaptive allocation is set, these store the
13 // max values we have seen so far
14 size_t procfile_max_lines = PFLINES_INCREASE_STEP;
15 size_t procfile_max_words = PFWORDS_INCREASE_STEP;
16 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
18 // ----------------------------------------------------------------------------
21 static inline pfwords *pfwords_add(pfwords *fw, char *str) NEVERNULL;
22 static inline pfwords *pfwords_add(pfwords *fw, char *str) {
23 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
25 if(unlikely(fw->len == fw->size)) {
26 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
28 fw = reallocz(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
29 fw->size += PFWORDS_INCREASE_STEP;
32 fw->words[fw->len++] = str;
37 static inline pfwords *pfwords_new(void) NEVERNULL;
38 static inline pfwords *pfwords_new(void) {
39 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
41 size_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
43 pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
49 static inline void pfwords_reset(pfwords *fw) {
50 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
54 static inline void pfwords_free(pfwords *fw) {
55 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
61 // ----------------------------------------------------------------------------
64 static inline pflines *pflines_add(pflines *fl, size_t first_word) NEVERNULL;
65 static inline pflines *pflines_add(pflines *fl, size_t first_word) {
66 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
68 if(unlikely(fl->len == fl->size)) {
69 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
71 fl = reallocz(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
72 fl->size += PFLINES_INCREASE_STEP;
75 fl->lines[fl->len].words = 0;
76 fl->lines[fl->len++].first = first_word;
81 static inline pflines *pflines_new(void) NEVERNULL;
82 static inline pflines *pflines_new(void) {
83 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
85 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
87 pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
93 static inline void pflines_reset(pflines *fl) {
94 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
99 static inline void pflines_free(pflines *fl) {
100 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
106 // ----------------------------------------------------------------------------
109 #define PF_CHAR_IS_SEPARATOR ' '
110 #define PF_CHAR_IS_NEWLINE 'N'
111 #define PF_CHAR_IS_WORD 'W'
112 #define PF_CHAR_IS_QUOTE 'Q'
113 #define PF_CHAR_IS_OPEN 'O'
114 #define PF_CHAR_IS_CLOSE 'C'
116 void procfile_close(procfile *ff) {
117 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
119 if(likely(ff->lines)) pflines_free(ff->lines);
120 if(likely(ff->words)) pfwords_free(ff->words);
122 if(likely(ff->fd != -1)) close(ff->fd);
126 static inline void procfile_parser(procfile *ff) {
127 // debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
129 register char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data;
130 register char *separators = ff->separators;
132 size_t l = 0, w = 0, opened = 0;
134 ff->lines = pflines_add(ff->lines, w);
136 while(likely(s < e)) {
137 // we are not at the end
139 switch(separators[(unsigned char)(*s)]) {
140 case PF_CHAR_IS_OPEN:
153 case PF_CHAR_IS_CLOSE:
159 ff->words = pfwords_add(ff->words, t);
160 ff->lines->lines[l].words++;
172 case PF_CHAR_IS_QUOTE:
173 if(unlikely(!quote && s == t)) {
174 // quote opened at the beginning
178 else if(unlikely(quote && quote == *s)) {
183 ff->words = pfwords_add(ff->words, t);
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 ff->lines->lines[l].words++;
216 case PF_CHAR_IS_NEWLINE:
220 ff->words = pfwords_add(ff->words, t);
221 ff->lines->lines[l].words++;
224 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
226 ff->lines = pflines_add(ff->lines, w);
238 if(likely(s > t && t < e)) {
240 if(likely(ff->len < ff->size))
243 // we are going to loose the last byte
244 ff->data[ff->size - 1] = '\0';
247 ff->words = pfwords_add(ff->words, t);
248 ff->lines->lines[l].words++;
252 procfile *procfile_readall(procfile *ff) {
253 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
258 while(likely(r > 0)) {
260 ssize_t x = ff->size - s;
263 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
265 ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
266 ff->size += PROCFILE_INCREMENT_BUFFER;
269 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %lu", ff->filename, s, ff->size - s);
270 r = read(ff->fd, &ff->data[s], ff->size - s);
271 if(unlikely(r == -1)) {
272 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
280 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
281 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
282 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
287 pflines_reset(ff->lines);
288 pfwords_reset(ff->words);
291 if(unlikely(procfile_adaptive_initial_allocation)) {
292 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
293 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
294 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
297 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
301 static void procfile_set_separators(procfile *ff, const char *separators) {
302 static char def[256] = { [0 ... 255] = 0 };
304 if(unlikely(!def[255])) {
305 // this is thread safe
306 // we check that the last byte is non-zero
307 // if it is zero, multiple threads may be executing this at the same time
308 // setting in def[] the exact same values
310 for(i = 0; likely(i < 256) ;i++) {
311 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
312 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
313 else def[i] = PF_CHAR_IS_WORD;
318 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
319 while(likely(ffd != ffe)) *ffs++ = *ffd++;
321 // set the separators
322 if(unlikely(!separators))
323 separators = " \t=|";
325 ffs = ff->separators;
326 const char *s = separators;
328 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
331 void procfile_set_quotes(procfile *ff, const char *quotes) {
334 for(i = 0; i < 256 ; i++)
335 if(unlikely(ff->separators[i] == PF_CHAR_IS_QUOTE))
336 ff->separators[i] = PF_CHAR_IS_WORD;
338 // if nothing given, return
339 if(unlikely(!quotes || !*quotes))
343 char *ffs = ff->separators;
344 const char *s = quotes;
346 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
349 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
350 // remove all open/close
352 for(i = 0; i < 256 ; i++)
353 if(unlikely(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE))
354 ff->separators[i] = PF_CHAR_IS_WORD;
356 // if nothing given, return
357 if(unlikely(!open || !*open || !close || !*close))
361 char *ffs = ff->separators;
362 const char *s = open;
364 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
368 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
371 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
372 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
374 int fd = open(filename, O_RDONLY, 0666);
375 if(unlikely(fd == -1)) {
376 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
380 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
381 procfile *ff = mallocz(sizeof(procfile) + size);
382 strncpyz(ff->filename, filename, FILENAME_MAX);
389 ff->lines = pflines_new();
390 ff->words = pfwords_new();
392 procfile_set_separators(ff, separators);
394 debug(D_PROCFILE, "File '%s' opened.", filename);
398 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
399 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
401 if(likely(ff->fd != -1)) close(ff->fd);
403 ff->fd = open(filename, O_RDONLY, 0666);
404 if(unlikely(ff->fd == -1)) {
409 strncpyz(ff->filename, filename, FILENAME_MAX);
413 // do not do the separators again if NULL is given
414 if(likely(separators)) procfile_set_separators(ff, separators);
419 // ----------------------------------------------------------------------------
420 // example parsing of procfile data
422 void procfile_print(procfile *ff) {
423 size_t lines = procfile_lines(ff), l;
426 debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", ff->filename, ff->lines->len, ff->words->len);
428 for(l = 0; likely(l < lines) ;l++) {
429 size_t words = procfile_linewords(ff, l);
431 debug(D_PROCFILE, " line %zu starts at word %zu and has %zu words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
434 for(w = 0; likely(w < words) ;w++) {
435 s = procfile_lineword(ff, l, w);
436 debug(D_PROCFILE, " [%zu.%zu] '%s'", l, w, s);