Debian Codegraph @

examples/libopenmpt_example_c_probe.c

raw
1 /*
2 * libopenmpt_example_c_probe.c
3 * ----------------------------
4 * Purpose: libopenmpt C API probing example
5 * Notes : (currently none)
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10 /*
11 * Usage: libopenmpt_example_c_probe SOMEMODULE ...
12 * Returns 0 on successful probing for all files.
13 * Returns 1 on failed probing for 1 or more files.
14 * Returns 2 on error.
15 */
16
17 #define LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY 1
18 #define LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT 2
19
20 #define LIBOPENMPT_EXAMPLE_PROBE_RESULT LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY
21
22 #if defined( __MINGW32__ ) && !defined( __MINGW64__ )
23 #include <sys/types.h>
24 #endif
25
26 #include <memory.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <libopenmpt/libopenmpt.h>
33 #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
34
35 #if defined( __DJGPP__ )
36 #include <crt0.h>
37 #endif /* __DJGPP__ */
38
39 static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
40 (void)userdata;
41
42 if ( message ) {
43 fprintf( stderr, "%s\n", message );
44 }
45 }
46
47 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
48 static int probe_file( const wchar_t * filename ) {
49 #else
50 static int probe_file( const char * filename ) {
51 #endif
52
53 int result = 0;
54 int mod_err = OPENMPT_ERROR_OK;
55 FILE * file = NULL;
56
57 #if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY )
58 int result_binary = 0;
59 int probe_file_header_result = OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE;
60 const char * probe_file_header_result_str = NULL;
61 #endif
62 #if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT )
63 double probability = 0.0;
64 #endif
65
66 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
67 if ( wcslen( filename ) == 0 ) {
68 fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE'." );
69 goto fail;
70 }
71 #else
72 if ( strlen( filename ) == 0 ) {
73 fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE'." );
74 goto fail;
75 }
76 #endif
77
78 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
79 file = _wfopen( filename, L"rb" );
80 #else
81 file = fopen( filename, "rb" );
82 #endif
83 if ( !file ) {
84 fprintf( stderr, "Error: %s\n", "fopen() failed." );
85 goto fail;
86 }
87
88 #if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY )
89 probe_file_header_result = openmpt_probe_file_header_from_stream( OPENMPT_PROBE_FILE_HEADER_FLAGS_DEFAULT, openmpt_stream_get_file_callbacks2(), file, &libopenmpt_example_logfunc, NULL, &openmpt_error_func_default, NULL, &mod_err, NULL );
90 probe_file_header_result_str = NULL;
91 switch ( probe_file_header_result ) {
92 case OPENMPT_PROBE_FILE_HEADER_RESULT_SUCCESS:
93 probe_file_header_result_str = "Success ";
94 result_binary = 1;
95 break;
96 case OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE:
97 probe_file_header_result_str = "Failure ";
98 result_binary = 0;
99 break;
100 case OPENMPT_PROBE_FILE_HEADER_RESULT_WANTMOREDATA:
101 probe_file_header_result_str = "WantMoreData";
102 result_binary = 0;
103 break;
104 case OPENMPT_PROBE_FILE_HEADER_RESULT_ERROR:
105 result_binary = 0;
106 (void)result_binary;
107 fprintf( stderr, "Error: %s\n", "openmpt_probe_file_header() failed." );
108 goto fail;
109 break;
110 default:
111 result_binary = 0;
112 (void)result_binary;
113 fprintf( stderr, "Error: %s\n", "openmpt_probe_file_header() failed." );
114 goto fail;
115 break;
116 }
117 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
118 fprintf( stdout, "%s - %ls\n", probe_file_header_result_str, filename );
119 #else
120 fprintf( stdout, "%s - %s\n", probe_file_header_result_str, filename );
121 #endif
122 if ( result_binary ) {
123 result = 0;
124 } else {
125 result = 1;
126 }
127 #elif ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT )
128 probability = openmpt_could_open_probability2( openmpt_stream_get_file_callbacks2(), file, 0.25, &libopenmpt_example_logfunc, NULL, &openmpt_error_func_default, NULL, &mod_err, NULL );
129 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
130 fprintf( stdout, "%s: %f - %ls\n", "Result", probability, filename );
131 #else
132 fprintf( stdout, "%s: %f - %s\n", "Result", probability, filename );
133 #endif
134 if ( probability >= 0.5 ) {
135 result = 0;
136 } else {
137 result = 1;
138 }
139 #else
140 #error "LIBOPENMPT_EXAMPLE_PROBE_RESULT is wrong"
141 #endif
142
143 goto cleanup;
144
145 fail:
146
147 result = 2;
148
149 cleanup:
150
151 if ( file ) {
152 fclose( file );
153 file = 0;
154 }
155
156 return result;
157 }
158
159 #if defined( __DJGPP__ )
160 /* clang-format off */
161 int _crt0_startup_flags = 0
162 | _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */
163 | _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */
164 | _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */
165 | 0;
166 /* clang-format on */
167 #endif /* __DJGPP__ */
168 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
169 #if defined( __clang__ ) && !defined( _MSC_VER )
170 int wmain( int argc, wchar_t * argv[] );
171 #endif
172 int wmain( int argc, wchar_t * argv[] ) {
173 #else
174 int main( int argc, char * argv[] ) {
175 #endif
176 #if defined( __DJGPP__ )
177 /* clang-format off */
178 _crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; /* disable automatic locking for all further memory allocations */
179 /* clang-format on */
180 #endif /* __DJGPP__ */
181
182 int global_result = 0;
183
184 if ( argc <= 1 ) {
185 fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE ...'." );
186 goto fail;
187 }
188
189 for ( int i = 1; i < argc; ++i ) {
190 int result = probe_file( argv[i] );
191 if ( result > global_result ) {
192 global_result = result;
193 }
194 }
195
196 goto cleanup;
197
198 fail:
199
200 global_result = 2;
201
202 cleanup:
203
204 return global_result;
205 }