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