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 */
20void 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 */
30size_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/* split string to list by given string */
49strlist *strsplit(const char *str, const char *split_s)
50{
51    char *cur, *next;
52    if(!str) return NULL;
53
54    strlist *sl = strlist_alloc(0);
55    char *ss = ld_strdup(str);
56    if (!sl || !ss) {
57        strlist_free(sl);
58        __libc_free(ss);
59        return NULL;
60    }
61
62    cur = ss;
63    while (next = strstr(cur, split_s)) {
64        *next = 0;
65        strtrim(cur);
66        strlist_set(sl, cur);
67        cur = next + strlen(split_s);
68    }
69    strtrim(cur);
70    strlist_set(sl, cur);
71    __libc_free(ss);
72    return sl;
73}
74
75#define STR_DEFAULT_SIZE   16
76
77strlist *strlist_alloc(size_t size)
78{
79    strlist *strs;
80    if (size < STR_DEFAULT_SIZE) size = STR_DEFAULT_SIZE ;
81
82    strs = (strlist *)__libc_calloc(1, sizeof *strs) ;
83
84    if (strs) {
85        strs->strs  = (char **)__libc_calloc(size, sizeof *strs->strs);
86        if (strs->strs) {
87            strs->size = size;
88        } else {
89            __libc_free(strs);
90            strs = NULL;
91        }
92    }
93    return strs ;
94}
95
96static void strlist_realloc(strlist *strs)
97{
98    if(!strs) return;
99    size_t size = 2*strs->size;
100    if (size) {
101        char **ss = (char **)__libc_realloc(strs->strs, size * (sizeof *strs->strs));
102        if (ss) {
103            strs->size = size;
104            strs->strs = ss;
105        }
106    }
107    return;
108}
109
110void strlist_free(strlist *strs)
111{
112    if (!strs) return;
113    for (size_t i=0; i < strs->num; i++) {
114        __libc_free(strs->strs[i]);
115    }
116    __libc_free(strs->strs);
117    __libc_free(strs);
118}
119
120void strlist_set(strlist *strs,const char *str)
121{
122    if (!strs || !str) return;
123    if (strs->num == strs->size) {
124       strlist_realloc(strs);
125    }
126    if (strs->num < strs->size) {
127        strs->strs[strs->num] = ld_strdup(str);
128        if (strs->strs[strs->num]) strs->num++;
129    }
130}
131
132char *ld_strdup(const char *s)
133{
134    size_t l = strlen(s);
135    char *d = __libc_malloc(l+1);
136    if (!d) return NULL;
137    return memcpy(d, s, l+1);
138}