d130564c0f87e1deef2742a11014e572fb6912de
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / architecture / osclib / faust / src / msg / Message.cpp
1 /*
2
3 INScore Project
4
5 Copyright (C) 2011 Grame
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
22 research@grame.fr
23
24 */
25
26 #include <iostream>
27 #include <sstream>
28
29 #include "Message.h"
30 #include "OSCFError.h"
31
32 using namespace std;
33
34 namespace oscfaust
35 {
36
37 //--------------------------------------------------------------------------
38 // Message implementation
39 //--------------------------------------------------------------------------
40 Message::Message(const Message& msg)
41 {
42 setAddress (msg.address());
43 fArguments = msg.params();
44 }
45
46 //--------------------------------------------------------------------------
47 static string escape (const string& str)
48 {
49 string out;
50 const char *ptr = str.c_str();
51 while (*ptr) {
52 char c = *ptr++;
53 if (c == '"')
54 out += "\\\"";
55 else out += c;
56 }
57 return out;
58 }
59
60 //--------------------------------------------------------------------------
61 void Message::print(std::ostream& out) const
62 {
63 out << address() << " " ;
64 argslist::const_iterator i = params().begin();
65
66 ios::fmtflags f = out.flags ( ios::showpoint );
67 while (i != params().end()) {
68 MsgParam<string>* s = dynamic_cast<MsgParam<string>*>((baseparam*)(*i));
69 if (s) out << "\"" << escape(s->getValue()) << "\" ";
70 MsgParam<int>* ip = dynamic_cast<MsgParam<int>*>((baseparam*)(*i));
71 if (ip) out << ip->getValue() << " ";
72 MsgParam<float>* f = dynamic_cast<MsgParam<float>*>((baseparam*)(*i));
73 if (f) out << f->getValue() << " ";
74 i++;
75 }
76 out.flags ( f );
77 }
78
79 //--------------------------------------------------------------------------
80 void Message::print(OSCStream& out) const
81 {
82 out << OSCStart(address().c_str());
83 printArgs(out);
84 out << OSCEnd();
85 }
86
87 //--------------------------------------------------------------------------
88 void Message::printArgs(OSCStream& out) const
89 {
90 for (int i=0; i < size(); i++) {
91 string str; float fv; int iv;
92 if (param(i, fv)) out << fv;
93 else if (param(i, iv)) out << iv;
94 else if (param(i, str)) out << str;
95 else OSCFErr << "Message::print(OSCStream& out): unknown message parameter type" << OSCFEndl;
96 }
97 }
98
99 } // end namespoace