]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
Changed __ENV_DJGPP__ to __DJGPP__, since it's built-in.
[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 0
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 #endif
63
64 #ifndef __MINGW32__
65 #ifndef __DJGPP__
66 void strlwr( char *s1 )
67 {
68         while( *s1 )    {
69                 *s1 = tolower(*s1);
70                 s1++;
71         }
72 }
73
74 void strupr( char *s1 )
75 {
76         while( *s1 )    {
77                 *s1 = toupper(*s1);
78                 s1++;
79         }
80 }
81
82 #endif
83
84 void strrev( char *s1 )
85 {
86         int i,l;
87         char *s2;
88         
89         s2 = (char *)d_malloc(strlen(s1) + 1);
90         strcpy(s2, s1);
91         l = strlen(s2);
92         for (i = 0; i < l; i++)
93                 s1[l-1-i] = s2[i];
94         d_free(s2);
95 }
96
97 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
98 {
99         char *s, *p;
100
101         p = name;
102         s = strchr(p, ':');
103         if ( s != NULL ) {
104                 if (drive) {
105                         *s = '\0';
106                         strcpy(drive, p);
107                         *s = ':';
108                 }
109                 p = s+1;
110                 if (!p)
111                         return;
112         } else if (drive)
113                 *drive = '\0';
114         
115         s = strrchr(p, '\\');
116         if ( s != NULL) {
117                 if (path) {
118                         char c;
119                         
120                         c = *(s+1);
121                         *(s+1) = '\0';
122                         strcpy(path, p);
123                         *(s+1) = c;
124                 }
125                 p = s+1;
126                 if (!p)
127                         return;
128         } else if (path)
129                 *path = '\0';
130
131         s = strchr(p, '.');
132         if ( s != NULL) {
133                 if (base) {
134                         *s = '\0';
135                         strcpy(base, p);
136                         *s = '.';
137                 }
138                 p = s+1;
139                 if (!p)
140                         return;
141         } else if (base)
142                 *base = '\0';
143                 
144         if (ext)
145                 strcpy(ext, p);         
146 }
147 #endif
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