/[svn]/linuxsampler/trunk/src/network/lscpresultset.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/network/lscpresultset.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1349 - (show annotations) (download)
Sat Sep 15 11:05:38 2007 UTC (16 years, 7 months ago) by persson
File size: 6516 byte(s)
* made sure that LSCP syntax is not affected by gigedit locale
  settings

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 /*********************************************************
25 * This class helps to constuct valid resultsets per
26 * LSCP protocol specification
27 *
28 * Valid results include:
29 * OK - to ack the request
30 * Single line to ack the requests and give status
31 * Several lines of information in the following format:
32 * LABEL0: VALUE0
33 * LABEL1: VALUE1
34 * VALELx: VALUEx
35 * .
36 *
37 * ******************************************************/
38
39 #include "lscpresultset.h"
40 #include <iomanip>
41
42 //Construct an empty resultset
43 //Default index is -1 meaning the resultset doesn't have an index
44 LSCPResultSet::LSCPResultSet(int index) {
45 result_index = index;
46 count = 0;
47 storage = "";
48 result_type = result_type_success;
49 }
50
51 //Construct a resultset with a single line
52 //Default index is -1 meaning the resultset doesn't have an index
53 LSCPResultSet::LSCPResultSet(String Value, int index) {
54 result_index = index;
55 count = 1;
56 storage = Value + "\r\n";
57 result_type = result_type_success;
58 }
59
60 //Add a label/value pair to the resultset
61 //Values could be of different types for now supports String, int and float.
62 void LSCPResultSet::Add(String Label, String Value) {
63 if (count == -1)
64 throw Exception("Attempting to change already produced resultset");
65 if (result_type != result_type_success)
66 throw Exception("Attempting to create illegal resultset");
67 storage = storage + Label + ": " + Value + "\r\n";
68 count = 2; // results in form of "Label: Value" should always be handled as multi line responses
69 }
70
71 void LSCPResultSet::Add(String Label, const char* pValue) {
72 Add(Label, String(pValue));
73 }
74
75 //Add SQL resultset row
76 void LSCPResultSet::Add(int columns, char** argv) {
77 for (int i = 0; i < columns; i++)
78 {
79 storage += argv[i];
80 if ((i+1) < columns)
81 storage += "|";
82 }
83 storage += "\r\n";
84 count = 2; //This result is always multiline.
85 }
86
87 void LSCPResultSet::Add(int Value) {
88 Add(ToString(Value));
89 }
90
91 void LSCPResultSet::Add(String Label, int Value) {
92 Add(Label, ToString(Value));
93 }
94
95 void LSCPResultSet::Add(String Label, float Value) {
96 std::stringstream ss;
97 ss.imbue(std::locale::classic());
98 ss << std::fixed << std::setprecision(3) << Value;
99 Add(Label, ss.str());
100 }
101
102 void LSCPResultSet::Add(String Label, bool Value) {
103 String s = (Value) ? "true" : "false";
104 Add(Label, s);
105 }
106
107 //Add a single string to the resultset
108 void LSCPResultSet::Add(String Value) {
109 if (result_type != result_type_success)
110 throw Exception("Attempting to create illegal resultset");
111 if (count == -1)
112 throw Exception("Attempting to change already produced resultset");
113 if (count != 0)
114 throw Exception("Attempting to create illegal resultset");
115 storage = Value + "\r\n";
116 count = 1;
117 }
118
119 //Generate an error result set from an exception.
120 //Per LSCP spec, error result is a sinle line in the following format:
121 //ERR:<CODE>:Message text\r\n
122 //This method will be used to generate unknown errors only (code 0)
123 //To generate errors with other codes as well as warnings use other methods (below).
124 //Because this is an unknown error, this method will also print message to the stderr.
125 void LSCPResultSet::Error(Exception e) {
126 e.PrintMessage();
127 Error(e.Message());
128 }
129
130 //This will construct an error with a string and error code
131 //code has a default of 0
132 //String has a default of "Undefined Error"
133 void LSCPResultSet::Error (String message, int code) {
134 //Even though this is must be a single line resultset we won't throw
135 //anything here because this is already part of exception handling.
136 //We'll just 'forget' all previous results (if any) from this resultset.
137 result_type = result_type_error;
138 storage = "ERR:" + ToString(code) + ":" + message + "\r\n";
139 count = 1;
140 }
141
142 //This will construct a warning with a string and error code
143 //code has a default of 0
144 //String has a default of "Undefined Error"
145 void LSCPResultSet::Warning (String message, int code) {
146 //FIXME: DO we want warnings as part of the resultset or
147 //do we want them to work like errors??? For now, make them work like errors.
148 result_type = result_type_warning;
149 if (result_index == -1)
150 storage = "WRN:" + ToString(code) + ":" + message + "\r\n";
151 else
152 storage = "WRN[" + ToString(result_index) + "]:" + ToString(code) + ":" + message + "\r\n";
153 count = 1;
154 }
155
156 //Produce resultset
157 String LSCPResultSet::Produce(void) {
158 //FIXME: I'm assuming that only a sinle like "OK" can have index
159 if (count == 0) //When there is nothing in the resultset we just send "OK" to ack the request
160 if (result_index == -1)
161 return "OK\r\n";
162 else
163 return "OK[" + ToString(result_index) + "]\r\n";
164 if (count == 1) //Single line results are just that, single line
165 return storage;
166 //Multiline results MUST end with a line with a single dot
167 return storage + ".\r\n";
168 }

  ViewVC Help
Powered by ViewVC