• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HC_STRING_H
17 #define HC_STRING_H
18 
19 #include "hcf_parcel.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25     typedef struct HcString {
26         HcParcel parcel; // parcel data, used to storage the string data
27     } HcString;
28 
29     /*
30     * Append string pointer
31     * Notice: It will add '\0' automatically.
32     * @param self: self pointer.
33     * @param str: string pointer.
34     * @return true (ok), false (error)
35     */
36     bool StringAppendPointer(HcString *self, const char *str);
37 
38     /*
39     * Assign a value to the HcString
40     * Notice: It will add '\0' automatically.
41     * @param self: self pointer.
42     * @param str: assign value of string pointer.
43     * @return true (ok), false (error)
44     */
45     bool StringSetPointer(HcString *self, const char *str);
46 
47     /*
48     * Assign a value to the HcString with fixed length
49     * Notice: It will add '\0' automatically.
50     * @param self: self pointer.
51     * @param str: assign value of string pointer.
52     * @param len: the length of string.
53     * @return true (ok), false (error)
54     */
55     bool StringSetPointerWithLength(HcString* self, const char *str, uint32_t len);
56 
57     /*
58     * Get the string pointer data
59     * @param self: self pointer.
60     * @return the pointer data of the string
61     */
62     const char* StringGet(const HcString *self);
63 
64     /*
65     * Get the length of the string
66     * @param self: self pointer.
67     * @return the length of the string
68     */
69     uint32_t StringLength(const HcString *self);
70 
71     /*
72     * Find a char from string
73     * @param self: self pointer.
74     * @param c: the char you want find
75     * @param begin: the position find from
76     * @return the position of the char
77     */
78     int StringFind(const HcString *self, char c, uint32_t begin);
79 
80     /*
81     * Get sub string from a string.
82     * @param self: self pointer.
83     * @param begin: the begin position of the sub string.
84     * @param len: the length of the sub string.
85     * @param dst: the string pointer which saved the sub string content.
86     * @return the operation result.
87     */
88     bool StringSubString(const HcString *self, uint32_t begin, uint32_t len, HcString* dst);
89 
90     /*
91     * Compare the string with another string.
92     * @param self: self pointer.
93     * @param dst: the pointer of another string.
94     * @return the compare result.
95     *  -1: self is smaller than dst
96     *   0: self is equal with dst
97     *   1: self is bigger than dst
98     */
99     int StringCompare(const HcString *self, const char* dst);
100 
101     /*
102     * Create a string.
103     * Notice: You should delete string when you don't need the string anymore.
104     * @return the created string.
105     */
106     HcString CreateString(void);
107 
108     /*
109     * Delete a string. In fact it will not destroy the string,
110     * but only free the allocated memory of the string and reset the member's value
111     * of the string. You can continue to use the string if you want.
112     * Notice: You should delete the string when you don't need it any more to avoid memory leak.
113     * @param str: The string you want to delete.
114     */
115     void DeleteString(HcString *str);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 #endif
121