-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanManager.cpp
More file actions
88 lines (73 loc) · 2.66 KB
/
PlanManager.cpp
File metadata and controls
88 lines (73 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "IIKH.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void PlanManager::readPlanInfo(string filename) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "FILE OPEN ERROR: " << filename << endl;
return;
}
while (getline(file, beforeParse)) {
if (beforeParse.empty() || beforeParse.find("//") != string::npos) continue; // Skip blank and "//"
parseLine(beforeParse);
string planName = afterParse[0];
string date = afterParse[1];
Plan mealPlan(planName, date); // Make new Plan
// Parse menu
for (int i = 2; i < parseIndex; ++i) {
string token = afterParse[i];
if (token.empty()) continue;
stringstream tokenStream(token);
string mealType, recipe, serving;
getline(tokenStream, mealType, ';');
getline(tokenStream, recipe, ';');
getline(tokenStream, serving, ';');
// Push for each case
if (mealType == "b") {
mealPlan.breakfast.push_back(recipe + " (SERVES " + serving + ")");
}
else if (mealType == "l") {
mealPlan.lunch.push_back(recipe + " (SERVES " + serving + ")");
}
else if (mealType == "d") {
mealPlan.dinner.push_back(recipe + " (SERVES " + serving + ")");
}
}
// Print
mealPlan.display();
}
file.close();
}
void PlanManager::writePlanInfo(string filename, string newPlan) {
ofstream file;
file.open(filename, ios::app);
if (file.is_open()) {
file << endl << newPlan << endl;
file.close();
}
else cout << "FILE OPEN ERROR";
}
void PlanManager::parseLine(const string& line) {
stringstream ss(line);
string token;
parseIndex = 0; // Initialize Index
while (getline(ss, token, '<')) { // Read token by parsing '<'
size_t endPos = token.find('>'); // Find the position of '>'
if (endPos != string::npos) { // Get string before '>'
token = token.substr(0, endPos);
token = trim(token); // Clear unnecessary blanks
if (!token.empty() && parseIndex < 10) { // Get only non-empty token and limit to ten
afterParse[parseIndex++] = token; // Put token in afterParse
}
}
}
}
string PlanManager::trim(const string& str) {
size_t first = str.find_first_not_of(' ');
if (first == string::npos) return ""; // If there isn't any word
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}