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);
238 ssize_t s, r = 1, x = ff->size;
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;
256 x = PROCFILE_INCREMENT_BUFFER;
259 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
260 r = read(ff->fd, &ff->data[s], ff->size - s);
261 if(unlikely(r == -1)) {
262 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
270 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
271 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
272 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
277 pflines_reset(ff->lines);
278 pfwords_reset(ff->words);
280 ff = procfile_parser(ff);
282 if(unlikely(procfile_adaptive_initial_allocation)) {
283 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
284 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
285 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
288 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
292 static void procfile_set_separators(procfile *ff, const char *separators) {
293 static char def[256] = { [0 ... 255] = 0 };
296 if(unlikely(!def[255])) {
297 // this is thread safe
298 // we check that the last byte is non-zero
299 // if it is zero, multiple threads may be executing this at the same time
300 // setting in def[] the exact same values
301 for(i = 0; likely(i < 256) ;i++) {
302 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
303 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
304 else def[i] = PF_CHAR_IS_WORD;
309 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
310 while(likely(ffd != ffe)) *ffs++ = *ffd++;
312 // set the separators
313 if(unlikely(!separators)) separators = " \t=|";
314 ffs = ff->separators;
315 const char *s = separators;
316 while(likely(*s)) ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
319 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
320 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
322 int fd = open(filename, O_RDONLY, 0666);
323 if(unlikely(fd == -1)) {
324 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
328 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
329 procfile *ff = malloc(sizeof(procfile) + size);
331 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
336 strncpy(ff->filename, filename, FILENAME_MAX);
337 ff->filename[FILENAME_MAX] = '\0';
344 ff->lines = pflines_new();
345 ff->words = pfwords_new();
347 if(unlikely(!ff->lines || !ff->words)) {
348 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
353 procfile_set_separators(ff, separators);
355 debug(D_PROCFILE, "File '%s' opened.", filename);
359 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
360 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
362 if(likely(ff->fd != -1)) close(ff->fd);
364 ff->fd = open(filename, O_RDONLY, 0666);
365 if(unlikely(ff->fd == -1)) {
370 strncpy(ff->filename, filename, FILENAME_MAX);
371 ff->filename[FILENAME_MAX] = '\0';
375 // do not do the separators again if NULL is given
376 if(likely(separators)) procfile_set_separators(ff, separators);
381 // ----------------------------------------------------------------------------
382 // example parsing of procfile data
384 void procfile_print(procfile *ff) {
385 uint32_t lines = procfile_lines(ff), l;
389 debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
391 for(l = 0; likely(l < lines) ;l++) {
392 words = procfile_linewords(ff, l);
394 debug(D_PROCFILE, " line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
396 for(w = 0; likely(w < words) ;w++) {
397 s = procfile_lineword(ff, l, w);
398 debug(D_PROCFILE, " [%d.%d] '%s'", l, w, s);