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