strtrim.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. *
  22. * DESCRIPTION
  23. *
  24. * PROGRAMMER
  25. * Denzil E. Long, Jr.
  26. *
  27. * VERSION INFO
  28. * $Author: Denzil_l $
  29. * $Revision: 2 $
  30. * $Modtime: 3/29/00 1:17p $
  31. * $Archive: /Commando/Code/Scripts/strtrim.cpp $
  32. *
  33. ******************************************************************************/
  34. #include "strtrim.h"
  35. #include <ctype.h>
  36. #include <stddef.h>
  37. #include <string.h>
  38. /******************************************************************************
  39. *
  40. * NAME
  41. * strtrim
  42. *
  43. * DESCRIPTION
  44. * Trim leading and trailing white space off of a string.
  45. *
  46. * INPUTS
  47. * char* buffer
  48. *
  49. * RESULTS
  50. * char*
  51. *
  52. ******************************************************************************/
  53. char* strtrim(char* buffer)
  54. {
  55. if (buffer != NULL)
  56. {
  57. // Strip leading white space from the string.
  58. char* source = buffer;
  59. while (isspace(*source))
  60. source++;
  61. if (source != buffer)
  62. strcpy(buffer, source);
  63. // Clip trailing white space from the string.
  64. for (int index = strlen(buffer) - 1; index >= 0; index--)
  65. {
  66. if (isspace(buffer[index]))
  67. buffer[index] = '\0';
  68. else
  69. break;
  70. }
  71. }
  72. return buffer;
  73. }