trim.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. *** 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:: /Commando/Code/wwlib/trim.cpp $*
  25. * *
  26. * $Author:: Denzil_l $*
  27. * *
  28. * $Modtime:: 11/08/01 11:35a $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "always.h"
  36. #include "trim.h"
  37. #include <string.h>
  38. #ifdef _UNIX
  39. #include <wctype.h>
  40. #endif // _UNIX
  41. /***********************************************************************************************
  42. * strtrim -- Trim leading and trailing white space off of string. *
  43. * *
  44. * This routine will remove the leading and trailing whitespace from the string specifed. *
  45. * The string is modified in place. *
  46. * *
  47. * INPUT: buffer -- Pointer to the string to be trimmed. *
  48. * *
  49. * OUTPUT: none *
  50. * *
  51. * WARNINGS: none *
  52. * *
  53. * HISTORY: *
  54. * 02/06/1997 JLB : Created. *
  55. *=============================================================================================*/
  56. char* strtrim(char* buffer)
  57. {
  58. if (buffer) {
  59. /* Strip leading white space from the string. */
  60. char* source = buffer;
  61. while ((*source != 0) && ((unsigned char)*source <= 32)) {
  62. ++source;
  63. }
  64. if (source != buffer) {
  65. strcpy(buffer, source);
  66. }
  67. /* Clip trailing white space from the string. */
  68. for (int index = strlen(buffer) - 1; index >= 0; --index) {
  69. if ((*source != 0) && ((unsigned char)buffer[index] <= 32)) {
  70. buffer[index] = '\0';
  71. } else {
  72. break;
  73. }
  74. }
  75. }
  76. return buffer;
  77. }
  78. wchar_t* wcstrim(wchar_t* buffer)
  79. {
  80. if (buffer) {
  81. /* Strip leading white space from the string. */
  82. wchar_t* source = buffer;
  83. while ((*source != 0) && ((unsigned int)*source <= 32)) {
  84. ++source;
  85. }
  86. if (source != buffer) {
  87. wcscpy(buffer, source);
  88. }
  89. /* Clip trailing white space from the string. */
  90. for (int index = wcslen(buffer) - 1; index >= 0; --index) {
  91. if ((*source != 0) && ((unsigned int)buffer[index] <= 32)) {
  92. buffer[index] = L'\0';
  93. } else {
  94. break;
  95. }
  96. }
  97. }
  98. return buffer;
  99. }