16 #include <sys/types.h>
24 #define PF_PREFIX "PROCFILE"
26 #define PFWORDS_INCREASE_STEP 200
27 #define PFLINES_INCREASE_STEP 10
28 #define PROCFILE_INCREMENT_BUFFER 512
30 int procfile_adaptive_initial_allocation = 0;
32 // if adaptive allocation is set, these store the
33 // max values we have seen so far
34 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
35 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
36 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
38 // ----------------------------------------------------------------------------
42 pfwords *pfwords_add(pfwords *fw, char *str) {
43 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
45 if(unlikely(fw->len == fw->size)) {
46 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
48 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
50 error(PF_PREFIX ": failed to expand words");
55 fw->size += PFWORDS_INCREASE_STEP;
58 fw->words[fw->len++] = str;
63 pfwords *pfwords_new(void) {
64 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
66 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
68 pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
69 if(unlikely(!new)) return NULL;
76 void pfwords_reset(pfwords *fw) {
77 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
81 void pfwords_free(pfwords *fw) {
82 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
88 // ----------------------------------------------------------------------------
91 pflines *pflines_add(pflines *fl, uint32_t first_word) {
92 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
94 if(unlikely(fl->len == fl->size)) {
95 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
97 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
99 error(PF_PREFIX ": failed to expand lines");
104 fl->size += PFLINES_INCREASE_STEP;
107 fl->lines[fl->len].words = 0;
108 fl->lines[fl->len++].first = first_word;
113 pflines *pflines_new(void) {
114 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
116 uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
118 pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
119 if(unlikely(!new)) return NULL;
126 void pflines_reset(pflines *fl) {
127 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
132 void pflines_free(pflines *fl) {
133 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
139 // ----------------------------------------------------------------------------
142 #define PF_CHAR_IS_SEPARATOR ' '
143 #define PF_CHAR_IS_NEWLINE 'N'
144 #define PF_CHAR_IS_WORD 'W'
146 void procfile_close(procfile *ff) {
147 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
149 if(likely(ff->lines)) pflines_free(ff->lines);
150 if(likely(ff->words)) pfwords_free(ff->words);
152 if(likely(ff->fd != -1)) close(ff->fd);
156 procfile *procfile_parser(procfile *ff) {
157 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
159 char *s = ff->data, *e = ff->data, *t = ff->data;
160 uint32_t l = 0, w = 0;
163 ff->lines = pflines_add(ff->lines, w);
164 if(unlikely(!ff->lines)) goto cleanup;
166 while(likely(s < e)) {
167 switch(ff->separators[(int)(*s)]) {
168 case PF_CHAR_IS_SEPARATOR:
170 // skip all leading white spaces
178 ff->words = pfwords_add(ff->words, t);
179 if(unlikely(!ff->words)) goto cleanup;
181 ff->lines->lines[l].words++;
187 case PF_CHAR_IS_NEWLINE:
191 ff->words = pfwords_add(ff->words, t);
192 if(unlikely(!ff->words)) goto cleanup;
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);
200 if(unlikely(!ff->lines)) goto cleanup;
214 if(likely(ff->len < ff->size)) *s = '\0';
216 // we are going to loose the last byte
217 ff->data[ff->size - 1] = '\0';
220 ff->words = pfwords_add(ff->words, t);
221 if(unlikely(!ff->words)) goto cleanup;
223 ff->lines->lines[l].words++;
230 error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
235 procfile *procfile_readall(procfile *ff) {
236 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
241 while(likely(r > 0)) {
246 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
248 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
250 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
255 ff->size += PROCFILE_INCREMENT_BUFFER;
258 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
259 r = read(ff->fd, &ff->data[s], ff->size - s);
260 if(unlikely(r == -1)) {
261 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
269 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
270 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
271 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
276 pflines_reset(ff->lines);
277 pfwords_reset(ff->words);
279 ff = procfile_parser(ff);
281 if(unlikely(procfile_adaptive_initial_allocation)) {
282 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
283 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
284 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
287 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
291 static void procfile_set_separators(procfile *ff, const char *separators) {
292 static char def[256] = { [0 ... 255] = 0 };
295 if(unlikely(!def[255])) {
296 // this is thread safe
297 // we check that the last byte is non-zero
298 // if it is zero, multiple threads may be executing this at the same time
299 // setting in def[] the exact same values
300 for(i = 0; likely(i < 256) ;i++) {
301 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
302 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
303 else def[i] = PF_CHAR_IS_WORD;
308 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
309 while(likely(ffd != ffe)) *ffs++ = *ffd++;
311 // set the separators
312 if(unlikely(!separators)) separators = " \t=|";
313 ffs = ff->separators;
314 const char *s = separators;
315 while(likely(*s)) ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
318 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
319 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
321 int fd = open(filename, O_RDONLY, 0666);
322 if(unlikely(fd == -1)) {
323 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
327 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
328 procfile *ff = malloc(sizeof(procfile) + size);
330 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
335 strncpy(ff->filename, filename, FILENAME_MAX);
336 ff->filename[FILENAME_MAX] = '\0';
343 ff->lines = pflines_new();
344 ff->words = pfwords_new();
346 if(unlikely(!ff->lines || !ff->words)) {
347 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
352 procfile_set_separators(ff, separators);
354 debug(D_PROCFILE, "File '%s' opened.", filename);
358 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
359 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
361 if(likely(ff->fd != -1)) close(ff->fd);
363 ff->fd = open(filename, O_RDONLY, 0666);
364 if(unlikely(ff->fd == -1)) {
369 strncpy(ff->filename, filename, FILENAME_MAX);
370 ff->filename[FILENAME_MAX] = '\0';
374 // do not do the separators again if NULL is given
375 if(likely(separators)) procfile_set_separators(ff, separators);
380 // ----------------------------------------------------------------------------
381 // example parsing of procfile data
383 void procfile_print(procfile *ff) {
384 uint32_t lines = procfile_lines(ff), l;
388 debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
390 for(l = 0; likely(l < lines) ;l++) {
391 words = procfile_linewords(ff, l);
393 debug(D_PROCFILE, " line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
395 for(w = 0; likely(w < words) ;w++) {
396 s = procfile_lineword(ff, l, w);
397 debug(D_PROCFILE, " [%d.%d] '%s'", l, w, s);