1 /*
2 * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
3 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for
6 * any purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /**
21 * DOC: qdf_str
22 * QCA driver framework (QDF) string APIs.
23 */
24
25 #ifndef __QDF_STR_H
26 #define __QDF_STR_H
27
28 #include "i_qdf_str.h"
29 #include "qdf_types.h"
30
31 /**
32 * qdf_is_space() - check if @c is a whitespace character
33 * @c: the character to check
34 *
35 * Whitespace characters include HT, LF, VT, FF, CR, space, and nbsp
36 *
37 * Return: true if @ is a whitespace character
38 */
qdf_is_space(char c)39 static inline bool qdf_is_space(char c)
40 {
41 return __qdf_is_space(c);
42 }
43
44 /**
45 * qdf_str_cmp - Compare two strings
46 * @str1: First string
47 * @str2: Second string
48 * Return:
49 * 0 - strings are equal
50 * <0 - str1 sorts lexicographically before str2
51 * >0 - str1 sorts lexicographically after str2
52 */
qdf_str_cmp(const char * str1,const char * str2)53 static inline int32_t qdf_str_cmp(const char *str1, const char *str2)
54 {
55 return __qdf_str_cmp(str1, str2);
56 }
57
58 /**
59 * qdf_str_dup() - duplicate null-terminated string @src
60 * @dest: double pointer to be populated
61 * @src: the null-terminated string to be duplicated
62 *
63 * @dest must be freed using qdf_mem_free() to avoid memory leaks.
64 *
65 * Return: QDF_STATUS; @dest set to NULL on failure, a valid address on success
66 */
67 QDF_STATUS qdf_str_dup(char **dest, const char *src);
68
69 /**
70 * qdf_str_eq - compare two null-terminated strings for equality
71 * @left: the string left of the equality
72 * @right: the string right of the equality
73 *
74 * This is a thin wrapper over `if (strcmp(left, right) == 0)` for clarity.
75 *
76 * Return: true if strings are equal
77 */
qdf_str_eq(const char * left,const char * right)78 static inline bool qdf_str_eq(const char *left, const char *right)
79 {
80 return qdf_str_cmp(left, right) == 0;
81 }
82
83 /**
84 * qdf_str_lcopy - Bounded copy from one string to another
85 * @dest: destination string
86 * @src: source string
87 * @dest_size: max number of bytes to copy (incl. null terminator)
88 *
89 * If the return value is >= @dest_size, @dest has been truncated.
90 *
91 * Return: length of @src
92 */
93 static inline qdf_size_t
qdf_str_lcopy(char * dest,const char * src,uint32_t dest_size)94 qdf_str_lcopy(char *dest, const char *src, uint32_t dest_size)
95 {
96 return __qdf_str_lcopy(dest, src, dest_size);
97 }
98
99 /**
100 * qdf_str_left_trim() - Trim any leading whitespace from @str
101 * @str: the string to trim
102 *
103 * Return: A pointer to the first non-space character in @str
104 */
qdf_str_left_trim(const char * str)105 static inline const char *qdf_str_left_trim(const char *str)
106 {
107 return __qdf_str_left_trim(str);
108 }
109
110 /**
111 * qdf_str_len() - returns the length of a null-terminated string
112 * @str: input string
113 *
114 * Return: length of @str (without null terminator)
115 */
qdf_str_len(const char * str)116 static inline qdf_size_t qdf_str_len(const char *str)
117 {
118 return __qdf_str_len(str);
119 }
120
121 /**
122 * qdf_str_right_trim() - Trim any trailing whitespace from @str
123 * @str: the string to trim
124 *
125 * Note: The first trailing whitespace character is replaced with a
126 * null-terminator
127 *
128 * Return: None
129 */
130 void qdf_str_right_trim(char *str);
131
132 /**
133 * qdf_str_trim() - Trim any leading/trailing whitespace from @str
134 * @str: the string to trim
135 *
136 * Note: The first trailing whitespace character is replaced with a
137 * null-terminator
138 *
139 * Return: A pointer to the first non-space character in @str
140 */
qdf_str_trim(char * str)141 static inline char *qdf_str_trim(char *str)
142 {
143 return __qdf_str_trim(str);
144 }
145
146 /**
147 * qdf_str_nlen() - Get string length up to @limit characters
148 * @str: the string to get the length of
149 * @limit: the maximum number of characters to check
150 *
151 * Return: the less of @limit or the length of @str (without null terminator)
152 */
qdf_str_nlen(const char * str,qdf_size_t limit)153 static inline qdf_size_t qdf_str_nlen(const char *str, qdf_size_t limit)
154 {
155 return __qdf_str_nlen(str, limit);
156 }
157
158 /**
159 * qdf_str_ncmp - Compare two strings
160 * @str1: First string
161 * @str2: Second string
162 * @limit: the maximum number of characters to check
163 * Return:
164 * 0 - strings are equal
165 * <0 - str1 sorts lexicographically before str2
166 * >0 - str1 sorts lexicographically after str2
167 */
168 static inline int32_t
qdf_str_ncmp(const char * str1,const char * str2,qdf_size_t limit)169 qdf_str_ncmp(const char *str1, const char *str2, qdf_size_t limit)
170 {
171 return __qdf_str_ncmp(str1, str2, limit);
172 }
173
174 /**
175 * qdf_str_sep - extract token from string
176 * @str: String buffer
177 * @delim: Delimitter
178 * Return: Pointer to the first token
179 *
180 */
qdf_str_sep(char ** str,char * delim)181 static inline char *qdf_str_sep(char **str, char *delim)
182 {
183 return __qdf_str_sep(str, delim);
184 }
185
186 /**
187 * qdf_str_copy_all_before_char() - API to copy all character before a
188 * particular char provided
189 * @str: Source string
190 * @str_len: Source string length
191 * @dst: Destination string
192 * @dst_len: Destination string length
193 * @c: Character before which all characters need to be copied
194 *
195 * Return: length of the copied string, if success. zero otherwise.
196 */
197 uint32_t
198 qdf_str_copy_all_before_char(char *str, uint32_t str_len,
199 char *dst, uint32_t dst_len, char c);
200 #endif /* __QDF_STR_H */
201