str.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-2023 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 2.2.4
  27. **/
  28. //Dependencies
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include "str.h"
  33. /**
  34. * @brief Duplicate a string
  35. * @param[in] s Pointer to a constant NULL-terminated character string
  36. * @return Address of the string that was copied, or NULL if the string cannot be copied
  37. **/
  38. char_t *strDuplicate(const char_t *s)
  39. {
  40. uint_t n;
  41. char_t *p;
  42. //Pointer to the newly created string
  43. p = NULL;
  44. //Valid string?
  45. if(s != NULL)
  46. {
  47. //Calculate the length occupied by the input string
  48. n = osStrlen(s) + 1;
  49. //Allocate memory to hold the new string
  50. p = osAllocMem(n);
  51. //Successful memory allocation?
  52. if(p != NULL)
  53. {
  54. //Make a copy of the input string
  55. osMemcpy(p, s, n);
  56. }
  57. }
  58. //Return a pointer to the newly created string
  59. return p;
  60. }
  61. /**
  62. * @brief Removes all leading and trailing whitespace from a string
  63. * @param[in] s The string that will be trimmed
  64. * @return String with whitespace stripped from the beginning and end
  65. **/
  66. char_t *strTrimWhitespace(char_t *s)
  67. {
  68. char_t *end;
  69. char_t *result;
  70. //Trim whitespace from the beginning
  71. while(osIsspace(*s))
  72. {
  73. s++;
  74. }
  75. //Save the current position
  76. result = s;
  77. //Search for the first whitespace to remove at the end of the string
  78. for(end = NULL; *s != '\0'; s++)
  79. {
  80. if(!osIsspace(*s))
  81. end = NULL;
  82. else if(!end)
  83. end = s;
  84. }
  85. //Trim whitespace from the end
  86. if(end)
  87. *end = '\0';
  88. //Return the string with leading and trailing whitespace omitted
  89. return result;
  90. }
  91. /**
  92. * @brief Removes all trailing whitespace from a string
  93. * @param[in,out] s Pointer to a NULL-terminated character string
  94. **/
  95. void strRemoveTrailingSpace(char_t *s)
  96. {
  97. char_t *end;
  98. //Search for the first whitespace to remove at the end of the string
  99. for(end = NULL; *s != '\0'; s++)
  100. {
  101. if(!osIsspace(*s))
  102. end = NULL;
  103. else if(!end)
  104. end = s;
  105. }
  106. //Trim whitespace from the end
  107. if(end)
  108. *end = '\0';
  109. }
  110. /**
  111. * @brief Replace all occurrences of the specified character
  112. * @param[in,out] s Pointer to a NULL-terminated character string
  113. * @param[in] oldChar The character to be replaced
  114. * @param[in] newChar The character that will replace all occurrences of oldChar
  115. **/
  116. void strReplaceChar(char_t *s, char_t oldChar, char_t newChar)
  117. {
  118. //Parse the specified string
  119. while(*s != '\0')
  120. {
  121. //Remplace all occurrences of the specified character
  122. if(*s == oldChar)
  123. *s = newChar;
  124. //Next character
  125. s++;
  126. }
  127. }
  128. /**
  129. * @brief Copy string
  130. * @param[out] dest Pointer to the destination string
  131. * @param[in] src Pointer to the source string
  132. * @param[in] destSize Size of the buffer allocated for the destination string
  133. * @return Error code
  134. **/
  135. error_t strSafeCopy(char_t *dest, const char_t *src, size_t destSize)
  136. {
  137. size_t n;
  138. //Check parameters
  139. if(dest == NULL || src == NULL || destSize < 1)
  140. return WINDOWS_ERROR_INVALID_PARAMETER;
  141. //Get the length of the source name
  142. n = osStrlen(src);
  143. //Limit the number of characters to be copied
  144. n = MIN(n, destSize - 1);
  145. //Copy the string
  146. osMemcpy(dest, src, n);
  147. //Properly terminate the string with a NULL character
  148. dest[n] = '\0';
  149. //Successful processing
  150. return SUCCESS;
  151. }