-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
167 lines (147 loc) · 3.19 KB
/
user.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
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
#include "user.h"
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cctype>
#include <sys/stat.h>
#include <dirent.h>
#include <ctime>
#include <pthread.h>
using namespace std;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
user::user(string aaa)
{
name=aaa;
mkdir(name.c_str(),0700);
}
bool user::sortnumb (string first, string second){
if (atoi(first.c_str())<atoi(second.c_str())) return true;
return false;
}
void user::do_del(int nmb)
{
}
void user::send(string to, string message)
{
message=name + "\n" + message;
mkdir(to.c_str(),0700);
ofstream handle;
time_t now;
stringstream strm;
string convert;
pthread_mutex_lock(&lock);
time(&now);
sleep(1);
pthread_mutex_unlock(&lock);
strm << now;
convert=strm.str();
convert=to +"/"+ convert;
cout << convert<< endl;
handle.open(convert.c_str(),ios::out);
handle<<message;
handle.close();
}
void user::send(list <string> to, string message)
{
ofstream handle;
time_t now;
while(to.begin()!=to.end())
{
message=name + "\n" + message;
mkdir(to.front().c_str(),0700);
stringstream strm;
string convert;
time(&now);
strm << now;
convert=strm.str();
convert=to.front() +"/"+ convert;
cout << convert<< endl;
handle.open(convert.c_str(),ios::out);
handle<<message;
handle.close();
to.pop_front();
}
}
string user::do_list()
{
string buffer;
list<string> filenames;
list<string>::iterator it;
// buffer.clear();
this->getfilenames(&filenames);
filenames.sort();//user::sortnumb);
for( it=filenames.begin() ; it != filenames.end(); it++ ){
buffer.append( *it=getfile(*it));
buffer.append("\n");
}
return buffer;
}
string user::do_read(int msg)
{
string h;
int i;
list<string> filenames;
list<string>::iterator it;
// buffer.clear();
getfilenames(&filenames);
if(msg>filenames.size())return "error";
filenames.sort();//user::sortnumb);
it=filenames.begin();
for(i=1;i<msg;i++)it++;
h=getfile(*it);
return h;
}
string user::getfile(string filename)
{
string back, line;
filename = name + "/"+ filename;
ifstream handle;
handle.open(filename.c_str(), ios::in);
while (handle.good())
{
getline (handle,line);
back=back+line;
back=back+"\n";
}
return back;
}
int user::getfilenames(list <string> *namelist){
DIR *dp;
int i=0;
struct dirent *dirp;
dp = opendir(name.c_str());
if(dp == NULL) {
cout << "did not get filenames" << endl;
return -1;
}
while ((dirp = readdir(dp)) != NULL)
{
i++;
(*namelist).push_back(string(dirp->d_name));
if( (*namelist).back() == "." || (*namelist).back()=="..")
{
(*namelist).pop_back();
}
}
closedir(dp);
return i;
}
void user::writefile(string message)
{
ofstream handle;
time_t now;
stringstream strm;
string convert;
time(&now);
strm << now;
convert=strm.str();
convert=name +"/"+ convert;
cout << convert<< endl;
handle.open(convert.c_str(),ios::out);
handle<<message;
handle.close();
}
user::~user()
{
}