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 #define PF_CHAR_IS_SEPARATOR ' '
131 #define PF_CHAR_IS_NEWLINE 'N'
132 #define PF_CHAR_IS_WORD 'W'
133 #define PF_CHAR_IS_QUOTE 'Q'
134 #define PF_CHAR_IS_OPEN 'O'
135 #define PF_CHAR_IS_CLOSE 'C'
137 void procfile_close(procfile *ff) {
138 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", procfile_filename(ff));
140 if(likely(ff->lines)) pflines_free(ff->lines);
141 if(likely(ff->words)) pfwords_free(ff->words);
143 if(likely(ff->fd != -1)) close(ff->fd);
147 static inline void procfile_parser(procfile *ff) {
148 // debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
150 register char *s = ff->data, *e = &ff->data[ff->len], *t = ff->data;
151 register char *separators = ff->separators;
153 size_t l = 0, w = 0, opened = 0;
155 ff->lines = pflines_add(ff->lines, w);
157 while(likely(s < e)) {
158 // we are not at the end
160 switch(separators[(unsigned char)(*s)]) {
161 case PF_CHAR_IS_OPEN:
174 case PF_CHAR_IS_CLOSE:
180 ff->words = pfwords_add(ff->words, t);
181 ff->lines->lines[l].words++;
193 case PF_CHAR_IS_QUOTE:
194 if(unlikely(!quote && s == t)) {
195 // quote opened at the beginning
199 else if(unlikely(quote && quote == *s)) {
204 ff->words = pfwords_add(ff->words, t);
205 ff->lines->lines[l].words++;
214 case PF_CHAR_IS_SEPARATOR:
215 if(unlikely(quote || opened)) {
216 // we are inside a quote
221 if(unlikely(s == t)) {
222 // skip all leading white spaces
230 ff->words = pfwords_add(ff->words, t);
231 ff->lines->lines[l].words++;
237 case PF_CHAR_IS_NEWLINE:
241 ff->words = pfwords_add(ff->words, t);
242 ff->lines->lines[l].words++;
245 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
247 ff->lines = pflines_add(ff->lines, w);
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 ff->lines->lines[l].words++;
273 procfile *procfile_readall(procfile *ff) {
274 // debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
276 ff->len = 0; // zero the used size
277 ssize_t r = 1; // read at least once
280 ssize_t x = ff->size - s;
283 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", procfile_filename(ff));
284 ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
285 ff->size += PROCFILE_INCREMENT_BUFFER;
288 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %lu", procfile_filename(ff), s, ff->size - s);
289 r = read(ff->fd, &ff->data[s], ff->size - s);
290 if(unlikely(r == -1)) {
291 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", procfile_filename(ff));
299 // debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
300 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
301 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
306 pflines_reset(ff->lines);
307 pfwords_reset(ff->words);
310 if(unlikely(procfile_adaptive_initial_allocation)) {
311 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
312 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
313 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
316 // debug(D_PROCFILE, "File '%s' updated.", ff->filename);
320 static void procfile_set_separators(procfile *ff, const char *separators) {
321 static char def[256] = { [0 ... 255] = 0 };
323 if(unlikely(!def[255])) {
324 // this is thread safe
325 // we check that the last byte is non-zero
326 // if it is zero, multiple threads may be executing this at the same time
327 // setting in def[] the exact same values
329 for(i = 0; likely(i < 256) ;i++) {
330 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
331 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
332 else def[i] = PF_CHAR_IS_WORD;
337 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
338 while(likely(ffd != ffe)) *ffs++ = *ffd++;
340 // set the separators
341 if(unlikely(!separators))
342 separators = " \t=|";
344 ffs = ff->separators;
345 const char *s = separators;
347 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
350 void procfile_set_quotes(procfile *ff, const char *quotes) {
353 for(i = 0; i < 256 ; i++)
354 if(unlikely(ff->separators[i] == PF_CHAR_IS_QUOTE))
355 ff->separators[i] = PF_CHAR_IS_WORD;
357 // if nothing given, return
358 if(unlikely(!quotes || !*quotes))
362 char *ffs = ff->separators;
363 const char *s = quotes;
365 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
368 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
369 // remove all open/close
371 for(i = 0; i < 256 ; i++)
372 if(unlikely(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE))
373 ff->separators[i] = PF_CHAR_IS_WORD;
375 // if nothing given, return
376 if(unlikely(!open || !*open || !close || !*close))
380 char *ffs = ff->separators;
381 const char *s = open;
383 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
387 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
390 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
391 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
393 int fd = open(filename, O_RDONLY, 0666);
394 if(unlikely(fd == -1)) {
395 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
399 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
400 procfile *ff = mallocz(sizeof(procfile) + size);
402 //strncpyz(ff->filename, filename, FILENAME_MAX);
403 ff->filename[0] = '\0';
410 ff->lines = pflines_new();
411 ff->words = pfwords_new();
413 procfile_set_separators(ff, separators);
415 debug(D_PROCFILE, "File '%s' opened.", filename);
419 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
420 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
422 if(likely(ff->fd != -1)) close(ff->fd);
424 ff->fd = open(filename, O_RDONLY, 0666);
425 if(unlikely(ff->fd == -1)) {
430 //strncpyz(ff->filename, filename, FILENAME_MAX);
431 ff->filename[0] = '\0';
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 size_t lines = procfile_lines(ff), l;
448 debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", procfile_filename(ff), ff->lines->len, ff->words->len);
450 for(l = 0; likely(l < lines) ;l++) {
451 size_t words = procfile_linewords(ff, l);
453 debug(D_PROCFILE, " line %zu starts at word %zu and has %zu 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, " [%zu.%zu] '%s'", l, w, s);