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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1009 - (hide annotations) (download)
Thu Jan 4 14:43:02 2007 UTC (17 years, 3 months ago) by schoenebeck
File size: 6435 byte(s)
* further bugfixes regarding MIDI instrument LSCP commands
  ("LIST MIDI_INSTRUMENTS ALL" reflected same bug as the
  previously fixed "LIST MIDI_INSTRUMENTS" command and
  "GET MIDI_INSTRUMENT_MAP INFO" result was not
  terminated by a "." line)

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

  ViewVC Help
Powered by ViewVC