-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShared.cpp
More file actions
85 lines (81 loc) · 1.95 KB
/
Shared.cpp
File metadata and controls
85 lines (81 loc) · 1.95 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
#include "Shared.h"
String toJSON(vector<Message> vec) {
if(vec.size() == 0) {
return "{ \"messages\": []}";
}
if(vec.size() == 1) {
return "{ \"messages\": [" + vec[0].toJSON() + "]}";
}
String response = "{ \"messages\": [";
for(int i = 0; i < vec.size()-1; ++i) {
response += vec[i].toJSON();
response += ",";
}
response += vec[vec.size()-1].toJSON();
response += "]}";
return response;
}
String getMessage(String str) {
int start = str.indexOf(':');
String message = "";
for (int i = start + 1; i < str.length(); ++i) {
if (str.charAt(i) == '\"')
break;
else
message += str.charAt(i);
}
return message;
}
// function to parse through data object
vector<String> getDataMessage(String str) {
/*
Data JSON Structure
{
"name" : "userName",
"message": "message",
"id": "id"
}
*/
// replaces ':' with ';'
// should back up to text file before mutating string
vector<String> resultMessage;
for (int index = 0; index < 3; index++) {
int start = str.indexOf(':');
String message = "";
for(int i = start+2; i < str.length(); ++i) {
if(str.charAt(i) == '\"')
break;
else
message += str.charAt(i);
}
return message;
}
// function to parse through data object
Message parseMessage(String str) {
/*
Data JSON Structure
{
"name" : "userName",
"message": "message",
"id": "id"
}
*/
// replaces ':' with ';'
// should back up to text file before mutating string
vector<String> result;
for(int i = 0; i < str.length(); ++i) {
if(str.charAt(i) == ':') {
String data = "";
for(int j = i+2; j < str.length(); ++j) {
if(str.charAt(j) == '\"') {
i = j;
result.push_back(data);
break;
}
data += str.charAt(j);
}
}
}
Message m(result[0], result[1], result[2]);
return m;
}