static void _BlockLocator_mark_thin(BlockLocator *self) { #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // Step on thin ice, if it breaks, it breaks here if (self->thin != NULL && _strip(self->thin, self->codestr_ptr, NULL)) { self->init = self->thin; self->thin = self->codestr_ptr + 1; } else if (self->thin == NULL && _strip(self->safe, self->codestr_ptr, NULL)) { self->thin = self->codestr_ptr + 1; } }
static void _BlockLocator_end_block1(BlockLocator *self) { int len, lineno = -1; Py_UNICODE *block_start = NULL; #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // Block ends: self->depth--; if (!self->skip) { self->end = self->codestr_ptr; len = _strip(self->init, self->start, &lineno, &block_start); self->block.selprop = block_start; self->block.selprop_sz = len; self->block.codestr = (self->start + 1); self->block.codestr_sz = (int)(self->end - (self->start + 1)); self->block.lineno = _BlockLocator_pop_lineno(self); self->block.error = 1; self->init = self->lose = self->end + 1; } self->skip = 0; }
static void _BlockLocator_end_property(BlockLocator *self) { int len, lineno = -1; #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // End of property (or block): self->init = self->codestr_ptr; if (self->lose <= self->init) { len = _strip(self->lose, self->init, &lineno); if (len) { if (lineno != -1) { self->lineno = lineno; } self->block.selprop = self->lose; self->block.selprop_sz = len; self->block.codestr = NULL; self->block.codestr_sz = 0; self->block.lineno = self->lineno; self->block.error = 1; } self->init = self->safe = self->lose = self->codestr_ptr + 1; } self->thin = NULL; }
static void _BlockLocator_end_block1(BlockLocator *self) { int len, lineno = -1; #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // Block ends: self->depth--; if (!self->skip) { self->end = self->codestr_ptr; len = _strip(self->init, self->start, &lineno); if (lineno != -1) { self->lineno = lineno; } self->block.selprop = self->init; self->block.selprop_sz = len; self->block.codestr = (self->start + 1); self->block.codestr_sz = (int)(self->end - (self->start + 1)); self->block.lineno = self->lineno; self->block.error = 1; self->init = self->safe = self->lose = self->end + 1; self->thin = NULL; } self->skip = 0; }
static void _BlockLocator_flush_properties(BlockLocator *self) { int len, lineno = -1; #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // Flush properties if (self->lose <= self->init) { len = _strip(self->lose, self->init, &lineno); if (len) { if (lineno != -1) { self->lineno = lineno; } self->block.selprop = self->lose; self->block.selprop_sz = len; self->block.codestr = NULL; self->block.codestr_sz = 0; self->block.lineno = self->lineno; self->block.error = 1; } self->lose = self->init; } }
static void _BlockLocator_mark_safe(BlockLocator *self) { #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // We are on a safe zone if (self->thin != NULL && _strip(self->thin, self->codestr_ptr, NULL)) { self->init = self->thin; } self->thin = NULL; self->safe = self->codestr_ptr + 1; }
static void _BlockLocator_start_block1(BlockLocator *self) { #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif // Start block: if (self->codestr_ptr > self->codestr && *(self->codestr_ptr - 1) == '#') { self->skip = 1; } else { self->start = self->codestr_ptr; if (self->thin != NULL && _strip(self->thin, self->codestr_ptr, NULL)) { self->init = self->thin; } _BlockLocator_flush_properties(self); self->thin = NULL; } self->depth++; }
struct rte_cfgfile * rte_cfgfile_load(const char *filename, int flags) { int allocated_sections = CFG_ALLOC_SECTION_BATCH; int allocated_entries = 0; int curr_section = -1; int curr_entry = -1; char buffer[256] = {0}; int lineno = 0; struct rte_cfgfile *cfg = NULL; FILE *f = fopen(filename, "r"); if (f == NULL) return NULL; cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) * allocated_sections); if (cfg == NULL) goto error2; memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections); while (fgets(buffer, sizeof(buffer), f) != NULL) { char *pos = NULL; size_t len = strnlen(buffer, sizeof(buffer)); lineno++; if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) { printf("Error line %d - no \\n found on string. " "Check if line too long\n", lineno); goto error1; } pos = memchr(buffer, ';', sizeof(buffer)); if (pos != NULL) { *pos = '\0'; len = pos - buffer; } len = _strip(buffer, len); if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL) continue; if (buffer[0] == '[') { /* section heading line */ char *end = memchr(buffer, ']', len); if (end == NULL) { printf("Error line %d - no terminating '['" "character found\n", lineno); goto error1; } *end = '\0'; _strip(&buffer[1], end - &buffer[1]); /* close off old section and add start new one */ if (curr_section >= 0) cfg->sections[curr_section]->num_entries = curr_entry + 1; curr_section++; /* resize overall struct if we don't have room for more sections */ if (curr_section == allocated_sections) { allocated_sections += CFG_ALLOC_SECTION_BATCH; struct rte_cfgfile *n_cfg = realloc(cfg, sizeof(*cfg) + sizeof(cfg->sections[0]) * allocated_sections); if (n_cfg == NULL) { printf("Error - no more memory\n"); goto error1; } cfg = n_cfg; } /* allocate space for new section */ allocated_entries = CFG_ALLOC_ENTRY_BATCH; curr_entry = -1; cfg->sections[curr_section] = malloc( sizeof(*cfg->sections[0]) + sizeof(cfg->sections[0]->entries[0]) * allocated_entries); if (cfg->sections[curr_section] == NULL) { printf("Error - no more memory\n"); goto error1; } snprintf(cfg->sections[curr_section]->name, sizeof(cfg->sections[0]->name), "%s", &buffer[1]); } else { /* value line */ if (curr_section < 0) { printf("Error line %d - value outside of" "section\n", lineno); goto error1; } struct rte_cfgfile_section *sect = cfg->sections[curr_section]; char *split[2]; if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=') != 2) { printf("Error at line %d - cannot split " "string\n", lineno); goto error1; } curr_entry++; if (curr_entry == allocated_entries) { allocated_entries += CFG_ALLOC_ENTRY_BATCH; struct rte_cfgfile_section *n_sect = realloc( sect, sizeof(*sect) + sizeof(sect->entries[0]) * allocated_entries); if (n_sect == NULL) { printf("Error - no more memory\n"); goto error1; } sect = cfg->sections[curr_section] = n_sect; } sect->entries[curr_entry] = malloc( sizeof(*sect->entries[0])); if (sect->entries[curr_entry] == NULL) { printf("Error - no more memory\n"); goto error1; } struct rte_cfgfile_entry *entry = sect->entries[ curr_entry]; snprintf(entry->name, sizeof(entry->name), "%s", split[0]); snprintf(entry->value, sizeof(entry->value), "%s", split[1]); _strip(entry->name, strnlen(entry->name, sizeof(entry->name))); _strip(entry->value, strnlen(entry->value, sizeof(entry->value))); } } fclose(f); cfg->flags = flags; cfg->num_sections = curr_section + 1; /* curr_section will still be -1 if we have an empty file */ if (curr_section >= 0) cfg->sections[curr_section]->num_entries = curr_entry + 1; return cfg; error1: rte_cfgfile_close(cfg); error2: fclose(f); return NULL; }