Skip to content

Commit f7b0877

Browse files
committed
Add helper methods for creating string representations of ColorContainer
1 parent c442292 commit f7b0877

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/AnimationData.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ class AnimationData {
223223
std::cerr << "Bad type for spacing" << data["spacing"].type_name() << std::endl;
224224
}
225225

226+
std::string colorsString() {
227+
std::string cols = "[";
228+
229+
for (auto c : colors) {
230+
cols.append(c.colorsString(true));
231+
cols.append(",");
232+
}
233+
if (!colors.empty())
234+
cols.pop_back();
235+
cols.append("]");
236+
return cols;
237+
}
226238

227239
int json(char ** buff) const {
228240
std::string data = "DATA:{";

src/AnimationSender.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class AnimationSender {
155155

156156
int connect() {
157157
if (::connect(socket_desc, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
158-
perror("connect()");
158+
perror((host_name + " " + std::to_string(port_num) + " connect()").c_str());
159159
return -1;
160160
}
161161
return 0;

src/ColorContainer.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,25 @@ class ColorContainer {
5252
std::cerr << "Bad type for ColorContainer colors" << data["colors"].type_name() << std::endl;
5353
}
5454

55-
int json(char ** buff) {
56-
std::string data = R"({"colors":[)";
57-
55+
std::string colorsString(bool hex = false) {
56+
std::string data = "[";
5857
for (auto c : colors) {
59-
data.append(std::to_string(c));
60-
data.append(",");
58+
std::stringstream ss;
59+
if (hex) ss << "0x" << std::hex;
60+
ss << c << ",";
61+
data.append(ss.str());
6162
}
6263
if (!colors.empty())
6364
data.pop_back();
64-
data.append("]}");
65+
data.append("]");
66+
return data;
67+
}
68+
69+
int json(char ** buff) {
70+
std::string data = R"({"colors":)";
71+
72+
data.append(colorsString(false));
73+
data.append("}");
6574

6675
strcpy(*buff, data.c_str());
6776
return data.size();

0 commit comments

Comments
 (0)