1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - util.h *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2012 CasualJames *
5 * Copyright (C) 2002 Hacktarux *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31 #include "osal/preproc.h"
33 /**********************
35 **********************/
37 typedef enum _file_status
46 * opens a file and reads the specified number of bytes.
47 * returns zero on success, nonzero on failure
49 file_status_t read_from_file(const char *filename, void *data, size_t size);
52 * opens a file and writes the specified number of bytes.
53 * returns zero on sucess, nonzero on failure
55 file_status_t write_to_file(const char *filename, const void *data, size_t size);
57 /**********************
59 **********************/
64 /* GCC has also byte swap intrinsics (__builtin_bswap32, etc.), but they were
65 * added in relatively recent versions. In addition, GCC can detect the byte
66 * swap code and optimize it with a high enough optimization level. */
68 static osal_inline unsigned short m64p_swap16(unsigned short x)
71 return _byteswap_ushort(x);
73 return ((x & 0x00FF) << 8) |
78 static osal_inline unsigned int m64p_swap32(unsigned int x)
81 return _byteswap_ulong(x); // long is always 32-bit in Windows
83 return ((x & 0x000000FF) << 24) |
84 ((x & 0x0000FF00) << 8) |
85 ((x & 0x00FF0000) >> 8) |
86 ((x & 0xFF000000) >> 24);
90 static osal_inline unsigned long long int m64p_swap64(unsigned long long int x)
93 return _byteswap_uint64(x);
95 return ((x & 0x00000000000000FFULL) << 56) |
96 ((x & 0x000000000000FF00ULL) << 40) |
97 ((x & 0x0000000000FF0000ULL) << 24) |
98 ((x & 0x00000000FF000000ULL) << 8) |
99 ((x & 0x000000FF00000000ULL) >> 8) |
100 ((x & 0x0000FF0000000000ULL) >> 24) |
101 ((x & 0x00FF000000000000ULL) >> 40) |
102 ((x & 0xFF00000000000000ULL) >> 56);
106 #ifdef M64P_BIG_ENDIAN
110 #define little16(x) m64p_swap16(x)
111 #define little32(x) m64p_swap32(x)
112 #define little64(x) m64p_swap64(x)
114 #define big16(x) m64p_swap16(x)
115 #define big32(x) m64p_swap32(x)
116 #define big64(x) m64p_swap64(x)
117 #define little16(x) (x)
118 #define little32(x) (x)
119 #define little64(x) (x)
122 /* Byte swaps, converts to little endian or converts to big endian a buffer,
123 * containing 'count' elements, each of size 'length'. */
124 void swap_buffer(void *buffer, size_t length, size_t count);
125 void to_little_endian_buffer(void *buffer, size_t length, size_t count);
126 void to_big_endian_buffer(void *buffer, size_t length, size_t count);
128 /**********************
130 **********************/
131 void countrycodestring(unsigned short countrycode, char *string);
132 void imagestring(unsigned char imagetype, char *string);
134 /**********************
136 **********************/
138 /* Extracts the full file name (with extension) from a path string.
139 * Returns the same string, advanced until the file name. */
140 const char* namefrompath(const char* path);
142 /* Creates a path string by joining two path strings.
143 * The given path strings may or may not start or end with a path separator.
144 * Returns a malloc'd string with the resulting path. */
145 char* combinepath(const char* first, const char *second);
147 /**********************
149 **********************/
152 * Removes leading and trailing whitespace from str. Function modifies str
153 * and also returns modified string.
155 char *trim(char *str);
157 /* Converts an string to an integer.
158 * Returns 1 on success, 0 on failure. 'result' is undefined on failure.
160 * The following conditions cause this function to fail:
162 * - Leading characters (including whitespace)
163 * - Trailing characters (including whitespace)
164 * - Overflow or underflow.
166 int string_to_int(const char *str, int *result);
168 /* Converts an string of hexadecimal characters to a byte array.
169 * 'output_size' is the number of bytes (hex digraphs) to convert.
170 * Returns 1 on success, 0 on failure. 'output' is undefined on failure. */
171 int parse_hex(const char *str, unsigned char *output, size_t output_size);
173 /* Formats an string, using the same syntax as printf.
174 * Returns the result in a malloc'd string. */
175 char* formatstr(const char* fmt, ...);
177 typedef enum _ini_line_type
186 typedef struct _ini_line
193 /* Parses the INI file line pointer by 'lineptr'.
194 * The first line pointed by 'lineptr' may be modifed.
195 * 'lineptr' will point to the next line after this function runs.
197 * Returns a ini_line structure with information about the line.
198 * For INI_COMMENT, the value field contains the comment.
199 * For INI_SECTION, the name field contains the section name.
200 * For INI_PROPERTY, the name and value fields contain the property parameters.
201 * The line type is INI_BLANK if the line is blank or invalid.
203 * The name and value fields (if any) of ini_line point to 'lineptr'
204 * (so their lifetime is associated to that of 'lineptr').
206 ini_line ini_parse_line(char **lineptr);