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 - 2016 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 * |
37 |
* ******************************************************/ |
* ******************************************************/ |
38 |
|
|
39 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
40 |
|
#include <iomanip> |
41 |
|
#include "../common/global_private.h" |
42 |
|
|
43 |
|
|
44 |
|
namespace LinuxSampler { |
45 |
|
|
46 |
//Construct an empty resultset |
//Construct an empty resultset |
47 |
//Default index is -1 meaning the resultset doesn't have an index |
//Default index is -1 meaning the resultset doesn't have an index |
65 |
//Values could be of different types for now supports String, int and float. |
//Values could be of different types for now supports String, int and float. |
66 |
void LSCPResultSet::Add(String Label, String Value) { |
void LSCPResultSet::Add(String Label, String Value) { |
67 |
if (count == -1) |
if (count == -1) |
68 |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
throw Exception("Attempting to change already produced resultset"); |
69 |
if (result_type != result_type_success) |
if (result_type != result_type_success) |
70 |
throw LinuxSamplerException("Attempting to create illegal resultset"); |
throw Exception("Attempting to create illegal resultset"); |
71 |
storage = storage + Label + ": " + Value + "\r\n"; |
storage = storage + Label + ": " + Value + "\r\n"; |
72 |
count++; |
count = 2; // results in form of "Label: Value" should always be handled as multi line responses |
73 |
} |
} |
74 |
|
|
75 |
void LSCPResultSet::Add(String Label, const char* pValue) { |
void LSCPResultSet::Add(String Label, const char* pValue) { |
76 |
Add(Label, String(pValue)); |
Add(Label, String(pValue)); |
77 |
} |
} |
78 |
|
|
79 |
|
//Add SQL resultset row |
80 |
|
void LSCPResultSet::Add(int columns, char** argv) { |
81 |
|
for (int i = 0; i < columns; i++) |
82 |
|
{ |
83 |
|
storage += argv[i]; |
84 |
|
if ((i+1) < columns) |
85 |
|
storage += "|"; |
86 |
|
} |
87 |
|
storage += "\r\n"; |
88 |
|
count = 2; //This result is always multiline. |
89 |
|
} |
90 |
|
|
91 |
void LSCPResultSet::Add(int Value) { |
void LSCPResultSet::Add(int Value) { |
92 |
Add(ToString(Value)); |
Add(ToString(Value)); |
93 |
} |
} |
97 |
} |
} |
98 |
|
|
99 |
void LSCPResultSet::Add(String Label, float Value) { |
void LSCPResultSet::Add(String Label, float Value) { |
100 |
char s[1024]; |
std::stringstream ss; |
101 |
snprintf(s, 1023, "%.3f", Value); |
ss.imbue(std::locale::classic()); |
102 |
Add(Label, String(s)); |
ss << std::fixed << std::setprecision(3) << Value; |
103 |
|
Add(Label, ss.str()); |
104 |
} |
} |
105 |
|
|
106 |
void LSCPResultSet::Add(String Label, bool Value) { |
void LSCPResultSet::Add(String Label, bool Value) { |
111 |
//Add a single string to the resultset |
//Add a single string to the resultset |
112 |
void LSCPResultSet::Add(String Value) { |
void LSCPResultSet::Add(String Value) { |
113 |
if (result_type != result_type_success) |
if (result_type != result_type_success) |
114 |
throw LinuxSamplerException("Attempting to create illegal resultset"); |
throw Exception("Attempting to create illegal resultset"); |
115 |
if (count == -1) |
if (count == -1) |
116 |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
throw Exception("Attempting to change already produced resultset"); |
117 |
if (count != 0) |
if (count != 0) |
118 |
throw LinuxSamplerException("Attempting to create illegal resultset"); |
throw Exception("Attempting to create illegal resultset"); |
119 |
storage = Value + "\r\n"; |
storage = Value + "\r\n"; |
120 |
count = 1; |
count = 1; |
121 |
} |
} |
122 |
|
|
123 |
|
void LSCPResultSet::Add(String Label, const std::vector<float>& v) { |
124 |
|
std::stringstream ss; |
125 |
|
ss.imbue(std::locale::classic()); |
126 |
|
for (int i = 0; i < v.size(); i++) { |
127 |
|
if (!ss.str().empty()) ss << ","; |
128 |
|
ss << std::fixed << std::setprecision(3) << v[i]; |
129 |
|
} |
130 |
|
Add(Label, ss.str()); |
131 |
|
} |
132 |
|
|
133 |
//Generate an error result set from an exception. |
//Generate an error result set from an exception. |
134 |
//Per LSCP spec, error result is a sinle line in the following format: |
//Per LSCP spec, error result is a sinle line in the following format: |
135 |
//ERR:<CODE>:Message text\r\n |
//ERR:<CODE>:Message text\r\n |
136 |
//This method will be used to generate unknown errors only (code 0) |
//This method will be used to generate unknown errors only (code 0) |
137 |
//To generate errors with other codes as well as warnings use other methods (below). |
//To generate errors with other codes as well as warnings use other methods (below). |
138 |
//Because this is an unknown error, this method will also print message to the stderr. |
//Because this is an unknown error, this method will also print message to the stderr. |
139 |
void LSCPResultSet::Error(LinuxSamplerException e) { |
void LSCPResultSet::Error(Exception e) { |
140 |
e.PrintMessage(); |
e.PrintMessage(); |
141 |
Error(e.Message()); |
Error(e.Message()); |
142 |
} |
} |
170 |
//Produce resultset |
//Produce resultset |
171 |
String LSCPResultSet::Produce(void) { |
String LSCPResultSet::Produce(void) { |
172 |
//FIXME: I'm assuming that only a sinle like "OK" can have index |
//FIXME: I'm assuming that only a sinle like "OK" can have index |
173 |
if (count == 0) //When there is nothing in the resultset we just send "OK" to ack the request |
if (count == 0) { //When there is nothing in the resultset we just send "OK" to ack the request |
174 |
if (result_index == -1) |
if (result_index == -1) |
175 |
return "OK\r\n"; |
return "OK\r\n"; |
176 |
else |
else |
177 |
return "OK[" + ToString(result_index) + "]\r\n"; |
return "OK[" + ToString(result_index) + "]\r\n"; |
178 |
|
} |
179 |
if (count == 1) //Single line results are just that, single line |
if (count == 1) //Single line results are just that, single line |
180 |
return storage; |
return storage; |
181 |
//Multiline results MUST end with a line with a single dot |
//Multiline results MUST end with a line with a single dot |
182 |
return storage + ".\r\n"; |
return storage + ".\r\n"; |
183 |
} |
} |
184 |
|
|
185 |
|
} |