/** * Program dispatch; get a line of input, and call the appropriate * command function. */ int main( int argc, char** argv ) { #if DEBUG printf( "Program executed with %d arguments: \n", argc-1 ); for ( int i = 1; i < argc; i++ ) { printf( "Argument: %s \n", argv[i] ); } printf( "\n" ); #endif // Check for -v argument char* verbose_arg = "-v"; for ( int i = 1; i < argc; i++ ) { if ( strcmp( verbose_arg, argv[i] ) == 0 ) { verbose = 1; } } // Initialize database and setup error function db_database* db = db_init(); database = db; atexit( cleanup_db ); // Main loop, get input and process it line by line char input[ LINE_SIZE ]; // input buffer char* tokens[ ARGS_SIZE ]; // array of pointers into input buffer int count; // count of input tokens char* command; // the command string while ( ( count = tokenize_input( input, tokens ) ) >= 0 ) { // Dispatch processing based on the inputted command command = tokens[0]; if ( strcmp( "student", command ) == 0 ) { db_new_student( db, tokens[1], tokens + 2 ); } else if ( strcmp( "open", command ) == 0 ) { db_new_course( db, tokens[1], atoi( tokens[2] ) ); } else if ( strcmp( "cancel", command ) == 0 ) { db_cancel_course( db, tokens[1] ); } else if ( strcmp( "enroll", command ) == 0 ) { db_enroll_student( db, tokens[1], tokens[2] ); } else if ( strcmp( "withdraw", command ) == 0 ) { db_withdraw_student( db, tokens[1], tokens[2] ); } } // Done processing input db_dump( db ); db_destroy( db ); database = NULL; return 0; }
void Node_Value_Parser::give_input(const std::string& i, Node_Value& nv, Moldable* bc) { input = i; if (input.empty()) { nv.set_string(""); return; } tokenize_input(input); reduce_expressions(bc); /* As long as we have input that is reducable, keep reducing it.*/ while (Mogu_Syntax::TOKEN_DELIM == tm.current_token() && (!tm.fetch_string().empty()) && (tm.fetch_string().at(0)!='"') && (tm.fetch_string().find_first_of(" ") != std::string::npos)) { std::string new_input = tm.fetch_string(); if (input!=new_input) { tm.reset(); input = new_input; tokenize_input(input); reduce_expressions(bc); } else break; } //if we have more than one token in numTokens at this point (or //two if the first token is TOKEN DELIM), something has gone wrong if(tm.current_token() == Mogu_Syntax::TOKEN_DELIM) nv.set_string(tm.fetch_string()); else nv.set_int((int)tm.current_token()); tm.reset(); // Clear the TokenManager to prepare for the next input }
void Node_Value_Parser::give_input(const std::string& i, Command_Value& cv, Moldable* bc) { Node_Value tmp {}; input = i; tokenize_input(input, true); // Make sure to return the iterator to // the BEGINNING // The first token is always an action cv.set(Command_Flags::action, tm.current_token()); if (Mogu_Syntax::append == cv.get(Command_Flags::action)) { handle_append_command(cv,bc); tm.reset(); return; } // The second token is awlays the start of the object set. tm.next(); int tok = tm.current_token(); cv.set(Command_Flags::object, tok); tm.next(); while (tok != Mogu_Syntax::OUT_OF_RANGE_END) { tok = tm.current_token(); if (tok == Mogu_Syntax::TOKEN_DELIM) { std::string string_token = tm.fetch_string(); if (tm.is_quoted_string(string_token)) { tm.reset(); return; } /* The identifier should always be the first TOKEN_DELIM * encountered. */ if (cv.get_identifier().empty()) { Node_Value tmp {}; tmp.set_string(string_token); cv.set(Command_Flags::identifier, tmp); } else { /* The only other TOKEN_DELIM that should be encountered before * a preposition should be an argument (ie: database hash field) */ tmp.set_string(string_token); cv.set(Command_Flags::arg, tmp); } } else if (is_state_token(tok)) { tmp.set_int(tok); cv.set(Command_Flags::arg, tmp); } else if (is_preposition_token(tok)) { bool reduce {true}; /* First, check to see if the next value is 'location' */ tm.next(); int tok = tm.current_token(); if (tm.current_token() == Mogu_Syntax::location) { reduce = false; tm.next(); tok = tm.current_token(); } std::stringstream buf; while (tok != Mogu_Syntax::OUT_OF_RANGE_END) { if (tok == Mogu_Syntax::TOKEN_DELIM) buf << tm.fetch_string(); else buf << tok; buf << " "; tm.next(); tok = tm.current_token(); } std::string s{buf.str()}; Node_Value r {}; if (reduce) { Node_Value_Parser p {}; p.set_user_id(user_id); p.set_group_id(group_id); p.give_input(s,r,bc); } else { r.set_string(s); } cv.set(Command_Flags::value, r); break; // After parsing a prepositional, there is nothing left to do. } tm.next(); } tm.reset(); }