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