Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- using namespace std;
- #include "BBoard.h"
- BBoard::BBoard() {
- title = "";
- userList.resize(0);
- currentUser = NULL;
- messageList.resize(0);
- }
- BBoard::BBoard(const string &ttl) {
- title = ttl;
- userList.resize(0);
- currentUser = NULL;
- messageList.resize(0);
- }
- BBoard::~BBoard() {
- for(unsigned i = 0; i < messageList.size(); ++i) {
- delete messageList.at(i);
- }
- }
- bool BBoard::loadUsers(const string &userfile) {
- string fileUser;
- string filePass;
- ifstream inFS;
- inFS.open(userfile.c_str());
- if(!inFS.is_open()) {
- return false;
- }
- else {
- while(inFS >> fileUser) {
- inFS >> filePass;
- addUser(fileUser, filePass);
- }
- inFS.close();
- return true;
- }
- }
- bool BBoard::loadMessages(const string &datafile) {
- ifstream messageLoad;
- int numMessages;
- int messageCounter = 0;
- string messageType;
- string header;
- messageLoad.open(datafile.c_str());
- if(!messageLoad.is_open()) {
- return false;
- }
- messageLoad >> numMessages;
- vector< vector<int> > child(numMessages);
- while (messageCounter < numMessages) {
- int id = 0;
- messageLoad >> messageType;
- header = "";
- string sbjct = "";
- string athr = "";
- string children = "";
- string body = "";
- while(header != "<end>") {
- messageLoad >> header;
- if(header == ":id:") {
- messageLoad >> id;
- }
- else if(header == ":subject:") {
- getline(messageLoad, sbjct );
- sbjct = sbjct.substr(1);
- }
- else if(header == ":from:") {
- messageLoad >> athr;
- }
- else if(header == ":children:") {
- getline(messageLoad, children);
- children = children.substr(1);
- istringstream inSS(children);
- int numChild;
- while(inSS >> numChild) {
- child.at(id - 1).push_back(numChild);
- }
- }
- else if(header == ":body:") {
- while(header != "<end>") {
- getline(messageLoad, header);
- if(header == "<end>") {
- break;
- }
- body = body + header + '\n';
- }
- body = body.substr(1);
- body = body.substr(0, body.size() - 1);
- }
- }
- if(messageType == "<begin_topic>") {
- messageList.push_back( new Topic(athr, sbjct, body, id) );
- }
- else if(messageType == "<begin_reply>" ) {
- messageList.push_back( new Reply(athr, sbjct, body, id) );
- }
- ++messageCounter;
- }
- for (unsigned i = 0; i < messageList.size(); ++i) {
- for (unsigned j = 0; j < child.at(i).size(); ++j) {
- messageList.at(i)->addChild(messageList.at(child.at(i).at(j) - 1));
- }
- }
- messageLoad.close();
- return true;
- }
- bool BBoard::saveMessages(const string &datafile) {
- ofstream saveFile;
- saveFile.open(datafile.c_str());
- if(!saveFile.is_open()) {
- return false;
- }
- saveFile << messageList.size() << endl;
- for (unsigned i = 0; i < messageList.size(); ++i) {
- saveFile << messageList.at(i) -> toFormattedString() << endl;
- }
- saveFile.close();
- return true;
- }
- void BBoard::login() {
- string userName;
- string passWord;
- bool corrLogin = false;
- cout << "Welcome to " << title << endl;
- while (corrLogin == false) {
- cout << "Enter your username ('Q' or 'q' to quit): ";
- cin >> userName;
- if (userName == "Q" || userName == "q") {
- cout << "Bye!" << endl;
- exit(0);
- }
- cout << "Enter your password: ";
- cin >> passWord;
- if(getUser(userName,passWord) != 0) {
- cout << "Welcome back " << userName << "!\n\n";
- currentUser = getUser(userName,passWord);
- corrLogin = true;
- }
- else {
- cout << "Invalid Username or Password!\n\n";
- }
- }
- }
- void BBoard::run() {
- char userOption;
- if(currentUser != NULL) {
- do {
- cout << "Menu" << endl;
- cout << "- Display Messages ('D' or 'd')" << endl;
- cout << "- Add New Topic ('N' or 'n')" << endl;
- cout << "- Add Reply to a Topic ('R' or 'r')" << endl;
- cout << "- Quit ('Q' or 'q')" << endl;
- cout << "Choose an action: ";
- cin >> userOption;
- if(userOption == 'D' || userOption == 'd') {
- display();
- }
- else if(userOption == 'N' || userOption == 'n') {
- addTopic();
- }
- else if(userOption == 'R' || userOption == 'r') {
- addReply();
- }
- else if(userOption == 'Q' || userOption == 'q') {
- cout << "Bye!" << endl;
- return;
- }
- } while(userOption != 'Q' || userOption != 'q');
- }
- }
- void BBoard::addUser(const string &name, const string &pass) {
- userList.push_back(User(name,pass));
- }
- const User* BBoard::getUser(const string &name, const string &pw) const {
- for(unsigned i = 0; i < userList.size(); ++i) {
- if(userList.at(i).check(name,pw)) {
- return &userList.at(i);
- }
- }
- return 0;
- }
- void BBoard::display() const {
- if (messageList.size() == 0) {
- cout << "\nNothing to Display." << endl << endl;
- }
- else {
- for(unsigned i = 0; i < messageList.size(); ++i) {
- if (!(messageList.at(i)->isReply())) {
- cout << endl << "---------------------------------------------------------" << endl;
- messageList.at(i)->print(0);
- }
- }
- cout << "---------------------------------------------------------" << endl << endl;
- }
- }
- void BBoard::addTopic() {
- string subject;
- string tempBody = " ";
- string body;
- cout << "Enter Subject: ";
- cin.ignore(1000, '\n');
- getline(cin,subject);
- cout << "Enter Body: \n";
- while(tempBody != "" ) {
- getline(cin, tempBody);
- if(tempBody == "" ) {
- break;
- }
- body = body + tempBody + '\n';
- }
- body = body.substr(0, body.size()-1);
- Topic* newTopic = new Topic(currentUser->getUsername(), subject, body, messageList.size() + 1);
- messageList.push_back(newTopic);
- }
- void BBoard::addReply() {
- bool replyId = true;
- unsigned int messageId;
- string bodyLine = " ";
- string body;
- string subject;
- string author;
- while(replyId) {
- cout << "Enter Message ID (-1 for Menu): ";
- cin >> messageId;
- if(messageId > messageList.size()) {
- cout << "Invalid Message ID!!" << endl;
- }
- else {
- replyId = false;
- }
- }
- subject = "Re: " + messageList.at(messageId - 1)->getSubject();
- cout << "Enter Body: ";
- cin.ignore(1000, '\n');
- while(bodyLine != "" ) {
- getline(cin, bodyLine);
- if(bodyLine == "" ) {
- break;
- }
- body = body + bodyLine + '\n';
- }
- body = body.substr(0, body.size()-1);
- Reply* newReply = new Reply(currentUser->getUsername(), subject, body, messageList.size() + 1);
- messageList.at(messageId - 1)->addChild(newReply);
- messageList.push_back(newReply);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement