+ Legal: use an up-to-date address of Free Software Foundation
[calf.git] / src / osctl.cpp
blob583ce2dfa398808f9cc7bbf518fc963c2a27194f
1 #include <calf/osctl.h>
2 #include <assert.h>
3 #include <arpa/inet.h>
4 #include <sys/socket.h>
5 #include <stdlib.h>
6 #include <sstream>
8 using namespace osctl;
9 using namespace std;
11 #if 0
12 std::string osc_data::to_string() const
14 std::stringstream ss;
15 switch(type)
17 case osc_i32: ss << i32val; break;
18 case osc_i64: ss << (int64_t)tsval; break;
19 case osc_f32: ss << f32val; break;
20 case osc_ts: ss << tsval; break;
21 case osc_string: return strval;
22 case osc_string_alt: return strval;
23 case osc_blob: return strval;
24 case osc_true: return "TRUE";
25 case osc_false: return "FALSE";
26 case osc_nil: return "NIL";
27 case osc_inf: return "INF";
28 case osc_start_array: return "[";
29 case osc_end_array: return "]";
30 default:
31 return "?";
33 return ss.str();
35 #endif
37 const char *osctl::osc_type_name(osc_type type)
39 switch(type)
41 case osc_i32: return "i32";
42 case osc_i64: return "i64";
43 case osc_f32: return "f32";
44 case osc_ts: return "ts";
45 case osc_string: return "str";
46 case osc_string_alt: return "stralt";
47 case osc_blob: return "blob";
48 case osc_char: return "char";
49 case osc_rgba: return "rgba";
50 case osc_midi: return "midi";
51 case osc_true: return "TRUE";
52 case osc_false: return "FALSE";
53 case osc_nil: return "NIL";
54 case osc_inf: return "INF";
55 case osc_start_array: return "[";
56 case osc_end_array: return "]";
57 default:
58 return "unknown";
62 #if 0
63 void osc_stream::read(osc_type type, osc_data &od)
65 od.type = type;
66 switch(type)
68 case osc_i32:
69 case osc_f32:
70 copy_from(&od.i32val, 4);
71 od.i32val = ntohl(od.i32val);
72 break;
73 case osc_ts:
74 copy_from(&od.tsval, 8);
75 if (ntohl(1) != 1) {
76 uint32_t a = ntohl(od.tsval), b = ntohl(od.tsval >> 32);
77 od.tsval = (((uint64_t)a) << 32) | b;
79 break;
80 case osc_string:
81 case osc_string_alt:
83 int len = 0, maxlen = 0;
84 maxlen = buffer.length() - pos;
85 while(len < maxlen && buffer[pos + len])
86 len++;
87 od.strval = buffer.substr(pos, len);
88 pos += (len + 4) &~ 3;
89 break;
91 case osc_blob: {
92 copy_from(&od.i32val, 4);
93 od.i32val = ntohl(od.i32val);
94 od.strval = buffer.substr(pos, od.i32val);
95 pos += (od.i32val + 3) &~ 3;
96 break;
98 case osc_true:
99 case osc_false:
100 case osc_nil:
101 return;
102 default:
103 assert(0);
107 void osc_stream::write(const osc_data &od)
109 uint32_t val;
110 switch(od.type)
112 case osc_i32:
113 case osc_f32:
115 val = ntohl(od.i32val);
116 buffer += string((const char *)&val, 4);
117 break;
119 case osc_ts: {
120 uint64_t ts = od.tsval;
121 if (ntohl(1) != 1) {
122 uint32_t a = ntohl(od.tsval), b = ntohl(od.tsval >> 32);
123 ts = (((uint64_t)a) << 32) | b;
125 buffer += string((const char *)&ts, 8);
126 break;
128 case osc_string:
129 case osc_string_alt:
131 buffer += od.strval;
132 val = 0;
133 buffer += string((const char *)&val, 4 - (od.strval.length() & 3));
134 break;
136 case osc_blob:
138 val = ntohl(od.strval.length());
139 buffer += string((const char *)&val, 4);
140 buffer += od.strval;
141 val = 0;
142 buffer += string((const char *)&val, 4 - (od.strval.length() & 3));
143 break;
145 case osc_true:
146 case osc_false:
147 case osc_nil:
148 return;
149 default:
150 assert(0);
154 void osc_stream::read(const char *tags, vector<osc_data> &data)
156 while(*tags)
158 data.push_back(osc_data());
159 read((osc_type)*tags++, *data.rbegin());
163 void osc_stream::write(const vector<osc_data> &data)
165 unsigned int pos = 0;
167 while(pos < data.size())
168 write(data[pos++]);
171 void osc_message_dump::receive_osc_message(std::string address, std::string type_tag, const std::vector<osc_data> &args)
173 printf("address: %s, type tag: %s\n", address.c_str(), type_tag.c_str());
174 for (unsigned int i = 0; i < args.size(); i++)
176 printf("argument %d is %s\n", i, args[i].to_string().c_str());
180 #endif