]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
must_free_canvas isn't used when AUTOMAP_DIRECT_RENDER is defined (whoops)
[btb/d2x.git] / misc / strutil.c
1 /* $Id: strutil.c,v 1.15 2004-12-20 07:12:25 btb 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 # if defined(NDEBUG)
29 char *strdup(const char *str)
30 {
31         char *newstr;
32
33         newstr = malloc(strlen(str) + 1);
34         strcpy(newstr, str);
35
36         return newstr;
37 }
38 # endif // NDEBUG
39
40 // string compare without regard to case
41
42 int stricmp( const char *s1, const char *s2 )
43 {
44         int u1;
45         int u2;
46
47         do {
48                 u1 = toupper((int) *s1);
49                 u2 = toupper((int) *s2);
50                 if (u1 != u2)
51                         return (u1 > u2) ? 1 : -1;
52
53                 s1++;
54                 s2++;
55         } while (u1 && u2);
56
57         return 0;
58 }
59
60 int strnicmp( const char *s1, const char *s2, int n )
61 {
62         int u1;
63         int u2;
64
65         do {
66                 u1 = toupper((int) *s1);
67                 u2 = toupper((int) *s2);
68                 if (u1 != u2)
69                         return (u1 > u2) ? 1 : -1;
70
71                 s1++;
72                 s2++;
73                 n--;
74         } while (u1 && u2 && n);
75
76         return 0;
77 }
78 #endif
79
80 #ifndef _WIN32
81 #ifndef __DJGPP__
82 void strlwr( char *s1 )
83 {
84         while( *s1 )    {
85                 *s1 = tolower(*s1);
86                 s1++;
87         }
88 }
89
90 void strupr( char *s1 )
91 {
92         while( *s1 )    {
93                 *s1 = toupper(*s1);
94                 s1++;
95         }
96 }
97
98 #endif
99
100 void strrev( char *s1 )
101 {
102         char *h, *t;
103         h = s1;
104         t = s1 + strlen(s1) - 1;
105         while (h < t) {
106                 char c;
107                 c = *h;
108                 *h++ = *t;
109                 *t-- = c;
110         }
111 }
112 #endif
113
114
115 // remove extension from filename, doesn't work with paths.
116 void removeext(const char *filename, char *out)
117 {
118         char *p;
119
120         if ((p = strrchr(filename, '.')))
121         {
122                 strncpy(out, filename, p - filename);
123                 out[p - filename] = 0;
124         }
125         else
126                 strcpy(out, filename);
127 }
128
129
130 #if !defined(__MSDOS__) && !(defined(_WIN32) && !defined(_WIN32_WCE))
131 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
132 {
133         char *s, *p;
134
135         p = name;
136         s = strchr(p, ':');
137         if ( s != NULL ) {
138                 if (drive) {
139                         *s = '\0';
140                         strcpy(drive, p);
141                         *s = ':';
142                 }
143                 p = s+1;
144                 if (!p)
145                         return;
146         } else if (drive)
147                 *drive = '\0';
148         
149         s = strrchr(p, '\\');
150         if ( s != NULL) {
151                 if (path) {
152                         char c;
153                         
154                         c = *(s+1);
155                         *(s+1) = '\0';
156                         strcpy(path, p);
157                         *(s+1) = c;
158                 }
159                 p = s+1;
160                 if (!p)
161                         return;
162         } else if (path)
163                 *path = '\0';
164
165         s = strchr(p, '.');
166         if ( s != NULL) {
167                 if (base) {
168                         *s = '\0';
169                         strcpy(base, p);
170                         *s = '.';
171                 }
172                 p = s+1;
173                 if (!p)
174                         return;
175         } else if (base)
176                 *base = '\0';
177                 
178         if (ext)
179                 strcpy(ext, p);         
180 }
181 #endif
182
183 #if 0
184 void main()
185 {
186         char drive[10], path[50], name[16], ext[5];
187         
188         drive[0] = path[0] = name[0] = ext[0] = '\0';
189         _splitpath("f:\\tmp\\x.out", drive, path, name, ext);
190         drive[0] = path[0] = name[0] = ext[0] = '\0';
191         _splitpath("tmp\\x.out", drive, path, name, ext);
192         drive[0] = path[0] = name[0] = ext[0] = '\0';
193         _splitpath("f:\\tmp\\a.out", NULL, NULL, name, NULL);
194         drive[0] = path[0] = name[0] = ext[0] = '\0';
195         _splitpath("tmp\\*.dem", drive, path, NULL, NULL);
196         drive[0] = path[0] = name[0] = ext[0] = '\0';
197         _splitpath(".\\tmp\\*.dem", drive, path, NULL, NULL);
198 }
199 #endif