// // parser.cpp // cards-proof // // Created by Ethan Miller on 3/9/12. // #include #include #include #include #include "deck.h" #include "card.h" #include "parser.h" using namespace std; // parse_deck can be passed either a file name (cstring) // or a filestream. Either work fine. deck & parse_deck( ifstream & in ) { // First version, contain text in deck. // Will be moved out eventually. deck *new_deck = new deck; string line; while ( in ) { getline( in, line ); cout << line << endl; if (line =="") { continue; } if (line == "deck[") { continue; } if (line == "card[") { parse_card(in); } } return *new_deck; } string & get_quoted_string(string in) { // Gets the quoted string out of a string of text. string * new_string = new string; // Move to start of string istringstream s_in(in); // Eat through first " while (s_in.get() != '"') { if (s_in.eof()) { break; } } // Set new_string to be the quoted portion. while (s_in) { new_string->push_back(s_in.get()); if (s_in.peek() == '"' || s_in.eof()) { break; } } return * new_string; } deck & parse_deck( const char * filename ) { // This function allows parse_deck to be given // either a file or a filename. Both will work fine. ifstream file( filename ); deck *new_deck = new deck; new_deck = & parse_deck( file ); return *new_deck; } card & parse_card( ifstream & in ) { card *new_card = new card; cout << "Parsing a card" << endl; string parse_line = ""; string token = ""; while ( parse_line != "]" ) { getline(in, parse_line); istringstream input(parse_line); input >> token; // cout << token << endl; if (token == "Quant" || token == "Quantity") { // TODO deal with quantities continue; } new_card->set_attribute(token, get_quoted_string(parse_line)); } cout << "Finished parsing the card" << endl; return *new_card; }