36 |
* ******************************************************/ |
* ******************************************************/ |
37 |
|
|
38 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
|
#include "../common/LinuxSamplerException.h" |
|
39 |
|
|
40 |
//Construct an empty resultset |
//Construct an empty resultset |
41 |
LSCPResultSet::LSCPResultSet(void) { |
//Default index is -1 meaning the resultset doesn't have an index |
42 |
|
LSCPResultSet::LSCPResultSet(int index) { |
43 |
|
result_index = index; |
44 |
count = 0; |
count = 0; |
45 |
storage = ""; |
storage = ""; |
46 |
|
result_type = result_type_success; |
47 |
} |
} |
48 |
|
|
49 |
//Construct a resultset with a single line |
//Construct a resultset with a single line |
50 |
LSCPResultSet::LSCPResultSet(String Value) { |
//Default index is -1 meaning the resultset doesn't have an index |
51 |
|
LSCPResultSet::LSCPResultSet(String Value, int index) { |
52 |
|
result_index = index; |
53 |
count = 1; |
count = 1; |
54 |
storage = Value + "\r\n"; |
storage = Value + "\r\n"; |
55 |
|
result_type = result_type_success; |
56 |
} |
} |
57 |
|
|
58 |
//Add a label/value pair to the resultset |
//Add a label/value pair to the resultset |
60 |
void LSCPResultSet::Add(String Label, String Value) { |
void LSCPResultSet::Add(String Label, String Value) { |
61 |
if (count == -1) |
if (count == -1) |
62 |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
63 |
|
if (result_type != result_type_success) |
64 |
|
throw LinuxSamplerException("Attempting to create illegal resultset"); |
65 |
storage = storage + Label + ": " + Value + "\r\n"; |
storage = storage + Label + ": " + Value + "\r\n"; |
66 |
count++; |
count++; |
67 |
} |
} |
68 |
|
|
69 |
|
void LSCPResultSet::Add(int Value) { |
70 |
|
Add(ToString(Value)); |
71 |
|
} |
72 |
|
|
73 |
void LSCPResultSet::Add(String Label, int Value) { |
void LSCPResultSet::Add(String Label, int Value) { |
74 |
char temp[16]; |
Add(Label, ToString(Value)); |
|
snprintf(temp, sizeof(temp), "%i", Value); |
|
|
Add(Label, temp); |
|
75 |
} |
} |
76 |
|
|
77 |
void LSCPResultSet::Add(String Label, float Value) { |
void LSCPResultSet::Add(String Label, float Value) { |
78 |
char temp[16]; |
std::stringstream ss; //fixme: had issues with template and float?! |
79 |
snprintf(temp, sizeof(temp), "%10.4f", Value); |
ss << Value; |
80 |
Add(Label, temp); |
Add(Label, ss.str()); |
81 |
} |
} |
82 |
|
|
83 |
//Add a single string to the resultset |
//Add a single string to the resultset |
84 |
void LSCPResultSet::Add(String Value) { |
void LSCPResultSet::Add(String Value) { |
85 |
|
if (result_type != result_type_success) |
86 |
|
throw LinuxSamplerException("Attempting to create illegal resultset"); |
87 |
if (count == -1) |
if (count == -1) |
88 |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
throw LinuxSamplerException("Attempting to change already produced resultset"); |
89 |
if (count != 0) |
if (count != 0) |
92 |
count = 1; |
count = 1; |
93 |
} |
} |
94 |
|
|
95 |
|
//Generate an error result set from an exception. |
96 |
|
//Per LSCP spec, error result is a sinle line in the following format: |
97 |
|
//ERR:<CODE>:Message text\r\n |
98 |
|
//This method will be used to generate unknown errors only (code 0) |
99 |
|
//To generate errors with other codes as well as warnings use other methods (below). |
100 |
|
//Because this is an unknown error, this method will also print message to the stderr. |
101 |
|
void LSCPResultSet::Error(LinuxSamplerException e) { |
102 |
|
e.PrintMessage(); |
103 |
|
Error(e.Message()); |
104 |
|
} |
105 |
|
|
106 |
|
//This will construct an error with a string and error code |
107 |
|
//code has a default of 0 |
108 |
|
//String has a default of "Undefined Error" |
109 |
|
void LSCPResultSet::Error (String message, int code) { |
110 |
|
//Even though this is must be a single line resultset we won't throw |
111 |
|
//anything here because this is already part of exception handling. |
112 |
|
//We'll just 'forget' all previous results (if any) from this resultset. |
113 |
|
result_type = result_type_error; |
114 |
|
storage = "ERR:" + ToString(code) + ":" + message + "\r\n"; |
115 |
|
count = 1; |
116 |
|
} |
117 |
|
|
118 |
|
//This will construct a warning with a string and error code |
119 |
|
//code has a default of 0 |
120 |
|
//String has a default of "Undefined Error" |
121 |
|
void LSCPResultSet::Warning (String message, int code) { |
122 |
|
//FIXME: DO we want warnings as part of the resultset or |
123 |
|
//do we want them to work like errors??? For now, make them work like errors. |
124 |
|
result_type = result_type_warning; |
125 |
|
if (result_index == -1) |
126 |
|
storage = "WRN:" + ToString(code) + ":" + message + "\r\n"; |
127 |
|
else |
128 |
|
storage = "WRN[" + ToString(result_index) + "]:" + ToString(code) + ":" + message + "\r\n"; |
129 |
|
count = 1; |
130 |
|
} |
131 |
|
|
132 |
//Produce resultset |
//Produce resultset |
133 |
String LSCPResultSet::Produce(void) { |
String LSCPResultSet::Produce(void) { |
134 |
|
//FIXME: I'm assuming that only a sinle like "OK" can have index |
135 |
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 |
136 |
return "OK\r\n"; |
if (result_index == -1) |
137 |
|
return "OK\r\n"; |
138 |
|
else |
139 |
|
return "OK[" + ToString(result_index) + "]\r\n"; |
140 |
if (count == 1) //Single line results are just that, single line |
if (count == 1) //Single line results are just that, single line |
141 |
return storage; |
return storage; |
142 |
//Multiline results MUST end with a line with a single dot |
//Multiline results MUST end with a line with a single dot |