14 #include <sys/types.h>
22 #define PF_PREFIX "PROCFILE"
24 #define PFWORDS_INCREASE_STEP 200
25 #define PFLINES_INCREASE_STEP 10
26 #define PROCFILE_INCREMENT_BUFFER 512
28 int procfile_adaptive_initial_allocation = 0;
30 // if adaptive allocation is set, these store the
31 // max values we have seen so far
32 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
33 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
34 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
36 // ----------------------------------------------------------------------------
40 pfwords *pfwords_add(pfwords *fw, char *str) {
41 // debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
43 if(unlikely(fw->len == fw->size)) {
44 // debug(D_PROCFILE, PF_PREFIX ": expanding words");
46 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
48 error(PF_PREFIX ": failed to expand words");
53 fw->size += PFWORDS_INCREASE_STEP;
56 fw->words[fw->len++] = str;
61 pfwords *pfwords_new(void) {
62 // debug(D_PROCFILE, PF_PREFIX ": initializing words");
64 uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
66 pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
67 if(unlikely(!new)) return NULL;
74 void pfwords_reset(pfwords *fw) {
75 // debug(D_PROCFILE, PF_PREFIX ": reseting words");
79 void pfwords_free(pfwords *fw) {
80 // debug(D_PROCFILE, PF_PREFIX ": freeing words");
86 // ----------------------------------------------------------------------------
89 pflines *pflines_add(pflines *fl, uint32_t first_word) {
90 // debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
92 if(unlikely(fl->len == fl->size)) {
93 // debug(D_PROCFILE, PF_PREFIX ": expanding lines");
95 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
97 error(PF_PREFIX ": failed to expand lines");
102 fl->size += PFLINES_INCREASE_STEP;
105 fl->lines[fl->len].words = 0;
106 fl->lines[fl->len++].first = first_word;
111 pflines *pflines_new(void) {
112 // debug(D_PROCFILE, PF_PREFIX ": initializing lines");
114 uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
116 pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
117 if(unlikely(!new)) return NULL;
124 void pflines_reset(pflines *fl) {
125 // debug(D_PROCFILE, PF_PREFIX ": reseting lines");
130 void pflines_free(pflines *fl) {
131 // debug(D_PROCFILE, PF_PREFIX ": freeing lines");
137 // ----------------------------------------------------------------------------
140 #define PF_CHAR_IS_SEPARATOR ' '
141 #define PF_CHAR_IS_NEWLINE 'N'
142 #define PF_CHAR_IS_WORD 'W'
144 void procfile_close(procfile *ff) {
145 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
147 if(likely(ff->lines)) pflines_free(ff->lines);
148 if(likely(ff->words)) pfwords_free(ff->words);
150 if(likely(ff->fd != -1)) close(ff->fd);
154 procfile *procfile_parser(procfile *ff) {
155 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
157 char *s = ff->data, *e = ff->data, *t = ff->data;
158 uint32_t l = 0, w = 0;
161 ff->lines = pflines_add(ff->lines, w);
162 if(unlikely(!ff->lines)) goto cleanup;
164 while(likely(s < e)) {
165 switch(ff->separators[(int)(*s)]) {
166 case PF_CHAR_IS_SEPARATOR:
168 // skip all leading white spaces
176 ff->words = pfwords_add(ff->words, t);
177 if(unlikely(!ff->words)) goto cleanup;
179 ff->lines->lines[l].words++;
185 case PF_CHAR_IS_NEWLINE:
189 ff->words = pfwords_add(ff->words, t);
190 if(unlikely(!ff->words)) goto cleanup;
192 ff->lines->lines[l].words++;
195 // debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
197 ff->lines = pflines_add(ff->lines, w);
198 if(unlikely(!ff->lines)) goto cleanup;
212 if(likely(ff->len < ff->size)) *s = '\0';
214 // we are going to loose the last byte
215 ff->data[ff->size - 1] = '\0';
218 ff->words = pfwords_add(ff->words, t);
219 if(unlikely(!ff->words)) goto cleanup;
221 ff->lines->lines[l].words++;
228 error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
233 procfile *procfile_readall(procfile *ff) {
234 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
236 ssize_t s = 0, r = ff->size, x = ff->size;
239 while(likely(r == x)) {
241 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
243 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
245 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
250 ff->size += PROCFILE_INCREMENT_BUFFER;
251 x = PROCFILE_INCREMENT_BUFFER;
254 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
255 r = read(ff->fd, &ff->data[s], ff->size - s);
256 if(unlikely(r == -1)) {
257 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
266 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
267 if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
268 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
273 pflines_reset(ff->lines);
274 pfwords_reset(ff->words);
276 ff = procfile_parser(ff);
278 if(unlikely(procfile_adaptive_initial_allocation)) {
279 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
280 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
281 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
284 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
288 static void procfile_set_separators(procfile *ff, const char *separators) {
289 static char def[256] = { [0 ... 255] = 0 };
292 if(unlikely(!def[255])) {
293 // this is thread safe
294 // we check that the last byte is non-zero
295 // if it is zero, multiple threads may be executing this at the same time
296 // setting in def[] the exact same values
297 for(i = 0; likely(i < 256) ;i++) {
298 if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
299 else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
300 else def[i] = PF_CHAR_IS_WORD;
305 char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
306 while(likely(ffd != ffe)) *ffs++ = *ffd++;
308 // set the separators
309 if(unlikely(!separators)) separators = " \t=|";
310 ffs = ff->separators;
311 const char *s = separators;
312 while(likely(*s)) ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
315 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
316 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
318 int fd = open(filename, O_RDONLY, 0666);
319 if(unlikely(fd == -1)) {
320 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
324 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
325 procfile *ff = malloc(sizeof(procfile) + size);
327 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
332 strncpy(ff->filename, filename, FILENAME_MAX);
333 ff->filename[FILENAME_MAX] = '\0';
340 ff->lines = pflines_new();
341 ff->words = pfwords_new();
343 if(unlikely(!ff->lines || !ff->words)) {
344 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
349 procfile_set_separators(ff, separators);
351 debug(D_PROCFILE, "File '%s' opened.", filename);
355 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
356 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
358 if(likely(ff->fd != -1)) close(ff->fd);
360 ff->fd = open(filename, O_RDONLY, 0666);
361 if(unlikely(ff->fd == -1)) {
366 strncpy(ff->filename, filename, FILENAME_MAX);
367 ff->filename[FILENAME_MAX] = '\0';
371 // do not do the separators again if NULL is given
372 if(likely(separators)) procfile_set_separators(ff, separators);
377 // ----------------------------------------------------------------------------
378 // example parsing of procfile data
380 void procfile_print(procfile *ff) {
381 uint32_t lines = procfile_lines(ff), l;
385 debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
387 for(l = 0; likely(l < lines) ;l++) {
388 words = procfile_linewords(ff, l);
390 debug(D_PROCFILE, " line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
392 for(w = 0; likely(w < words) ;w++) {
393 s = procfile_lineword(ff, l, w);
394 debug(D_PROCFILE, " [%d.%d] '%s'", l, w, s);