* wildmat.c, braindamage.c: added
[citadel.git] / webcit / tools.c
1 /*
2  * tools.c -- Miscellaneous routines 
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "webcit.h"
9
10 char *safestrncpy(char *dest, const char *src, size_t n)
11 {
12         if (dest == NULL || src == NULL) {
13                 fprintf(stderr, "safestrncpy: NULL argument\n");
14                 abort();
15         }
16         strncpy(dest, src, n);
17         dest[n - 1] = 0;
18         return dest;
19 }
20
21
22 /*
23  * num_parms()  -  discover number of parameters...
24  */
25 int num_parms(char *source)
26 {
27         int a;
28         int count = 1;
29
30         for (a = 0; a < strlen(source); ++a)
31                 if (source[a] == '|')
32                         ++count;
33         return (count);
34 }
35
36 /*
37  * extract()  -  extract a parameter from a series of "|" separated...
38  */
39 void extract(char *dest, char *source, int parmnum)
40 {
41         char buf[256];
42         int count = 0;
43         int n;
44
45         if (strlen(source) == 0) {
46                 strcpy(dest, "");
47                 return;
48         }
49         n = num_parms(source);
50
51         if (parmnum >= n) {
52                 strcpy(dest, "");
53                 return;
54         }
55         strcpy(buf, source);
56         if ((parmnum == 0) && (n == 1)) {
57                 strcpy(dest, buf);
58                 for (n = 0; n < strlen(dest); ++n)
59                         if (dest[n] == '|')
60                                 dest[n] = 0;
61                 return;
62         }
63         while (count++ < parmnum)
64                 do {
65                         strcpy(buf, &buf[1]);
66                 } while ((strlen(buf) > 0) && (buf[0] != '|'));
67         if (buf[0] == '|')
68                 strcpy(buf, &buf[1]);
69         for (count = 0; count < strlen(buf); ++count)
70                 if (buf[count] == '|')
71                         buf[count] = 0;
72         strcpy(dest, buf);
73 }
74
75 /*
76  * extract_int()  -  extract an int parm w/o supplying a buffer
77  */
78 int extract_int(char *source, int parmnum)
79 {
80         char buf[256];
81
82         extract(buf, source, parmnum);
83         return (atoi(buf));
84 }
85
86 /*
87  * extract_long()  -  extract an long parm w/o supplying a buffer
88  */
89 long extract_long(char *source, long int parmnum)
90 {
91         char buf[256];
92
93         extract(buf, source, parmnum);
94         return (atol(buf));
95 }
96
97
98 /*
99  * check for the presence of a character within a string (returns count)
100  */
101 int haschar(st, ch)
102 char st[];
103 int ch;
104 {
105         int a, b;
106         b = 0;
107         for (a = 0; a < strlen(st); ++a)
108                 if (st[a] == ch)
109                         ++b;
110         return (b);
111 }