-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
275 lines (236 loc) · 7.67 KB
/
app.cpp
File metadata and controls
275 lines (236 loc) · 7.67 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <fstream>
#include "app.h"
#include "config.h"
#include <filesystem>
#include "time.h"
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
App::App(std::istream& is, std::ostream& os): is(is), os(os) {
// TODO
}
bool App::authenticate(std::string id, std::string pass){
std::ifstream file;
std::string realpass;
file.open(SERVER_STORAGE_DIR+id+"/password.txt");
if (file.is_open()){
file>>realpass;
}
if (realpass==pass){
return true;
}
else{
return false;
}
}
void App::post(std::string id, std::string title, std::string content) {
int number = 0;
int max = 0;
std::string path = SERVER_STORAGE_DIR;
for (const auto &file: std::filesystem::directory_iterator(path)) {
for (const auto &sub: std::filesystem::directory_iterator(file.path() / "post")) {
std::string name = sub.path().filename().string();
std::string deli = ".";
number = std::stoi(name.substr(0, name.find(deli)));
if (number > max) {
max = number;
}
}
}
std::ofstream write(SERVER_STORAGE_DIR + id + "/post/" + std::to_string(max+1) + ".txt");
time_t currentTime = time(NULL);
struct tm *local = localtime(¤tTime);
std::string date=std::to_string(local->tm_year+1900)+"/"+std::to_string(local->tm_mon+1)+"/"+std::to_string(
local->tm_mday)+" "+std::to_string(local->tm_hour)+":"+std::to_string(local->tm_min)+":"+std::to_string(local->tm_sec);
write<<date+"\n";
write<<title+"\n\n";
write<<content;
write.close();
}
bool compare2(std::string a1, std::string a2){
std::string line1;
std::string line2;
line1=a1.substr(a1.find("t: "), a1.find("t: ")+5);
line2=a2.substr(a2.find("t: "), a1.find("t: ")+5);
line1=line1.substr(3, line1.size());
line2=line2.substr(3, line2.size());
return (line1>line2);
}
void App::recommend(std::string id){
std::ifstream read(SERVER_STORAGE_DIR + id + "/friend.txt");
std::string friends;
std::string oneline;
std::vector<std::string> str;
while(!read.eof()){
std::getline(read, friends);
std::string path = SERVER_STORAGE_DIR+friends+"/post";
for (const auto &file: std::filesystem::directory_iterator(path)) {
std::string tmpread="";
tmpread+=("id: "+file.path().filename().string().substr(0, file.path().filename().string().find("."))+"\ncreated at: ");
std::ifstream tmp(file.path());
std::getline(tmp, oneline);
tmpread+=oneline+"\ntitle: ";
std::getline(tmp, oneline);
tmpread+=oneline+"\ncontent:\n";
while(!tmp.eof()){
std::getline(tmp, oneline);
if (oneline!=""){
tmpread+=oneline+"\n";
}
}
tmpread.erase(tmpread.find_last_not_of("\n"));
str.push_back(tmpread);
}
}
std::sort(str.begin(), str.end(), compare2);
for(int i=0; i<9; i++){
os<<str.at(i);
os<<"\n-----------------------------------\n";
}
os<<str.at(9)<<"\n";
}
bool comparison(std::string s1, std::string s2){
std:: istringstream ss(s1);
std:: istringstream ss2(s2);
std::string f;
std::string f2;
getline(ss, f, '\n');
getline(ss2, f2, '\n');
if (f!=f2){
return (f>f2);
}
else{
return compare2(s1, s2);
}
}
void App::count(std::vector<std::string> str){
std::string path = SERVER_STORAGE_DIR;
std::string tmp_;
std::string oneline;
std::vector<std::string>::iterator it;
std::vector<std::string>::iterator it2;
std::vector<std::string> word;
std::vector<std::string> stringcollect;
for (const auto &file: std::filesystem::directory_iterator(path)) {
for (const auto &sub: std::filesystem::directory_iterator(file.path() / "post")) {
std::string tmpread="";
tmp_=("id: "+sub.path().filename().string().substr(0, sub.path().filename().string().find("."))+", ");
std::ifstream tmp(sub.path());
std::getline(tmp, oneline);
tmp_+=("created at: "+oneline+", ");
std::getline(tmp, oneline);
tmp_+=("title: "+oneline);
tmpread+=oneline+" ";
while(!tmp.eof()){
std::getline(tmp, oneline);
if (oneline!=""){
tmpread+=oneline+" ";
}
}
int count=0;
std:: istringstream ss(tmpread);
std::string f;
while(getline(ss, f, ' ')){
word.push_back(f);
}
int i=0;
for (it=str.begin(); it!=str.end(); it++){
int j=0;
for(it2=word.begin(); it2!=word.end(); it2++){
if (str.at(i)==word.at(j)){
count++;
}
j++;
}
i++;
}
word.clear();
tmp_=std::to_string(count)+"\n"+tmp_;
if (count>0){
stringcollect.push_back(tmp_);
}
}
}
std::sort(stringcollect.begin(), stringcollect.end(), comparison);
if (stringcollect.size()>=10){
for(int i=0; i<10; i++){
os<<stringcollect.at(i).substr(stringcollect.at(i).find("\n")+1, stringcollect.at(i).size())<<"\n";
}
}
else{
for(int i=0; i<stringcollect.size()-1; i++){
os<<stringcollect.at(i).substr(stringcollect.at(i).find("\n")+1, stringcollect.at(i).size())<<"\n";
}
os<<stringcollect.at(stringcollect.size()-1).
substr(stringcollect.at(stringcollect.size()-1).find("\n")+1, stringcollect.at(stringcollect.size()-1).size())<<"\n";
}
}
void App::search(std::string keyword){
std::vector<std::string> only;
std:: istringstream ss(keyword);
std::string f;
while(getline(ss, f, ' ')){
only.push_back(f);
}
std::sort(only.begin(), only.end());
only.erase(std::unique(only.begin(), only.end()), only.end());
std::unique(only.begin(), only.end());
count(only);
}
void App::run() {
// TODO
std::string id;
std::string pass;
std::string command;
os<<"------ Authentication ------\n";
os<<"id=";
is>>id;
os<<"passwd=";
is>>pass;
is.ignore();
if (authenticate(id, pass)){
}
else{
os<<"Failed Authentication.\n";
return;
}
while (true){
os<<"-----------------------------------\n";
os<<id<<"@sns.com\n";
os<<"post : Post contents\nrecommend : recommend interesting posts\nsearch <keyword> : List post entries whose contents contain <keyword>\nexit : Terminate this program\n";
os<<"-----------------------------------\n";
os<<"Command=";
std::getline(is, command);
if (command=="exit"){
break;
}
if (command=="post"){
std::string title;
std::string tmpcontent="33";
std::string content;
os<<"-----------------------------------\n";
os<<"New Post\n";
os<<"* Title=";
is.ignore();
std::getline(is, title);
os<<"* Content\n";
do{
os<<">";
std::getline(is, tmpcontent);
content+=(tmpcontent+"\n");
}while(tmpcontent.length()!=0);
post(id ,title, content);
}
if (command=="recommend"){
os<<"-----------------------------------\n";
recommend(id);
}
std::string tt=command;
if (tt.substr(0, 6)=="search"){
os<<"-----------------------------------\n";
search(command.substr(7 , command.size()));
}
}
}