1 /*
2 * Copyright (c) 2022 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 <ctype.h>
17 #include "strops.h"
18 #include "malloc_impl.h"
19
20 /* string to lower */
strlwc(char * str)21 void strlwc(char *str)
22 {
23 if (str == NULL) return;
24 while (*str != '\0') {
25 *str = (char)tolower(*str);
26 str++ ;
27 }
28 return;
29 }
30 /* trim head and tail spaces of string */
strtrim(char * str)31 size_t strtrim(char *str)
32 {
33 char *last = NULL;
34 char *dest = str;
35
36 if (str == NULL) return 0;
37
38 last = str + strlen(str);
39 while (isspace((int)*str) && *str) str++;
40 while (last > str) {
41 if (!isspace((int)*(last-1))) break;
42 last--;
43 }
44 *last = (char)0;
45
46 memmove(dest, str, last-str+1);
47 return last-str;
48 }
49 /* split string to list by given string */
strsplit(const char * str,const char * split_s)50 strlist *strsplit(const char *str, const char *split_s)
51 {
52 char *cur, *next;
53 if(!str) return NULL;
54
55 strlist *sl = strlist_alloc(0);
56 char *ss = ld_strdup(str);
57 if (!sl || !ss) {
58 strlist_free(sl);
59 internal_free(ss);
60 return NULL;
61 }
62
63 cur = ss;
64 while (next = strstr(cur, split_s)) {
65 *next = 0;
66 strtrim(cur);
67 strlist_set(sl, cur);
68 cur = next + strlen(split_s);
69 }
70 strtrim(cur);
71 strlist_set(sl, cur);
72 internal_free(ss);
73 return sl;
74 }
75
76 #define STR_DEFAULT_SIZE 16
77
strlist_alloc(size_t size)78 strlist *strlist_alloc(size_t size)
79 {
80 strlist *strs;
81 if (size < STR_DEFAULT_SIZE) size = STR_DEFAULT_SIZE ;
82
83 strs = (strlist *)internal_calloc(1, sizeof *strs) ;
84
85 if (strs) {
86 strs->strs = (char **)internal_calloc(size, sizeof *strs->strs);
87 if (strs->strs) {
88 strs->size = size;
89 } else {
90 internal_free(strs);
91 strs = NULL;
92 }
93 }
94 return strs ;
95 }
96
strlist_realloc(strlist * strs)97 static void strlist_realloc(strlist *strs)
98 {
99 if(!strs) return;
100 size_t size = 2*strs->size;
101 if (size) {
102 char **ss = (char **)internal_realloc(strs->strs, size * (sizeof *strs->strs));
103 if (ss) {
104 strs->size = size;
105 strs->strs = ss;
106 }
107 }
108 return;
109 }
110
strlist_free(strlist * strs)111 void strlist_free(strlist *strs)
112 {
113 if (!strs) return;
114 for (size_t i=0; i < strs->num; i++) {
115 internal_free(strs->strs[i]);
116 }
117 internal_free(strs->strs);
118 internal_free(strs);
119 }
120
strlist_set(strlist * strs,const char * str)121 void strlist_set(strlist *strs,const char *str)
122 {
123 if (!strs || !str) return;
124 if (strs->num == strs->size) {
125 strlist_realloc(strs);
126 }
127 if (strs->num < strs->size) {
128 strs->strs[strs->num] = ld_strdup(str);
129 if (strs->strs[strs->num]) strs->num++;
130 }
131 }
132
ld_strdup(const char * s)133 char *ld_strdup(const char *s)
134 {
135 size_t l = strlen(s);
136 char *d = internal_malloc(l+1);
137 if (!d) return NULL;
138 return memcpy(d, s, l+1);
139 }