]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
removed old fileutil stuff
[btb/d2x.git] / misc / strutil.c
1 /* $Id: strutil.c,v 1.8 2003-02-18 20:35:35 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 __MINGW32__
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
87 void _splitpath(char *name, char *drive, char *path, char *base, char *ext)
88 {
89         char *s, *p;
90
91         p = name;
92         s = strchr(p, ':');
93         if ( s != NULL ) {
94                 if (drive) {
95                         *s = '\0';
96                         strcpy(drive, p);
97                         *s = ':';
98                 }
99                 p = s+1;
100                 if (!p)
101                         return;
102         } else if (drive)
103                 *drive = '\0';
104         
105         s = strrchr(p, '\\');
106         if ( s != NULL) {
107                 if (path) {
108                         char c;
109                         
110                         c = *(s+1);
111                         *(s+1) = '\0';
112                         strcpy(path, p);
113                         *(s+1) = c;
114                 }
115                 p = s+1;
116                 if (!p)
117                         return;
118         } else if (path)
119                 *path = '\0';
120
121         s = strchr(p, '.');
122         if ( s != NULL) {
123                 if (base) {
124                         *s = '\0';
125                         strcpy(base, p);
126                         *s = '.';
127                 }
128                 p = s+1;
129                 if (!p)
130                         return;
131         } else if (base)
132                 *base = '\0';
133                 
134         if (ext)
135                 strcpy(ext, p);         
136 }
137 #endif
138
139 #if 0
140 void main()
141 {
142         char drive[10], path[50], name[16], ext[5];
143         
144         drive[0] = path[0] = name[0] = ext[0] = '\0';
145         _splitpath("f:\\tmp\\x.out", drive, path, name, ext);
146         drive[0] = path[0] = name[0] = ext[0] = '\0';
147         _splitpath("tmp\\x.out", drive, path, name, ext);
148         drive[0] = path[0] = name[0] = ext[0] = '\0';
149         _splitpath("f:\\tmp\\a.out", NULL, NULL, name, NULL);
150         drive[0] = path[0] = name[0] = ext[0] = '\0';
151         _splitpath("tmp\\*.dem", drive, path, NULL, NULL);
152         drive[0] = path[0] = name[0] = ext[0] = '\0';
153         _splitpath(".\\tmp\\*.dem", drive, path, NULL, NULL);
154 }
155 #endif