OscPrintReceivedElements.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. oscpack -- Open Sound Control packet manipulation library
  3. http://www.audiomulch.com/~rossb/oscpack
  4. Copyright (c) 2004-2005 Ross Bencina <[email protected]>
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. Any person wishing to distribute modifications to the Software is
  15. requested to send the modifications to the original developer so that
  16. they can be incorporated into the canonical version.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  21. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "OscPrintReceivedElements.h"
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <ctime>
  29. namespace osc{
  30. std::ostream& operator<<( std::ostream & os,
  31. const ReceivedMessageArgument& arg )
  32. {
  33. switch( arg.TypeTag() ){
  34. case TRUE_TYPE_TAG:
  35. os << "bool:true";
  36. break;
  37. case FALSE_TYPE_TAG:
  38. os << "bool:false";
  39. break;
  40. case NIL_TYPE_TAG:
  41. os << "(Nil)";
  42. break;
  43. case INFINITUM_TYPE_TAG:
  44. os << "(Infinitum)";
  45. break;
  46. case INT32_TYPE_TAG:
  47. os << "int32:" << arg.AsInt32Unchecked();
  48. break;
  49. case FLOAT_TYPE_TAG:
  50. os << "float32:" << arg.AsFloatUnchecked();
  51. break;
  52. case CHAR_TYPE_TAG:
  53. {
  54. char s[2] = {0};
  55. s[0] = arg.AsCharUnchecked();
  56. os << "char:'" << s << "'";
  57. }
  58. break;
  59. case RGBA_COLOR_TYPE_TAG:
  60. {
  61. uint32 color = arg.AsRgbaColorUnchecked();
  62. os << "RGBA:0x"
  63. << std::hex << std::setfill('0')
  64. << std::setw(2) << (int)((color>>24) & 0xFF)
  65. << std::setw(2) << (int)((color>>16) & 0xFF)
  66. << std::setw(2) << (int)((color>>8) & 0xFF)
  67. << std::setw(2) << (int)(color & 0xFF)
  68. << std::setfill(' ');
  69. os.unsetf(std::ios::basefield);
  70. }
  71. break;
  72. case MIDI_MESSAGE_TYPE_TAG:
  73. {
  74. uint32 m = arg.AsMidiMessageUnchecked();
  75. os << "midi (port, status, data1, data2):<<"
  76. << std::hex << std::setfill('0')
  77. << "0x" << std::setw(2) << (int)((m>>24) & 0xFF)
  78. << " 0x" << std::setw(2) << (int)((m>>16) & 0xFF)
  79. << " 0x" << std::setw(2) << (int)((m>>8) & 0xFF)
  80. << " 0x" << std::setw(2) << (int)(m & 0xFF)
  81. << std::setfill(' ') << ">>";
  82. os.unsetf(std::ios::basefield);
  83. }
  84. break;
  85. case INT64_TYPE_TAG:
  86. os << "int64:" << arg.AsInt64Unchecked();
  87. break;
  88. case TIME_TAG_TYPE_TAG:
  89. {
  90. os << "OSC-timetag:" << arg.AsTimeTagUnchecked();
  91. std::time_t t =
  92. (unsigned long)( arg.AsTimeTagUnchecked() >> 32 );
  93. // strip trailing newline from string returned by ctime
  94. const char *timeString = std::ctime( &t );
  95. size_t len = strlen( timeString );
  96. char *s = new char[ len + 1 ];
  97. strcpy( s, timeString );
  98. if( len )
  99. s[ len - 1 ] = '\0';
  100. os << " " << s;
  101. }
  102. break;
  103. case DOUBLE_TYPE_TAG:
  104. os << "double:" << arg.AsDoubleUnchecked();
  105. break;
  106. case STRING_TYPE_TAG:
  107. os << "OSC-string:`" << arg.AsStringUnchecked() << "'";
  108. break;
  109. case SYMBOL_TYPE_TAG:
  110. os << "OSC-string (symbol):`" << arg.AsSymbolUnchecked() << "'";
  111. break;
  112. case BLOB_TYPE_TAG:
  113. {
  114. unsigned long size;
  115. const void *data;
  116. arg.AsBlobUnchecked( data, size );
  117. os << "OSC-blob:<<" << std::hex << std::setfill('0');
  118. unsigned char *p = (unsigned char*)data;
  119. for( unsigned long i = 0; i < size; ++i ){
  120. os << "0x" << std::setw(2) << int(p[i]);
  121. if( i != size-1 )
  122. os << ' ';
  123. }
  124. os.unsetf(std::ios::basefield);
  125. os << ">>" << std::setfill(' ');
  126. }
  127. break;
  128. default:
  129. os << "unknown";
  130. }
  131. return os;
  132. }
  133. std::ostream& operator<<( std::ostream & os, const ReceivedMessage& m )
  134. {
  135. os << "[" << m.AddressPattern();
  136. bool first = true;
  137. for( ReceivedMessage::const_iterator i = m.ArgumentsBegin();
  138. i != m.ArgumentsEnd(); ++i ){
  139. if( first ){
  140. os << " ";
  141. first = false;
  142. }else{
  143. os << ", ";
  144. }
  145. os << *i;
  146. }
  147. os << "]";
  148. return os;
  149. }
  150. std::ostream& operator<<( std::ostream & os, const ReceivedBundle& b )
  151. {
  152. static int indent = 0;
  153. for( int j=0; j < indent; ++j )
  154. os << " ";
  155. os << "{ ( ";
  156. if( b.TimeTag() == 1 )
  157. os << "immediate";
  158. else
  159. os << b.TimeTag();
  160. os << " )\n";
  161. ++indent;
  162. for( ReceivedBundle::const_iterator i = b.ElementsBegin();
  163. i != b.ElementsEnd(); ++i ){
  164. if( i->IsBundle() ){
  165. ReceivedBundle b(*i);
  166. os << b << "\n";
  167. }else{
  168. ReceivedMessage m(*i);
  169. for( int j=0; j < indent; ++j )
  170. os << " ";
  171. os << m << "\n";
  172. }
  173. }
  174. --indent;
  175. for( int j=0; j < indent; ++j )
  176. os << " ";
  177. os << "}";
  178. return os;
  179. }
  180. std::ostream& operator<<( std::ostream & os, const ReceivedPacket& p )
  181. {
  182. if( p.IsBundle() ){
  183. ReceivedBundle b(p);
  184. os << b << "\n";
  185. }else{
  186. ReceivedMessage m(p);
  187. os << m << "\n";
  188. }
  189. return os;
  190. }
  191. } // namespace osc