Some compile fixes, not enough
[erwise] / Cl / WWWLibrary / HTString.c
1 /*              Case-independent string comparison              HTString.c
2 **
3 **      Original version came with listserv implementation.
4 **      Version TBL Oct 91 replaces one which modified the strings.
5 **      02-Dec-91 (JFG) Added stralloccopy and stralloccat
6 **      23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
7 */
8 #include <ctype.h>
9 #include "HTUtils.h"
10 #include "tcp.h"
11
12 #ifndef VM              /* VM has these already it seems */
13         
14 /*      Strings of any length
15 **      ---------------------
16 */
17 PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
18 {
19         CONST char *p =a;
20         CONST char *q =b;
21         for(p=a, q=b; *p && *q; p++, q++) {
22             int diff = TOLOWER(*p) - TOLOWER(*q);
23             if (diff) return diff;
24         }
25         if (*p) return 1;       /* p was longer than q */
26         if (*q) return -1;      /* p was shorter than q */
27         return 0;               /* Exact match */
28 }
29
30
31 /*      With count limit
32 **      ----------------
33 */
34 PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
35 {
36         CONST char *p =a;
37         CONST char *q =b;
38         
39         for(p=a, q=b;; p++, q++) {
40             int diff;
41             if (p == a+n) return 0;     /*   Match up to n characters */
42             if (!(*p && *q)) return *p - *q;
43             diff = TOLOWER(*p) - TOLOWER(*q);
44             if (diff) return diff;
45         }
46         /*NOTREACHED*/
47 }
48 #endif
49
50 /*      Allocate a new copy of a string, and returns it
51 */
52 PUBLIC char * HTSACopy
53   ARGS2 (char **,dest, CONST char *,src)
54 {
55   if (*dest) free(*dest);
56   if (! src)
57     *dest = NULL;
58   else {
59     *dest = (char *) malloc (strlen(src) + 1);
60     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
61     strcpy (*dest, src);
62   }
63   return *dest;
64 }
65
66 PUBLIC char * HTSACat
67   ARGS2 (char **,dest, CONST char *,src)
68 {
69   if (src && *src) {
70     if (*dest) {
71       int length = strlen (*dest);
72       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
73       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
74       strcpy (*dest + length, src);
75     } else {
76       *dest = (char *) malloc (strlen(src) + 1);
77       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
78       strcpy (*dest, src);
79     }
80   }
81   return *dest;
82 }