]> icculus.org git repositories - btb/d2x.git/blob - misc/strutil.c
This commit was generated by cvs2svn to compensate for changes in r2,
[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 // string compare without regard to case
34
35 int stricmp( char *s1, char *s2 )
36 {
37         while( *s1 && *s2 )     {
38                 if ( tolower(*s1) != tolower(*s2) )     return 1;
39                 s1++;
40                 s2++;
41         }
42         if ( *s1 || *s2 ) return 1;
43         return 0;
44 }
45
46 int strnicmp( char *s1, char *s2, int n )
47 {
48         while( *s1 && *s2 && n) {
49                 if ( tolower(*s1) != tolower(*s2) )     return 1;
50                 s1++;
51                 s2++;
52                 n--;
53         }
54         if (n) return 1;
55         return 0;
56 }
57
58 void strlwr( char *s1 )
59 {
60         while( *s1 )    {
61                 *s1 = tolower(*s1);
62                 s1++;
63         }
64 }
65
66 void strupr( char *s1 )
67 {
68         while( *s1 )    {
69                 *s1 = toupper(*s1);
70                 s1++;
71         }
72 }
73
74 void strrev( char *s1 )
75 {
76         int i,l;
77         char *s2;
78         
79         s2 = (char *)d_malloc(strlen(s1) + 1);
80         strcpy(s2, s1);
81         l = strlen(s2);
82         for (i = 0; i < l; i++)
83                 s1[l-1-i] = s2[i];
84         d_free(s2);
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
138 #if 0
139 void main()
140 {
141         char drive[10], path[50], name[16], ext[5];
142         
143         drive[0] = path[0] = name[0] = ext[0] = '\0';
144         _splitpath("f:\\tmp\\x.out", drive, path, name, ext);
145         drive[0] = path[0] = name[0] = ext[0] = '\0';
146         _splitpath("tmp\\x.out", drive, path, name, ext);
147         drive[0] = path[0] = name[0] = ext[0] = '\0';
148         _splitpath("f:\\tmp\\a.out", NULL, NULL, name, NULL);
149         drive[0] = path[0] = name[0] = ext[0] = '\0';
150         _splitpath("tmp\\*.dem", drive, path, NULL, NULL);
151         drive[0] = path[0] = name[0] = ext[0] = '\0';
152         _splitpath(".\\tmp\\*.dem", drive, path, NULL, NULL);
153 }
154 #endif