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