]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
deb stuff
[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 #if 0
37 // string compare without regard to case
38
39 int stricmp( char *s1, char *s2 )
40 {
41         while( *s1 && *s2 )     {
42                 if ( tolower(*s1) != tolower(*s2) )     return 1;
43                 s1++;
44                 s2++;
45         }
46         if ( *s1 || *s2 ) return 1;
47         return 0;
48 }
49
50 int strnicmp( char *s1, char *s2, int n )
51 {
52         while( *s1 && *s2 && n) {
53                 if ( tolower(*s1) != tolower(*s2) )     return 1;
54                 s1++;
55                 s2++;
56                 n--;
57         }
58         if (n) return 1;
59         return 0;
60 }
61 #endif
62
63 #ifndef __MINGW32__
64 #ifndef __DJGPP__
65 void strlwr( char *s1 )
66 {
67         while( *s1 )    {
68                 *s1 = tolower(*s1);
69                 s1++;
70         }
71 }
72
73 void strupr( char *s1 )
74 {
75         while( *s1 )    {
76                 *s1 = toupper(*s1);
77                 s1++;
78         }
79 }
80
81 #endif
82
83 void strrev( char *s1 )
84 {
85         char *h, *t;
86         h = s1;
87         t = s1 + strlen(s1) - 1;
88         while (h < t) {
89                 char c;
90                 c = *h;
91                 *h++ = *t;
92                 *t-- = c;
93         }
94 }
95
96 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
97 {
98         char *s, *p;
99
100         p = name;
101         s = strchr(p, ':');
102         if ( s != NULL ) {
103                 if (drive) {
104                         *s = '\0';
105                         strcpy(drive, p);
106                         *s = ':';
107                 }
108                 p = s+1;
109                 if (!p)
110                         return;
111         } else if (drive)
112                 *drive = '\0';
113         
114         s = strrchr(p, '\\');
115         if ( s != NULL) {
116                 if (path) {
117                         char c;
118                         
119                         c = *(s+1);
120                         *(s+1) = '\0';
121                         strcpy(path, p);
122                         *(s+1) = c;
123                 }
124                 p = s+1;
125                 if (!p)
126                         return;
127         } else if (path)
128                 *path = '\0';
129
130         s = strchr(p, '.');
131         if ( s != NULL) {
132                 if (base) {
133                         *s = '\0';
134                         strcpy(base, p);
135                         *s = '.';
136                 }
137                 p = s+1;
138                 if (!p)
139                         return;
140         } else if (base)
141                 *base = '\0';
142                 
143         if (ext)
144                 strcpy(ext, p);         
145 }
146 #endif
147
148 #if 0
149 void main()
150 {
151         char drive[10], path[50], name[16], ext[5];
152         
153         drive[0] = path[0] = name[0] = ext[0] = '\0';
154         _splitpath("f:\\tmp\\x.out", drive, path, name, ext);
155         drive[0] = path[0] = name[0] = ext[0] = '\0';
156         _splitpath("tmp\\x.out", drive, path, name, ext);
157         drive[0] = path[0] = name[0] = ext[0] = '\0';
158         _splitpath("f:\\tmp\\a.out", NULL, NULL, name, NULL);
159         drive[0] = path[0] = name[0] = ext[0] = '\0';
160         _splitpath("tmp\\*.dem", drive, path, NULL, NULL);
161         drive[0] = path[0] = name[0] = ext[0] = '\0';
162         _splitpath(".\\tmp\\*.dem", drive, path, NULL, NULL);
163 }
164 #endif