-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDatabase.cpp
58 lines (47 loc) · 1.29 KB
/
UserDatabase.cpp
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
#include "UserDatabase.h"
#include "User.h"
#include <string>
#include <vector>
#include <fstream>
using namespace std;
bool UserDatabase::load(const string& filename)
{
// don't open file if we already loaded users
if (m_loaded)
return false;
// open file
ifstream infile(filename);
if ( !infile ) {
cerr << "Error: could not open file!" << endl;
return false;
}
// variables to store data
string name, email, temp;
int numMovies;
vector<std::string> movies;
while (getline(infile, name)) {
// read user data
getline(infile, email);
infile >> numMovies;
infile.ignore(10000, '\n');
for (int i=0; i<numMovies; i++) {
string movie;
getline(infile, movie);
movies.push_back(movie);
}
// skip empty line
getline(infile, temp);
// add user to tree multimap
m_data.insert(email, User(name, email, movies));
// clear movies vector for reuse
movies.clear();
}
m_loaded = true;
return true;
}
User* UserDatabase::get_user_from_email(const string& email) const
{
auto i = m_data.find(email);
if (!i.is_valid()) return nullptr;
return &(i.get_value());
}