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
34static ESR_SessionType* ESR_Session = NULL;
35#define CHECK_SESSION_OR_RETURN if(!ESR_Session) return ESR_INVALID_ARGUMENT
36
37ESR_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;
52CLEANUP:
53  ESR_SessionDestroy();
54  return rc;
55}
56
57ESR_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
63ESR_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
69ESR_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
75ESR_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
82ESR_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
88ESR_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
94ESR_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
100ESR_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
106ESR_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
112ESR_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
118ESR_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
124ESR_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
130ESR_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
136ESR_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
142ESR_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
148ESR_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
154ESR_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
160ESR_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
166ESR_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
172ESR_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
178ESR_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
184ESR_ReturnCode ESR_SessionRemoveProperty(const LCHAR* name)
185{
186  CHECK_SESSION_OR_RETURN;
187  return ESR_Session->removeProperty(ESR_Session, name);
188}
189
190ESR_ReturnCode ESR_SessionRemoveAndFreeProperty(const LCHAR* name)
191{
192  CHECK_SESSION_OR_RETURN;
193  return ESR_Session->removeAndFreeProperty(ESR_Session, name);
194}
195
196ESR_ReturnCode ESR_SessionImportCommandLine(int argc, LCHAR* argv[])
197{
198  CHECK_SESSION_OR_RETURN;
199  return ESR_Session->importCommandLine(ESR_Session, argc, argv);
200}
201
202ESR_ReturnCode ESR_SessionGetSize(size_t* size)
203{
204  CHECK_SESSION_OR_RETURN;
205  return ESR_Session->getSize(ESR_Session, size);
206}
207
208ESR_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
214ESR_ReturnCode ESR_SessionConvertToInt(const LCHAR* key)
215{
216  CHECK_SESSION_OR_RETURN;
217  return ESR_Session->convertToInt(ESR_Session, key);
218}
219
220ESR_ReturnCode ESR_SessionConvertToUint16_t(const LCHAR* key)
221{
222  CHECK_SESSION_OR_RETURN;
223  return ESR_Session->convertToUint16_t(ESR_Session, key);
224}
225
226ESR_ReturnCode ESR_SessionConvertToSize_t(const LCHAR* key)
227{
228  CHECK_SESSION_OR_RETURN;
229  return ESR_Session->convertToSize_t(ESR_Session, key);
230}
231
232ESR_ReturnCode ESR_SessionConvertToFloat(const LCHAR* key)
233{
234  CHECK_SESSION_OR_RETURN;
235  return ESR_Session->convertToFloat(ESR_Session, key);
236}
237
238ESR_ReturnCode ESR_SessionConvertToBool(const LCHAR* key)
239{
240  CHECK_SESSION_OR_RETURN;
241  return ESR_Session->convertToBool(ESR_Session, key);
242}
243
244ESR_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
250ESR_ReturnCode ESR_SessionImportParFile(const LCHAR* filename)
251{
252  CHECK_SESSION_OR_RETURN;
253  return ESR_Session->importParFile(ESR_Session, filename);
254}
255
256ESR_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;
266CLEANUP:
267  return rc;
268}
269
270ESR_ReturnCode ESR_SessionExists(ESR_BOOL* val)
271{
272  *val = (ESR_Session != NULL);
273  return ESR_SUCCESS;
274}
275
276ESR_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;
292CLEANUP:
293  return rc;
294}
295
296ESR_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
302ESR_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