1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - util.h                                                  *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2012 CasualJames                                        *
5  *   Copyright (C) 2002 Hacktarux                                          *
6  *                                                                         *
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.                                   *
11  *                                                                         *
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.                          *
16  *                                                                         *
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  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
23 #ifndef __UTIL_H__
24 #define __UTIL_H__
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <string.h>
31 
32 #include <retro_inline.h>
33 
34 /**********************
35    Byte swap utilities
36  **********************/
37 #ifdef _MSC_VER
38 #include <stdlib.h>
39 #endif
40 
41 #if defined(_MSC_VER)
42 #define BUILTIN_BSWAP16 _byteswap_ushort
43 #define BUILTIN_BSWAP32 _byteswap_ulong
44 #define BUILTIN_BSWAP64 _byteswap_uint64
45 #endif
46 
m64p_swap16(unsigned short x)47 static INLINE unsigned short m64p_swap16(unsigned short x)
48 {
49 #ifdef BUILTIN_BSWAP16
50    return BUILTIN_BSWAP16(x);
51 #else
52    return ((x & 0x00FF) << 8) |
53       ((x & 0xFF00) >> 8);
54 #endif
55 }
56 
m64p_swap32(unsigned int x)57 static INLINE unsigned int m64p_swap32(unsigned int x)
58 {
59 #ifdef BUILTIN_BSWAP32
60    return BUILTIN_BSWAP32(x); // long is always 32-bit in Windows
61 #else
62    return ((x & 0x000000FF) << 24) |
63       ((x & 0x0000FF00) << 8) |
64       ((x & 0x00FF0000) >> 8) |
65       ((x & 0xFF000000) >> 24);
66 #endif
67 }
68 
m64p_swap64(unsigned long long int x)69 static INLINE unsigned long long int m64p_swap64(unsigned long long int x)
70 {
71 #ifdef BUILTIN_BSWAP64
72    return BUILTIN_BSWAP64(x);
73 #else
74    return ((x & 0x00000000000000FFULL) << 56) |
75       ((x & 0x000000000000FF00ULL) << 40) |
76       ((x & 0x0000000000FF0000ULL) << 24) |
77       ((x & 0x00000000FF000000ULL) << 8) |
78       ((x & 0x000000FF00000000ULL) >> 8) |
79       ((x & 0x0000FF0000000000ULL) >> 24) |
80       ((x & 0x00FF000000000000ULL) >> 40) |
81       ((x & 0xFF00000000000000ULL) >> 56);
82 #endif
83 }
84 
85 #ifdef MSB_FIRST
86 #define big16(x) (x)
87 #define big32(x) (x)
88 #define big64(x) (x)
89 #define little16(x) m64p_swap16(x)
90 #define little32(x) m64p_swap32(x)
91 #define little64(x) m64p_swap64(x)
92 #else
93 #define big16(x) m64p_swap16(x)
94 #define big32(x) m64p_swap32(x)
95 #define big64(x) m64p_swap64(x)
96 #define little16(x) (x)
97 #define little32(x) (x)
98 #define little64(x) (x)
99 #endif
100 
101 /* Byte swaps, converts to little endian or converts to big endian a buffer,
102  * containing 'count' elements, each of size 'length'. */
103 void swap_buffer(void *buffer, size_t length, size_t count);
104 void to_little_endian_buffer(void *buffer, size_t length, size_t count);
105 void to_big_endian_buffer(void *buffer, size_t length, size_t count);
106 
107 /**********************
108      GUI utilities
109  **********************/
110 void countrycodestring(char countrycode, char *string);
111 void imagestring(unsigned char imagetype, char *string);
112 
113 /**********************
114      Path utilities
115  **********************/
116 
117 /* Extracts the full file name (with extension) from a path string.
118  * Returns the same string, advanced until the file name. */
119 const char* namefrompath(const char* path);
120 
121 /* Creates a path string by joining two path strings.
122  * The given path strings may or may not start or end with a path separator.
123  * Returns a malloc'd string with the resulting path. */
124 char* combinepath(const char* first, const char *second);
125 
126 /**********************
127     String utilities
128  **********************/
129 
130 /** trim
131  *    Removes leading and trailing whitespace from str. Function modifies str
132  *    and also returns modified string.
133  */
134 char *trim(char *str);
135 
136 /* Converts an string to an integer.
137  * Returns 1 on success, 0 on failure. 'result' is undefined on failure.
138  *
139  * The following conditions cause this function to fail:
140  * - Empty string
141  * - Leading characters (including whitespace)
142  * - Trailing characters (including whitespace)
143  * - Overflow or underflow.
144  */
145 int string_to_int(const char *str, int *result);
146 
147 /* Converts an string of hexadecimal characters to a byte array.
148  * 'output_size' is the number of bytes (hex digraphs) to convert.
149  * Returns 1 on success, 0 on failure. 'output' is undefined on failure. */
150 int parse_hex(const char *str, unsigned char *output, size_t output_size);
151 
152 /* Formats an string, using the same syntax as printf.
153  * Returns the result in a malloc'd string. */
154 char* formatstr(const char* fmt, ...);
155 
156 typedef enum _ini_line_type
157 {
158     INI_BLANK,
159     INI_COMMENT,
160     INI_SECTION,
161     INI_PROPERTY,
162     INI_TRASH
163 } ini_line_type;
164 
165 typedef struct _ini_line
166 {
167     ini_line_type type;
168     char *name;
169     char *value;
170 } ini_line;
171 
172 /* Parses the INI file line pointer by 'lineptr'.
173  * The first line pointed by 'lineptr' may be modifed.
174  * 'lineptr' will point to the next line after this function runs.
175  *
176  * Returns a ini_line structure with information about the line.
177  * For INI_COMMENT, the value field contains the comment.
178  * For INI_SECTION, the name field contains the section name.
179  * For INI_PROPERTY, the name and value fields contain the property parameters.
180  * The line type is INI_BLANK if the line is blank or invalid.
181  *
182  * The name and value fields (if any) of ini_line point to 'lineptr'
183  * (so their lifetime is associated to that of 'lineptr').
184  */
185 ini_line ini_parse_line(char **lineptr);
186 
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif // __UTIL_H__
192 
193