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