std::vector<std::string *> SQL_ROW::parse_delimited(std::string &s,char c_start,char c_end) { // parse a section of a comma separated string delimited by the characters // c_start and c_end. The substring parsed will include everything after // c_start and before the comma or end of string following c_end. This allows // sql types of the format "ROW(a,b,c)::type_name" to be properly parsed // to return a,b and c. std::string *sub(string_delimited(s,c_start,c_end)); // Get delimited string std::string tmp(*sub,1,sub->size()-1); // Remove opening delimiter std::vector<std::string *> rv(parse_row(tmp)); delete sub; #ifdef DEBUG_ALLOCATIONS fprintf(stderr,"std::string deleted at 0x%p\n",sub); fflush(stderr); #endif return rv; }
SQL_ROW::SQL_ROW(const std::string *s,size_t nfields) : track_mem<SQL_ROW>("SQL_ROW") { if (s) { std::string tmp(*s); pval=parse_row(tmp); } else { pval.clear(); for (size_t i=0;i<nfields;i++) { pval.push_back(new std::string(empty_string)); #ifdef DEBUG_ALLOCATIONS fprintf(stderr,"std::string allocated at 0x%p\n",pval[i]); fflush(stderr); #endif } } }
struct List parse_db(char *path) { struct List entries = list_init(); char row[1024]; FILE *db = fopen(path, "r"); if (db == NULL) { perror ("Error opening database"); exit(-1); } while(!feof(db)) { if (fgets(row, 1024, db) && *row != '#') { /* ignore comments */ list_append(&entries, parse_row(row)); } } fclose(db); return entries; }
// taken from ex4_readline.c // adapted to special needs LinearProgram *new_lp_from_file(const char* filename) { assert(NULL != filename); assert(0 < strlen(filename)); int rows = 0; int cols; char* end_ptr = NULL; LinearProgram* lp = NULL; /* counts the constraint that were read from file * if constraints < rows -> error * */ int constraints = 0; /* used to mark distinguish the current state of the parser */ int parser_state = READ_COLS; FILE* fp; char buf[MAX_LINE_LEN]; char* s; int lines = 0; if (NULL == (fp = fopen(filename, "r"))) { fprintf(stderr, "Can't open file %s\n", filename); return NULL; } while(NULL != (s = fgets(buf, sizeof(buf), fp))) { char* t = strpbrk(s, "#\n\r"); lines++; if (NULL != t) /* else line is not terminated or too long */ *t = '\0'; /* clip comment or newline */ /* Skip over leading space */ s = skip_spaces(s); /* Skip over empty lines */ if (!*s) { /* <=> (*s == '\0') */ continue; } /* line is nonempty, so try to parse data */ if (parser_state == READ_COLS) { cols = (int) strtol(s, &end_ptr, 10); if (cols <= 0) { fprintf(stderr, "please specify a positive number of cols.\n"); goto read_error; } parser_state = READ_ROWS; } else if (parser_state == READ_ROWS) { rows = (int) strtol(s, &end_ptr, 10); if (rows <= 0) { fprintf(stderr, "please specify a positive number of rows.\n"); goto read_error; } lp = lp_new(rows, cols); parser_state = READ_CONSTRAINTS; } else { /* stop if a row does not match the format */ if (constraints >= rows) { lp_free(lp); fprintf(stderr, "too many constraints"); goto read_error; } bool valid_format = parse_row(s, constraints, lp); if (!valid_format) { lp_free(lp); goto read_error; } constraints++; } } fclose(fp); if (constraints != rows) { fprintf(stderr, "speciefied #(rows) does not match: %d expected, %d found\n", rows, constraints); return NULL; } if (can_overflow(lp)) { return NULL; } printf("%d lines\n", lines); return lp; read_error: printf("error in line %d\n", lines); fclose(fp); return NULL; }