Jamoma API  0.6.0.a19
OscPrintReceivedElements.cpp
1 /*
2  oscpack -- Open Sound Control (OSC) packet manipulation library
3  http://www.rossbencina.com/code/oscpack
4 
5  Copyright (c) 2004-2013 Ross Bencina <rossb@audiomulch.com>
6 
7  Permission is hereby granted, free of charge, to any person obtaining
8  a copy of this software and associated documentation files
9  (the "Software"), to deal in the Software without restriction,
10  including without limitation the rights to use, copy, modify, merge,
11  publish, distribute, sublicense, and/or sell copies of the Software,
12  and to permit persons to whom the Software is furnished to do so,
13  subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
22  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26 
27 /*
28  The text above constitutes the entire oscpack license; however,
29  the oscpack developer(s) also make the following non-binding requests:
30 
31  Any person wishing to distribute modifications to the Software is
32  requested to send the modifications to the original developer so that
33  they can be incorporated into the canonical version. It is also
34  requested that these non-binding requests be included whenever the
35  above license is reproduced.
36 */
37 #include "OscPrintReceivedElements.h"
38 
39 #include <cstring>
40 #include <ctime>
41 #include <iostream>
42 #include <iomanip>
43 
44 #if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug
45 namespace std {
46 using ::__strcpy__; // avoid error: E2316 '__strcpy__' is not a member of 'std'.
47 }
48 #endif
49 
50 namespace osc{
51 
52 
53 std::ostream& operator<<( std::ostream & os,
54  const ReceivedMessageArgument& arg )
55 {
56  switch( arg.TypeTag() ){
57  case TRUE_TYPE_TAG:
58  os << "bool:true";
59  break;
60 
61  case FALSE_TYPE_TAG:
62  os << "bool:false";
63  break;
64 
65  case NIL_TYPE_TAG:
66  os << "(Nil)";
67  break;
68 
69  case INFINITUM_TYPE_TAG:
70  os << "(Infinitum)";
71  break;
72 
73  case INT32_TYPE_TAG:
74  os << "int32:" << arg.AsInt32Unchecked();
75  break;
76 
77  case FLOAT_TYPE_TAG:
78  os << "float32:" << arg.AsFloatUnchecked();
79  break;
80 
81  case CHAR_TYPE_TAG:
82  {
83  char s[2] = {0};
84  s[0] = arg.AsCharUnchecked();
85  os << "char:'" << s << "'";
86  }
87  break;
88 
89  case RGBA_COLOR_TYPE_TAG:
90  {
91  uint32 color = arg.AsRgbaColorUnchecked();
92 
93  os << "RGBA:0x"
94  << std::hex << std::setfill('0')
95  << std::setw(2) << (int)((color>>24) & 0xFF)
96  << std::setw(2) << (int)((color>>16) & 0xFF)
97  << std::setw(2) << (int)((color>>8) & 0xFF)
98  << std::setw(2) << (int)(color & 0xFF)
99  << std::setfill(' ');
100  os.unsetf(std::ios::basefield);
101  }
102  break;
103 
104  case MIDI_MESSAGE_TYPE_TAG:
105  {
106  uint32 m = arg.AsMidiMessageUnchecked();
107  os << "midi (port, status, data1, data2):<<"
108  << std::hex << std::setfill('0')
109  << "0x" << std::setw(2) << (int)((m>>24) & 0xFF)
110  << " 0x" << std::setw(2) << (int)((m>>16) & 0xFF)
111  << " 0x" << std::setw(2) << (int)((m>>8) & 0xFF)
112  << " 0x" << std::setw(2) << (int)(m & 0xFF)
113  << std::setfill(' ') << ">>";
114  os.unsetf(std::ios::basefield);
115  }
116  break;
117 
118  case INT64_TYPE_TAG:
119  os << "int64:" << arg.AsInt64Unchecked();
120  break;
121 
122  case TIME_TAG_TYPE_TAG:
123  {
124  os << "OSC-timetag:" << arg.AsTimeTagUnchecked() << " ";
125 
126  std::time_t t =
127  (unsigned long)( arg.AsTimeTagUnchecked() >> 32 );
128 
129  const char *timeString = std::ctime( &t );
130  size_t len = std::strlen( timeString );
131 
132  // -1 to omit trailing newline from string returned by ctime()
133  if( len > 1 )
134  os.write( timeString, len - 1 );
135  }
136  break;
137 
138  case DOUBLE_TYPE_TAG:
139  os << "double:" << arg.AsDoubleUnchecked();
140  break;
141 
142  case STRING_TYPE_TAG:
143  os << "OSC-string:`" << arg.AsStringUnchecked() << "'";
144  break;
145 
146  case SYMBOL_TYPE_TAG:
147  os << "OSC-string (symbol):`" << arg.AsSymbolUnchecked() << "'";
148  break;
149 
150  case BLOB_TYPE_TAG:
151  {
152  const void *data;
153  osc_bundle_element_size_t size;
154  arg.AsBlobUnchecked( data, size );
155  os << "OSC-blob:<<" << std::hex << std::setfill('0');
156  unsigned char *p = (unsigned char*)data;
157  for( osc_bundle_element_size_t i = 0; i < size; ++i ){
158  os << "0x" << std::setw(2) << int(p[i]);
159  if( i != size-1 )
160  os << ' ';
161  }
162  os.unsetf(std::ios::basefield);
163  os << ">>" << std::setfill(' ');
164  }
165  break;
166 
167  case ARRAY_BEGIN_TYPE_TAG:
168  os << "[";
169  break;
170 
171  case ARRAY_END_TYPE_TAG:
172  os << "]";
173  break;
174 
175  default:
176  os << "unknown";
177  }
178 
179  return os;
180 }
181 
182 
183 std::ostream& operator<<( std::ostream & os, const ReceivedMessage& m )
184 {
185  os << "[";
186  if( m.AddressPatternIsUInt32() )
187  os << m.AddressPatternAsUInt32();
188  else
189  os << m.AddressPattern();
190 
191  bool first = true;
192  for( ReceivedMessage::const_iterator i = m.ArgumentsBegin();
193  i != m.ArgumentsEnd(); ++i ){
194  if( first ){
195  os << " ";
196  first = false;
197  }else{
198  os << ", ";
199  }
200 
201  os << *i;
202  }
203 
204  os << "]";
205 
206  return os;
207 }
208 
209 
210 std::ostream& operator<<( std::ostream & os, const ReceivedBundle& b )
211 {
212  static int indent = 0;
213 
214  for( int j=0; j < indent; ++j )
215  os << " ";
216  os << "{ ( ";
217  if( b.TimeTag() == 1 )
218  os << "immediate";
219  else
220  os << b.TimeTag();
221  os << " )\n";
222 
223  ++indent;
224 
225  for( ReceivedBundle::const_iterator i = b.ElementsBegin();
226  i != b.ElementsEnd(); ++i ){
227  if( i->IsBundle() ){
228  ReceivedBundle b(*i);
229  os << b << "\n";
230  }else{
231  ReceivedMessage m(*i);
232  for( int j=0; j < indent; ++j )
233  os << " ";
234  os << m << "\n";
235  }
236  }
237 
238  --indent;
239 
240  for( int j=0; j < indent; ++j )
241  os << " ";
242  os << "}";
243 
244  return os;
245 }
246 
247 
248 std::ostream& operator<<( std::ostream & os, const ReceivedPacket& p )
249 {
250  if( p.IsBundle() ){
251  ReceivedBundle b(p);
252  os << b << "\n";
253  }else{
254  ReceivedMessage m(p);
255  os << m << "\n";
256  }
257 
258  return os;
259 }
260 
261 } // namespace osc
STL namespace.