1 |
|
2 |
#include <string> |
3 |
#include <vector> |
4 |
|
5 |
#include <iostream> |
6 |
#include "Sms.h" |
7 |
#include "util.h" |
8 |
|
9 |
|
10 |
std::string GetSmsCommand(SMS& sms) |
11 |
{ |
12 |
unsigned int pos = sms.message.find(" "); |
13 |
|
14 |
if (pos == std::string::npos) |
15 |
return sms.message; |
16 |
else |
17 |
return sms.message.substr(0, pos); |
18 |
} |
19 |
|
20 |
std::string GetSmsData(SMS& sms) |
21 |
{ |
22 |
unsigned int pos = sms.message.find(" "); |
23 |
|
24 |
if (pos == std::string::npos) |
25 |
return ""; |
26 |
else |
27 |
{ |
28 |
pos++; |
29 |
|
30 |
std::string data = sms.message.substr(pos, sms.message.length()-pos ); |
31 |
|
32 |
while (data.at(0) == ' ') |
33 |
data = data.substr(1, data.length() -1); |
34 |
return data; |
35 |
} |
36 |
} |
37 |
|
38 |
SMS::SMS() |
39 |
{ |
40 |
} |
41 |
|
42 |
SMS::SMS(std::string _sender, std::string _message, std::string _timestamp) |
43 |
: sender(_sender), message(_message), timestamp(_timestamp) |
44 |
{ |
45 |
} |
46 |
|
47 |
SMS SMS::FromRawString(const std::string& input) |
48 |
{ |
49 |
std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: " |
50 |
|
51 |
std::vector<std::string> lines = Util::str_split(smsline, "\r\n"); |
52 |
std::vector<std::string> fields = Util::str_split(lines[0],","); |
53 |
|
54 |
std::string body; |
55 |
for (unsigned i=1; i<lines.size(); ++i) |
56 |
{ |
57 |
std::string body_line = lines[i]; |
58 |
|
59 |
|
60 |
if (body_line != "") |
61 |
{ |
62 |
if (body.length() > 0) |
63 |
body += "\r\n"; |
64 |
body += body_line; |
65 |
} |
66 |
} |
67 |
|
68 |
while(body.at(0) == ' ') //remove starting space from body |
69 |
body = body.substr( 1, body.length() -1); |
70 |
|
71 |
SMS newsms; |
72 |
|
73 |
newsms.sms_index = fields[0]; |
74 |
|
75 |
newsms.sender = fields[2]; |
76 |
newsms.sender = Util::str_replace(newsms.sender, "\""); |
77 |
|
78 |
newsms.timestamp = fields[4] + std::string(",") + fields[5]; |
79 |
newsms.timestamp = Util::str_replace(newsms.timestamp, "\""); |
80 |
|
81 |
newsms.message = body; |
82 |
|
83 |
return newsms; |
84 |
} |