Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. #include "BBoard.h"
  9.  
  10. BBoard::BBoard() {
  11. title = "";
  12. userList.resize(0);
  13. currentUser = NULL;
  14. messageList.resize(0);
  15. }
  16.  
  17. BBoard::BBoard(const string &ttl) {
  18. title = ttl;
  19. userList.resize(0);
  20. currentUser = NULL;
  21. messageList.resize(0);
  22. }
  23.  
  24. BBoard::~BBoard() {
  25. for(unsigned i = 0; i < messageList.size(); ++i) {
  26. delete messageList.at(i);
  27. }
  28. }
  29.  
  30. bool BBoard::loadUsers(const string &userfile) {
  31. string fileUser;
  32. string filePass;
  33. ifstream inFS;
  34. inFS.open(userfile.c_str());
  35.  
  36. if(!inFS.is_open()) {
  37. return false;
  38. }
  39.  
  40. else {
  41. while(inFS >> fileUser) {
  42. inFS >> filePass;
  43. addUser(fileUser, filePass);
  44. }
  45. inFS.close();
  46. return true;
  47. }
  48. }
  49.  
  50. bool BBoard::loadMessages(const string &datafile) {
  51. ifstream messageLoad;
  52. int numMessages;
  53. int messageCounter = 0;
  54. string messageType;
  55. string header;
  56.  
  57. messageLoad.open(datafile.c_str());
  58. if(!messageLoad.is_open()) {
  59. return false;
  60. }
  61. messageLoad >> numMessages;
  62. vector< vector<int> > child(numMessages);
  63.  
  64. while (messageCounter < numMessages) {
  65. int id = 0;
  66. messageLoad >> messageType;
  67. header = "";
  68. string sbjct = "";
  69. string athr = "";
  70. string children = "";
  71. string body = "";
  72.  
  73. while(header != "<end>") {
  74. messageLoad >> header;
  75.  
  76. if(header == ":id:") {
  77. messageLoad >> id;
  78. }
  79.  
  80. else if(header == ":subject:") {
  81. getline(messageLoad, sbjct );
  82. sbjct = sbjct.substr(1);
  83. }
  84.  
  85. else if(header == ":from:") {
  86. messageLoad >> athr;
  87. }
  88.  
  89. else if(header == ":children:") {
  90. getline(messageLoad, children);
  91. children = children.substr(1);
  92.  
  93. istringstream inSS(children);
  94. int numChild;
  95. while(inSS >> numChild) {
  96. child.at(id - 1).push_back(numChild);
  97. }
  98. }
  99. else if(header == ":body:") {
  100. while(header != "<end>") {
  101.  
  102. getline(messageLoad, header);
  103.  
  104. if(header == "<end>") {
  105. break;
  106. }
  107.  
  108. body = body + header + '\n';
  109. }
  110.  
  111. body = body.substr(1);
  112. body = body.substr(0, body.size() - 1);
  113. }
  114.  
  115. }
  116.  
  117. if(messageType == "<begin_topic>") {
  118. messageList.push_back( new Topic(athr, sbjct, body, id) );
  119. }
  120.  
  121. else if(messageType == "<begin_reply>" ) {
  122. messageList.push_back( new Reply(athr, sbjct, body, id) );
  123. }
  124.  
  125. ++messageCounter;
  126. }
  127.  
  128. for (unsigned i = 0; i < messageList.size(); ++i) {
  129. for (unsigned j = 0; j < child.at(i).size(); ++j) {
  130. messageList.at(i)->addChild(messageList.at(child.at(i).at(j) - 1));
  131. }
  132. }
  133. messageLoad.close();
  134.  
  135. return true;
  136. }
  137.  
  138. bool BBoard::saveMessages(const string &datafile) {
  139. ofstream saveFile;
  140. saveFile.open(datafile.c_str());
  141.  
  142. if(!saveFile.is_open()) {
  143. return false;
  144. }
  145.  
  146. saveFile << messageList.size() << endl;
  147. for (unsigned i = 0; i < messageList.size(); ++i) {
  148. saveFile << messageList.at(i) -> toFormattedString() << endl;
  149. }
  150. saveFile.close();
  151. return true;
  152.  
  153. }
  154.  
  155. void BBoard::login() {
  156. string userName;
  157. string passWord;
  158. bool corrLogin = false;
  159.  
  160. cout << "Welcome to " << title << endl;
  161. while (corrLogin == false) {
  162. cout << "Enter your username ('Q' or 'q' to quit): ";
  163. cin >> userName;
  164.  
  165. if (userName == "Q" || userName == "q") {
  166. cout << "Bye!" << endl;
  167. exit(0);
  168. }
  169.  
  170. cout << "Enter your password: ";
  171. cin >> passWord;
  172.  
  173. if(getUser(userName,passWord) != 0) {
  174. cout << "Welcome back " << userName << "!\n\n";
  175. currentUser = getUser(userName,passWord);
  176. corrLogin = true;
  177. }
  178. else {
  179. cout << "Invalid Username or Password!\n\n";
  180. }
  181.  
  182. }
  183. }
  184.  
  185. void BBoard::run() {
  186. char userOption;
  187. if(currentUser != NULL) {
  188. do {
  189. cout << "Menu" << endl;
  190. cout << "- Display Messages ('D' or 'd')" << endl;
  191. cout << "- Add New Topic ('N' or 'n')" << endl;
  192. cout << "- Add Reply to a Topic ('R' or 'r')" << endl;
  193. cout << "- Quit ('Q' or 'q')" << endl;
  194. cout << "Choose an action: ";
  195. cin >> userOption;
  196.  
  197. if(userOption == 'D' || userOption == 'd') {
  198. display();
  199. }
  200.  
  201. else if(userOption == 'N' || userOption == 'n') {
  202. addTopic();
  203. }
  204.  
  205. else if(userOption == 'R' || userOption == 'r') {
  206. addReply();
  207. }
  208.  
  209. else if(userOption == 'Q' || userOption == 'q') {
  210. cout << "Bye!" << endl;
  211. return;
  212. }
  213. } while(userOption != 'Q' || userOption != 'q');
  214. }
  215. }
  216.  
  217. void BBoard::addUser(const string &name, const string &pass) {
  218. userList.push_back(User(name,pass));
  219. }
  220.  
  221. const User* BBoard::getUser(const string &name, const string &pw) const {
  222. for(unsigned i = 0; i < userList.size(); ++i) {
  223. if(userList.at(i).check(name,pw)) {
  224. return &userList.at(i);
  225. }
  226. }
  227. return 0;
  228. }
  229.  
  230. void BBoard::display() const {
  231. if (messageList.size() == 0) {
  232. cout << "\nNothing to Display." << endl << endl;
  233. }
  234.  
  235. else {
  236. for(unsigned i = 0; i < messageList.size(); ++i) {
  237. if (!(messageList.at(i)->isReply())) {
  238. cout << endl << "---------------------------------------------------------" << endl;
  239. messageList.at(i)->print(0);
  240. }
  241. }
  242. cout << "---------------------------------------------------------" << endl << endl;
  243. }
  244. }
  245.  
  246. void BBoard::addTopic() {
  247. string subject;
  248. string tempBody = " ";
  249. string body;
  250.  
  251. cout << "Enter Subject: ";
  252. cin.ignore(1000, '\n');
  253. getline(cin,subject);
  254. cout << "Enter Body: \n";
  255.  
  256. while(tempBody != "" ) {
  257. getline(cin, tempBody);
  258.  
  259. if(tempBody == "" ) {
  260. break;
  261. }
  262.  
  263. body = body + tempBody + '\n';
  264. }
  265.  
  266. body = body.substr(0, body.size()-1);
  267.  
  268. Topic* newTopic = new Topic(currentUser->getUsername(), subject, body, messageList.size() + 1);
  269. messageList.push_back(newTopic);
  270. }
  271.  
  272. void BBoard::addReply() {
  273. bool replyId = true;
  274. unsigned int messageId;
  275. string bodyLine = " ";
  276. string body;
  277. string subject;
  278. string author;
  279.  
  280. while(replyId) {
  281. cout << "Enter Message ID (-1 for Menu): ";
  282. cin >> messageId;
  283.  
  284. if(messageId > messageList.size()) {
  285. cout << "Invalid Message ID!!" << endl;
  286. }
  287. else {
  288. replyId = false;
  289. }
  290. }
  291.  
  292. subject = "Re: " + messageList.at(messageId - 1)->getSubject();
  293. cout << "Enter Body: ";
  294.  
  295. cin.ignore(1000, '\n');
  296.  
  297. while(bodyLine != "" ) {
  298. getline(cin, bodyLine);
  299.  
  300. if(bodyLine == "" ) {
  301. break;
  302. }
  303.  
  304. body = body + bodyLine + '\n';
  305. }
  306.  
  307. body = body.substr(0, body.size()-1);
  308.  
  309. Reply* newReply = new Reply(currentUser->getUsername(), subject, body, messageList.size() + 1);
  310. messageList.at(messageId - 1)->addChild(newReply);
  311. messageList.push_back(newReply);
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement