]> arthur.barton.de Git - ngircd-alex.git/blob - src/tool/tool.c
Fix "implicit conversion shortens 64-bit value" warning
[ngircd-alex.git] / src / tool / tool.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2009 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 #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 == '\n' || *start == '\r')
45                 start++;
46
47         if (!*start) {
48                 *String = '\0';
49                 return;
50         }
51
52         /* ... and at the end: */
53         end = strchr(start, '\0');
54         end--;
55         while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
56                && end >= start)
57                 end--;
58
59         /* New trailing NULL byte */
60         *(++end) = '\0';
61
62         memmove(String, start, (size_t)(end - start)+1);
63 } /* ngt_TrimStr */
64
65
66 /**
67  * Convert a string to uppercase letters.
68  */
69 GLOBAL char *
70 ngt_UpperStr(char *String)
71 {
72         char *ptr;
73
74         assert(String != NULL);
75
76         ptr = String;
77         while(*ptr) {
78                 *ptr = toupper(*ptr);
79                 ptr++;
80         }
81         return String;
82 } /* ngt_UpperStr */
83
84
85 /**
86  * Convert a string to lowercase letters.
87  */
88 GLOBAL char *
89 ngt_LowerStr(char *String)
90 {
91         char *ptr;
92
93         assert(String != NULL);
94
95         ptr = String;
96         while(*ptr) {
97                 *ptr = tolower(*ptr);
98                 ptr++;
99         }
100         return String;
101 } /* ngt_LowerStr */
102
103
104 GLOBAL void
105 ngt_TrimLastChr( char *String, const char Chr)
106 {
107         /* If last character in the string matches Chr, remove it.
108          * Empty strings are handled correctly. */
109
110         size_t len;
111
112         assert(String != NULL);
113
114         len = strlen(String);
115         if(len == 0)
116                 return;
117
118         len--;
119
120         if(String[len] == Chr)
121                 String[len] = '\0';
122 } /* ngt_TrimLastChr */
123
124
125 /* -eof- */