14 #include <sys/types.h>
21 #define PF_PREFIX "PROCFILE"
23 // ----------------------------------------------------------------------------
26 #define PFWORDS_INCREASE_STEP 200
28 pfwords *pfwords_add(pfwords *fw, char *str) {
29 debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
31 if(fw->len == fw->size) {
32 debug(D_PROCFILE, PF_PREFIX ": expanding words");
34 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
36 error(PF_PREFIX ": failed to expand words");
41 fw->size += PFWORDS_INCREASE_STEP;
44 fw->words[fw->len++] = str;
49 pfwords *pfwords_new(void) {
50 debug(D_PROCFILE, PF_PREFIX ": initializing words");
52 pfwords *new = malloc(sizeof(pfwords) + PFWORDS_INCREASE_STEP * sizeof(char *));
56 new->size = PFWORDS_INCREASE_STEP;
60 void pfwords_reset(pfwords *fw) {
61 debug(D_PROCFILE, PF_PREFIX ": reseting words");
65 void pfwords_free(pfwords *fw) {
66 debug(D_PROCFILE, PF_PREFIX ": freeing words");
72 // ----------------------------------------------------------------------------
75 #define PFLINES_INCREASE_STEP 10
77 pflines *pflines_add(pflines *fl, uint32_t first_word) {
78 debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
80 if(fl->len == fl->size) {
81 debug(D_PROCFILE, PF_PREFIX ": expanding lines");
83 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
85 error(PF_PREFIX ": failed to expand lines");
90 fl->size += PFLINES_INCREASE_STEP;
93 fl->lines[fl->len].words = 0;
94 fl->lines[fl->len++].first = first_word;
99 pflines *pflines_new(void) {
100 debug(D_PROCFILE, PF_PREFIX ": initializing lines");
102 pflines *new = malloc(sizeof(pflines) + PFLINES_INCREASE_STEP * sizeof(ffline));
103 if(!new) return NULL;
106 new->size = PFLINES_INCREASE_STEP;
110 void pflines_reset(pflines *fl) {
111 debug(D_PROCFILE, PF_PREFIX ": reseting lines");
116 void pflines_free(pflines *fl) {
117 debug(D_PROCFILE, PF_PREFIX ": freeing lines");
123 // ----------------------------------------------------------------------------
126 #define PROCFILE_INITIAL_BUFFER 256
127 #define PROCFILE_INCREMENT_BUFFER 512
129 #define PF_CHAR_IS_SEPARATOR 0
130 #define PF_CHAR_IS_NEWLINE 1
131 #define PF_CHAR_IS_WORD 2
133 void procfile_close(procfile *ff) {
134 debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'. Reason: %s", ff->filename, strerror(errno));
136 if(ff->lines) pflines_free(ff->lines);
137 if(ff->words) pfwords_free(ff->words);
139 if(ff->fd != -1) close(ff->fd);
143 procfile *procfile_parser(procfile *ff) {
144 debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
146 char *s = ff->data, *e = ff->data, *t = ff->data;
147 uint32_t l = 0, w = 0;
150 ff->lines = pflines_add(ff->lines, w);
151 if(!ff->lines) goto cleanup;
154 switch(ff->separators[(int)(*s)]) {
155 case PF_CHAR_IS_SEPARATOR:
157 // skip all leading white spaces
165 ff->words = pfwords_add(ff->words, t);
166 if(!ff->words) goto cleanup;
168 ff->lines->lines[l].words++;
174 case PF_CHAR_IS_NEWLINE:
178 ff->words = pfwords_add(ff->words, t);
179 if(!ff->words) goto cleanup;
181 ff->lines->lines[l].words++;
184 debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
186 ff->lines = pflines_add(ff->lines, w);
187 if(!ff->lines) goto cleanup;
201 if(ff->len < ff->size) *s = '\0';
203 // we are going to loose the last byte
204 ff->data[ff->size - 1] = '\0';
207 ff->words = pfwords_add(ff->words, t);
208 if(!ff->words) goto cleanup;
210 ff->lines->lines[l].words++;
217 error(PF_PREFIX ": Failed to parse file '%s'. Reason: %s", ff->filename, strerror(errno));
222 procfile *procfile_readall(procfile *ff) {
223 debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
225 ssize_t s = 0, r = ff->size, x = ff->size;
230 debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
232 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
234 error(PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s", ff->filename, strerror(errno));
239 ff->size += PROCFILE_INCREMENT_BUFFER;
240 x = PROCFILE_INCREMENT_BUFFER;
243 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
244 r = read(ff->fd, &ff->data[s], ff->size - s);
246 error(PF_PREFIX ": Cannot read from file '%s'. Reason: %s", ff->filename, strerror(errno));
255 debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
256 if(lseek(ff->fd, 0, SEEK_SET) == -1) {
257 error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
262 pflines_reset(ff->lines);
263 pfwords_reset(ff->words);
265 ff = procfile_parser(ff);
267 debug(D_PROCFILE, "File '%s' updated.", ff->filename);
271 procfile *procfile_open(const char *filename, const char *separators) {
272 debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
274 int fd = open(filename, O_RDONLY, 0666);
276 error(PF_PREFIX ": Cannot open file '%s'. Reason: %s", filename, strerror(errno));
280 procfile *ff = malloc(sizeof(procfile) + PROCFILE_INITIAL_BUFFER);
282 error(PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s", filename, strerror(errno));
287 strncpy(ff->filename, filename, FILENAME_MAX);
288 ff->filename[FILENAME_MAX] = '\0';
291 ff->size = PROCFILE_INITIAL_BUFFER;
294 ff->lines = pflines_new();
295 ff->words = pfwords_new();
297 if(!ff->lines || !ff->words) {
298 error(PF_PREFIX ": Cannot initialize parser for file '%s'. Reason: %s", filename, strerror(errno));
304 for(i = 0; i < 256 ;i++) {
305 if(i == '\n' || i == '\r') ff->separators[i] = PF_CHAR_IS_NEWLINE;
306 else if(isspace(i) || !isprint(i)) ff->separators[i] = PF_CHAR_IS_SEPARATOR;
307 else ff->separators[i] = PF_CHAR_IS_WORD;
310 if(!separators) separators = " \t=|";
311 const char *s = separators;
312 while(*s) ff->separators[(int)*s++] = PF_CHAR_IS_SEPARATOR;
314 debug(D_PROCFILE, "File '%s' opened.", filename);
319 // ----------------------------------------------------------------------------
320 // example parsing of procfile data
322 void procfile_print(procfile *ff) {
323 uint32_t lines = procfile_lines(ff), l;
327 debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
329 for(l = 0; l < lines ;l++) {
330 words = procfile_linewords(ff, l);
332 debug(D_PROCFILE, " line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
334 for(w = 0; w < words ;w++) {
335 s = procfile_lineword(ff, l, w);
336 debug(D_PROCFILE, " [%d.%d] '%s'", l, w, s);