trim.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ** Command & Conquer Generals(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /VSS_Sync/wwlib/trim.cpp $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 8/29/01 10:24p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "always.h"
  36. #include "trim.h"
  37. #include <ctype.h>
  38. #include <stddef.h>
  39. #include <string.h>
  40. #ifdef _UNIX
  41. #include <wctype.h>
  42. #endif // _UNIX
  43. /***********************************************************************************************
  44. * strtrim -- Trim leading and trailing white space off of string. *
  45. * *
  46. * This routine will remove the leading and trailing whitespace from the string specifed. *
  47. * The string is modified in place. *
  48. * *
  49. * INPUT: buffer -- Pointer to the string to be trimmed. *
  50. * *
  51. * OUTPUT: none *
  52. * *
  53. * WARNINGS: none *
  54. * *
  55. * HISTORY: *
  56. * 02/06/1997 JLB : Created. *
  57. *=============================================================================================*/
  58. char * strtrim(char * buffer)
  59. {
  60. if (buffer != NULL) {
  61. /*
  62. ** Strip leading white space from the string.
  63. */
  64. char * source = buffer;
  65. while (isspace(*source)) {
  66. source++;
  67. }
  68. if (source != buffer) {
  69. strcpy(buffer, source);
  70. }
  71. /*
  72. ** Clip trailing white space from the string.
  73. */
  74. for (int index = strlen(buffer)-1; index >= 0; index--) {
  75. if (isspace(buffer[index])) {
  76. buffer[index] = '\0';
  77. } else {
  78. break;
  79. }
  80. }
  81. }
  82. return(buffer);
  83. }
  84. wchar_t * wcstrim(wchar_t * buffer)
  85. {
  86. if (buffer != NULL) {
  87. /*
  88. ** Strip leading white space from the string.
  89. */
  90. wchar_t * source = buffer;
  91. while (iswspace(*source)) {
  92. source++;
  93. }
  94. if (source != buffer) {
  95. wcscpy(buffer, source);
  96. }
  97. /*
  98. ** Clip trailing white space from the string.
  99. */
  100. for (int index = wcslen(buffer)-1; index >= 0; index--) {
  101. if (iswspace(buffer[index])) {
  102. buffer[index] = L'\0';
  103. } else {
  104. break;
  105. }
  106. }
  107. }
  108. return(buffer);
  109. }