Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: tool.c,v 1.6 2006/04/09 12:53:07 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "exp.h"
26 #include "tool.h"
29 /**
30 * Removes all leading and trailing whitespaces of a string.
31 * @param String The string to remove whitespaces from.
32 */
33 GLOBAL void
34 ngt_TrimStr(char *String)
35 {
36 char *start, *end;
38 assert(String != NULL);
40 start = String;
42 /* Remove whitespaces at the beginning of the string ... */
43 while (*start == ' ' || *start == '\t' ||
44 *start == '\n' || *start == '\r')
45 start++;
47 if (!*start) {
48 *String = '\0';
49 return;
50 }
52 /* ... and at the end: */
53 end = strchr(start, '\0');
54 end--;
55 while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
56 && end >= start)
57 end--;
59 /* New trailing NULL byte */
60 *(++end) = '\0';
62 memmove(String, start, (size_t)(end - start)+1);
63 } /* ngt_TrimStr */
66 GLOBAL char *
67 ngt_LowerStr( char *String )
68 {
69 /* String in Kleinbuchstaben konvertieren. Der uebergebene
70 * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
71 * wird dieser auch als Pointer geliefert. */
73 char *ptr;
75 assert( String != NULL );
77 /* Zeichen konvertieren */
78 ptr = String;
79 while( *ptr )
80 {
81 *ptr = tolower( *ptr );
82 ptr++;
83 }
85 return String;
86 } /* ngt_LowerStr */
89 GLOBAL void
90 ngt_TrimLastChr( char *String, const char Chr)
91 {
92 /* If last character in the string matches Chr, remove it.
93 * Empty strings are handled correctly. */
95 unsigned int len;
97 assert( String != NULL );
99 len = strlen( String );
100 if( len == 0 ) return;
102 len--;
104 if( String[len] == Chr ) String[len] = '\0';
105 } /* ngt_TrimLastChr */
108 /* -eof- */