-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFile.cpp
More file actions
140 lines (126 loc) · 3.89 KB
/
Copy pathFile.cpp
File metadata and controls
140 lines (126 loc) · 3.89 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "File.h"
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
bool saveUserToFile(User *user)
{
try
{
const String filePath = "userData/" + user->getUsername() + ".txt";
std::ofstream file(filePath, std::ios::app);
if (file.is_open() == false)
{
std::cout << "Error: Could not open file " << filePath << " for writing" << std::endl;
return false;
}
file << user->getUsername() << std::endl;
file << user->getName() << std::endl;
file << user->getPassword() << std::endl;
file << user->getEmail() << std::endl;
file << user->getAge() << std::endl;
file << user->getGenderAsString() << std::endl;
std::vector<String> qualities = user->getQualities();
int size_qualities = qualities.size();
std::vector<String> followers = user->getFollowers();
int size_followers = followers.size();
file << "Qualities: ";
for(int index = 0; index < size_qualities; index++)
file << qualities[index] << ", ";
file << std::endl;
file << "Followers: ";
for(int index = 0; index < size_followers; index++)
file << followers[index] << ", ";
file << std::endl;
if (file.good() == false)
{
std::cout << "Error: Writing to file failed" << std::endl;
return false;
}
}
catch (const std::ofstream::failure& e)
{
std::cerr << "Exception while handling file: " << e.what() << std::endl;
return false;
}
return true;
}
User* loadFromFile(const String& filename)
{
std::ifstream file(filename);
if (file.is_open() == false)
{
std::cerr << "Error: Could not open file" << filename << std::endl;
return nullptr;
}
String username, name, password, email, genderStr, line;
int age;
bool gender;
std::vector<String> qualities, followers;
getline(file, username);
getline(file, name);
getline(file, password);
getline(file, email);
getline(file, line);
try
{
age = std::stoi(line);
}
catch (const std::invalid_argument& e)
{
std::cerr << "Invalid age value in file " << filename << ": " << line << std::endl;
return nullptr;
}
getline(file, line);
gender = (line == "Male") ? false : true;
getline(file, line);
String qualitiesStr = line;
std::stringstream ss(qualitiesStr);
String quality;
while (getline(ss, quality, ',') && qualities.size() < 5)
{
qualities.push_back(quality);
}
getline(file, line);
String followersStr = line;
std::stringstream fs(followersStr);
String follower;
while (getline(fs, follower, ','))
{
followers.push_back(follower);
}
file.close();
return new User(name, username, password, email, age, gender, qualities, followers);
}
Node *parseUserFiles(const String &path)
{
AVLTree root;
if (fs::exists(path) == false)
{
std::cerr << "Error: Path does not exist: " << path << std::endl;
return nullptr;
}
if (fs::is_directory(path) == false)
{
std::cerr << "Error: Path is not a directory: " << path << std::endl;
return nullptr;
}
for (const auto& entry : fs::directory_iterator(path))
{
String filepath = entry.path().string();
if(filepath.find(".txt") == String::npos)
{
std::cout << "Parsing complete" << std::endl;
break;
}
User* user = loadFromFile(filepath);
if(user == nullptr)
{
std::cerr << "Error loading user from file: " << filepath << std::endl;
continue;
}
else
{
root.insert(user);
}
}
return root.getRoot();
}