]> arthur.barton.de Git - ngircd-alex.git/blob - src/tool/tool.c
ngt_TrimStr(): code cleanup.
[ngircd-alex.git] / src / tool / tool.c
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
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: tool.c,v 1.4 2006/03/22 08:05:10 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "exp.h"
26 #include "tool.h"
27
28
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;
37
38         assert(String != NULL);
39
40         start = String;
41
42         /* Remove whitespaces at the beginning of the string ... */
43         while (*start == ' ' || *start == '\t')
44                 start++;
45
46         /* ... and at the end: */
47         end = strchr(start, '\0');
48         while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
49                && end >= start)
50                 end--;
51
52         /* New trailing NULL byte */
53         *(++end) = '\0';
54
55         memmove(String, start, (size_t)(end - start));
56 } /* ngt_TrimStr */
57
58
59 GLOBAL char *
60 ngt_LowerStr( char *String )
61 {
62         /* String in Kleinbuchstaben konvertieren. Der uebergebene
63          * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
64          * wird dieser auch als Pointer geliefert. */
65
66         char *ptr;
67
68         assert( String != NULL );
69
70         /* Zeichen konvertieren */
71         ptr = String;
72         while( *ptr )
73         {
74                 *ptr = tolower( *ptr );
75                 ptr++;
76         }
77         
78         return String;
79 } /* ngt_LowerStr */
80
81
82 GLOBAL void
83 ngt_TrimLastChr( char *String, const char Chr)
84 {
85         /* If last character in the string matches Chr, remove it.
86          * Empty strings are handled correctly. */
87
88         unsigned int len;
89
90         assert( String != NULL );
91
92         len = strlen( String );
93         if( len == 0 ) return;
94
95         len--;
96
97         if( String[len] == Chr ) String[len] = '\0';
98 } /* ngt_TrimLastChr */
99
100
101 /* -eof- */