1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <string.h>
17 #include "hc_string.h"
18
19 const uint32_t STRING_ALLOC_SIZE = 10;
20 const uint32_t STRING_END_CHAR_LENGTH = 1;
21 const char STRING_END_CHAR = '\0';
22
23 /*
24 * Append a HcString
25 * Notice: It will add '\0' automatically.
26 * @param self: self pointer.
27 * @param str: append string.
28 * @return HC_TRUE (ok), HC_FALSE (error)
29 */
StringAppend(HcString * self,HcString str)30 HcBool StringAppend(HcString *self, HcString str)
31 {
32 uint32_t length = GetParcelDataSize(&str.parcel);
33 if (self != NULL && length > 0) {
34 // remove '\0'
35 ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
36 // append string(include '\0')
37 return StringAppendPointer(self, GetParcelData(&str.parcel));
38 }
39
40 return HC_FALSE;
41 }
42
43 /*
44 * Append string pointer
45 * Notice: It will add '\0' automatically.
46 * @param self: self pointer.
47 * @param str: string pointer.
48 * @return HC_TRUE (ok), HC_FALSE (error)
49 */
StringAppendPointer(HcString * self,const char * str)50 HcBool StringAppendPointer(HcString *self, const char *str)
51 {
52 if (self != NULL && str != NULL) {
53 // remove '\0'
54 ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
55 // append string (include '\0')
56 return ParcelWrite(&self->parcel, (void *)str, strlen(str) + 1);
57 }
58
59 return HC_FALSE;
60 }
61
62 /*
63 * Append a char
64 * Notice: It will add '\0' automatically.
65 * @param self: self pointer.
66 * @param str: char.
67 * @return HC_TRUE (ok), HC_FALSE (error)
68 */
StringAppendChar(HcString * self,char c)69 HcBool StringAppendChar(HcString *self, char c)
70 {
71 if (self != NULL && c != STRING_END_CHAR) {
72 // remove '\0'
73 ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
74
75 if (ParcelWriteInt8(&self->parcel, c)) {
76 return ParcelWriteInt8(&self->parcel, (uint32_t)STRING_END_CHAR);
77 }
78 }
79
80 return HC_FALSE;
81 }
82
83 /*
84 * Assign a value to the HcString
85 * Notice: It will add '\0' automatically.
86 * @param self: self pointer.
87 * @param str: assign value of ta_sting.
88 * @return HC_TRUE (ok), HC_FALSE (error)
89 */
StringSet(HcString * self,HcString str)90 HcBool StringSet(HcString *self, HcString str)
91 {
92 if (self != NULL) {
93 DeleteParcel(&self->parcel);
94 return StringAppend(self, str);
95 }
96
97 return HC_FALSE;
98 }
99
100 /*
101 * Assign a value to the HcString
102 * Notice: It will add '\0' automatically.
103 * @param self: self pointer.
104 * @param str: assign value of string pointer.
105 * @return HC_TRUE (ok), HC_FALSE (error)
106 */
StringSetPointer(HcString * self,const char * str)107 HcBool StringSetPointer(HcString *self, const char *str)
108 {
109 if (self != NULL) {
110 DeleteParcel(&self->parcel);
111 return StringAppendPointer(self, str);
112 }
113
114 return HC_FALSE;
115 }
116
117 /*
118 * Get the string pointer data
119 * @param self: self pointer.
120 * @return the pointer data of the string
121 */
StringGet(const HcString * self)122 const char *StringGet(const HcString *self)
123 {
124 if (self == NULL) {
125 return NULL;
126 }
127
128 return GetParcelData(&self->parcel);
129 }
130
131 /*
132 * Get the length of the string
133 * @param self: self pointer.
134 * @return the length of the string
135 */
StringLength(const HcString * self)136 uint32_t StringLength(const HcString *self)
137 {
138 if (self == NULL) {
139 return 0;
140 } else {
141 uint32_t length = GetParcelDataSize(&self->parcel);
142 if (length > 0) {
143 return length - STRING_END_CHAR_LENGTH;
144 } else {
145 return 0;
146 }
147 }
148 }
149
150 /*
151 * Create a string.
152 * Notice: You should delete_string when you don't need the string anymore.
153 * @return return the created string.
154 */
CreateString(void)155 HcString CreateString(void)
156 {
157 HcString str;
158 str.parcel = CreateParcel(0, STRING_ALLOC_SIZE);
159 ParcelWriteInt8(&str.parcel, STRING_END_CHAR);
160 return str;
161 }
162
163 /*
164 * Delete a string. In fact it will not destroy the string,
165 * but only free the allocate memory of the string and reset the member's value
166 * of the string.
167 * You can continue to use the string if you want.
168 * Notice: You should delete the string when you don't need it any more to avoid memory leak.
169 * @param str: The string you want to delete.
170 */
DeleteString(HcString * str)171 void DeleteString(HcString *str)
172 {
173 if (str != NULL) {
174 DeleteParcel(&str->parcel);
175 }
176 }
177