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 if(ff->filename[0]) return ff->filename;
24 char buffer[FILENAME_MAX + 1];
25 snprintfz(buffer, FILENAME_MAX, "/proc/self/fd/%d", ff->fd);
27 ssize_t l = readlink(buffer, ff->filename, FILENAME_MAX);
29 snprintfz(ff->filename, FILENAME_MAX, "unknown filename for fd %d", ff->fd);
31 ff->filename[l] = '\0';
33 // on non-linux systems, something like this will be needed
34 // fcntl(ff->fd, F_GETPATH, ff->filename)
39 // ----------------------------------------------------------------------------
42 static inline pfwords *pfwords_add(pfwords *fw, char *str) NEVERNULL;
43 static inline 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 fw = reallocz(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
50 fw->size += PFWORDS_INCREASE_STEP;
53 fw->words[fw->len++] = str;
58 static inline pfwords *pfwords_new(void) NEVERNULL;
59 static inline pfwords *pfwords_new(void) {
60 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
62 size_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
64 pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
70 static inline void pfwords_reset(pfwords *fw) {
71 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
75 static inline void pfwords_free(pfwords *fw) {
76 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
82 // ----------------------------------------------------------------------------
85 static inline pflines *pflines_add(pflines *fl, size_t first_word) NEVERNULL;
86 static inline pflines *pflines_add(pflines *fl, size_t first_word) {
87 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
89 if(unlikely(fl->len == fl->size)) {
90 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
92 fl = reallocz(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
93 fl->size += PFLINES_INCREASE_STEP;
96 fl->lines[fl->len].words = 0;
97 fl->lines[fl->len++].first = first_word;
102 static inline pflines *pflines_new(void) NEVERNULL;
103 static inline pflines *pflines_new(void) {
104 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
106 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
108 pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
114 static inline void pflines_reset(pflines *fl) {
115 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
120 static inline void pflines_free(pflines *fl) {
121 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
127 // ----------------------------------------------------------------------------
130 void procfile_close(procfile *ff) {
131 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", procfile_filename(ff));
133 if(likely(ff->lines)) pflines_free(ff->lines);
134 if(likely(ff->words)) pfwords_free(ff->words);
136 if(likely(ff->fd != -1)) close(ff->fd);
140 static inline void procfile_parser(procfile *ff) {
141 // debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
143 char *s = ff->data // our current position
144 , *e = &ff->data[ff->len] // the terminating null
145 , *t = ff->data; // the first character of a quoted or a parenthesized string
147 // the look up array to find our type of character
148 PF_CHAR_TYPE *separators = ff->separators;
150 char quote = 0; // the quote character - only when in quoted string
153 l = 0 // counts the number of lines we added
154 , w = 0 // counts the number of words we added
155 , opened = 0; // counts the number of open parenthesis
157 ff->lines = pflines_add(ff->lines, w);
159 while(likely(s < e)) {
160 // we are not at the end
161 PF_CHAR_TYPE ct = separators[(unsigned char)(*s)];
163 // this is faster than a switch()
164 if(likely(ct == PF_CHAR_IS_WORD)) {
167 else if(likely(ct == PF_CHAR_IS_SEPARATOR)) {
168 if(unlikely(quote || opened)) {
169 // we are inside a quote
174 if(unlikely(s == t)) {
175 // skip all leading white spaces
183 ff->words = pfwords_add(ff->words, t);
184 ff->lines->lines[l].words++;
189 else if(likely(ct == PF_CHAR_IS_NEWLINE)) {
193 ff->words = pfwords_add(ff->words, t);
194 ff->lines->lines[l].words++;
197 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
199 ff->lines = pflines_add(ff->lines, w);
204 else if(likely(ct == PF_CHAR_IS_QUOTE)) {
205 if(unlikely(!quote && s == t)) {
206 // quote opened at the beginning
210 else if(unlikely(quote && quote == *s)) {
215 ff->words = pfwords_add(ff->words, t);
216 ff->lines->lines[l].words++;
224 else if(likely(ct == PF_CHAR_IS_OPEN)) {
236 else if(likely(ct == PF_CHAR_IS_CLOSE)) {
242 ff->words = pfwords_add(ff->words, t);
243 ff->lines->lines[l].words++;
255 fatal("Internal Error: procfile_readall() does not handle all the cases.");
258 if(likely(s > t && t < e)) {
260 if(likely(ff->len < ff->size))
263 // we are going to loose the last byte
264 ff->data[ff->size - 1] = '\0';
267 ff->words = pfwords_add(ff->words, t);
268 ff->lines->lines[l].words++;
272 procfile *procfile_readall(procfile *ff) {
273 // debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
275 ff->len = 0; // zero the used size
276 ssize_t r = 1; // read at least once
279 ssize_t x = ff->size - s;
282 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", procfile_filename(ff));
283 ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
284 ff->size += PROCFILE_INCREMENT_BUFFER;
287 debug(D_PROCFILE, "Reading file '%s', from position %zd with length %zd", procfile_filename(ff), s, (ssize_t)(ff->size - s));
288 r = read(ff->fd, &ff->data[s], ff->size - s);
289 if(unlikely(r == -1)) {
290 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", procfile_filename(ff));
298 // debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
299 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
300 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
305 pflines_reset(ff->lines);
306 pfwords_reset(ff->words);
309 if(unlikely(procfile_adaptive_initial_allocation)) {
310 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
311 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
312 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
315 // debug(D_PROCFILE, "File '%s' updated.", ff->filename);
319 static inline void procfile_set_separators(procfile *ff, const char *separators) {
320 static PF_CHAR_TYPE def[256];
321 static char initilized = 0;
323 if(unlikely(!initilized)) {
324 // this is thread safe
325 // if initialized is zero, multiple threads may be executing
326 // this code at the same time, setting in def[] the exact same values
329 if(unlikely(i == '\n' || i == '\r'))
330 def[i] = PF_CHAR_IS_NEWLINE;
332 else if(unlikely(isspace(i) || !isprint(i)))
333 def[i] = PF_CHAR_IS_SEPARATOR;
336 def[i] = PF_CHAR_IS_WORD;
343 PF_CHAR_TYPE *ffs = ff->separators, *ffd = def, *ffe = &def[256];
347 // set the separators
348 if(unlikely(!separators))
349 separators = " \t=|";
351 ffs = ff->separators;
352 const char *s = separators;
354 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
357 void procfile_set_quotes(procfile *ff, const char *quotes) {
358 PF_CHAR_TYPE *ffs = ff->separators;
363 if(unlikely(ffs[i] == PF_CHAR_IS_QUOTE))
364 ffs[i] = PF_CHAR_IS_WORD;
366 // if nothing given, return
367 if(unlikely(!quotes || !*quotes))
371 const char *s = quotes;
373 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
376 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
377 PF_CHAR_TYPE *ffs = ff->separators;
379 // remove all open/close
382 if(unlikely(ffs[i] == PF_CHAR_IS_OPEN || ffs[i] == PF_CHAR_IS_CLOSE))
383 ffs[i] = PF_CHAR_IS_WORD;
385 // if nothing given, return
386 if(unlikely(!open || !*open || !close || !*close))
390 const char *s = open;
392 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
397 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
400 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
401 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
403 int fd = open(filename, O_RDONLY, 0666);
404 if(unlikely(fd == -1)) {
405 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
409 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
410 procfile *ff = mallocz(sizeof(procfile) + size);
412 //strncpyz(ff->filename, filename, FILENAME_MAX);
413 ff->filename[0] = '\0';
420 ff->lines = pflines_new();
421 ff->words = pfwords_new();
423 procfile_set_separators(ff, separators);
425 debug(D_PROCFILE, "File '%s' opened.", filename);
429 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
430 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
432 if(likely(ff->fd != -1)) close(ff->fd);
434 ff->fd = open(filename, O_RDONLY, 0666);
435 if(unlikely(ff->fd == -1)) {
440 //strncpyz(ff->filename, filename, FILENAME_MAX);
441 ff->filename[0] = '\0';
445 // do not do the separators again if NULL is given
446 if(likely(separators)) procfile_set_separators(ff, separators);
451 // ----------------------------------------------------------------------------
452 // example parsing of procfile data
454 void procfile_print(procfile *ff) {
455 size_t lines = procfile_lines(ff), l;
458 debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", procfile_filename(ff), ff->lines->len, ff->words->len);
460 for(l = 0; likely(l < lines) ;l++) {
461 size_t words = procfile_linewords(ff, l);
463 debug(D_PROCFILE, " line %zu starts at word %zu and has %zu words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
466 for(w = 0; likely(w < words) ;w++) {
467 s = procfile_lineword(ff, l, w);
468 debug(D_PROCFILE, " [%zu.%zu] '%s'", l, w, s);