14 #include <sys/types.h>
21 #define PF_PREFIX "PROCFILE"
23 #define PFWORDS_INCREASE_STEP 200
24 #define PFLINES_INCREASE_STEP 10
25 #define PROCFILE_INCREMENT_BUFFER 512
27 int procfile_adaptive_initial_allocation = 0;
29 // if adaptive allocation is set, these store the
30 // max values we have seen so far
31 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
32 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
33 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
35 // ----------------------------------------------------------------------------
39 pfwords *pfwords_add(pfwords *fw, char *str) {
40 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
42 if(fw->len == fw->size) {
43 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
45 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
47 error(PF_PREFIX ": failed to expand words");
52 fw->size += PFWORDS_INCREASE_STEP;
55 fw->words[fw->len++] = str;
60 pfwords *pfwords_new(void) {
61 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
63 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
65 pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
73 void pfwords_reset(pfwords *fw) {
74 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
78 void pfwords_free(pfwords *fw) {
79 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
85 // ----------------------------------------------------------------------------
88 pflines *pflines_add(pflines *fl, uint32_t first_word) {
89 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
91 if(fl->len == fl->size) {
92 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
94 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
96 error(PF_PREFIX ": failed to expand lines");
101 fl->size += PFLINES_INCREASE_STEP;
104 fl->lines[fl->len].words = 0;
105 fl->lines[fl->len++].first = first_word;
110 pflines *pflines_new(void) {
111 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
113 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFLINES_INCREASE_STEP;
115 pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
116 if(!new) return NULL;
123 void pflines_reset(pflines *fl) {
124 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
129 void pflines_free(pflines *fl) {
130 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
136 // ----------------------------------------------------------------------------
139 #define PF_CHAR_IS_SEPARATOR ' '
140 #define PF_CHAR_IS_NEWLINE 'N'
141 #define PF_CHAR_IS_WORD 'W'
143 void procfile_close(procfile *ff) {
144 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
146 if(ff->lines) pflines_free(ff->lines);
147 if(ff->words) pfwords_free(ff->words);
149 if(ff->fd != -1) close(ff->fd);
153 procfile *procfile_parser(procfile *ff) {
154 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
156 char *s = ff->data, *e = ff->data, *t = ff->data;
157 uint32_t l = 0, w = 0;
160 ff->lines = pflines_add(ff->lines, w);
161 if(!ff->lines) goto cleanup;
164 switch(ff->separators[(int)(*s)]) {
165 case PF_CHAR_IS_SEPARATOR:
167 // skip all leading white spaces
175 ff->words = pfwords_add(ff->words, t);
176 if(!ff->words) goto cleanup;
178 ff->lines->lines[l].words++;
184 case PF_CHAR_IS_NEWLINE:
188 ff->words = pfwords_add(ff->words, t);
189 if(!ff->words) goto cleanup;
191 ff->lines->lines[l].words++;
194 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
196 ff->lines = pflines_add(ff->lines, w);
197 if(!ff->lines) goto cleanup;
211 if(ff->len < ff->size) *s = '\0';
213 // we are going to loose the last byte
214 ff->data[ff->size - 1] = '\0';
217 ff->words = pfwords_add(ff->words, t);
218 if(!ff->words) goto cleanup;
220 ff->lines->lines[l].words++;
227 error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
232 procfile *procfile_readall(procfile *ff) {
233 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
235 ssize_t s = 0, r = ff->size, x = ff->size;
240 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
242 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
244 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
249 ff->size += PROCFILE_INCREMENT_BUFFER;
250 x = PROCFILE_INCREMENT_BUFFER;
253 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
254 r = read(ff->fd, &ff->data[s], ff->size - s);
256 if(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO)) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
265 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
266 if(lseek(ff->fd, 0, SEEK_SET) == -1) {
267 if(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO)) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
272 pflines_reset(ff->lines);
273 pfwords_reset(ff->words);
275 ff = procfile_parser(ff);
277 if(procfile_adaptive_initial_allocation) {
278 if(ff->len > procfile_max_allocation) procfile_max_allocation = ff->len;
279 if(ff->lines->len > procfile_max_lines) procfile_max_lines = ff->lines->len;
280 if(ff->words->len > procfile_max_words) procfile_max_words = ff->words->len;
283 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
287 static void procfile_set_separators(procfile *ff, const char *separators) {
288 static char def[256] = { [0 ... 255] = 0 };
292 // this is thread safe
293 // we check that the last byte is non-zero
294 // if it is zero, multiple threads may be executing this at the same time
295 // setting in def[] the exact same values
296 for(i = 0; i < 256 ;i++) {
297 if(i == '\n' || i == '\r') def[i] = PF_CHAR_IS_NEWLINE;
298 else if(isspace(i) || !isprint(i)) def[i] = PF_CHAR_IS_SEPARATOR;
299 else def[i] = PF_CHAR_IS_WORD;
304 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
305 while(ffd != ffe) *ffs++ = *ffd++;
307 // set the separators
308 if(!separators) separators = " \t=|";
309 ffs = ff->separators;
310 const char *s = separators;
311 while(*s) ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
314 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
315 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
317 int fd = open(filename, O_RDONLY, 0666);
319 if(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO)) error(PF_PREFIX ": Cannot open file '%s'", filename);
323 size_t size = (procfile_adaptive_initial_allocation) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
324 procfile *ff = malloc(sizeof(procfile) + size);
326 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
331 strncpy(ff->filename, filename, FILENAME_MAX);
332 ff->filename[FILENAME_MAX] = '\0';
339 ff->lines = pflines_new();
340 ff->words = pfwords_new();
342 if(!ff->lines || !ff->words) {
343 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
348 procfile_set_separators(ff, separators);
350 debug(D_PROCFILE, "File '%s' opened.", filename);
354 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
355 if(!ff) return procfile_open(filename, separators, flags);
357 if(ff->fd != -1) close(ff->fd);
359 ff->fd = open(filename, O_RDONLY, 0666);
365 strncpy(ff->filename, filename, FILENAME_MAX);
366 ff->filename[FILENAME_MAX] = '\0';
370 // do not do the separators again if NULL is given
371 if(separators) procfile_set_separators(ff, separators);
376 // ----------------------------------------------------------------------------
377 // example parsing of procfile data
379 void procfile_print(procfile *ff) {
380 uint32_t lines = procfile_lines(ff), l;
384 debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
386 for(l = 0; l < lines ;l++) {
387 words = procfile_linewords(ff, l);
389 debug(D_PROCFILE, " line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
391 for(w = 0; w < words ;w++) {
392 s = procfile_lineword(ff, l, w);
393 debug(D_PROCFILE, " [%d.%d] '%s'", l, w, s);