3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* 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 * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
21 |
* MA 02111-1307 USA * |
* 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" |
#include "lscpresultset.h" |
40 |
#include "../common/LinuxSamplerException.h" |
#include <iomanip> |
41 |
|
#include "../common/global_private.h" |
42 |
|
|
43 |
LSCPResultSet::LSCPResultSet(void) { |
//Construct an empty resultset |
44 |
|
//Default index is -1 meaning the resultset doesn't have an index |
45 |
|
LSCPResultSet::LSCPResultSet(int index) { |
46 |
|
result_index = index; |
47 |
count = 0; |
count = 0; |
48 |
storage = ""; |
storage = ""; |
49 |
|
result_type = result_type_success; |
50 |
} |
} |
51 |
|
|
52 |
LSCPResultSet::LSCPResultSet(String Value) { |
//Construct a resultset with a single line |
53 |
|
//Default index is -1 meaning the resultset doesn't have an index |
54 |
|
LSCPResultSet::LSCPResultSet(String Value, int index) { |
55 |
|
result_index = index; |
56 |
count = 1; |
count = 1; |
57 |
storage = Value + "\r\n"; |
storage = Value + "\r\n"; |
58 |
|
result_type = result_type_success; |
59 |
} |
} |
60 |
|
|
61 |
|
//Add a label/value pair to the resultset |
62 |
|
//Values could be of different types for now supports String, int and float. |
63 |
void LSCPResultSet::Add(String Label, String Value) { |
void LSCPResultSet::Add(String Label, String Value) { |
64 |
if (count == -1) |
if (count == -1) |
65 |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
throw Exception("Attempting to change already produced resultset"); |
66 |
|
if (result_type != result_type_success) |
67 |
|
throw Exception("Attempting to create illegal resultset"); |
68 |
storage = storage + Label + ": " + Value + "\r\n"; |
storage = storage + Label + ": " + Value + "\r\n"; |
69 |
count++; |
count = 2; // results in form of "Label: Value" should always be handled as multi line responses |
70 |
|
} |
71 |
|
|
72 |
|
void LSCPResultSet::Add(String Label, const char* pValue) { |
73 |
|
Add(Label, String(pValue)); |
74 |
|
} |
75 |
|
|
76 |
|
//Add SQL resultset row |
77 |
|
void LSCPResultSet::Add(int columns, char** argv) { |
78 |
|
for (int i = 0; i < columns; i++) |
79 |
|
{ |
80 |
|
storage += argv[i]; |
81 |
|
if ((i+1) < columns) |
82 |
|
storage += "|"; |
83 |
|
} |
84 |
|
storage += "\r\n"; |
85 |
|
count = 2; //This result is always multiline. |
86 |
|
} |
87 |
|
|
88 |
|
void LSCPResultSet::Add(int Value) { |
89 |
|
Add(ToString(Value)); |
90 |
} |
} |
91 |
|
|
92 |
void LSCPResultSet::Add(String Label, int Value) { |
void LSCPResultSet::Add(String Label, int Value) { |
93 |
char temp[16]; |
Add(Label, ToString(Value)); |
|
snprintf(temp, sizeof(temp), "%i", Value); |
|
|
Add(Label, temp); |
|
94 |
} |
} |
95 |
|
|
96 |
void LSCPResultSet::Add(String Label, float Value) { |
void LSCPResultSet::Add(String Label, float Value) { |
97 |
char temp[16]; |
std::stringstream ss; |
98 |
snprintf(temp, sizeof(temp), "%10.4f", Value); |
ss.imbue(std::locale::classic()); |
99 |
Add(Label, temp); |
ss << std::fixed << std::setprecision(3) << Value; |
100 |
|
Add(Label, ss.str()); |
101 |
} |
} |
102 |
|
|
103 |
|
void LSCPResultSet::Add(String Label, bool Value) { |
104 |
|
String s = (Value) ? "true" : "false"; |
105 |
|
Add(Label, s); |
106 |
|
} |
107 |
|
|
108 |
|
//Add a single string to the resultset |
109 |
void LSCPResultSet::Add(String Value) { |
void LSCPResultSet::Add(String Value) { |
110 |
|
if (result_type != result_type_success) |
111 |
|
throw Exception("Attempting to create illegal resultset"); |
112 |
if (count == -1) |
if (count == -1) |
113 |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
throw Exception("Attempting to change already produced resultset"); |
114 |
if (count != 0) |
if (count != 0) |
115 |
throw LinuxSamplerException("Attempting to create illegal resultset"); |
throw Exception("Attempting to create illegal resultset"); |
116 |
storage = Value + "\r\n"; |
storage = Value + "\r\n"; |
117 |
count = 1; |
count = 1; |
118 |
} |
} |
119 |
|
|
120 |
|
//Generate an error result set from an exception. |
121 |
|
//Per LSCP spec, error result is a sinle line in the following format: |
122 |
|
//ERR:<CODE>:Message text\r\n |
123 |
|
//This method will be used to generate unknown errors only (code 0) |
124 |
|
//To generate errors with other codes as well as warnings use other methods (below). |
125 |
|
//Because this is an unknown error, this method will also print message to the stderr. |
126 |
|
void LSCPResultSet::Error(Exception e) { |
127 |
|
e.PrintMessage(); |
128 |
|
Error(e.Message()); |
129 |
|
} |
130 |
|
|
131 |
|
//This will construct an error with a string and error code |
132 |
|
//code has a default of 0 |
133 |
|
//String has a default of "Undefined Error" |
134 |
|
void LSCPResultSet::Error (String message, int code) { |
135 |
|
//Even though this is must be a single line resultset we won't throw |
136 |
|
//anything here because this is already part of exception handling. |
137 |
|
//We'll just 'forget' all previous results (if any) from this resultset. |
138 |
|
result_type = result_type_error; |
139 |
|
storage = "ERR:" + ToString(code) + ":" + message + "\r\n"; |
140 |
|
count = 1; |
141 |
|
} |
142 |
|
|
143 |
|
//This will construct a warning with a string and error code |
144 |
|
//code has a default of 0 |
145 |
|
//String has a default of "Undefined Error" |
146 |
|
void LSCPResultSet::Warning (String message, int code) { |
147 |
|
//FIXME: DO we want warnings as part of the resultset or |
148 |
|
//do we want them to work like errors??? For now, make them work like errors. |
149 |
|
result_type = result_type_warning; |
150 |
|
if (result_index == -1) |
151 |
|
storage = "WRN:" + ToString(code) + ":" + message + "\r\n"; |
152 |
|
else |
153 |
|
storage = "WRN[" + ToString(result_index) + "]:" + ToString(code) + ":" + message + "\r\n"; |
154 |
|
count = 1; |
155 |
|
} |
156 |
|
|
157 |
|
//Produce resultset |
158 |
String LSCPResultSet::Produce(void) { |
String LSCPResultSet::Produce(void) { |
159 |
if (count == 0) |
//FIXME: I'm assuming that only a sinle like "OK" can have index |
160 |
return "OK\r\n"; |
if (count == 0) //When there is nothing in the resultset we just send "OK" to ack the request |
161 |
if (count == 1) |
if (result_index == -1) |
162 |
|
return "OK\r\n"; |
163 |
|
else |
164 |
|
return "OK[" + ToString(result_index) + "]\r\n"; |
165 |
|
if (count == 1) //Single line results are just that, single line |
166 |
return storage; |
return storage; |
167 |
|
//Multiline results MUST end with a line with a single dot |
168 |
return storage + ".\r\n"; |
return storage + ".\r\n"; |
169 |
} |
} |