]> arthur.barton.de Git - ngircd-alex.git/blob - src/tool/tool.c
const'ify ngt_SyslogFacilityName() function
[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  * Tool functions
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <netinet/in.h>
24
25 #ifdef SYSLOG
26 #define SYSLOG_NAMES 1
27 #include <syslog.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 /**
72  * Convert a string to uppercase letters.
73  */
74 GLOBAL char *
75 ngt_UpperStr(char *String)
76 {
77         char *ptr;
78
79         assert(String != NULL);
80
81         ptr = String;
82         while(*ptr) {
83                 *ptr = toupper(*ptr);
84                 ptr++;
85         }
86         return String;
87 } /* ngt_UpperStr */
88
89
90 /**
91  * Convert a string to lowercase letters.
92  */
93 GLOBAL char *
94 ngt_LowerStr(char *String)
95 {
96         char *ptr;
97
98         assert(String != NULL);
99
100         ptr = String;
101         while(*ptr) {
102                 *ptr = tolower(*ptr);
103                 ptr++;
104         }
105         return String;
106 } /* ngt_LowerStr */
107
108
109 GLOBAL void
110 ngt_TrimLastChr( char *String, const char Chr)
111 {
112         /* If last character in the string matches Chr, remove it.
113          * Empty strings are handled correctly. */
114
115         size_t len;
116
117         assert(String != NULL);
118
119         len = strlen(String);
120         if(len == 0)
121                 return;
122
123         len--;
124
125         if(String[len] == Chr)
126                 String[len] = '\0';
127 } /* ngt_TrimLastChr */
128
129
130 #ifdef SYSLOG
131
132
133 #ifndef INTERNAL_MARK
134
135 #ifndef _code
136 typedef struct _code {
137         char    *c_name;
138         int     c_val;
139 } CODE;
140 #endif
141
142 CODE facilitynames[] = {
143 #ifdef LOG_AUTH
144         { "auth",       LOG_AUTH },
145 #endif
146 #ifdef LOG_AUTHPRIV
147         { "authpriv",   LOG_AUTHPRIV },
148 #endif
149 #ifdef LOG_CRON
150         { "cron",       LOG_CRON },
151 #endif
152 #ifdef LOG_DAEMON
153         { "daemon",     LOG_DAEMON },
154 #endif
155 #ifdef LOG_FTP
156         { "ftp",        LOG_FTP },
157 #endif
158 #ifdef LOG_LPR
159         { "lpr",        LOG_LPR },
160 #endif
161 #ifdef LOG_MAIL
162         { "mail",       LOG_MAIL },
163 #endif
164 #ifdef LOG_NEWS
165         { "news",       LOG_NEWS },
166 #endif
167 #ifdef LOG_UUCP
168         { "uucp",       LOG_UUCP },
169 #endif
170 #ifdef LOG_USER
171         { "user",       LOG_USER },
172 #endif
173 #ifdef LOG_LOCAL7
174         { "local0",     LOG_LOCAL0 },
175         { "local1",     LOG_LOCAL1 },
176         { "local2",     LOG_LOCAL2 },
177         { "local3",     LOG_LOCAL3 },
178         { "local4",     LOG_LOCAL4 },
179         { "local5",     LOG_LOCAL5 },
180         { "local6",     LOG_LOCAL6 },
181         { "local7",     LOG_LOCAL7 },
182 #endif
183         { 0,            -1 }
184 };
185
186 #endif
187
188
189 GLOBAL const char
190 *ngt_SyslogFacilityName(int Facility)
191 {
192         int i = 0;
193         while(facilitynames[i].c_name) {
194                 if (facilitynames[i].c_val == Facility)
195                         return facilitynames[i].c_name;
196                 i++;
197         }
198         return "unknown";
199 }
200
201
202 GLOBAL int
203 ngt_SyslogFacilityID(char *Name, int DefaultFacility)
204 {
205         int i = 0;
206         while(facilitynames[i].c_name) {
207                 if (strcasecmp(facilitynames[i].c_name, Name) == 0)
208                         return facilitynames[i].c_val;
209                 i++;
210         }
211         return DefaultFacility;
212 }
213
214
215 #endif
216
217
218 /* -eof- */