• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*---------------------------------------------------------------------------*
2  *  LStringImpl.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 "lstring.h"
21 #include "LStringImpl.h"
22 #include "plog.h"
23 #include "pmemory.h"
24 
25 #define MTAG NULL
26 #define INITIAL_SIZE 32
27 
LStringCreate(LString ** self)28 ESR_ReturnCode LStringCreate(LString** self)
29 {
30   LStringImpl* impl;
31 
32   impl = NEW(LStringImpl, MTAG);
33   if (impl == NULL)
34     return ESR_OUT_OF_MEMORY;
35   impl->Interface.append = &LString_Append;
36   impl->Interface.toLCHAR = &LString_ToLCHAR;
37   impl->Interface.reset = &LString_Reset;
38   impl->Interface.destroy = &LString_Destroy;
39   impl->size = INITIAL_SIZE;
40   impl->value = MALLOC(sizeof(LCHAR) * INITIAL_SIZE, MTAG);
41   if (impl->value == NULL)
42   {
43     PLogError(L("ESR_OUT_OF_MEMORY"));
44     return ESR_OUT_OF_MEMORY;
45   }
46   LSTRCPY(impl->value, L(""));
47   if (impl->value == NULL)
48     return ESR_OUT_OF_MEMORY;
49   *self = (LString*) impl;
50   return ESR_SUCCESS;
51 }
52 
LString_Append(LString * self,const LCHAR * value)53 ESR_ReturnCode LString_Append(LString* self, const LCHAR* value)
54 {
55   LStringImpl* impl = (LStringImpl*) self;
56   size_t needed;
57 
58   needed = LSTRLEN(impl->value) + LSTRLEN(value) + 1;
59 
60   if (needed > impl->size)
61   {
62     LCHAR* temp = REALLOC(impl->value, sizeof(LCHAR) * (needed + (impl->size / 2)));
63     if (temp == NULL)
64       return ESR_OUT_OF_MEMORY;
65     impl->size = sizeof(LCHAR) * (needed + (impl->size / 2));
66     impl->value = temp;
67   }
68   LSTRCAT(impl->value, value);
69   return ESR_SUCCESS;
70 }
71 
LString_Reset(LString * self)72 ESR_ReturnCode LString_Reset(LString* self)
73 {
74   LStringImpl* impl = (LStringImpl*) self;
75 
76   LSTRCPY(impl->value, L(""));
77   return ESR_SUCCESS;
78 }
79 
LString_ToLCHAR(LString * self,LCHAR ** result)80 ESR_ReturnCode LString_ToLCHAR(LString* self, LCHAR** result)
81 {
82   LStringImpl* impl = (LStringImpl*) self;
83 
84   if (result == NULL)
85     return ESR_INVALID_ARGUMENT;
86   *result = impl->value;
87   impl->value = NULL;
88   return self->destroy(self);
89 }
90 
LString_Destroy(LString * self)91 ESR_ReturnCode LString_Destroy(LString* self)
92 {
93   LStringImpl* impl = (LStringImpl*) self;
94 
95   FREE(impl->value);
96   FREE(impl);
97   return ESR_SUCCESS;
98 }
99