1
2 /**
3 *
4 * @file util.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created main.c
10 * - 22nd July 2008: Created util.c from parts of main.c
11 * - 3rd February 2009: Renamed util.c to util.cpp
12 * - 3rd February 2009: Created file.cpp from parts of util.cpp
13 * - 4th February 2009: Created palette.cpp from parts of main.cpp and util.cpp
14 * - 13th July 2009: Created graphics.cpp from parts of util.cpp
15 *
16 * @par Licence:
17 * Copyright (c) 2005-2012 Alister Thomson
18 *
19 * OpenJazz is distributed under the terms of
20 * the GNU General Public License, version 2.0
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * @par Description:
27 * Contains core utility functions.
28 *
29 */
30
31
32 #include "util.h"
33
34 #include "io/file.h"
35
36 #include <string.h>
37
38
39 /**
40 * Check if a file exists.
41 *
42 * @param fileName The file to check
43 *
44 * @return Whether or not the file exists
45 */
fileExists(const char * fileName)46 bool fileExists (const char * fileName) {
47
48 File *file;
49
50 #ifdef VERBOSE
51 printf("Check: ");
52 #endif
53
54 try {
55
56 file = new File(fileName, false);
57
58 } catch (int e) {
59
60 return false;
61
62 }
63
64 delete file;
65
66 return true;
67
68 }
69
70
71 /**
72 * Create a short based on the little-endian contents of the first two bytes in
73 * the given memory location.
74 *
75 * @param data Pointer to the memory location
76 *
77 * @return The generated short
78 */
createShort(unsigned char * data)79 unsigned short int createShort (unsigned char* data) {
80
81 unsigned short int val;
82
83 val = data[0] + (data[1] << 8);
84
85 return val;
86
87 }
88
89
90 /**
91 * Create an int based on the little-endian contents of the first two bytes in
92 * the given memory location.
93 *
94 * @param data Pointer to the memory location
95 *
96 * @return The generated int
97 */
createInt(unsigned char * data)98 int createInt (unsigned char* data) {
99
100 unsigned int val;
101
102 val = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);
103
104 return *((int *)&val);
105
106 }
107
108
109 /**
110 * Create a new string from the contents of an existing string.
111 *
112 * @param string The existing string
113 *
114 * @return The new string
115 */
createString(const char * string)116 char * createString (const char *string) {
117
118 char *cloned;
119
120 cloned = new char[strlen(string) + 1];
121 strcpy(cloned, string);
122
123 return cloned;
124
125 }
126
127
128 /**
129 * Create a new string from the concatenation of two existing strings.
130 *
131 * @param first The existing string to form the start of the new string
132 * @param second The exisitng string to form the end of the new string
133 *
134 * @return The new string
135 */
createString(const char * first,const char * second)136 char * createString (const char *first, const char *second) {
137
138 char *concatenated;
139
140 concatenated = new char[strlen(first) + strlen(second) + 1];
141 strcpy(concatenated, first);
142 strcat(concatenated, second);
143
144 return concatenated;
145
146 }
147
148
149 /**
150 * Create a new file name string with a 3-digit numerical extension.
151 *
152 * @param type The pre-dot file name
153 * @param extension The number to constitute the extension
154 *
155 * @return The new file name string
156 */
createFileName(const char * type,int extension)157 char * createFileName (const char *type, int extension) {
158
159 char *fileName;
160 int pos;
161
162 pos = strlen(type);
163 fileName = new char[pos + 5];
164 strcpy(fileName, type);
165 fileName[pos++] = '.';
166 fileName[pos++] = '0' + ((extension / 100) % 10);
167 fileName[pos++] = '0' + ((extension / 10) % 10);
168 fileName[pos++] = '0' + (extension % 10);
169 fileName[pos] = 0;
170
171 return fileName;
172
173 }
174
175
176 /**
177 * Create a new file name string with the given extension.
178 *
179 * @param type The pre-dot file name
180 * @param extension The extension
181 *
182 * @return The new file name string
183 */
createFileName(const char * type,const char * extension)184 char * createFileName (const char *type, const char *extension) {
185
186 char *fileName;
187 int pos;
188
189 pos = strlen(type);
190 fileName = new char[pos + strlen(extension) + 2];
191 strcpy(fileName, type);
192 fileName[pos++] = '.';
193 strcpy(fileName + pos, extension);
194
195 return fileName;
196
197 }
198
199
200 /**
201 * Create a new file name string with a 1-digit numerical suffix and a 3-digit
202 * numerical extension.
203 *
204 * @param type The pre-dot file name
205 * @param level The number to constitute the suffix
206 * @param extension The number to constitute the extension
207 *
208 * @return The new file name string
209 */
createFileName(const char * type,int level,int extension)210 char * createFileName (const char *type, int level, int extension) {
211
212 char *fileName;
213 int pos;
214
215 pos = strlen(type);
216 fileName = new char[pos + 6];
217 strcpy(fileName, type);
218 fileName[pos++] = '0' + (level % 10);
219 fileName[pos++] = '.';
220 fileName[pos++] = '0' + ((extension / 100) % 10);
221 fileName[pos++] = '0' + ((extension / 10) % 10);
222 fileName[pos++] = '0' + (extension % 10);
223 fileName[pos] = 0;
224
225 return fileName;
226
227 }
228
229
230 /**
231 * Create a new variable-length string from the contents of an existing string.
232 *
233 * @param string The existing string
234 *
235 * @return The new string
236 */
createEditableString(const char * string)237 char * createEditableString (const char *string) {
238
239 char *cloned;
240
241 cloned = new char[STRING_LENGTH + 1];
242 strcpy(cloned, string);
243
244 return cloned;
245
246 }
247
248
249 /**
250 * Add a message to the log.
251 *
252 * @param message The log message
253 */
log(const char * message)254 void log (const char *message) {
255
256 printf("%s\n", message);
257
258 return;
259
260 }
261
262
263 /**
264 * Add a message with a detail message to the log.
265 *
266 * @param message The log message
267 * @param detail The detail message
268 */
log(const char * message,const char * detail)269 void log (const char *message, const char *detail) {
270
271 printf("%s: %s\n", message, detail);
272
273 return;
274
275 }
276
277
278 /**
279 * Add a message with a detail number to the log.
280 *
281 * @param message The log message
282 * @param number The detail number
283 */
log(const char * message,int number)284 void log (const char *message, int number) {
285
286 printf("%s: %d\n", message, number);
287
288 return;
289
290 }
291
292
293 /**
294 * Add a message with a detail message to the error log.
295 *
296 * @param message The log message
297 * @param detail The detail message
298 */
logError(const char * message,const char * detail)299 void logError (const char *message, const char *detail) {
300
301 fprintf(stderr, "%s: %s\n", message, detail);
302
303 return;
304
305 }
306
307
308 /**
309 * Get the sine of the given angle
310 *
311 * @param angle The given angle (where 1024 represents a full circle)
312 *
313 * @return The sine of the angle
314 */
fSin(fixed angle)315 fixed fSin (fixed angle) {
316
317 return sinLut[angle & 1023];
318
319 }
320
321
322 /**
323 * Get the cosine of the given angle
324 *
325 * @param angle The given angle (where 1024 represetns a full circle)
326 *
327 * @return The cosine of the angle
328 */
fCos(fixed angle)329 fixed fCos (fixed angle) {
330
331 return sinLut[(angle + 256) & 1023];
332
333 }
334
335