exip
Alpha 0.5.4
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
Pages
src
common
src
ASCII_stringManipulate.c
Go to the documentation of this file.
1
/*==================================================================*\
2
| EXIP - Embeddable EXI Processor in C |
3
|--------------------------------------------------------------------|
4
| This work is licensed under BSD 3-Clause License |
5
| The full license terms and conditions are located in LICENSE.txt |
6
\===================================================================*/
7
18
#include "stringManipulate.h"
19
#include "
memManagement.h
"
20
21
#define PARSING_STRING_MAX_LENGTH 100
22
23
errorCode
allocateStringMemory
(
CharType
** str,
Index
UCSchars)
24
{
25
*str =
EXIP_MALLOC
(
sizeof
(
CharType
)*UCSchars);
26
if
((*str) ==
NULL
)
27
return
EXIP_MEMORY_ALLOCATION_ERROR
;
28
return
EXIP_OK
;
29
}
30
31
errorCode
allocateStringMemoryManaged
(
CharType
** str,
Index
UCSchars,
AllocList
* memList)
32
{
33
(*str) = (
CharType
*)
memManagedAllocate
(memList,
sizeof
(
CharType
)*UCSchars);
34
if
((*str) ==
NULL
)
35
return
EXIP_MEMORY_ALLOCATION_ERROR
;
36
return
EXIP_OK
;
37
}
38
42
errorCode
writeCharToString
(
String
* str,
uint32_t
code_point,
Index
* writerPosition)
43
{
44
if
(*writerPosition >= str->
length
)
45
return
EXIP_OUT_OF_BOUND_BUFFER
;
46
str->
str
[*writerPosition] = (
CharType
) code_point;
47
*writerPosition += 1;
48
return
EXIP_OK
;
49
}
50
51
void
getEmptyString
(
String
* emptyStr)
52
{
53
emptyStr->
length
= 0;
54
emptyStr->
str
=
NULL
;
55
}
56
57
boolean
isStringEmpty
(
const
String
* str)
58
{
59
if
(str ==
NULL
|| str->
length
== 0)
60
return
1;
61
return
0;
62
}
63
64
errorCode
asciiToString
(
const
char
* inStr,
String
* outStr,
AllocList
* memList,
boolean
clone)
65
{
66
outStr->
length
= strlen(inStr);
67
if
(outStr->
length
> 0)
// If == 0 -> empty string
68
{
69
if
(clone ==
FALSE
)
70
{
71
outStr->
str
= (
CharType
*) inStr;
72
return
EXIP_OK
;
73
}
74
else
75
{
76
outStr->
str
= (
CharType
*)
memManagedAllocate
(memList,
sizeof
(
CharType
)*(outStr->
length
));
77
if
(outStr->
str
==
NULL
)
78
return
EXIP_MEMORY_ALLOCATION_ERROR
;
79
memcpy(outStr->
str
, inStr, outStr->
length
);
80
return
EXIP_OK
;
81
}
82
}
83
else
84
outStr->
str
=
NULL
;
85
return
EXIP_OK
;
86
}
87
88
boolean
stringEqual
(
const
String
str1,
const
String
str2)
89
{
90
if
(str1.
length
!= str2.
length
)
91
return
0;
92
else
if
(str1.
length
== 0)
93
return
1;
94
else
// The strings have the same length
95
{
96
Index
i;
97
for
(i = 0; i < str1.
length
; i++)
98
{
99
if
(str1.
str
[i] != str2.
str
[i])
100
return
0;
101
}
102
return
1;
103
}
104
}
105
106
boolean
stringEqualToAscii
(
const
String
str1,
const
char
* str2)
107
{
108
if
(str1.
length
!= strlen(str2))
109
return
0;
110
else
// The strings have the same length
111
{
112
Index
i;
113
for
(i = 0; i < str1.
length
; i++)
114
{
115
if
(str1.
str
[i] != str2[i])
116
return
0;
117
}
118
return
1;
119
}
120
}
121
122
int
stringCompare
(
const
String
str1,
const
String
str2)
123
{
124
/* Check for NULL string pointers */
125
if
(str1.
str
==
NULL
)
126
{
127
if
(str2.
str
==
NULL
)
128
return
0;
129
return
-1;
130
}
131
else
if
(str2.
str
==
NULL
)
132
return
1;
133
else
// None of the strings is NULL
134
{
135
int
diff;
136
Index
i;
137
for
(i = 0; i < str1.
length
&& i < str2.
length
; i++)
138
{
139
diff = str1.
str
[i] - str2.
str
[i];
140
if
(diff)
141
return
diff;
// There is a difference in the characters at index i
142
}
143
/* Up to index i the strings have the same characters and might differ only in length*/
144
return
str1.
length
- str2.
length
;
145
}
146
}
147
148
uint32_t
readCharFromString
(
const
String
* str,
Index
* readerPosition)
149
{
150
assert
(*readerPosition < str->length);
151
*readerPosition += 1;
152
return
(
uint32_t
) str->
str
[*readerPosition - 1];
153
}
154
155
errorCode
cloneString
(
const
String
* src,
String
* newStr)
156
{
157
if
(newStr ==
NULL
)
158
return
EXIP_NULL_POINTER_REF
;
159
newStr->
str
=
EXIP_MALLOC
(
sizeof
(
CharType
)*src->
length
);
160
if
(newStr->
str
==
NULL
)
161
return
EXIP_MEMORY_ALLOCATION_ERROR
;
162
newStr->
length
= src->
length
;
163
memcpy(newStr->
str
, src->
str
, src->
length
);
164
return
EXIP_OK
;
165
}
166
167
errorCode
cloneStringManaged
(
const
String
* src,
String
* newStr,
AllocList
* memList)
168
{
169
if
(newStr ==
NULL
)
170
return
EXIP_NULL_POINTER_REF
;
171
newStr->
str
=
memManagedAllocate
(memList,
sizeof
(
CharType
)*src->
length
);
172
if
(newStr->
str
==
NULL
)
173
return
EXIP_MEMORY_ALLOCATION_ERROR
;
174
newStr->
length
= src->
length
;
175
memcpy(newStr->
str
, src->
str
, src->
length
);
176
return
EXIP_OK
;
177
}
178
179
Index
getIndexOfChar
(
const
String
* src,
CharType
sCh)
180
{
181
Index
i;
182
183
for
(i = 0; i < src->
length
; i++)
184
{
185
if
(src->
str
[i] == sCh)
186
return
i;
187
}
188
return
INDEX_MAX
;
189
}
190
191
errorCode
stringToInteger
(
const
String
* src,
int
* number)
192
{
193
char
buff[
PARSING_STRING_MAX_LENGTH
];
194
long
result;
195
char
*endPointer;
196
197
if
(src->
length
== 0 || src->
length
>=
PARSING_STRING_MAX_LENGTH
)
198
return
EXIP_INVALID_STRING_OPERATION
;
199
200
memcpy(buff, src->
str
, src->
length
);
201
buff[src->
length
] =
'\0'
;
202
203
result = strtol(buff, &endPointer, 10);
204
205
if
(result == LONG_MAX || result == LONG_MIN || *src->
str
== *endPointer)
206
return
EXIP_INVALID_STRING_OPERATION
;
207
208
if
(result >= INT_MAX || result <= INT_MIN)
209
return
EXIP_OUT_OF_BOUND_BUFFER
;
210
211
*number = (int) result;
212
213
return
EXIP_OK
;
214
}
215
216
errorCode
stringToInt64
(
const
String
* src,
int64_t
* number)
217
{
218
char
buff[
PARSING_STRING_MAX_LENGTH
];
219
long
long
result;
220
char
*endPointer;
221
222
if
(src->
length
== 0 || src->
length
>=
PARSING_STRING_MAX_LENGTH
)
223
return
EXIP_INVALID_STRING_OPERATION
;
224
225
memcpy(buff, src->
str
, src->
length
);
226
buff[src->
length
] =
'\0'
;
227
228
result =
EXIP_STRTOLL
(buff, &endPointer, 10);
229
230
if
(result ==
LLONG_MAX
|| result ==
LLONG_MIN
|| *src->
str
== *endPointer)
231
return
EXIP_INVALID_STRING_OPERATION
;
232
233
if
(result >=
LLONG_MAX
|| result <=
LLONG_MIN
)
234
return
EXIP_OUT_OF_BOUND_BUFFER
;
235
236
*number = (
int64_t
) result;
237
238
return
EXIP_OK
;
239
}
240
241
#if EXIP_IMPLICIT_DATA_TYPE_CONVERSION
242
243
errorCode
integerToString
(
Integer
number,
String
* outStr)
244
{
245
return
EXIP_NOT_IMPLEMENTED_YET
;
246
}
247
248
errorCode
booleanToString
(
boolean
b,
String
* outStr)
249
{
250
return
EXIP_NOT_IMPLEMENTED_YET
;
251
}
252
253
errorCode
floatToString
(
Float
f,
String
* outStr)
254
{
255
return
EXIP_NOT_IMPLEMENTED_YET
;
256
}
257
258
errorCode
decimalToString
(
Decimal
d,
String
* outStr)
259
{
260
return
EXIP_NOT_IMPLEMENTED_YET
;
261
}
262
263
errorCode
dateTimeToString
(
EXIPDateTime
dt,
String
* outStr)
264
{
265
return
EXIP_NOT_IMPLEMENTED_YET
;
266
}
267
268
#endif
/* EXIP_IMPLICIT_DATA_TYPE_CONVERSION */
269
270
#if EXIP_DEBUG == ON
271
272
void
printString
(
const
String
* inStr)
273
{
274
if
(inStr->
length
== 0)
275
return
;
276
277
DEBUG_OUTPUT
((
"%.*s"
, inStr->
length
, inStr->
str
));
278
}
279
280
#endif
/* EXIP_DEBUG */
Generated on Thu Nov 27 2014 10:56:08 for exip by
1.8.4