1c27c82be1c1e3f1cc6224f31e59ded21dead893
[ardour.git] / libs / surfaces / mackie / midi_byte_array.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "midi_byte_array.h"
19
20 #include <iostream>
21 #include <string>
22 #include <sstream>
23 #include <vector>
24 #include <algorithm>
25 #include <cstdarg>
26 #include <iomanip>
27 #include <stdexcept>
28
29 using namespace std;
30
31 MidiByteArray::MidiByteArray( size_t size, MIDI::byte array[] )
32 : std::vector<MIDI::byte>()
33 {
34         for ( size_t i = 0; i < size; ++i )
35         {
36                 push_back( array[i] );
37         }                       
38 }
39
40 MidiByteArray::MidiByteArray( size_t count, MIDI::byte first, ... )
41 : vector<MIDI::byte>()
42 {
43         push_back( first );
44         va_list var_args;
45         va_start( var_args, first );
46         for ( size_t i = 1; i < count; ++i )
47         {
48                 MIDI::byte b = va_arg( var_args, int );
49                 push_back( b );
50         }
51         va_end( var_args );
52 }
53
54 boost::shared_array<MIDI::byte> MidiByteArray::bytes() const
55 {
56         MIDI::byte * buf = new MIDI::byte[size()];
57         const_iterator it = begin();
58         for( MIDI::byte * ptr = buf; it != end(); ++it )
59         {
60                 *ptr++ = *it;
61         }
62         return boost::shared_array<MIDI::byte>( buf );
63 }
64
65 void MidiByteArray::copy( size_t count, MIDI::byte * arr )
66 {
67         for( size_t i = 0; i < count; ++i )
68         {
69                 push_back( arr[i] );
70         }
71 }
72
73 MidiByteArray & operator << ( MidiByteArray & mba, const MIDI::byte & b )
74 {
75         mba.push_back( b );
76         return mba;
77 }
78
79 MidiByteArray & operator << ( MidiByteArray & mba, const MidiByteArray & barr )
80 {
81         back_insert_iterator<MidiByteArray> bit( mba );
82         copy( barr.begin(), barr.end(), bit );
83         return mba;
84 }
85
86 ostream & operator << ( ostream & os, const MidiByteArray & mba )
87 {
88         os << "[";
89         char fill = os.fill('0');
90         for( MidiByteArray::const_iterator it = mba.begin(); it != mba.end(); ++it )
91         {
92                 if ( it != mba.begin() ) os << " ";
93                 os << hex << setw(2) << (int)*it;
94         }
95         os.fill( fill );
96         os << dec;
97         os << "]";
98         return os;
99 }
100
101 MidiByteArray & operator << ( MidiByteArray & mba, const std::string & st )
102 {
103         for ( string::const_iterator it = st.begin(); it != st.end(); ++it )
104         {
105                 mba << *it;
106         }
107         return mba;
108 }