]> arthur.barton.de Git - ngircd-alex.git/blob - src/tool/tool.c
fix ngt_TrimStr(), fix format string
[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.5 2006/03/24 23:25:39 fw 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         if (!*start) {
47                 *String = 0;
48                 return;
49         }
50         /* ... and at the end: */
51         end = strchr(start, '\0');
52         end--;
53         while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
54                && end > start)
55                 end--;
56
57         /* New trailing NULL byte */
58         *(++end) = '\0';
59
60         memmove(String, start, (size_t)(end - start)+1);
61 } /* ngt_TrimStr */
62
63
64 GLOBAL char *
65 ngt_LowerStr( char *String )
66 {
67         /* String in Kleinbuchstaben konvertieren. Der uebergebene
68          * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
69          * wird dieser auch als Pointer geliefert. */
70
71         char *ptr;
72
73         assert( String != NULL );
74
75         /* Zeichen konvertieren */
76         ptr = String;
77         while( *ptr )
78         {
79                 *ptr = tolower( *ptr );
80                 ptr++;
81         }
82         
83         return String;
84 } /* ngt_LowerStr */
85
86
87 GLOBAL void
88 ngt_TrimLastChr( char *String, const char Chr)
89 {
90         /* If last character in the string matches Chr, remove it.
91          * Empty strings are handled correctly. */
92
93         unsigned int len;
94
95         assert( String != NULL );
96
97         len = strlen( String );
98         if( len == 0 ) return;
99
100         len--;
101
102         if( String[len] == Chr ) String[len] = '\0';
103 } /* ngt_TrimLastChr */
104
105
106 /* -eof- */