]> arthur.barton.de Git - ngircd-alex.git/blob - src/tool/tool.c
Add Doxygen @file documentation to each source and header file
[ngircd-alex.git] / src / tool / tool.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 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
12 #include "portab.h"
13
14 /**
15  * @file
16  * Tool functions
17  */
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 #ifdef SYSLOG
28 #define SYSLOG_NAMES 1
29 #include <syslog.h>
30 #endif
31
32 #include "exp.h"
33 #include "tool.h"
34
35
36 /**
37  * Removes all leading and trailing whitespaces of a string.
38  * @param String The string to remove whitespaces from.
39  */
40 GLOBAL void
41 ngt_TrimStr(char *String)
42 {
43         char *start, *end;
44
45         assert(String != NULL);
46
47         start = String;
48
49         /* Remove whitespaces at the beginning of the string ... */
50         while (*start == ' ' || *start == '\t' ||
51                *start == '\n' || *start == '\r')
52                 start++;
53
54         if (!*start) {
55                 *String = '\0';
56                 return;
57         }
58
59         /* ... and at the end: */
60         end = strchr(start, '\0');
61         end--;
62         while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
63                && end >= start)
64                 end--;
65
66         /* New trailing NULL byte */
67         *(++end) = '\0';
68
69         memmove(String, start, (size_t)(end - start)+1);
70 } /* ngt_TrimStr */
71
72
73 /**
74  * Convert a string to uppercase letters.
75  */
76 GLOBAL char *
77 ngt_UpperStr(char *String)
78 {
79         char *ptr;
80
81         assert(String != NULL);
82
83         ptr = String;
84         while(*ptr) {
85                 *ptr = toupper(*ptr);
86                 ptr++;
87         }
88         return String;
89 } /* ngt_UpperStr */
90
91
92 /**
93  * Convert a string to lowercase letters.
94  */
95 GLOBAL char *
96 ngt_LowerStr(char *String)
97 {
98         char *ptr;
99
100         assert(String != NULL);
101
102         ptr = String;
103         while(*ptr) {
104                 *ptr = tolower(*ptr);
105                 ptr++;
106         }
107         return String;
108 } /* ngt_LowerStr */
109
110
111 GLOBAL void
112 ngt_TrimLastChr( char *String, const char Chr)
113 {
114         /* If last character in the string matches Chr, remove it.
115          * Empty strings are handled correctly. */
116
117         size_t len;
118
119         assert(String != NULL);
120
121         len = strlen(String);
122         if(len == 0)
123                 return;
124
125         len--;
126
127         if(String[len] == Chr)
128                 String[len] = '\0';
129 } /* ngt_TrimLastChr */
130
131
132 #ifdef SYSLOG
133
134
135 #ifndef INTERNAL_MARK
136
137 #ifndef _code
138 typedef struct _code {
139         char    *c_name;
140         int     c_val;
141 } CODE;
142 #endif
143
144 CODE facilitynames[] = {
145 #ifdef LOG_AUTH
146         { "auth",       LOG_AUTH },
147 #endif
148 #ifdef LOG_AUTHPRIV
149         { "authpriv",   LOG_AUTHPRIV },
150 #endif
151 #ifdef LOG_CRON
152         { "cron",       LOG_CRON },
153 #endif
154 #ifdef LOG_DAEMON
155         { "daemon",     LOG_DAEMON },
156 #endif
157 #ifdef LOG_FTP
158         { "ftp",        LOG_FTP },
159 #endif
160 #ifdef LOG_LPR
161         { "lpr",        LOG_LPR },
162 #endif
163 #ifdef LOG_MAIL
164         { "mail",       LOG_MAIL },
165 #endif
166 #ifdef LOG_NEWS
167         { "news",       LOG_NEWS },
168 #endif
169 #ifdef LOG_UUCP
170         { "uucp",       LOG_UUCP },
171 #endif
172 #ifdef LOG_USER
173         { "user",       LOG_USER },
174 #endif
175 #ifdef LOG_LOCAL7
176         { "local0",     LOG_LOCAL0 },
177         { "local1",     LOG_LOCAL1 },
178         { "local2",     LOG_LOCAL2 },
179         { "local3",     LOG_LOCAL3 },
180         { "local4",     LOG_LOCAL4 },
181         { "local5",     LOG_LOCAL5 },
182         { "local6",     LOG_LOCAL6 },
183         { "local7",     LOG_LOCAL7 },
184 #endif
185         { 0,            -1 }
186 };
187
188 #endif
189
190
191 GLOBAL const char*
192 ngt_SyslogFacilityName(int Facility)
193 {
194         int i = 0;
195         while(facilitynames[i].c_name) {
196                 if (facilitynames[i].c_val == Facility)
197                         return facilitynames[i].c_name;
198                 i++;
199         }
200         return "unknown";
201 }
202
203
204 GLOBAL int
205 ngt_SyslogFacilityID(char *Name, int DefaultFacility)
206 {
207         int i = 0;
208         while(facilitynames[i].c_name) {
209                 if (strcasecmp(facilitynames[i].c_name, Name) == 0)
210                         return facilitynames[i].c_val;
211                 i++;
212         }
213         return DefaultFacility;
214 }
215
216
217 #endif
218
219
220 /* -eof- */