]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
05b2d69fc61936e1389a06c525391ddc3d516c11
[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 "error.h"
25
26 char *d_strdup(char *str)
27 {
28         char *a;
29
30         a = d_malloc(strlen(str) + 1);
31         strcpy(a, str);
32
33         return a;
34 }
35
36
37 #if !defined  __ENV_LINUX__ && !defined __ENV_DJGPP__
38 // string compare without regard to case
39
40 int stricmp( char *s1, char *s2 )
41 {
42         while( *s1 && *s2 )     {
43                 if ( tolower(*s1) != tolower(*s2) )     return 1;
44                 s1++;
45                 s2++;
46         }
47         if ( *s1 || *s2 ) return 1;
48         return 0;
49 }
50
51 int strnicmp( char *s1, char *s2, int n )
52 {
53         while( *s1 && *s2 && n) {
54                 if ( tolower(*s1) != tolower(*s2) )     return 1;
55                 s1++;
56                 s2++;
57                 n--;
58         }
59         if (n) return 1;
60         return 0;
61 }
62
63 #endif
64
65 #if !defined __ENV_DJGPP__ && !defined __CYGWIN__
66
67 void strlwr( char *s1 )
68 {
69         while( *s1 )    {
70                 *s1 = tolower(*s1);
71                 s1++;
72         }
73 }
74
75 void strupr( char *s1 )
76 {
77         while( *s1 )    {
78                 *s1 = toupper(*s1);
79                 s1++;
80         }
81 }
82
83 #endif
84
85 void strrev( char *s1 )
86 {
87         int i,l;
88         char *s2;
89         
90         s2 = (char *)d_malloc(strlen(s1) + 1);
91         strcpy(s2, s1);
92         l = strlen(s2);
93         for (i = 0; i < l; i++)
94                 s1[l-1-i] = s2[i];
95         d_free(s2);
96 }
97
98 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
99 {
100         char *s, *p;
101
102         p = name;
103         s = strchr(p, ':');
104         if ( s != NULL ) {
105                 if (drive) {
106                         *s = '\0';
107                         strcpy(drive, p);
108                         *s = ':';
109                 }
110                 p = s+1;
111                 if (!p)
112                         return;
113         } else if (drive)
114                 *drive = '\0';
115         
116         s = strrchr(p, '\\');
117         if ( s != NULL) {
118                 if (path) {
119                         char c;
120                         
121                         c = *(s+1);
122                         *(s+1) = '\0';
123                         strcpy(path, p);
124                         *(s+1) = c;
125                 }
126                 p = s+1;
127                 if (!p)
128                         return;
129         } else if (path)
130                 *path = '\0';
131
132         s = strchr(p, '.');
133         if ( s != NULL) {
134                 if (base) {
135                         *s = '\0';
136                         strcpy(base, p);
137                         *s = '.';
138                 }
139                 p = s+1;
140                 if (!p)
141                         return;
142         } else if (base)
143                 *base = '\0';
144                 
145         if (ext)
146                 strcpy(ext, p);         
147 }
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