add MidiByteArray::compare_n()
[ardour.git] / libs / surfaces / push2 / 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
55 void MidiByteArray::copy (size_t count, MIDI::byte * arr)
56 {
57         for (size_t i = 0; i < count; ++i) {
58                 push_back (arr[i]);
59         }
60 }
61
62 MidiByteArray & operator <<  (MidiByteArray & mba, const MIDI::byte & b)
63 {
64         mba.push_back (b);
65         return mba;
66 }
67
68 MidiByteArray & operator <<  (MidiByteArray & mba, const MidiByteArray & barr)
69 {
70         back_insert_iterator<MidiByteArray> bit (mba);
71         copy (barr.begin(), barr.end(), bit);
72         return mba;
73 }
74
75 ostream & operator <<  (ostream & os, const MidiByteArray & mba)
76 {
77         os << "[";
78         char fill = os.fill('0');
79         for (MidiByteArray::const_iterator it = mba.begin(); it != mba.end(); ++it) {
80                 if  (it != mba.begin()) os << " ";
81                 os << hex << setw(2) << (int)*it;
82         }
83         os.fill (fill);
84         os << dec;
85         os << "]";
86         return os;
87 }
88
89 MidiByteArray & operator <<  (MidiByteArray & mba, const std::string & st)
90 {
91         /* note that this assumes that "st" is ASCII encoded
92          */
93
94         mba.insert (mba.end(), st.begin(), st.end());
95         return mba;
96 }
97
98 bool
99 MidiByteArray::compare_n (const MidiByteArray& other, MidiByteArray::size_type n) const
100 {
101         MidiByteArray::const_iterator us = begin();
102         MidiByteArray::const_iterator them = other.begin();
103
104         while (n && us != end() && them != other.end()) {
105                 if ((*us) != (*them)) {
106                         return false;
107                 }
108                 --n;
109                 ++us;
110                 ++them;
111         }
112
113         return true;
114 }
115