1/*---------------------------------------------------------------------------*
2 *  SemanticResultImpl.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#include "SR_SemanticResult.h"
21#include "SR_SemanticResultImpl.h"
22#include <pmemory.h>
23#include "plog.h"
24
25
26static const char* MTAG = __FILE__;
27
28
29ESR_ReturnCode SR_SemanticResultCreate(SR_SemanticResult** self)
30{
31  SR_SemanticResultImpl* impl;
32  ESR_ReturnCode rc;
33
34  if (self == NULL)
35  {
36    PLogError(L("ESR_INVALID_ARGUMENT"));
37    return ESR_INVALID_ARGUMENT;
38  }
39  impl = NEW(SR_SemanticResultImpl, MTAG);
40  if (impl == NULL)
41  {
42    PLogError(L("ESR_OUT_OF_MEMORY"));
43    return ESR_OUT_OF_MEMORY;
44  }
45
46  impl->Interface.destroy = &SR_SemanticResult_Destroy;
47  impl->Interface.getKeyCount = &SR_SemanticResult_GetKeyCount;
48  impl->Interface.getKeyList = &SR_SemanticResult_GetKeyList;
49  impl->Interface.getValue = &SR_SemanticResult_GetValue;
50  impl->results = NULL;
51
52  rc = HashMapCreate(&impl->results);
53  if (rc != ESR_SUCCESS)
54    goto CLEANUP;
55  *self = (SR_SemanticResult*) impl;
56  return ESR_SUCCESS;
57CLEANUP:
58  impl->Interface.destroy(&impl->Interface);
59  return rc;
60}
61
62ESR_ReturnCode SR_SemanticResult_GetKeyCount(SR_SemanticResult* self, size_t* count)
63{
64  SR_SemanticResultImpl* impl = (SR_SemanticResultImpl*) self;
65  ESR_ReturnCode rc;
66
67  CHKLOG(rc, impl->results->getSize(impl->results, count));
68  return ESR_SUCCESS;
69CLEANUP:
70  return rc;
71}
72
73ESR_ReturnCode SR_SemanticResult_GetKeyList(SR_SemanticResult* self, LCHAR** list, size_t* count)
74{
75  SR_SemanticResultImpl* impl = (SR_SemanticResultImpl*) self;
76  LCHAR* theKey;
77  ESR_ReturnCode rc;
78  size_t size, i;
79
80  CHKLOG(rc, HashMapGetSize(impl->results, &size));
81
82  if (size > *count)
83  {
84    PLogError(L("ESR_BUFFER_OVERFLOW"));
85    *count = size;
86    return ESR_BUFFER_OVERFLOW;
87  }
88  else if (list == NULL)
89  {
90    PLogError(L("ESR_INVALID_ARGUMENT"));
91    return ESR_INVALID_ARGUMENT;
92  }
93  *count = size;
94  for (i = 0; i < size; ++i)
95  {
96    CHKLOG(rc, HashMapGetKeyAtIndex(impl->results, i, &theKey));
97    list[i] = theKey;
98  }
99  return ESR_SUCCESS;
100CLEANUP:
101  return rc;
102}
103
104ESR_ReturnCode SR_SemanticResult_GetValue(SR_SemanticResult* self, const LCHAR* key, LCHAR* value, size_t* len)
105{
106  SR_SemanticResultImpl* impl = (SR_SemanticResultImpl*) self;
107  LCHAR* theValue;
108  ESR_ReturnCode rc;
109
110  CHKLOG(rc, impl->results->get(impl->results, key, (void **)&theValue));
111  if (LSTRLEN(theValue) + 1 > *len)
112  {
113    *len = LSTRLEN(theValue) + 1;
114    PLogError(L("ESR_BUFFER_OVERFLOW, requires len>=%d"), LSTRLEN(theValue) + 1);
115    return ESR_BUFFER_OVERFLOW;
116  }
117  LSTRCPY(value, theValue);
118  return ESR_SUCCESS;
119CLEANUP:
120  return rc;
121}
122
123ESR_ReturnCode SR_SemanticResult_Destroy(SR_SemanticResult* self)
124{
125  SR_SemanticResultImpl* impl = (SR_SemanticResultImpl*) self;
126  ESR_ReturnCode rc = ESR_SUCCESS;
127
128  CHKLOG(rc, HashMapRemoveAndFreeAll(impl->results));
129  CHKLOG(rc, HashMapDestroy(impl->results));
130  FREE(impl);
131  return rc;
132CLEANUP:
133  return rc;
134}
135