root/oscpack/tags/release_1_0_2/osc/OscPrintReceivedElements.cpp

Revision 16, 6.8 kB (checked in by ross, 3 years ago)

updated copyright notice to 2004-2005 and added license to files where it was missing

  • Property svn:eol-style set to native
Line 
1 /*
2         oscpack -- Open Sound Control packet manipulation library
3         http://www.audiomulch.com/~rossb/oscpack
4
5         Copyright (c) 2004-2005 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         Any person wishing to distribute modifications to the Software is
19         requested to send the modifications to the original developer so that
20         they can be incorporated into the canonical version.
21
22         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23         EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24         MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25         IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
26         ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27         CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28         WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30 #include "OscPrintReceivedElements.h"
31
32 #include <iostream>
33 #include <iomanip>
34 #include <ctime>
35
36
37 namespace osc{
38
39
40 std::ostream& operator<<( std::ostream & os,
41         const ReceivedMessageArgument& arg )
42 {
43     switch( arg.TypeTag() ){
44         case TRUE_TYPE_TAG:
45             os << "bool:true";
46             break;
47                
48         case FALSE_TYPE_TAG:
49             os << "bool:false";
50             break;
51
52         case NIL_TYPE_TAG:
53             os << "(Nil)";
54             break;
55
56         case INFINITUM_TYPE_TAG:
57             os << "(Infinitum)";
58             break;
59
60         case INT32_TYPE_TAG:
61             os << "int32:" << arg.AsInt32Unchecked();
62             break;
63
64         case FLOAT_TYPE_TAG:
65             os << "float32:" << arg.AsFloatUnchecked();
66             break;
67
68         case CHAR_TYPE_TAG:
69             {
70                 char s[2] = {0};
71                 s[0] = arg.AsCharUnchecked();
72                 os << "char:'" << s << "'";
73             }
74             break;
75
76         case RGBA_COLOR_TYPE_TAG:
77             {
78                 uint32 color = arg.AsRgbaColorUnchecked();
79                
80                 os << "RGBA:0x"
81                         << std::hex << std::setfill('0')
82                         << std::setw(2) << (int)((color>>24) & 0xFF)
83                         << std::setw(2) << (int)((color>>16) & 0xFF)
84                         << std::setw(2) << (int)((color>>8) & 0xFF)
85                         << std::setw(2) << (int)(color & 0xFF)
86                         << std::setfill(' ');
87                 os.unsetf(std::ios::basefield);
88             }
89             break;
90
91         case MIDI_MESSAGE_TYPE_TAG:
92             {
93                 uint32 m = arg.AsMidiMessageUnchecked();
94                 os << "midi (port, status, data1, data2):<<"
95                         << std::hex << std::setfill('0')
96                         << "0x" << std::setw(2) << (int)((m>>24) & 0xFF)
97                         << " 0x" << std::setw(2) << (int)((m>>16) & 0xFF)
98                         << " 0x" << std::setw(2) << (int)((m>>8) & 0xFF)
99                         << " 0x" << std::setw(2) << (int)(m & 0xFF)
100                         << std::setfill(' ') << ">>";
101                 os.unsetf(std::ios::basefield);
102             }
103             break;
104                                
105         case INT64_TYPE_TAG:
106             os << "int64:" << arg.AsInt64Unchecked();
107             break;
108
109         case TIME_TAG_TYPE_TAG:
110             {
111                 os << "OSC-timetag:" << arg.AsTimeTagUnchecked();
112
113                 std::time_t t =
114                         (unsigned long)( arg.AsTimeTagUnchecked() >> 32 );
115
116                 // strip trailing newline from string returned by ctime
117                 const char *timeString = std::ctime( &t );
118                 size_t len = strlen( timeString );
119                 char *s = new char[ len + 1 ];
120                 strcpy( s, timeString );
121                 if( len )
122                     s[ len - 1 ] = '\0';
123                    
124                 os << " " << s;
125             }
126             break;
127                
128         case DOUBLE_TYPE_TAG:
129             os << "double:" << arg.AsDoubleUnchecked();
130             break;
131
132         case STRING_TYPE_TAG:
133             os << "OSC-string:`" << arg.AsStringUnchecked() << "'";
134             break;
135                
136         case SYMBOL_TYPE_TAG:
137             os << "OSC-string (symbol):`" << arg.AsSymbolUnchecked() << "'";
138             break;
139
140         case BLOB_TYPE_TAG:
141             {
142                 unsigned long size;
143                 const void *data;
144                 arg.AsBlobUnchecked( data, size );
145                 os << "OSC-blob:<<" << std::hex << std::setfill('0');
146                 unsigned char *p = (unsigned char*)data;
147                 for( unsigned long i = 0; i < size; ++i ){
148                     os << "0x" << std::setw(2) << int(p[i]);
149                     if( i != size-1 )
150                         os << ' ';
151                 }
152                 os.unsetf(std::ios::basefield);
153                 os << ">>" << std::setfill(' ');
154             }
155             break;
156
157         default:
158             os << "unknown";
159     }
160
161     return os;
162 }
163
164
165 std::ostream& operator<<( std::ostream & os, const ReceivedMessage& m )
166 {
167
168     os << "[" << m.AddressPattern();
169     bool first = true;
170
171     for( ReceivedMessage::const_iterator i = m.ArgumentsBegin();
172             i != m.ArgumentsEnd(); ++i ){
173         if( first ){
174             os << " ";
175             first = false;
176         }else{
177             os << ", ";
178         }
179
180         os << *i;
181     }
182
183     os << "]";
184
185     return os;
186 }
187
188
189 std::ostream& operator<<( std::ostream & os, const ReceivedBundle& b )
190 {
191     static int indent = 0;
192
193     for( int j=0; j < indent; ++j )
194         os << "  ";
195     os << "{ ( ";
196     if( b.TimeTag() == 1 )
197         os << "immediate";
198     else
199         os << b.TimeTag();
200     os << " )\n";
201
202     ++indent;
203    
204     for( ReceivedBundle::const_iterator i = b.ElementsBegin();
205             i != b.ElementsEnd(); ++i ){
206         if( i->IsBundle() ){
207             ReceivedBundle b(*i);
208             os << b << "\n";
209         }else{
210             ReceivedMessage m(*i);
211             for( int j=0; j < indent; ++j )
212                 os << "  ";
213             os << m << "\n";
214         }
215     }
216
217     --indent;
218
219     for( int j=0; j < indent; ++j )
220         os << "  ";
221     os << "}";
222
223     return os;
224 }
225
226
227 std::ostream& operator<<( std::ostream & os, const ReceivedPacket& p )
228 {
229     if( p.IsBundle() ){
230         ReceivedBundle b(p);
231         os << b << "\n";
232     }else{
233         ReceivedMessage m(p);
234         os << m << "\n";
235     }
236
237     return os;
238 }
239
240 } // namespace osc
241
Note: See TracBrowser for help on using the browser.