]> code.citadel.org Git - citadel.git/blob - webcit/tools.c
Brought over message reading functions from old WebCit
[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     {
14       fprintf(stderr, "safestrncpy: NULL argument\n");
15       abort();
16     }
17   strncpy(dest, src, n);
18   dest[n - 1] = 0;
19   return dest;
20 }
21
22
23 /*
24  * num_parms()  -  discover number of parameters...
25  */
26 int num_parms(char *source)
27 {
28         int a;
29         int count = 1;
30
31         for (a=0; a<strlen(source); ++a) 
32                 if (source[a]=='|') ++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
50         n = num_parms(source);
51
52         if (parmnum >= n) {
53                 strcpy(dest,"");
54                 return;
55                 }
56         strcpy(buf,source);
57         if ( (parmnum == 0) && (n == 1) ) {
58                 strcpy(dest,buf);
59                 for (n=0; n<strlen(dest); ++n)
60                         if (dest[n]=='|') dest[n] = 0;
61                 return;
62                 }
63
64         while (count++ < parmnum) do {
65                 strcpy(buf,&buf[1]);
66                 } while( (strlen(buf)>0) && (buf[0]!='|') );
67         if (buf[0]=='|') strcpy(buf,&buf[1]);
68         for (count = 0; count<strlen(buf); ++count)
69                 if (buf[count] == '|') buf[count] = 0;
70         strcpy(dest,buf);
71         }
72
73 /*
74  * extract_int()  -  extract an int parm w/o supplying a buffer
75  */
76 int extract_int(char *source, int parmnum)
77 {
78         char buf[256];
79         
80         extract(buf,source,parmnum);
81         return(atoi(buf));
82         }
83
84 /*
85  * extract_long()  -  extract an long parm w/o supplying a buffer
86  */
87 long extract_long(char *source, long int parmnum)
88 {
89         char buf[256];
90         
91         extract(buf,source,parmnum);
92         return(atol(buf));
93         }
94
95
96 /*
97  * check for the presence of a character within a string (returns count)
98  */
99 int haschar(st,ch)
100 char st[];
101 int ch; {
102         int a,b;
103         b=0;
104         for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
105         return(b);
106         }
107