1 /*---------------------------------------------------------------------------*
2 * ESR_Session.c *
3 * *
4 * Copyright 2007, 2008 Nuance Communciations, Inc. *
5 * *
6 * Licensed under the Apache License, Version 2.0 (the 'License'); *
7 * you may not use this file except in compliance with the License. *
8 * *
9 * You may obtain a copy of the License at *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, software *
13 * distributed under the License is distributed on an 'AS IS' BASIS, *
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 * See the License for the specific language governing permissions and *
16 * limitations under the License. *
17 * *
18 *---------------------------------------------------------------------------*/
19
20
21 #include "ESR_Session.h"
22 #include "ESR_SessionType.h"
23 #include "ESR_SessionTypeImpl.h"
24 #include <string.h>
25 #include "HashMap.h"
26 #include "IntArrayList.h"
27 #include "LCHAR.h"
28 #include "lstring.h"
29 #include "passert.h"
30 #include "plog.h"
31 #include "ptrd.h"
32 #include "pstdio.h"
33
34 static ESR_SessionType* ESR_Session = NULL;
35 #define CHECK_SESSION_OR_RETURN if(!ESR_Session) return ESR_INVALID_ARGUMENT
36
ESR_SessionCreate(const LCHAR * filename)37 ESR_ReturnCode ESR_SessionCreate(const LCHAR* filename)
38 {
39 ESR_ReturnCode rc;
40
41 CHKLOG(rc, ESR_SessionTypeCreate(&ESR_Session));
42
43 /* Initialize default values here */
44 CHKLOG(rc, ESR_Session->setLCHAR(ESR_Session, L("cmdline.nametagPath"), L("")));
45 #ifdef USE_THREAD
46 CHKLOG(rc, ESR_Session->setUint16_t(ESR_Session, L("thread.priority"), PtrdThreadNormalPriority));
47 #endif
48
49 /* End of default values */
50 CHKLOG(rc, ESR_Session->importParFile(ESR_Session, filename));
51 return ESR_SUCCESS;
52 CLEANUP:
53 ESR_SessionDestroy();
54 return rc;
55 }
56
ESR_SessionGetProperty(const LCHAR * name,void ** value,VariableTypes type)57 ESR_ReturnCode ESR_SessionGetProperty(const LCHAR* name, void** value, VariableTypes type)
58 {
59 CHECK_SESSION_OR_RETURN;
60 return ESR_Session->getProperty(ESR_Session, name, value, type);
61 }
62
ESR_SessionGetInt(const LCHAR * name,int * value)63 ESR_SHARED_API ESR_ReturnCode ESR_SessionGetInt(const LCHAR* name, int* value)
64 {
65 CHECK_SESSION_OR_RETURN;
66 return ESR_Session->getInt(ESR_Session, name, value);
67 }
68
ESR_SessionGetUint16_t(const LCHAR * name,asr_uint16_t * value)69 ESR_SHARED_API ESR_ReturnCode ESR_SessionGetUint16_t(const LCHAR* name, asr_uint16_t* value)
70 {
71 CHECK_SESSION_OR_RETURN;
72 return ESR_Session->getUint16_t(ESR_Session, name, value);
73 }
74
ESR_SessionGetSize_t(const LCHAR * name,size_t * value)75 ESR_SHARED_API ESR_ReturnCode ESR_SessionGetSize_t(const LCHAR* name,
76 size_t* value)
77 {
78 CHECK_SESSION_OR_RETURN;
79 return ESR_Session->getSize_t(ESR_Session, name, value);
80 }
81
ESR_SessionGetFloat(const LCHAR * name,float * value)82 ESR_SHARED_API ESR_ReturnCode ESR_SessionGetFloat(const LCHAR* name, float* value)
83 {
84 CHECK_SESSION_OR_RETURN;
85 return ESR_Session->getFloat(ESR_Session, name, value);
86 }
87
ESR_SessionGetBool(const LCHAR * name,ESR_BOOL * value)88 ESR_SHARED_API ESR_ReturnCode ESR_SessionGetBool(const LCHAR* name, ESR_BOOL* value)
89 {
90 CHECK_SESSION_OR_RETURN;
91 return ESR_Session->getBool(ESR_Session, name, value);
92 }
93
ESR_SessionGetLCHAR(const LCHAR * name,LCHAR * value,size_t * len)94 ESR_SHARED_API ESR_ReturnCode ESR_SessionGetLCHAR(const LCHAR* name, LCHAR* value, size_t* len)
95 {
96 CHECK_SESSION_OR_RETURN;
97 return ESR_Session->getLCHAR(ESR_Session, name, value, len);
98 }
99
ESR_SessionContains(const LCHAR * name,ESR_BOOL * exists)100 ESR_ReturnCode ESR_SessionContains(const LCHAR* name, ESR_BOOL* exists)
101 {
102 CHECK_SESSION_OR_RETURN;
103 return ESR_Session->contains(ESR_Session, name, exists);
104 }
105
ESR_SessionSetProperty(const LCHAR * name,void * value,VariableTypes type)106 ESR_ReturnCode ESR_SessionSetProperty(const LCHAR* name, void* value, VariableTypes type)
107 {
108 CHECK_SESSION_OR_RETURN;
109 return ESR_Session->setProperty(ESR_Session, name, value, type);
110 }
111
ESR_SessionSetInt(const LCHAR * name,int value)112 ESR_ReturnCode ESR_SessionSetInt(const LCHAR* name, int value)
113 {
114 CHECK_SESSION_OR_RETURN;
115 return ESR_Session->setInt(ESR_Session, name, value);
116 }
117
ESR_SessionSetUint16_t(const LCHAR * name,asr_uint16_t value)118 ESR_ReturnCode ESR_SessionSetUint16_t(const LCHAR* name, asr_uint16_t value)
119 {
120 CHECK_SESSION_OR_RETURN;
121 return ESR_Session->setUint16_t(ESR_Session, name, value);
122 }
123
ESR_SessionSetSize_t(const LCHAR * name,size_t value)124 ESR_ReturnCode ESR_SessionSetSize_t(const LCHAR* name, size_t value)
125 {
126 CHECK_SESSION_OR_RETURN;
127 return ESR_Session->setSize_t(ESR_Session, name, value);
128 }
129
ESR_SessionSetFloat(const LCHAR * name,float value)130 ESR_ReturnCode ESR_SessionSetFloat(const LCHAR* name, float value)
131 {
132 CHECK_SESSION_OR_RETURN;
133 return ESR_Session->setFloat(ESR_Session, name, value);
134 }
135
ESR_SessionSetBool(const LCHAR * name,ESR_BOOL value)136 ESR_ReturnCode ESR_SessionSetBool(const LCHAR* name, ESR_BOOL value)
137 {
138 CHECK_SESSION_OR_RETURN;
139 return ESR_Session->setBool(ESR_Session, name, value);
140 }
141
ESR_SessionSetLCHAR(const LCHAR * name,LCHAR * value)142 ESR_ReturnCode ESR_SessionSetLCHAR(const LCHAR* name, LCHAR* value)
143 {
144 CHECK_SESSION_OR_RETURN;
145 return ESR_Session->setLCHAR(ESR_Session, name, value);
146 }
147
ESR_SessionSetIntIfEmpty(const LCHAR * name,int value)148 ESR_ReturnCode ESR_SessionSetIntIfEmpty(const LCHAR* name, int value)
149 {
150 CHECK_SESSION_OR_RETURN;
151 return ESR_Session->setIntIfEmpty(ESR_Session, name, value);
152 }
153
ESR_SessionSetUint16_tIfEmpty(const LCHAR * name,asr_uint16_t value)154 ESR_ReturnCode ESR_SessionSetUint16_tIfEmpty(const LCHAR* name, asr_uint16_t value)
155 {
156 CHECK_SESSION_OR_RETURN;
157 return ESR_Session->setUint16_tIfEmpty(ESR_Session, name, value);
158 }
159
ESR_SessionSetSize_tIfEmpty(const LCHAR * name,size_t value)160 ESR_ReturnCode ESR_SessionSetSize_tIfEmpty(const LCHAR* name, size_t value)
161 {
162 CHECK_SESSION_OR_RETURN;
163 return ESR_Session->setSize_tIfEmpty(ESR_Session, name, value);
164 }
165
ESR_SessionSetFloatIfEmpty(const LCHAR * name,float value)166 ESR_ReturnCode ESR_SessionSetFloatIfEmpty(const LCHAR* name, float value)
167 {
168 CHECK_SESSION_OR_RETURN;
169 return ESR_Session->setFloatIfEmpty(ESR_Session, name, value);
170 }
171
ESR_SessionSetBoolIfEmpty(const LCHAR * name,ESR_BOOL value)172 ESR_ReturnCode ESR_SessionSetBoolIfEmpty(const LCHAR* name, ESR_BOOL value)
173 {
174 CHECK_SESSION_OR_RETURN;
175 return ESR_Session->setBoolIfEmpty(ESR_Session, name, value);
176 }
177
ESR_SessionSetLCHARIfEmpty(const LCHAR * name,LCHAR * value)178 ESR_ReturnCode ESR_SessionSetLCHARIfEmpty(const LCHAR* name, LCHAR* value)
179 {
180 CHECK_SESSION_OR_RETURN;
181 return ESR_Session->setLCHARIfEmpty(ESR_Session, name, value);
182 }
183
ESR_SessionRemoveProperty(const LCHAR * name)184 ESR_ReturnCode ESR_SessionRemoveProperty(const LCHAR* name)
185 {
186 CHECK_SESSION_OR_RETURN;
187 return ESR_Session->removeProperty(ESR_Session, name);
188 }
189
ESR_SessionRemoveAndFreeProperty(const LCHAR * name)190 ESR_ReturnCode ESR_SessionRemoveAndFreeProperty(const LCHAR* name)
191 {
192 CHECK_SESSION_OR_RETURN;
193 return ESR_Session->removeAndFreeProperty(ESR_Session, name);
194 }
195
ESR_SessionImportCommandLine(int argc,LCHAR * argv[])196 ESR_ReturnCode ESR_SessionImportCommandLine(int argc, LCHAR* argv[])
197 {
198 CHECK_SESSION_OR_RETURN;
199 return ESR_Session->importCommandLine(ESR_Session, argc, argv);
200 }
201
ESR_SessionGetSize(size_t * size)202 ESR_ReturnCode ESR_SessionGetSize(size_t* size)
203 {
204 CHECK_SESSION_OR_RETURN;
205 return ESR_Session->getSize(ESR_Session, size);
206 }
207
ESR_SessionGetKeyAtIndex(size_t index,LCHAR ** key)208 ESR_ReturnCode ESR_SessionGetKeyAtIndex(size_t index, LCHAR** key)
209 {
210 CHECK_SESSION_OR_RETURN;
211 return ESR_Session->getKeyAtIndex(ESR_Session, index, key);
212 }
213
ESR_SessionConvertToInt(const LCHAR * key)214 ESR_ReturnCode ESR_SessionConvertToInt(const LCHAR* key)
215 {
216 CHECK_SESSION_OR_RETURN;
217 return ESR_Session->convertToInt(ESR_Session, key);
218 }
219
ESR_SessionConvertToUint16_t(const LCHAR * key)220 ESR_ReturnCode ESR_SessionConvertToUint16_t(const LCHAR* key)
221 {
222 CHECK_SESSION_OR_RETURN;
223 return ESR_Session->convertToUint16_t(ESR_Session, key);
224 }
225
ESR_SessionConvertToSize_t(const LCHAR * key)226 ESR_ReturnCode ESR_SessionConvertToSize_t(const LCHAR* key)
227 {
228 CHECK_SESSION_OR_RETURN;
229 return ESR_Session->convertToSize_t(ESR_Session, key);
230 }
231
ESR_SessionConvertToFloat(const LCHAR * key)232 ESR_ReturnCode ESR_SessionConvertToFloat(const LCHAR* key)
233 {
234 CHECK_SESSION_OR_RETURN;
235 return ESR_Session->convertToFloat(ESR_Session, key);
236 }
237
ESR_SessionConvertToBool(const LCHAR * key)238 ESR_ReturnCode ESR_SessionConvertToBool(const LCHAR* key)
239 {
240 CHECK_SESSION_OR_RETURN;
241 return ESR_Session->convertToBool(ESR_Session, key);
242 }
243
ESR_SessionGetPropertyType(const LCHAR * name,VariableTypes * type)244 ESR_ReturnCode ESR_SessionGetPropertyType(const LCHAR* name, VariableTypes* type)
245 {
246 CHECK_SESSION_OR_RETURN;
247 return ESR_Session->getPropertyType(ESR_Session, name, type);
248 }
249
ESR_SessionImportParFile(const LCHAR * filename)250 ESR_ReturnCode ESR_SessionImportParFile(const LCHAR* filename)
251 {
252 CHECK_SESSION_OR_RETURN;
253 return ESR_Session->importParFile(ESR_Session, filename);
254 }
255
ESR_SessionDestroy()256 ESR_ReturnCode ESR_SessionDestroy()
257 {
258 ESR_ReturnCode rc;
259
260 if (ESR_Session != NULL)
261 {
262 CHKLOG(rc, ESR_Session->destroy(ESR_Session));
263 ESR_Session = NULL;
264 }
265 return ESR_SUCCESS;
266 CLEANUP:
267 return rc;
268 }
269
ESR_SessionExists(ESR_BOOL * val)270 ESR_ReturnCode ESR_SessionExists(ESR_BOOL* val)
271 {
272 *val = (ESR_Session != NULL);
273 return ESR_SUCCESS;
274 }
275
ESR_SessionPrefixWithBaseDirectory(LCHAR * path,size_t * len)276 ESR_ReturnCode ESR_SessionPrefixWithBaseDirectory(LCHAR* path, size_t* len)
277 {
278 ESR_ReturnCode rc;
279 LCHAR baseDirectory[P_PATH_MAX];
280 ESR_BOOL isAbsolute;
281 size_t len2 = P_PATH_MAX;
282
283 /* Skip absolute paths. */
284 CHKLOG(rc, pf_convert_backslashes_to_forwardslashes (path));
285 CHKLOG(rc, pf_is_path_absolute (path, &isAbsolute));
286 if (isAbsolute)
287 return ESR_SUCCESS;
288
289 CHKLOG(rc, ESR_SessionGetLCHAR(L("parFile.baseDirectory"), baseDirectory, &len2));
290 CHKLOG(rc, lstrinsert(baseDirectory, path, 0, len));
291 return ESR_SUCCESS;
292 CLEANUP:
293 return rc;
294 }
295
ESR_SessionAddListener(ESR_SessionType * self,ESR_SessionTypeListenerPair * listener)296 ESR_SHARED_API ESR_ReturnCode ESR_SessionAddListener(ESR_SessionType* self, ESR_SessionTypeListenerPair* listener)
297 {
298 CHECK_SESSION_OR_RETURN;
299 return ESR_Session->addListener(ESR_Session, listener);
300 }
301
ESR_SessionRemoveListener(ESR_SessionType * self,ESR_SessionTypeListenerPair * listener)302 ESR_SHARED_API ESR_ReturnCode ESR_SessionRemoveListener(ESR_SessionType* self, ESR_SessionTypeListenerPair* listener)
303 {
304 CHECK_SESSION_OR_RETURN;
305 return ESR_Session->removeListener(ESR_Session, listener);
306 }
307