]> arthur.barton.de Git - ngircd-alex.git/blob - src/tool/tool.c
dc2384519cd54e6337a0fd2cdc35722a83ecb257
[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.7 2007/11/23 16:26:05 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 <netinet/in.h>
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
28 #endif
29
30 #include "exp.h"
31 #include "tool.h"
32
33
34 /**
35  * Removes all leading and trailing whitespaces of a string.
36  * @param String The string to remove whitespaces from.
37  */
38 GLOBAL void
39 ngt_TrimStr(char *String)
40 {
41         char *start, *end;
42
43         assert(String != NULL);
44
45         start = String;
46
47         /* Remove whitespaces at the beginning of the string ... */
48         while (*start == ' ' || *start == '\t' ||
49                *start == '\n' || *start == '\r')
50                 start++;
51
52         if (!*start) {
53                 *String = '\0';
54                 return;
55         }
56
57         /* ... and at the end: */
58         end = strchr(start, '\0');
59         end--;
60         while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
61                && end >= start)
62                 end--;
63
64         /* New trailing NULL byte */
65         *(++end) = '\0';
66
67         memmove(String, start, (size_t)(end - start)+1);
68 } /* ngt_TrimStr */
69
70
71 GLOBAL char *
72 ngt_LowerStr( char *String )
73 {
74         /* String in Kleinbuchstaben konvertieren. Der uebergebene
75          * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
76          * wird dieser auch als Pointer geliefert. */
77
78         char *ptr;
79
80         assert( String != NULL );
81
82         /* Zeichen konvertieren */
83         ptr = String;
84         while( *ptr )
85         {
86                 *ptr = tolower( *ptr );
87                 ptr++;
88         }
89         
90         return String;
91 } /* ngt_LowerStr */
92
93
94 GLOBAL void
95 ngt_TrimLastChr( char *String, const char Chr)
96 {
97         /* If last character in the string matches Chr, remove it.
98          * Empty strings are handled correctly. */
99
100         unsigned int len;
101
102         assert( String != NULL );
103
104         len = strlen( String );
105         if( len == 0 ) return;
106
107         len--;
108
109         if( String[len] == Chr ) String[len] = '\0';
110 } /* ngt_TrimLastChr */
111
112
113 GLOBAL bool
114 ngt_IPStrToBin(const char *ip_str, struct in_addr *inaddr)
115 {
116         /* AF is always AF_INET for now */
117 #ifdef HAVE_INET_ATON
118         if (inet_aton(ip_str, inaddr) == 0)
119                 return false;
120 #else
121         inaddr->s_addr = inet_addr(ip_str);
122         if (inaddr->s_addr == (unsigned)-1)
123                 return false;
124 #endif
125         return true;
126 }
127
128
129
130
131 /* -eof- */