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;
19 // ----------------------------------------------------------------------------
21 char *procfile_filename(procfile *ff) {
22 char buffer[FILENAME_MAX + 1];
23 snprintfz(buffer, FILENAME_MAX, "/proc/self/fd/%d", ff->fd);
25 ssize_t l = readlink(buffer, ff->filename, FILENAME_MAX);
27 snprintfz(ff->filename, FILENAME_MAX, "unknown filename for fd %d", ff->fd);
29 ff->filename[l] = '\0';
31 // on non-linux systems, something like this will be needed
32 // fcntl(ff->fd, F_GETPATH, ff->filename)
37 // ----------------------------------------------------------------------------
40 static inline pfwords *pfwords_add(pfwords *fw, char *str) NEVERNULL;
41 static inline pfwords *pfwords_add(pfwords *fw, char *str) {
42 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
44 if(unlikely(fw->len == fw->size)) {
45 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
47 fw = reallocz(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
48 fw->size += PFWORDS_INCREASE_STEP;
51 fw->words[fw->len++] = str;
56 static inline pfwords *pfwords_new(void) NEVERNULL;
57 static inline pfwords *pfwords_new(void) {
58 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
60 size_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
62 pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
68 static inline void pfwords_reset(pfwords *fw) {
69 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
73 static inline void pfwords_free(pfwords *fw) {
74 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
80 // ----------------------------------------------------------------------------
83 static inline pflines *pflines_add(pflines *fl, size_t first_word) NEVERNULL;
84 static inline pflines *pflines_add(pflines *fl, size_t first_word) {
85 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
87 if(unlikely(fl->len == fl->size)) {
88 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
90 fl = reallocz(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
91 fl->size += PFLINES_INCREASE_STEP;
94 fl->lines[fl->len].words = 0;
95 fl->lines[fl->len++].first = first_word;
100 static inline pflines *pflines_new(void) NEVERNULL;
101 static inline pflines *pflines_new(void) {
102 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
104 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
106 pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
112 static inline void pflines_reset(pflines *fl) {
113 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
118 static inline void pflines_free(pflines *fl) {
119 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
125 // ----------------------------------------------------------------------------
128 #define PF_CHAR_IS_SEPARATOR ' '
129 #define PF_CHAR_IS_NEWLINE 'N'
130 #define PF_CHAR_IS_WORD 'W'
131 #define PF_CHAR_IS_QUOTE 'Q'
132 #define PF_CHAR_IS_OPEN 'O'
133 #define PF_CHAR_IS_CLOSE 'C'
135 void procfile_close(procfile *ff) {
136 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", procfile_filename(ff));
138 if(likely(ff->lines)) pflines_free(ff->lines);
139 if(likely(ff->words)) pfwords_free(ff->words);
141 if(likely(ff->fd != -1)) close(ff->fd);
145 static inline void procfile_parser(procfile *ff) {
146 // debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
148 register char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data;
149 register char *separators = ff->separators;
151 size_t l = 0, w = 0, opened = 0;
153 ff->lines = pflines_add(ff->lines, w);
155 while(likely(s < e)) {
156 // we are not at the end
158 switch(separators[(unsigned char)(*s)]) {
159 case PF_CHAR_IS_OPEN:
172 case PF_CHAR_IS_CLOSE:
178 ff->words = pfwords_add(ff->words, t);
179 ff->lines->lines[l].words++;
191 case PF_CHAR_IS_QUOTE:
192 if(unlikely(!quote && s == t)) {
193 // quote opened at the beginning
197 else if(unlikely(quote && quote == *s)) {
202 ff->words = pfwords_add(ff->words, t);
203 ff->lines->lines[l].words++;
212 case PF_CHAR_IS_SEPARATOR:
213 if(unlikely(quote || opened)) {
214 // we are inside a quote
219 if(unlikely(s == t)) {
220 // skip all leading white spaces
228 ff->words = pfwords_add(ff->words, t);
229 ff->lines->lines[l].words++;
235 case PF_CHAR_IS_NEWLINE:
239 ff->words = pfwords_add(ff->words, t);
240 ff->lines->lines[l].words++;
243 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
245 ff->lines = pflines_add(ff->lines, w);
257 if(likely(s > t && t < e)) {
259 if(likely(ff->len < ff->size))
262 // we are going to loose the last byte
263 ff->data[ff->size - 1] = '\0';
266 ff->words = pfwords_add(ff->words, t);
267 ff->lines->lines[l].words++;
271 procfile *procfile_readall(procfile *ff) {
272 // debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
274 ff->len = 0; // zero the used size
275 ssize_t r = 1; // read at least once
278 ssize_t x = ff->size - s;
281 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", procfile_filename(ff));
282 ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
283 ff->size += PROCFILE_INCREMENT_BUFFER;
286 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %lu", procfile_filename(ff), s, ff->size - s);
287 r = read(ff->fd, &ff->data[s], ff->size - s);
288 if(unlikely(r == -1)) {
289 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", procfile_filename(ff));
297 // debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
298 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
299 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
304 pflines_reset(ff->lines);
305 pfwords_reset(ff->words);
308 if(unlikely(procfile_adaptive_initial_allocation)) {
309 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
310 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
311 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
314 // debug(D_PROCFILE, "File '%s' updated.", ff->filename);
318 static void procfile_set_separators(procfile *ff, const char *separators) {
319 static char def[256] = { [0 ... 255] = 0 };
321 if(unlikely(!def[255])) {
322 // this is thread safe
323 // we check that the last byte is non-zero
324 // if it is zero, multiple threads may be executing this at the same time
325 // setting in def[] the exact same values
327 for(i = 0; likely(i < 256) ;i++) {
328 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
329 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
330 else def[i] = PF_CHAR_IS_WORD;
335 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
336 while(likely(ffd != ffe)) *ffs++ = *ffd++;
338 // set the separators
339 if(unlikely(!separators))
340 separators = " \t=|";
342 ffs = ff->separators;
343 const char *s = separators;
345 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
348 void procfile_set_quotes(procfile *ff, const char *quotes) {
351 for(i = 0; i < 256 ; i++)
352 if(unlikely(ff->separators[i] == PF_CHAR_IS_QUOTE))
353 ff->separators[i] = PF_CHAR_IS_WORD;
355 // if nothing given, return
356 if(unlikely(!quotes || !*quotes))
360 char *ffs = ff->separators;
361 const char *s = quotes;
363 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
366 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
367 // remove all open/close
369 for(i = 0; i < 256 ; i++)
370 if(unlikely(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE))
371 ff->separators[i] = PF_CHAR_IS_WORD;
373 // if nothing given, return
374 if(unlikely(!open || !*open || !close || !*close))
378 char *ffs = ff->separators;
379 const char *s = open;
381 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
385 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
388 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
389 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
391 int fd = open(filename, O_RDONLY, 0666);
392 if(unlikely(fd == -1)) {
393 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
397 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
398 procfile *ff = mallocz(sizeof(procfile) + size);
400 //strncpyz(ff->filename, filename, FILENAME_MAX);
407 ff->lines = pflines_new();
408 ff->words = pfwords_new();
410 procfile_set_separators(ff, separators);
412 debug(D_PROCFILE, "File '%s' opened.", filename);
416 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
417 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
419 if(likely(ff->fd != -1)) close(ff->fd);
421 ff->fd = open(filename, O_RDONLY, 0666);
422 if(unlikely(ff->fd == -1)) {
427 //strncpyz(ff->filename, filename, FILENAME_MAX);
431 // do not do the separators again if NULL is given
432 if(likely(separators)) procfile_set_separators(ff, separators);
437 // ----------------------------------------------------------------------------
438 // example parsing of procfile data
440 void procfile_print(procfile *ff) {
441 size_t lines = procfile_lines(ff), l;
444 debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", procfile_filename(ff), ff->lines->len, ff->words->len);
446 for(l = 0; likely(l < lines) ;l++) {
447 size_t words = procfile_linewords(ff, l);
449 debug(D_PROCFILE, " line %zu starts at word %zu and has %zu words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
452 for(w = 0; likely(w < words) ;w++) {
453 s = procfile_lineword(ff, l, w);
454 debug(D_PROCFILE, " [%zu.%zu] '%s'", l, w, s);