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