- initial commit of Genesis3D 1.6
[genesis3d.git] / mkactor / AStudio / Util / UTIL.C
1 /****************************************************************************************/\r
2 /*  UTIL.C                                                                                                                                                              */\r
3 /*                                                                                      */\r
4 /*  Author: Jim Mischel                                                                     */\r
5 /*  Description: Various useful utility functions.                                                                              */\r
6 /*                                                                                      */\r
7 /*  The contents of this file are subject to the Genesis3D Public License               */\r
8 /*  Version 1.01 (the "License"); you may not use this file except in                   */\r
9 /*  compliance with the License. You may obtain a copy of the License at                */\r
10 /*  http://www.genesis3d.com                                                            */\r
11 /*                                                                                      */\r
12 /*  Software distributed under the License is distributed on an "AS IS"                 */\r
13 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */\r
14 /*  the License for the specific language governing rights and limitations              */\r
15 /*  under the License.                                                                  */\r
16 /*                                                                                      */\r
17 /*  The Original Code is Genesis3D, released March 25, 1999.                            */\r
18 /*Genesis3D Version 1.1 released November 15, 1999                            */\r
19 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */\r
20 /*                                                                                      */\r
21 /****************************************************************************************/\r
22 #include "util.h"\r
23 #include "ram.h"\r
24 #include <string.h>\r
25 #include <assert.h>\r
26 #include <ctype.h>\r
27 #include <io.h>\r
28 \r
29 \r
30 char *Util_Strdup (const char *s)\r
31 {\r
32     char *rslt;\r
33 \r
34         assert (s != NULL);\r
35 \r
36         rslt = geRam_Allocate (strlen (s) + 1);\r
37         if (rslt != NULL)\r
38         {\r
39                 strcpy (rslt, s);\r
40         }\r
41         return rslt;\r
42 }\r
43 \r
44 geBoolean Util_SetString (char **ppString, const char *NewValue)\r
45 {\r
46         char *pNewString;\r
47 \r
48         if (NewValue == NULL)\r
49         {\r
50                 pNewString = NULL;\r
51         }\r
52         else \r
53         {\r
54                 pNewString = Util_Strdup (NewValue);\r
55                 if (pNewString == NULL)\r
56                 {\r
57                         return GE_FALSE;\r
58                 }\r
59         }\r
60         if (*ppString != NULL)\r
61         {\r
62                 geRam_Free (*ppString);\r
63         }\r
64         *ppString = pNewString;\r
65 \r
66         return GE_TRUE;\r
67 }\r
68 \r
69 \r
70 geBoolean Util_IsValidInt\r
71     (\r
72           char const *Text,\r
73           int *TheVal\r
74         )\r
75 {\r
76         // no overflow checking here...\r
77         // also no hex numbers supported yet...\r
78         char const *c;\r
79     int sign;\r
80         int val;\r
81 \r
82         sign = 1;\r
83         c = Text;\r
84         val = 0;\r
85 \r
86         if (*c == '-')\r
87         {\r
88             sign = -1;\r
89                 ++c;\r
90         }\r
91         else if (*c == '+')\r
92         {\r
93             ++c;\r
94         }\r
95 \r
96         while (isdigit (*c))\r
97         {\r
98                 val *= 10;\r
99                 val += (*c - '0');\r
100             ++c;\r
101         }\r
102 \r
103         if (*c == '\0')\r
104         {\r
105             *TheVal = sign * val;\r
106                 return GE_TRUE;\r
107         }\r
108         else\r
109         {\r
110             return GE_FALSE;\r
111         }\r
112 }\r
113 \r
114 geBoolean Util_IsValidFloat\r
115     (\r
116           const char *Text,\r
117           geFloat *TheFloat\r
118         )\r
119 {\r
120     char const *c;\r
121         int sign;\r
122         geFloat num;\r
123 \r
124         num = 0;\r
125         sign = 1;\r
126 \r
127         c = Text;\r
128         if (*c == '-')\r
129         {\r
130             sign = -1;\r
131                 ++c;\r
132         }\r
133         else if (*c == '+')\r
134         {\r
135             ++c;\r
136         }\r
137 \r
138         while (isdigit (*c))\r
139         {\r
140                 num = (num * 10) + (*c - '0');\r
141                 ++c;\r
142         }\r
143 \r
144         if (*c == '.')\r
145         {\r
146                 geFloat div;\r
147 \r
148                 div = 10.0;\r
149                 ++c;\r
150                 while (isdigit (*c))\r
151                 {\r
152                     num += ((geFloat)(*c - '0'))/div;\r
153                         div *= 10.0;\r
154                         ++c;\r
155                 }\r
156         }\r
157 \r
158         if (*c == '\0')\r
159         {\r
160                 *TheFloat = num;\r
161                 return GE_TRUE;\r
162         }\r
163         else\r
164         {\r
165                 return GE_FALSE;\r
166         }\r
167 }\r
168 \r
169 unsigned int    Util_htoi (const char *s)\r
170 {\r
171     unsigned int hexValue;\r
172     const char *c;\r
173 \r
174         hexValue = 0;\r
175         if ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X')))\r
176         {\r
177                 c = &(s[2]);\r
178                 while (isxdigit (*c))\r
179                 {\r
180                         // I know there's a faster way, I just don't remember\r
181                         // what it is....\r
182                         hexValue <<= 4;\r
183                         if ((*c >= '0') && (*c <= '9'))\r
184                         {\r
185                                 hexValue += (*c - '0');\r
186                         }\r
187                         else if ((*c >= 'A') && (*c <= 'F'))\r
188                         {\r
189                                 hexValue += (*c - 'A') + 10;\r
190                         }\r
191                         else // a..f\r
192                         {\r
193                                 hexValue += (*c - 'a') + 10;\r
194                         }\r
195                         c++;\r
196                 }\r
197         }\r
198     return hexValue;\r
199 }\r
200 \r
201 void Util_QuoteString (const char *s, char *d)\r
202 {\r
203         unsigned int dest, src;\r
204         \r
205         assert (s != d);\r
206         dest = 0;\r
207         d[dest++] = '"';\r
208         for (src = 0; src < strlen (s); ++src)\r
209         {\r
210                 char c;\r
211 \r
212                 c = s[src];\r
213                 // need to escape quotes and backslashes\r
214                 if ((c == '"') || (c == '\\'))\r
215                 {\r
216                         d[dest++] = '\\';\r
217                 }\r
218                 d[dest++] = c;\r
219         }\r
220         d[dest++] = '"';\r
221         d[dest] = '\0';\r
222 }\r
223 \r
224 \r
225 \r
226 geBoolean Util_GetEndStringValue\r
227     ( \r
228       const char *psz, \r
229       int32 *pVal, \r
230       int32 *pLastChar \r
231     )\r
232 {\r
233     int i;\r
234     int Num;\r
235     int Power;\r
236     geBoolean bValueFound = GE_FALSE ;\r
237 \r
238     assert (psz != NULL);\r
239     assert (pVal != NULL);\r
240     assert (pLastChar != NULL);\r
241 \r
242     Num = 0;\r
243     Power = 1;\r
244     i = strlen( psz );\r
245     while( (i > 0) && (isdigit (psz[i-1])) )\r
246     {\r
247                 char c;\r
248 \r
249                 c = psz[i-1];\r
250                 Num += ((c - '0') * Power);\r
251                 Power *= 10;\r
252                 --i;\r
253                 bValueFound = GE_TRUE ;\r
254     }\r
255     \r
256     if( bValueFound )\r
257     {\r
258                 *pVal = Num ;\r
259                 *pLastChar = i ;\r
260     }\r
261 \r
262     return bValueFound ;\r
263 }\r
264 \r
265 geBoolean Util_FileExists (const char *Filename)\r
266 {\r
267         return (_access (Filename, 0) == 0) ? GE_TRUE : GE_FALSE;\r
268 }\r