]> jfr.im git - irc/thales.git/blob - src/misc.c
Initial revision
[irc/thales.git] / src / misc.c
1 /* Thales - IRC to Relational Database Gateway
2 * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "thales.h"
20 #include "misc.h"
21
22 /*************************************************************************/
23
24 /* strscpy: Copy at most len-1 characters from a string to a buffer, and
25 * add a null terminator after the last character copied.
26 */
27
28 char *strscpy(char *d, const char *s, size_t len)
29 {
30 char *d_orig = d;
31
32 if (!len)
33 return d;
34 while (--len && (*d++ = *s++))
35 ;
36 *d = 0;
37 return d_orig;
38 }
39
40 /*************************************************************************/
41
42 /* stristr: Search case-insensitively for string s2 within string s1,
43 * returning the first occurrence of s2 or NULL if s2 was not
44 * found.
45 */
46
47 char *stristr(char *s1, char *s2)
48 {
49 register char *s = s1, *d = s2;
50
51 while (*s1)
52 {
53 if (tolower(*s1) == tolower(*d))
54 {
55 s1++;
56 d++;
57 if (*d == 0)
58 return s;
59 }
60 else
61 {
62 s = ++s1;
63 d = s2;
64 }
65 }
66 return NULL;
67 }
68
69 /*************************************************************************/
70
71 /* strnrepl: Replace occurrences of `old' with `new' in string `s'. Stop
72 * replacing if a replacement would cause the string to exceed
73 * `size' bytes (including the null terminator). Return the
74 * string.
75 */
76
77 char *strnrepl(char *s, int size, const char *old, const char *new)
78 {
79 char *ptr = s;
80 int left = strlen(s);
81 int avail = size - (left + 1);
82 int oldlen = strlen(old);
83 int newlen = strlen(new);
84 int diff = newlen - oldlen;
85
86 while (left >= oldlen)
87 {
88 if (strncmp(ptr, old, oldlen) != 0)
89 {
90 left--;
91 ptr++;
92 continue;
93 }
94 if (diff > avail)
95 break;
96 if (diff != 0)
97 memmove(ptr + oldlen + diff, ptr + oldlen, left + 1);
98 strncpy(ptr, new, newlen);
99 ptr += newlen;
100 left -= oldlen;
101 }
102 return s;
103 }
104
105 /*************************************************************************/
106 /*************************************************************************/
107
108 /* merge_args: Take an argument count and argument vector and merge them
109 * into a single string in which each argument is separated by
110 * a space.
111 */
112
113 char *merge_args(int argc, char **argv)
114 {
115 int i;
116 static char s[4096];
117 char *t;
118
119 t = s;
120 for (i = 0; i < argc; i++)
121 t += snprintf(t, sizeof(s) - (t - s), "%s%s", *argv++,
122 (i < argc - 1) ? " " : "");
123 return s;
124 }
125
126 /*************************************************************************/
127
128 /* dotime: Return the number of seconds corresponding to the given time
129 * string. If the given string does not represent a valid time,
130 * return -1.
131 *
132 * A time string is either a plain integer (representing a number
133 * of seconds), or an integer followed by one of these characters:
134 * "s" (seconds), "m" (minutes), "h" (hours), or "d" (days).
135 */
136
137 int dotime(const char *s)
138 {
139 int amount;
140
141 amount = strtol(s, (char **) &s, 10);
142 if (*s)
143 {
144 switch (*s)
145 {
146 case 's':
147 return amount;
148 case 'm':
149 return amount * 60;
150 case 'h':
151 return amount * 3600;
152 case 'd':
153 return amount * 86400;
154 default:
155 return -1;
156 }
157 }
158 else
159 {
160 return amount;
161 }
162 }
163
164 void strtolwr(char *ch)
165 {
166 while (*ch)
167 {
168 *ch = tolower(*ch);
169 ch++;
170 }
171 }