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