]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
Rename include/error.h to include/dxxerror.h
[btb/d2x.git] / misc / strutil.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <string.h>
22
23 #include "u_mem.h"
24 #include "dxxerror.h"
25 #include "inferno.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 //give a filename a new extension, doesn't work with paths.
131 void change_filename_extension( char *dest, const char *src, char *ext )
132 {
133         char *p;
134         
135         strcpy (dest, src);
136         
137         if (ext[0] == '.')
138                 ext++;
139         
140         p = strrchr(dest, '.');
141         if (!p) {
142                 p = dest + strlen(dest);
143                 *p = '.';
144         }
145         
146         Assert((p + strlen(ext)) - dest < FILENAME_LEN);
147         strcpy(p+1,ext);
148 }
149
150 #if !defined(__MSDOS__) && !(defined(_WIN32) && !defined(_WIN32_WCE))
151 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
152 {
153         char *s, *p;
154
155         p = name;
156         s = strchr(p, ':');
157         if ( s != NULL ) {
158                 if (drive) {
159                         *s = '\0';
160                         strcpy(drive, p);
161                         *s = ':';
162                 }
163                 p = s+1;
164                 if (!p)
165                         return;
166         } else if (drive)
167                 *drive = '\0';
168         
169         s = strrchr(p, '\\');
170         if ( s != NULL) {
171                 if (path) {
172                         char c;
173                         
174                         c = *(s+1);
175                         *(s+1) = '\0';
176                         strcpy(path, p);
177                         *(s+1) = c;
178                 }
179                 p = s+1;
180                 if (!p)
181                         return;
182         } else if (path)
183                 *path = '\0';
184
185         s = strchr(p, '.');
186         if ( s != NULL) {
187                 if (base) {
188                         *s = '\0';
189                         strcpy(base, p);
190                         *s = '.';
191                 }
192                 p = s+1;
193                 if (!p)
194                         return;
195         } else if (base)
196                 *base = '\0';
197                 
198         if (ext)
199                 strcpy(ext, p);         
200 }
201 #endif
202
203 #if 0
204 void main()
205 {
206         char drive[10], path[50], name[16], ext[5];
207         
208         drive[0] = path[0] = name[0] = ext[0] = '\0';
209         _splitpath("f:\\tmp\\x.out", drive, path, name, ext);
210         drive[0] = path[0] = name[0] = ext[0] = '\0';
211         _splitpath("tmp\\x.out", drive, path, name, ext);
212         drive[0] = path[0] = name[0] = ext[0] = '\0';
213         _splitpath("f:\\tmp\\a.out", NULL, NULL, name, NULL);
214         drive[0] = path[0] = name[0] = ext[0] = '\0';
215         _splitpath("tmp\\*.dem", drive, path, NULL, NULL);
216         drive[0] = path[0] = name[0] = ext[0] = '\0';
217         _splitpath(".\\tmp\\*.dem", drive, path, NULL, NULL);
218 }
219 #endif