trim.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #ifndef TRIM_H
  28. #define TRIM_H
  29. #include "str.h"
  30. /*
  31. * This switch-case statement is used in
  32. * trim_leading and trim_trailing. You can
  33. * define characters that should be skipped
  34. * here.
  35. */
  36. #define TRIM_SWITCH(c) switch(c) { \
  37. case ' ': \
  38. case '\t': \
  39. case '\r': \
  40. case '\n': \
  41. break; \
  42. \
  43. default: \
  44. return; \
  45. }
  46. /*
  47. * Remove any leading whitechars, like spaces,
  48. * horizontal tabs, carriage returns and line
  49. * feeds
  50. *
  51. * WARNING: String descriptor structure will be
  52. * modified ! Make a copy otherwise you
  53. * might be unable to free _s->s for
  54. * example !
  55. *
  56. */
  57. static inline void trim_leading(str* _s)
  58. {
  59. for(; _s->len > 0; _s->len--, _s->s++) {
  60. TRIM_SWITCH(*(_s->s));
  61. }
  62. }
  63. /*
  64. * Remove any trailing white char, like spaces,
  65. * horizontal tabs, carriage returns and line feeds
  66. *
  67. * WARNING: String descriptor structure will be
  68. * modified ! Make a copy otherwise you
  69. * might be unable to free _s->s for
  70. * example !
  71. */
  72. static inline void trim_trailing(str* _s)
  73. {
  74. for(; _s->len > 0; _s->len--) {
  75. TRIM_SWITCH(_s->s[_s->len - 1]);
  76. }
  77. }
  78. /*
  79. * Do trim_leading and trim_trailing
  80. *
  81. * WARNING: String structure will be modified !
  82. * Make a copy otherwise you might be
  83. * unable to free _s->s for example !
  84. */
  85. static inline void trim(str* _s)
  86. {
  87. trim_leading(_s);
  88. trim_trailing(_s);
  89. }
  90. /*
  91. * right and left space trimming
  92. *
  93. * WARNING: String structure will be modified !
  94. * Make a copy otherwise you might be
  95. * unable to free _s_->s for example !
  96. */
  97. #define trim_spaces_lr(_s_) \
  98. do{ \
  99. for(;(_s_).s[(_s_).len-1]==' ';(_s_).s[--(_s_).len]=0); \
  100. for(;(_s_).s[0]==' ';(_s_).s=(_s_).s+1,(_s_).len--); \
  101. \
  102. } while(0);
  103. #endif /* TRIM_H */