tool.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2005 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. static char UNUSED id[] = "$Id: tool.c,v 1.9 2008/02/26 22:04:18 fw Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <netinet/in.h>
  21. #include "exp.h"
  22. #include "tool.h"
  23. /**
  24. * Removes all leading and trailing whitespaces of a string.
  25. * @param String The string to remove whitespaces from.
  26. */
  27. GLOBAL void
  28. ngt_TrimStr(char *String)
  29. {
  30. char *start, *end;
  31. assert(String != NULL);
  32. start = String;
  33. /* Remove whitespaces at the beginning of the string ... */
  34. while (*start == ' ' || *start == '\t' ||
  35. *start == '\n' || *start == '\r')
  36. start++;
  37. if (!*start) {
  38. *String = '\0';
  39. return;
  40. }
  41. /* ... and at the end: */
  42. end = strchr(start, '\0');
  43. end--;
  44. while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
  45. && end >= start)
  46. end--;
  47. /* New trailing NULL byte */
  48. *(++end) = '\0';
  49. memmove(String, start, (size_t)(end - start)+1);
  50. } /* ngt_TrimStr */
  51. GLOBAL char *
  52. ngt_LowerStr( char *String )
  53. {
  54. /* String in Kleinbuchstaben konvertieren. Der uebergebene
  55. * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
  56. * wird dieser auch als Pointer geliefert. */
  57. char *ptr;
  58. assert( String != NULL );
  59. /* Zeichen konvertieren */
  60. ptr = String;
  61. while( *ptr )
  62. {
  63. *ptr = tolower( *ptr );
  64. ptr++;
  65. }
  66. return String;
  67. } /* ngt_LowerStr */
  68. GLOBAL void
  69. ngt_TrimLastChr( char *String, const char Chr)
  70. {
  71. /* If last character in the string matches Chr, remove it.
  72. * Empty strings are handled correctly. */
  73. unsigned int len;
  74. assert( String != NULL );
  75. len = strlen( String );
  76. if( len == 0 ) return;
  77. len--;
  78. if( String[len] == Chr ) String[len] = '\0';
  79. } /* ngt_TrimLastChr */
  80. /* -eof- */