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