Core commit. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-core / src / main / util.h
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 #include "osal/preproc.h"
32
33 /**********************
34      File utilities
35  **********************/
36
37 typedef enum _file_status
38 {
39     file_ok,
40     file_open_error,
41     file_read_error,
42     file_write_error
43 } file_status_t;
44
45 /** read_from_file
46  *    opens a file and reads the specified number of bytes.
47  *    returns zero on success, nonzero on failure
48  */
49 file_status_t read_from_file(const char *filename, void *data, size_t size);
50
51 /** write_to_file
52  *    opens a file and writes the specified number of bytes.
53  *    returns zero on sucess, nonzero on failure
54  */ 
55 file_status_t write_to_file(const char *filename, const void *data, size_t size);
56
57 /**********************
58    Byte swap utilities
59  **********************/
60 #ifdef _MSC_VER
61 #include <stdlib.h>
62 #endif
63
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. */
67
68 static osal_inline unsigned short m64p_swap16(unsigned short x)
69 {
70     #ifdef _MSC_VER
71     return _byteswap_ushort(x);
72     #else
73     return ((x & 0x00FF) << 8) |
74            ((x & 0xFF00) >> 8);
75     #endif
76 }
77
78 static osal_inline unsigned int m64p_swap32(unsigned int x)
79 {
80     #ifdef _MSC_VER
81     return _byteswap_ulong(x); // long is always 32-bit in Windows
82     #else
83     return ((x & 0x000000FF) << 24) |
84            ((x & 0x0000FF00) << 8) |
85            ((x & 0x00FF0000) >> 8) |
86            ((x & 0xFF000000) >> 24);
87     #endif
88 }
89
90 static osal_inline unsigned long long int m64p_swap64(unsigned long long int x)
91 {
92     #ifdef _MSC_VER
93     return _byteswap_uint64(x);
94     #else
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);
103     #endif
104 }
105
106 #ifdef M64P_BIG_ENDIAN
107 #define big16(x) (x)
108 #define big32(x) (x)
109 #define big64(x) (x)
110 #define little16(x) m64p_swap16(x)
111 #define little32(x) m64p_swap32(x)
112 #define little64(x) m64p_swap64(x)
113 #else
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)
120 #endif
121
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);
127
128 /**********************
129      GUI utilities
130  **********************/
131 void countrycodestring(unsigned short countrycode, char *string);
132 void imagestring(unsigned char imagetype, char *string);
133
134 /**********************
135      Path utilities
136  **********************/
137
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);
141
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);
146
147 /**********************
148     String utilities
149  **********************/
150
151 /** trim
152  *    Removes leading and trailing whitespace from str. Function modifies str
153  *    and also returns modified string.
154  */
155 char *trim(char *str);
156
157 /* Converts an string to an integer.
158  * Returns 1 on success, 0 on failure. 'result' is undefined on failure.
159  *
160  * The following conditions cause this function to fail:
161  * - Empty string
162  * - Leading characters (including whitespace)
163  * - Trailing characters (including whitespace)
164  * - Overflow or underflow.
165  */
166 int string_to_int(const char *str, int *result);
167
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);
172
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, ...);
176
177 typedef enum _ini_line_type
178 {
179     INI_BLANK,
180     INI_COMMENT,
181     INI_SECTION,
182     INI_PROPERTY,
183     INI_TRASH
184 } ini_line_type;
185
186 typedef struct _ini_line
187 {
188     ini_line_type type;
189     char *name;
190     char *value;
191 } ini_line;
192
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.
196  *
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.
202  *
203  * The name and value fields (if any) of ini_line point to 'lineptr'
204  * (so their lifetime is associated to that of 'lineptr').
205  */
206 ini_line ini_parse_line(char **lineptr);
207
208 #ifdef __cplusplus
209 }
210 #endif
211
212 #endif // __UTIL_H__
213