tool.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.6 2006/04/09 12:53:07 alex Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <string.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. GLOBAL char *
  51. ngt_LowerStr( char *String )
  52. {
  53. /* String in Kleinbuchstaben konvertieren. Der uebergebene
  54. * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
  55. * wird dieser auch als Pointer geliefert. */
  56. char *ptr;
  57. assert( String != NULL );
  58. /* Zeichen konvertieren */
  59. ptr = String;
  60. while( *ptr )
  61. {
  62. *ptr = tolower( *ptr );
  63. ptr++;
  64. }
  65. return String;
  66. } /* ngt_LowerStr */
  67. GLOBAL void
  68. ngt_TrimLastChr( char *String, const char Chr)
  69. {
  70. /* If last character in the string matches Chr, remove it.
  71. * Empty strings are handled correctly. */
  72. unsigned int len;
  73. assert( String != NULL );
  74. len = strlen( String );
  75. if( len == 0 ) return;
  76. len--;
  77. if( String[len] == Chr ) String[len] = '\0';
  78. } /* ngt_TrimLastChr */
  79. /* -eof- */