str.c
Go to the documentation of this file.
1 /**
2  * @file str.c
3  * @brief String manipulation helper functions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 2.4.4
27  **/
28 
29 //Dependencies
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include "str.h"
34 
35 
36 /**
37  * @brief Duplicate a string
38  * @param[in] s Pointer to a constant NULL-terminated character string
39  * @return Address of the string that was copied, or NULL if the string cannot be copied
40  **/
41 
43 {
44  uint_t n;
45  char_t *p;
46 
47  //Pointer to the newly created string
48  p = NULL;
49 
50  //Valid string?
51  if(s != NULL)
52  {
53  //Calculate the length occupied by the input string
54  n = osStrlen(s) + 1;
55 
56  //Allocate memory to hold the new string
57  p = osAllocMem(n);
58 
59  //Successful memory allocation?
60  if(p != NULL)
61  {
62  //Make a copy of the input string
63  osMemcpy(p, s, n);
64  }
65  }
66 
67  //Return a pointer to the newly created string
68  return p;
69 }
70 
71 
72 /**
73  * @brief Removes all leading and trailing whitespace from a string
74  * @param[in] s The string that will be trimmed
75  * @return String with whitespace stripped from the beginning and end
76  **/
77 
79 {
80  char_t *end;
81  char_t *result;
82 
83  //Trim whitespace from the beginning
84  while(osIsspace(*s))
85  {
86  s++;
87  }
88 
89  //Save the current position
90  result = s;
91 
92  //Search for the first whitespace to remove at the end of the string
93  for(end = NULL; *s != '\0'; s++)
94  {
95  if(!osIsspace(*s))
96  {
97  end = NULL;
98  }
99  else if(!end)
100  {
101  end = s;
102  }
103  }
104 
105  //Trim whitespace from the end
106  if(end)
107  *end = '\0';
108 
109  //Return the string with leading and trailing whitespace omitted
110  return result;
111 }
112 
113 
114 /**
115  * @brief Removes all trailing whitespace from a string
116  * @param[in,out] s Pointer to a NULL-terminated character string
117  **/
118 
120 {
121  char_t *end;
122 
123  //Search for the first whitespace to remove at the end of the string
124  for(end = NULL; *s != '\0'; s++)
125  {
126  if(!osIsspace(*s))
127  {
128  end = NULL;
129  }
130  else if(!end)
131  {
132  end = s;
133  }
134  }
135 
136  //Trim whitespace from the end
137  if(end)
138  *end = '\0';
139 }
140 
141 
142 /**
143  * @brief Replace all occurrences of the specified character
144  * @param[in,out] s Pointer to a NULL-terminated character string
145  * @param[in] oldChar The character to be replaced
146  * @param[in] newChar The character that will replace all occurrences of oldChar
147  **/
148 
149 void strReplaceChar(char_t *s, char_t oldChar, char_t newChar)
150 {
151  //Parse the specified string
152  while(*s != '\0')
153  {
154  //Remplace all occurrences of the specified character
155  if(*s == oldChar)
156  *s = newChar;
157 
158  //Next character
159  s++;
160  }
161 }
162 
163 
164 /**
165  * @brief Copy string
166  * @param[out] dest Pointer to the destination string
167  * @param[in] src Pointer to the source string
168  * @param[in] destSize Size of the buffer allocated for the destination string
169  * @return Error code
170  **/
171 
172 error_t strSafeCopy(char_t *dest, const char_t *src, size_t destSize)
173 {
174  size_t n;
175 
176  //Check parameters
177  if(dest == NULL || src == NULL || destSize < 1)
179 
180  //Get the length of the source name
181  n = osStrlen(src);
182  //Limit the number of characters to be copied
183  n = MIN(n, destSize - 1);
184 
185  //Copy the string
186  osMemcpy(dest, src, n);
187  //Properly terminate the string with a NULL character
188  dest[n] = '\0';
189 
190  //Successful processing
191  return NO_ERROR;
192 }
String manipulation helper functions.
uint8_t p
Definition: ndp.h:300
char_t * strTrimWhitespace(char_t *s)
Removes all leading and trailing whitespace from a string.
Definition: str.c:78
void strRemoveTrailingSpace(char_t *s)
Removes all trailing whitespace from a string.
Definition: str.c:119
#define osStrlen(s)
Definition: os_port.h:165
char_t * strDuplicate(const char_t *s)
Duplicate a string.
Definition: str.c:42
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
error_t
Error codes.
Definition: error.h:43
void strReplaceChar(char_t *s, char_t oldChar, char_t newChar)
Replace all occurrences of the specified character.
Definition: str.c:149
#define osIsspace(c)
Definition: os_port.h:291
#define MIN(a, b)
Definition: os_port.h:63
char char_t
Definition: compiler_port.h:48
error_t strSafeCopy(char_t *dest, const char_t *src, size_t destSize)
Copy string.
Definition: str.c:172
uint8_t n
uint8_t s
Definition: igmp_common.h:234
unsigned int uint_t
Definition: compiler_port.h:50
@ NO_ERROR
Success.
Definition: error.h:44