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
19 /* string to lower */
strlwc(char * str)20 void strlwc(char *str)
21 {
22 if (str == NULL) return;
23 while (*str != '\0') {
24 *str = (char)tolower(*str);
25 str++ ;
26 }
27 return;
28 }
29 /* trim head and tail spaces of string */
strtrim(char * str)30 size_t strtrim(char *str)
31 {
32 char *last = NULL;
33 char *dest = str;
34
35 if (str == NULL) return 0;
36
37 last = str + strlen(str);
38 while (isspace((int)*str) && *str) str++;
39 while (last > str) {
40 if (!isspace((int)*(last-1))) break;
41 last--;
42 }
43 *last = (char)0;
44
45 memmove(dest, str, last-str+1);
46 return last-str;
47 }
48
49 static strlist slist;
50 static char* list[2];
51
strsplit(const char * str,const char * split_s)52 strlist *strsplit(const char *str, const char *split_s)
53 {
54 if(!str) return NULL;
55
56 slist.num = 2;
57 slist.size = 16;
58
59 slist.strs = list;
60
61 slist.strs[0] = "/data/test";
62 slist.strs[1] = "/etc";
63 return &slist;
64 }
65
66 #define STR_DEFAULT_SIZE 16
67
strlist_alloc(size_t size)68 strlist *strlist_alloc(size_t size)
69 {
70 return NULL;
71 }
72
strlist_realloc(strlist * strs)73 static void strlist_realloc(strlist *strs)
74 {
75 }
76
strlist_free(strlist * strs)77 void strlist_free(strlist *strs)
78 {
79 }
80
strlist_set(strlist * strs,const char * str)81 void strlist_set(strlist *strs,const char *str)
82 {
83 if (!strs || !str) return;
84 if (strs->num == strs->size) {
85 strlist_realloc(strs);
86 }
87 if (strs->num < strs->size) {
88 strs->strs[strs->num] = ld_strdup(str);
89 if (strs->strs[strs->num]) strs->num++;
90 }
91 }
92
93 static char starupbuf[80];
ld_strdup(const char * s)94 char *ld_strdup(const char *s)
95 {
96 size_t l = strlen(s);
97 return memcpy(starupbuf, s, l+1);
98 }