/* Advance the current file position to beginning of next line in file buffer * FILE or to end of file. */ static void skip_line(struct file_buffer* file) { while ((current_char(file) != '\n') && (current_char(file) != EOF)) file->pos++; if (current_char(file) == '\n') file->pos++; }
/* Advance the current file pointer of file buffer FILE until the current * character is no longer a whitespace or until the end of line or file is * reached. Return 0 if at least one whitespace character was encountered, * non-zero otherwise. */ static int skip_whitespaces(struct file_buffer* file) { int rc; rc = -1; while ((current_char(file) != '\n') && isspace(current_char(file))) { rc = 0; file->pos++; } return rc; }
/* Scan for the specified STRING at the current position of file buffer FILE * and advance the position respectively. Upon success, return zero. Return * non-zero otherwise. */ static int scan_string(struct file_buffer* file, const char *string) { int i; for (i=0; string[i] && (current_char(file) == string[i]); i++, file->pos++); if (string[i] == '\0') return 0; return -1; }
/* Scan a device node name at the current position of file buffer FILE and * advance the position respectively. Upon success, return zero and set * NAME to contain a copy of the scanned name. Return non-zero otherwise. */ static int scan_name(struct file_buffer* file, char** name) { off_t start_pos; start_pos = file->pos; while (!isspace(current_char(file)) && (current_char(file) != EOF)) file->pos++; if (file->pos > start_pos) { *name = (char *) malloc(file->pos - start_pos + 1); if (*name == NULL) return -1; memcpy((void *) *name, (void *) &file->buffer[start_pos], file->pos - start_pos); (*name)[file->pos - start_pos] = 0; return 0; } else return -1; }
/* Scan a positive integer number at the current position of file buffer FILE * and advance the position respectively. Upon success, return zero and set * NUMBER to contain the scanned number. Return non-zero otherwise. */ static int scan_number(struct file_buffer* file, size_t* number) { int rc; size_t old_number; *number=0; rc = -1; while (isdigit(current_char(file))) { rc = 0; old_number = *number; *number = *number * 10 + current_char(file) - '0'; /* Check for overflow */ if (old_number > *number) { rc = -1; break; } file->pos++; } return rc; }
static void update_quote_status() { char c = current_char(); if (in_quotes == true) { if (c == starting_quote) { in_quotes = false; starting_quote = c; } } else if (a_quote(c)) { in_quotes = true; starting_quote = c; } }
static void add_current_char() { update_quote_status(); add_to_buffer(current_char()); advance_char(); }
static void eat_whitespace() { if (in_quotes == false && current_char() == SPACE && next_char() == SPACE) { advance_char(); eat_whitespace(); } }
std::vector<std::string> Parser::parse(std::string unparsed, bool &failed) { failed = false; std::vector<std::string> parsed; std::string buffer; // Strip spaces from unparsed unparsed = stripspaces(unparsed); std::string returnment; if(!isvalidated(unparsed, returnment)) { failed = true; // Ugly parsed = {returnment}; return parsed; } for(unsigned i = 0; i < unparsed.length(); i++) { std::string current_char(1, unparsed[i]); // If current char is a digit if(isdigit(unparsed[i]) || (i == 0 && current_char == "-")) { buffer += current_char; } else if(buffer.length() > 0 && current_char == ".") { buffer += current_char; } else if(buffer.length() == 0 && current_char == "-" && isdigit(unparsed[i + 1]) && !isdigit(unparsed[i - 1]) && unparsed[i - 1] != ')') { buffer += current_char; } // Is not number else { // If first char is operator; exit if(i == 0 && isoperator(unparsed[i])) { failed = true; parsed = {"Operator mismatch"}; return parsed; } // If two operators after eachother if(isoperator(unparsed[i]) && isoperator(unparsed[i+1]) && unparsed[i +1] != '-') { failed = true; parsed = {"Operator mismatch"}; return parsed; } // add numbers to parsed if(buffer.size() != 0) { parsed.push_back(buffer); buffer.clear(); } parsed.push_back(current_char); } } if(buffer.empty() && parsed.back() != ")") { failed = true; parsed = {"Operator mismatch"}; return parsed; } if(!buffer.empty()) { parsed.push_back(buffer); } return parsed; }