-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.cpp
116 lines (103 loc) · 3.1 KB
/
message.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
#include "message.h"
Message::Message (unsigned int number, const std::string& filepath) :
_number (number), _deleted (false), _file_path (filepath) { }
Message::~Message ()
{
if ( deleted() )
{
if ( remove(_file_path.c_str()) != 0 )
throw std::runtime_error("unable to delete message: " + _file_path);
}
}
unsigned long Message::size () const
{
unsigned long begin, end;
std::ifstream in_file(_file_path.c_str());
if ( in_file.is_open() )
{
// Get position of the get pointer
begin = in_file.tellg();
// Set the postion of the get pointer to the end of the file
in_file.seekg(0, std::ios::end);
// Get the position of the get pointer
end = in_file.tellg();
in_file.close();
unsigned long bits(end - begin);
if ( bits == 0 )
return bits;
else
// We get the size size of the file by substracting end from begin
return ceil(bits / 8);
}
else
throw std::runtime_error("unable to open message: " + _file_path);
}
std::string Message::message_string () const
{
// Output string stream where we will put the text of the message
std::ostringstream oss;
std::ifstream in_file(_file_path.c_str());
std::string line;
if ( in_file.is_open() )
{
// Put each line of the file in the output stream
while ( !in_file.eof() )
{
getline(in_file, line);
oss << line << std::endl;
}
in_file.close();
// Return the actual string of the oss
return oss.str();
}
else
throw std::runtime_error("unable to open message: " + _file_path);
}
std::string Message::message_string (int n) const
{
// Output string stream where we will put the text of the message
std::ostringstream oss;
std::ifstream in_file(_file_path.c_str());
std::string line;
if ( in_file.is_open() )
{
// Counter to keep track of how many lines we already got
int counter(0);
/* Put each line of the file in the output stream
* until counter = n or we are at the end of the file */
while ( (!(counter == n)) && (!in_file.eof()) )
{
getline(in_file, line);
oss << line << std::endl;
counter++;
}
in_file.close();
// Return the actual string of the oss
return oss.str(); // Return the actual string of the oss
}
else
throw std::runtime_error("unable to open message: " + _file_path);
}
std::string Message::uidl () const
{
std::ostringstream oss;
std::size_t size(_file_path.size());
// We start at position size-1
int i(--size);
/* Seek the file path backwards and put every char in oss, until we find '/'
* eg /home/wouter/123 will return "123" */
while ( _file_path.at(i) != '/' )
{
oss << _file_path.at(i);
i--;
}
std::string uidl_string(oss.str());
/* Since we put the characters on the oss backwards
* we have to reverse the string now */
std::reverse(uidl_string.begin(), uidl_string.end());
return uidl_string;
}
std::ostream & operator<< (std::ostream& os, const Message& msg)
{
return os << msg.number() << " " << msg.size();
}