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