00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "WOscMessage.h"
00040 #include <string.h>
00041 #include "WOscString.h"
00042 #include "WOscUtil.h"
00043 #include "WOscException.h"
00044
00045
00046 #define W_OSC_EMPTY_TYPE_TAG ",\0\0\0"
00047
00048 #define W_OSC_EMPTY_TYPE_TAG_LEN 4
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 WOscMessage::WOscMessage(const char* address){
00072
00073
00074 if ( address == NULL )
00075 throw WOscException(ERR_NULL_ADDRESS, __FILE__, __LINE__);
00076
00077 if ( address[0] != '/' )
00078 throw WOscException(ERR_INVALID_ADDRESS_NO_SLASH, __FILE__, __LINE__);
00079
00080
00081 m_address = address;
00082
00083
00084 m_typeTag = W_OSC_EMPTY_TYPE_TAG;
00085
00086 m_floats = NULL;
00087 m_numFloats = 0;
00088 m_ints = NULL;
00089 m_numInts = 0;
00090 m_strings = NULL;
00091 m_numStrings = 0;
00092 m_blobs = NULL;
00093 m_numBlobs = 0;
00094
00095 m_buffer = NULL;
00096
00097 GenerateBufferFromMembers();
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 WOscMessage::WOscMessage(const char* buffer, int bufferLen){
00131
00132 m_floats = NULL;
00133 m_numFloats = 0;
00134 m_ints = NULL;
00135 m_numInts = 0;
00136 m_strings = NULL;
00137 m_numStrings = 0;
00138 m_blobs = NULL;
00139 m_numBlobs = 0;
00140
00141 m_buffer = NULL;
00142
00143
00144 if (bufferLen < 1)
00145 throw WOscException(ERR_NULL_BUFFER, __FILE__, __LINE__);
00146
00147 if (buffer[0] != '/')
00148 throw WOscException(ERR_INVALID_ADDRESS_NO_SLASH, __FILE__, __LINE__);
00149
00150
00151 m_address = buffer;
00152
00153
00154 int pos = m_address.GetSize();
00155
00156 m_typeTag = W_OSC_EMPTY_TYPE_TAG;
00157
00158
00159 if (bufferLen <= pos){
00160
00161 GenerateBufferFromMembers();
00162 return;
00163 }
00164
00165 WOscString OscTypeTag = buffer + pos;
00166
00167
00168 const char* typeTag = OscTypeTag.GetBuffer();
00169 int typeTagSize = OscTypeTag.GetSize();
00170 pos += typeTagSize;
00171
00172 for ( int i = 0; i < typeTagSize; i++ ){
00173 switch (typeTag[i]){
00174 case 'i':
00175 Add(WOscUtil::BufferTo<int>(buffer + pos));
00176 pos += WOscUtil::OSC_INT_SIZE;
00177 break;
00178 case 'f':
00179 Add(WOscUtil::BufferTo<float>(buffer + pos));
00180 pos += WOscUtil::OSC_FLOAT_SIZE;
00181 break;
00182 case 's':
00183 Add(buffer+pos);
00184 pos += m_strings[m_numStrings-1].GetSize();
00185 break;
00186 case 'b':{
00187 WOscBlob* blob = new WOscBlob(buffer + pos);
00188 Add( blob );
00189 pos += blob->GetBufferLen();
00190 delete blob;
00191 break;
00192 }
00193
00194 case ',':
00195 break;
00196 case '\0':
00197 i = typeTagSize;
00198 break;
00199 default:
00200 i = typeTagSize;
00201 break;
00202 }
00203 }
00204
00205 GenerateBufferFromMembers();
00206 }
00207
00208
00209
00210
00211
00212
00213 WOscMessage::~WOscMessage(){
00214
00215 if ( m_buffer )
00216 delete [] m_buffer;
00217
00218 if (m_floats) delete [] m_floats;
00219 if (m_ints) delete [] m_ints;
00220 if (m_strings) delete [] m_strings;
00221 if (m_blobs){
00222 for(int i=0; i<m_numBlobs; i++)
00223 delete m_blobs[i];
00224 delete [] m_blobs;
00225 }
00226
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236 WOscMessage::WOscMessage(const WOscMessage& rhs){
00237 Copy(rhs);
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 WOscMessage& WOscMessage::operator= (const WOscMessage& rhs){
00250 Copy(rhs);
00251 return *this;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 WOscMessage& WOscMessage::operator+= (const WOscMessage& rhs){
00269
00270 return *this;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 WOscMessage WOscMessage::operator+ (const WOscMessage& rhs){
00289 WOscMessage tmp(*this);
00290 tmp += rhs;
00291 return tmp;
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 WOscMessage::WOscMessage(const WOscMessage* message){
00305 Copy(*message);
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 void WOscMessage::Copy(const WOscMessage &msg){
00323
00324 int i;
00325
00326 m_address = msg.m_address;
00327 m_typeTag = msg.m_typeTag;
00328
00329 if ( (m_numFloats = msg.m_numFloats) > 0 ){
00330 m_floats = new float[m_numFloats];
00331 for ( i = 0; i < m_numFloats ; i++ )
00332 m_floats[i] = msg.m_floats[i];
00333 }else
00334 m_floats = NULL;
00335
00336 if ( (m_numInts = msg.m_numInts) > 0 ){
00337 m_ints = new int[m_numInts];
00338 for ( i = 0; i < m_numInts ; i++ )
00339 m_ints[i] = msg.m_ints[i];
00340 }else
00341 m_ints = NULL;
00342
00343 if ( (m_numStrings = msg.m_numStrings) > 0 ){
00344 m_strings = new WOscString[m_numStrings];
00345 for ( i = 0; i < m_numStrings ; i++ )
00346 m_strings[i] = msg.m_strings[i];
00347 }else
00348 m_strings = NULL;
00349
00350 if ( (m_numBlobs = msg.m_numBlobs) > 0 ){
00351 m_blobs = new WOscBlob*[m_numBlobs];
00352 for ( i = 0; i < m_numBlobs ; i++ )
00353 m_blobs[i] = new WOscBlob( msg.m_blobs[i] );
00354 }else
00355 m_blobs = NULL;
00356
00357 if ( (m_bufferLen = msg.m_bufferLen) > 0 ){
00358 m_buffer = new char[m_bufferLen];
00359 memcpy(m_buffer, msg.m_buffer, m_bufferLen);
00360 }else
00361 m_buffer = NULL;
00362
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 void WOscMessage::GenerateBufferFromMembers(){
00377
00378 int i, bufWrPos, size;
00379 int idx_i = 0, idx_f = 0, idx_s = 0, idx_b = 0;
00380
00381
00382 if (m_buffer)
00383 delete [] m_buffer;
00384
00385
00386
00387
00388
00389 int typeTagSize = m_typeTag.GetSize();
00390 m_bufferLen = m_address.GetSize() + typeTagSize;
00391
00392
00393
00394 const char* typeTag = m_typeTag.GetBuffer();
00395
00396 for ( i = 0; i < typeTagSize; i++ ){
00397 switch (typeTag[i]){
00398 case 'i':
00399 m_bufferLen += WOscUtil::OSC_INT_SIZE;
00400 break;
00401 case 'f':
00402 m_bufferLen += WOscUtil::OSC_FLOAT_SIZE;
00403 break;
00404 case 's':
00405 m_bufferLen += m_strings[idx_s++].GetSize();
00406 break;
00407 case 'b':
00408 m_bufferLen += m_blobs[idx_b++]->GetBufferLen();
00409 break;
00410
00411 case ',':
00412 break;
00413 case '\0':
00414 i = typeTagSize;
00415 break;
00416 default:
00417 i = typeTagSize;
00418 break;
00419 }
00420 }
00421
00422
00423 m_buffer = new char[m_bufferLen];
00424
00425
00426
00427
00428 m_address.GetBuffer(m_buffer, m_address.GetSize());
00429
00430
00431 m_typeTag.GetBuffer(m_buffer + m_address.GetSize(), m_typeTag.GetSize());
00432 bufWrPos = m_address.GetSize() + m_typeTag.GetSize();
00433
00434
00435 idx_i = idx_f = idx_s = idx_b = 0;
00436 for ( i = 0; i < typeTagSize; i++ ){
00437 switch (typeTag[i]){
00438 case 'i':
00439
00440 WOscUtil::FillBufferWith(&(m_buffer[bufWrPos]), m_ints[idx_i]);
00441
00442 bufWrPos += WOscUtil::OSC_INT_SIZE;
00443
00444 idx_i++;
00445 break;
00446 case 'f':
00447 WOscUtil::FillBufferWith(&(m_buffer[bufWrPos]), m_floats[idx_f]);
00448 bufWrPos += WOscUtil::OSC_FLOAT_SIZE;
00449 idx_f++;
00450 break;
00451 case 's':
00452 size = m_strings[idx_s].GetSize();
00453 m_strings[idx_s].GetBuffer(&(m_buffer[bufWrPos]), size );
00454 bufWrPos += size;
00455 idx_s++;
00456 break;
00457 case 'b':
00458 size = m_blobs[idx_b]->GetBufferLen();
00459 m_blobs[idx_b]->GetBuffer(&(m_buffer[bufWrPos]), size );
00460 bufWrPos += size;
00461 idx_b++;
00462 break;
00463 case ',':
00464
00465 break;
00466 case '\0':
00467 i = typeTagSize;
00468 break;
00469 default:
00470 i = typeTagSize;
00471 break;
00472 }
00473 }
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 void WOscMessage::Add(float floatArg){
00489
00490 m_typeTag += 'f';
00491
00492 if ( m_floats == NULL ){
00493 m_floats = new float[1];
00494 m_floats[0] = floatArg;
00495 m_numFloats = 1;
00496 }else{
00497 m_numFloats++;
00498 float* newFloats = new float[m_numFloats];
00499 for( int i = 0; i < m_numFloats-1; i++ )
00500 newFloats[i] = m_floats[i];
00501 newFloats[m_numFloats-1] = floatArg;
00502 delete [] m_floats;
00503 m_floats = newFloats;
00504 }
00505 GenerateBufferFromMembers();
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 void WOscMessage::Add(int intArg){
00520
00521 m_typeTag += 'i';
00522
00523 if ( m_ints == NULL ){
00524 m_ints = new int[1];
00525 m_ints[0] = intArg;
00526 m_numInts = 1;
00527 }else{
00528 m_numInts++;
00529 int* newInts = new int[m_numInts];
00530 for( int i = 0; i < m_numInts-1; i++ )
00531 newInts[i] = m_ints[i];
00532 newInts[m_numInts-1] = intArg;
00533 delete [] m_ints;
00534 m_ints = newInts;
00535 }
00536 GenerateBufferFromMembers();
00537 }
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 void WOscMessage::Add(const char* s){
00551 m_typeTag += 's';
00552
00553 if ( m_strings == NULL ){
00554 m_strings = new WOscString[1];
00555 m_strings[0] = s;
00556 m_numStrings = 1;
00557 }else{
00558 m_numStrings++;
00559 WOscString* newStrings = new WOscString[m_numStrings];
00560 for( int i = 0; i < m_numStrings-1; i++ )
00561 newStrings[i] = m_strings[i];
00562 newStrings[m_numStrings-1] = s;
00563 delete [] m_strings;
00564 m_strings = newStrings;
00565 }
00566 GenerateBufferFromMembers();
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582 void WOscMessage::Add(WOscString& s){
00583
00584 m_typeTag += 's';
00585
00586 if ( m_strings == NULL ){
00587 m_strings = new WOscString[1];
00588 m_strings[0] = s;
00589 m_numStrings = 1;
00590 }else{
00591 m_numStrings++;
00592 WOscString* newStrings = new WOscString[m_numStrings];
00593 for( int i = 0; i < m_numStrings-1; i++ )
00594 newStrings[i] = m_strings[i];
00595 newStrings[m_numStrings-1] = s;
00596 delete [] m_strings;
00597 m_strings = newStrings;
00598 }
00599 GenerateBufferFromMembers();
00600 }
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 void WOscMessage::Add(WOscBlob* b){
00618 m_typeTag += 'b';
00619
00620 if ( m_blobs ){
00621 m_numBlobs++;
00622 WOscBlob** newBlobs = new WOscBlob*[m_numStrings];
00623 for( int i = 0; i < m_numBlobs-1; i++ )
00624 newBlobs[i] = m_blobs[i];
00625 newBlobs[m_numBlobs-1] = new WOscBlob(b);
00626 delete [] m_blobs;
00627 m_blobs = newBlobs;
00628 }else{
00629 m_blobs = new WOscBlob*[1];
00630 m_blobs[0] = new WOscBlob(b);
00631 m_numBlobs++;
00632 }
00633 GenerateBufferFromMembers();
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 int WOscMessage::GetNumFloats() const {
00646 return m_numFloats;
00647 }
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 int WOscMessage::GetNumInts() const {
00659 return m_numInts;
00660 }
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671 int WOscMessage::GetNumStrings() const {
00672 return m_numStrings;
00673 }
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683 int WOscMessage::GetNumBlobs() const {
00684 return m_numBlobs;
00685 }
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706 float
00707 WOscMessage::GetFloat(int idx) const
00708 {
00709 if ( idx > m_numFloats )
00710 throw WOscException(ERR_INVALID_INDEX, __FILE__, __LINE__);
00711 return m_floats[idx];
00712 }
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 int
00732 WOscMessage::GetInt(int idx) const
00733 {
00734 if ( idx > m_numInts )
00735 throw WOscException(ERR_INVALID_INDEX, __FILE__, __LINE__);
00736 return m_ints[idx];
00737 }
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 const WOscString& WOscMessage::GetString(int idx) const {
00757 if ( idx > m_numStrings )
00758 throw WOscException(ERR_INVALID_INDEX, __FILE__, __LINE__);
00759 return m_strings[idx];
00760 }
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779 const WOscBlob*
00780 WOscMessage::GetBlob(int idx) const
00781 {
00782 if ( idx > m_numBlobs )
00783 throw WOscException(ERR_INVALID_INDEX, __FILE__, __LINE__);
00784 return m_blobs[idx];
00785 }
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807 void
00808 WOscMessage::GetBuffer(char* buffer, int bufferLen)
00809 {
00810 if ( bufferLen < m_bufferLen )
00811 throw WOscException(ERR_OSC_STR_BUFF_TOO_SMALL, __FILE__, __LINE__);
00812 for ( int i = 0; i < bufferLen; i++ )
00813 buffer[i] = m_buffer[i];
00814 }
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833 const char* WOscMessage::GetBuffer(){
00834 return m_buffer;
00835 }
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846 int WOscMessage::GetBufferLen() {
00847 return m_bufferLen;
00848 }
00849
00850
00851
00852
00853
00854
00855
00856
00857 const WOscString& WOscMessage::GetOscAddress() const
00858 {
00859 return m_address;
00860 }