Webserver+3d print
Embed:
(wiki syntax)
Show/hide line numbers
str.c
Go to the documentation of this file.
00001 /** 00002 * @file str.c 00003 * @brief String manipulation helper functions 00004 * 00005 * @section License 00006 * 00007 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software Foundation, 00021 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 * 00023 * @author Oryx Embedded SARL (www.oryx-embedded.com) 00024 * @version 1.7.6 00025 **/ 00026 00027 //Dependencies 00028 #include <stdio.h> 00029 #include <string.h> 00030 #include <ctype.h> 00031 #include "str.h" 00032 00033 00034 /** 00035 * @brief Duplicate a string 00036 * @param[in] s Pointer to a constant NULL-terminated character string 00037 * @return Address of the string that was copied, or NULL if the string cannot be copied 00038 **/ 00039 00040 char_t *strDuplicate(const char_t *s) 00041 { 00042 uint_t n; 00043 char_t *p; 00044 00045 //Calculate the length occupied by the input string 00046 n = strlen(s) + 1; 00047 00048 //Allocate memory to hold the new string 00049 p = osAllocMem(n); 00050 //Failed to allocate memory? 00051 if(!p) 00052 return NULL; 00053 00054 //Make a copy of the input string 00055 memcpy(p, s, n); 00056 00057 //Return a pointer to the newly created string 00058 return p; 00059 } 00060 00061 00062 /** 00063 * @brief Removes all leading and trailing whitespace from a string 00064 * @param[in] s The string that will be trimmed 00065 * @return String with whitespace stripped from the beginning and end 00066 **/ 00067 00068 char_t *strTrimWhitespace(char_t *s) 00069 { 00070 char_t *end; 00071 char_t *result; 00072 00073 //Trim whitespace from the beginning 00074 while(isspace((uint8_t) *s)) s++; 00075 //Save the current position 00076 result = s; 00077 00078 //Search for the first whitespace to remove 00079 //at the end of the string 00080 for(end = NULL; *s != '\0'; s++) 00081 { 00082 if(!isspace((uint8_t) *s)) 00083 end = NULL; 00084 else if(!end) 00085 end = s; 00086 } 00087 00088 //Trim whitespace from the end 00089 if(end) *end = '\0'; 00090 00091 //Return the string with leading and 00092 //trailing whitespace omitted 00093 return result; 00094 } 00095 00096 00097 /** 00098 * @brief Removes all trailing whitespace from a string 00099 * @param[in,out] s Pointer to a NULL-terminated character string 00100 **/ 00101 00102 void strRemoveTrailingSpace(char_t *s) 00103 { 00104 char_t *end; 00105 00106 //Search for the first whitespace to remove 00107 //at the end of the string 00108 for(end = NULL; *s != '\0'; s++) 00109 { 00110 if(!isspace((uint8_t) *s)) 00111 end = NULL; 00112 else if(!end) 00113 end = s; 00114 } 00115 00116 //Trim whitespace from the end 00117 if(end) *end = '\0'; 00118 } 00119 00120 00121 /** 00122 * @brief Replace all occurrences of the specified character 00123 * @param[in,out] s Pointer to a NULL-terminated character string 00124 * @param[in] oldChar The character to be replaced 00125 * @param[in] newChar The character that will replace all occurrences of oldChar 00126 **/ 00127 00128 void strReplaceChar(char_t *s, char_t oldChar, char_t newChar) 00129 { 00130 //Parse the specified string 00131 while(*s != '\0') 00132 { 00133 //Remplace all occurrences of the specified character 00134 if(*s == oldChar) 00135 *s = newChar; 00136 } 00137 } 00138 00139 00140 /** 00141 * @brief Copy string 00142 * @param[out] dest Pointer to the destination string 00143 * @param[in] src Pointer to the source string 00144 * @param[in] destSize Size of the buffer allocated for the destination string 00145 * @return Error code 00146 **/ 00147 00148 error_t strSafeCopy(char_t *dest, const char_t *src, size_t destSize) 00149 { 00150 size_t n; 00151 00152 //Check parameters 00153 if(dest == NULL || src == NULL || destSize < 1) 00154 return ERROR_INVALID_PARAMETER; 00155 00156 //Get the length of the source name 00157 n = strlen(src); 00158 //Limit the number of characters to be copied 00159 n = MIN(n, destSize - 1); 00160 00161 //Copy the string 00162 strncpy(dest, src, n); 00163 //Properly terminate the string with a NULL character 00164 dest[n] = '\0'; 00165 00166 //Successful processing 00167 return NO_ERROR; 00168 } 00169
Generated on Tue Jul 12 2022 17:10:17 by
