void AvailableTickets::parse() { /* Read the contents of the current user accounts and store each user */ FileInputStream fis(atf_file); if (fis.good()) { string read; RegularExpression re("^(.{1,19}?)_+_([A-Za-z0-9_]{1,15}?)_+([0-9]{3})_([0-9]{3}\\.[0-9]{2})$"); RegularExpression re_end("END_{33}0{3}_0{3}\\.0{2}"); RegularExpression::MatchVec matches; while (getline(fis, read)) { /* Stop if END of file reached */ if (re_end.match(read)) { return; } /* Add each ticket found to the list of tickets */ if (Validate::atf_entry(read)) { re.match(read, 0, matches); /* Replace each _ with a space character for event title */ string event = replace(read.substr(matches[1].offset, matches[1].length), "_", " "); string seller = read.substr(matches[2].offset, matches[2].length); int volume = NumberParser::parse((read.substr(matches[3].offset, matches[3].length))); double price = NumberParser::parseFloat(read.substr(matches[4].offset, matches[4].length)); /* Add the ticket to the list of tickets */ this->tickets.push_back(Ticket(event, seller, volume, price)); } /* Corrupted entry in file */ else { throw Exception(CORRUPT_ATF); } } } /* No END of file identifier found or file corrupted */ throw Exception(CORRUPT_ATF); }
void StringTest::testReplace() { std::string s("aabbccdd"); assert (replace(s, std::string("aa"), std::string("xx")) == "xxbbccdd"); assert (replace(s, std::string("bb"), std::string("xx")) == "aaxxccdd"); assert (replace(s, std::string("dd"), std::string("xx")) == "aabbccxx"); assert (replace(s, std::string("bbcc"), std::string("xx")) == "aaxxdd"); assert (replace(s, std::string("b"), std::string("xx")) == "aaxxxxccdd"); assert (replace(s, std::string("bb"), std::string("")) == "aaccdd"); assert (replace(s, std::string("b"), std::string("")) == "aaccdd"); assert (replace(s, std::string("ee"), std::string("xx")) == "aabbccdd"); assert (replace(s, std::string("dd"), std::string("")) == "aabbcc"); assert (replace(s, "aa", "xx") == "xxbbccdd"); assert (replace(s, "bb", "xx") == "aaxxccdd"); assert (replace(s, "dd", "xx") == "aabbccxx"); assert (replace(s, "bbcc", "xx") == "aaxxdd"); assert (replace(s, "bb", "") == "aaccdd"); assert (replace(s, "b", "") == "aaccdd"); assert (replace(s, "ee", "xx") == "aabbccdd"); assert (replace(s, "dd", "") == "aabbcc"); s = "aabbaabb"; assert (replace(s, std::string("aa"), std::string("")) == "bbbb"); assert (replace(s, std::string("a"), std::string("")) == "bbbb"); assert (replace(s, std::string("a"), std::string("x")) == "xxbbxxbb"); assert (replace(s, std::string("a"), std::string("xx")) == "xxxxbbxxxxbb"); assert (replace(s, std::string("aa"), std::string("xxx")) == "xxxbbxxxbb"); assert (replace(s, std::string("aa"), std::string("xx"), 2) == "aabbxxbb"); assert (replace(s, "aa", "") == "bbbb"); assert (replace(s, "a", "") == "bbbb"); assert (replace(s, "a", "x") == "xxbbxxbb"); assert (replace(s, "a", "xx") == "xxxxbbxxxxbb"); assert (replace(s, "aa", "xxx") == "xxxbbxxxbb"); assert (replace(s, "aa", "xx", 2) == "aabbxxbb"); }