]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
ifix stricmp and strnicmp
[btb/d2x.git] / misc / strutil.c
1 /* $Id: strutil.c,v 1.12 2004-12-03 18:39:20 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <string.h>
23
24 #include "u_mem.h"
25 #include "error.h"
26
27 #ifdef macintosh
28 // string compare without regard to case
29
30 int stricmp( const char *s1, const char *s2 )
31 {
32         do {
33                 int u1 = toupper((int) *s1);
34                 int u2 = toupper((int) *s2);
35                 if (u1 != u2)
36                         return (u1 > u2) ? 1 : -1;
37
38                 s1++;
39                 s2++;
40         } while (u1 && u2)
41
42         return 0;
43 }
44
45 int strnicmp( const char *s1, const char *s2, int n )
46 {
47         do {
48                 int u1 = toupper((int) *s1);
49                 int u2 = toupper((int) *s2);
50                 if (u1 != u2)
51                         return (u1 > u2) ? 1 : -1;
52
53                 s1++;
54                 s2++;
55                 n--;
56         } while (u1 && u2 && n)
57
58         return 0;
59 }
60 #endif
61
62 #ifndef _WIN32
63 #ifndef __DJGPP__
64 void strlwr( char *s1 )
65 {
66         while( *s1 )    {
67                 *s1 = tolower(*s1);
68                 s1++;
69         }
70 }
71
72 void strupr( char *s1 )
73 {
74         while( *s1 )    {
75                 *s1 = toupper(*s1);
76                 s1++;
77         }
78 }
79
80 #endif
81
82 void strrev( char *s1 )
83 {
84         char *h, *t;
85         h = s1;
86         t = s1 + strlen(s1) - 1;
87         while (h < t) {
88                 char c;
89                 c = *h;
90                 *h++ = *t;
91                 *t-- = c;
92         }
93 }
94 #endif
95
96 #if !defined(__MSDOS__) && !(defined(_WIN32) && !defined(_WIN32_WCE))
97 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
98 {
99         char *s, *p;
100
101         p = name;
102         s = strchr(p, ':');
103         if ( s != NULL ) {
104                 if (drive) {
105                         *s = '\0';
106                         strcpy(drive, p);
107                         *s = ':';
108                 }
109                 p = s+1;
110                 if (!p)
111                         return;
112         } else if (drive)
113                 *drive = '\0';
114         
115         s = strrchr(p, '\\');
116         if ( s != NULL) {
117                 if (path) {
118                         char c;
119                         
120                         c = *(s+1);
121                         *(s+1) = '\0';
122                         strcpy(path, p);
123                         *(s+1) = c;
124                 }
125                 p = s+1;
126                 if (!p)
127                         return;
128         } else if (path)
129                 *path = '\0';
130
131         s = strchr(p, '.');
132         if ( s != NULL) {
133                 if (base) {
134                         *s = '\0';
135                         strcpy(base, p);
136                         *s = '.';
137                 }
138                 p = s+1;
139                 if (!p)
140                         return;
141         } else if (base)
142                 *base = '\0';
143                 
144         if (ext)
145                 strcpy(ext, p);         
146 }
147 #endif
148
149 #if 0
150 void main()
151 {
152         char drive[10], path[50], name[16], ext[5];
153         
154         drive[0] = path[0] = name[0] = ext[0] = '\0';
155         _splitpath("f:\\tmp\\x.out", drive, path, name, ext);
156         drive[0] = path[0] = name[0] = ext[0] = '\0';
157         _splitpath("tmp\\x.out", drive, path, name, ext);
158         drive[0] = path[0] = name[0] = ext[0] = '\0';
159         _splitpath("f:\\tmp\\a.out", NULL, NULL, name, NULL);
160         drive[0] = path[0] = name[0] = ext[0] = '\0';
161         _splitpath("tmp\\*.dem", drive, path, NULL, NULL);
162         drive[0] = path[0] = name[0] = ext[0] = '\0';
163         _splitpath(".\\tmp\\*.dem", drive, path, NULL, NULL);
164 }
165 #endif