xref: /wlan-driver/qca-wifi-host-cmn/qdf/src/qdf_str.c (revision 5113495b16420b49004c444715d2daae2066e7dc)
1 /*
2  * Copyright (c) 2018, 2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "qdf_mem.h"
20 #include "qdf_module.h"
21 #include "qdf_str.h"
22 #include "qdf_trace.h"
23 
qdf_str_dup(char ** dest,const char * src)24 QDF_STATUS qdf_str_dup(char **dest, const char *src)
25 {
26 	qdf_size_t size;
27 	char *dup;
28 
29 	*dest = NULL;
30 
31 	QDF_BUG(src);
32 	if (!src)
33 		return QDF_STATUS_E_INVAL;
34 
35 	/* size = length + null-terminator */
36 	size = qdf_str_len(src) + 1;
37 	dup = qdf_mem_malloc(size);
38 	if (!dup)
39 		return QDF_STATUS_E_NOMEM;
40 
41 	qdf_mem_copy(dup, src, size);
42 	*dest = dup;
43 
44 	return QDF_STATUS_SUCCESS;
45 }
46 qdf_export_symbol(qdf_str_dup);
47 
qdf_str_right_trim(char * str)48 void qdf_str_right_trim(char *str)
49 {
50 	char *end = str + qdf_str_len(str) - 1;
51 
52 	while (end >= str && qdf_is_space(*end))
53 		end--;
54 
55 	end[1] = '\0';
56 }
57 qdf_export_symbol(qdf_str_right_trim);
58 
59 uint32_t
qdf_str_copy_all_before_char(char * str,uint32_t str_len,char * dst,uint32_t dst_len,char c)60 qdf_str_copy_all_before_char(char *str, uint32_t str_len,
61 			     char *dst, uint32_t dst_len, char c)
62 {
63 	uint32_t len = 0;
64 
65 	if (!str)
66 		return len;
67 
68 	while ((len < str_len) && (len < dst_len) &&
69 	       (*str != '\0') && (*str != c)) {
70 		*dst++ = *str++;
71 		len++;
72 	}
73 
74 	return len;
75 }
76 qdf_export_symbol(qdf_str_copy_all_before_char);
77