tool.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2009 Alexander Barton (alex@barton.de)
  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 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. *
  11. * Tool functions
  12. */
  13. #include "portab.h"
  14. #include "imp.h"
  15. #include <assert.h>
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <netinet/in.h>
  20. #include "exp.h"
  21. #include "tool.h"
  22. /**
  23. * Removes all leading and trailing whitespaces of a string.
  24. * @param String The string to remove whitespaces from.
  25. */
  26. GLOBAL void
  27. ngt_TrimStr(char *String)
  28. {
  29. char *start, *end;
  30. assert(String != NULL);
  31. start = String;
  32. /* Remove whitespaces at the beginning of the string ... */
  33. while (*start == ' ' || *start == '\t' ||
  34. *start == '\n' || *start == '\r')
  35. start++;
  36. if (!*start) {
  37. *String = '\0';
  38. return;
  39. }
  40. /* ... and at the end: */
  41. end = strchr(start, '\0');
  42. end--;
  43. while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
  44. && end >= start)
  45. end--;
  46. /* New trailing NULL byte */
  47. *(++end) = '\0';
  48. memmove(String, start, (size_t)(end - start)+1);
  49. } /* ngt_TrimStr */
  50. /**
  51. * Convert a string to uppercase letters.
  52. */
  53. GLOBAL char *
  54. ngt_UpperStr(char *String)
  55. {
  56. char *ptr;
  57. assert(String != NULL);
  58. ptr = String;
  59. while(*ptr) {
  60. *ptr = toupper(*ptr);
  61. ptr++;
  62. }
  63. return String;
  64. } /* ngt_UpperStr */
  65. /**
  66. * Convert a string to lowercase letters.
  67. */
  68. GLOBAL char *
  69. ngt_LowerStr(char *String)
  70. {
  71. char *ptr;
  72. assert(String != NULL);
  73. ptr = String;
  74. while(*ptr) {
  75. *ptr = tolower(*ptr);
  76. ptr++;
  77. }
  78. return String;
  79. } /* ngt_LowerStr */
  80. GLOBAL void
  81. ngt_TrimLastChr( char *String, const char Chr)
  82. {
  83. /* If last character in the string matches Chr, remove it.
  84. * Empty strings are handled correctly. */
  85. size_t len;
  86. assert(String != NULL);
  87. len = strlen(String);
  88. if(len == 0)
  89. return;
  90. len--;
  91. if(String[len] == Chr)
  92. String[len] = '\0';
  93. } /* ngt_TrimLastChr */
  94. /* -eof- */