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