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.0
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  end = NULL;
97  else if(!end)
98  end = s;
99  }
100 
101  //Trim whitespace from the end
102  if(end)
103  *end = '\0';
104 
105  //Return the string with leading and trailing whitespace omitted
106  return result;
107 }
108 
109 
110 /**
111  * @brief Removes all trailing whitespace from a string
112  * @param[in,out] s Pointer to a NULL-terminated character string
113  **/
114 
116 {
117  char_t *end;
118 
119  //Search for the first whitespace to remove at the end of the string
120  for(end = NULL; *s != '\0'; s++)
121  {
122  if(!osIsspace(*s))
123  end = NULL;
124  else if(!end)
125  end = s;
126  }
127 
128  //Trim whitespace from the end
129  if(end)
130  *end = '\0';
131 }
132 
133 
134 /**
135  * @brief Replace all occurrences of the specified character
136  * @param[in,out] s Pointer to a NULL-terminated character string
137  * @param[in] oldChar The character to be replaced
138  * @param[in] newChar The character that will replace all occurrences of oldChar
139  **/
140 
141 void strReplaceChar(char_t *s, char_t oldChar, char_t newChar)
142 {
143  //Parse the specified string
144  while(*s != '\0')
145  {
146  //Remplace all occurrences of the specified character
147  if(*s == oldChar)
148  *s = newChar;
149 
150  //Next character
151  s++;
152  }
153 }
154 
155 
156 /**
157  * @brief Copy string
158  * @param[out] dest Pointer to the destination string
159  * @param[in] src Pointer to the source string
160  * @param[in] destSize Size of the buffer allocated for the destination string
161  * @return Error code
162  **/
163 
164 error_t strSafeCopy(char_t *dest, const char_t *src, size_t destSize)
165 {
166  size_t n;
167 
168  //Check parameters
169  if(dest == NULL || src == NULL || destSize < 1)
171 
172  //Get the length of the source name
173  n = osStrlen(src);
174  //Limit the number of characters to be copied
175  n = MIN(n, destSize - 1);
176 
177  //Copy the string
178  osMemcpy(dest, src, n);
179  //Properly terminate the string with a NULL character
180  dest[n] = '\0';
181 
182  //Successful processing
183  return NO_ERROR;
184 }
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t s
Definition: ndp.h:345
uint8_t p
Definition: ndp.h:300
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
#define osStrlen(s)
Definition: os_port.h:165
#define osIsspace(c)
Definition: os_port.h:285
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
void strRemoveTrailingSpace(char_t *s)
Removes all trailing whitespace from a string.
Definition: str.c:115
char_t * strDuplicate(const char_t *s)
Duplicate a string.
Definition: str.c:42
void strReplaceChar(char_t *s, char_t oldChar, char_t newChar)
Replace all occurrences of the specified character.
Definition: str.c:141
error_t strSafeCopy(char_t *dest, const char_t *src, size_t destSize)
Copy string.
Definition: str.c:164
char_t * strTrimWhitespace(char_t *s)
Removes all leading and trailing whitespace from a string.
Definition: str.c:78
String manipulation helper functions.