libopenmpt_example_c_probe.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * Usage: libopenmpt_example_c_probe SOMEMODULE ...
  11. * Returns 0 on successful probing for all files.
  12. * Returns 1 on failed probing for 1 or more files.
  13. * Returns 2 on error.
  14. */
  15. #define LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY 1
  16. #define LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT 2
  17. #define LIBOPENMPT_EXAMPLE_PROBE_RESULT LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY
  18. #include <memory.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <libopenmpt/libopenmpt.h>
  24. #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
  25. static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
  26. (void)userdata;
  27. if ( message ) {
  28. fprintf( stderr, "%s\n", message );
  29. }
  30. }
  31. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  32. static int probe_file( const wchar_t * filename ) {
  33. #else
  34. static int probe_file( const char * filename ) {
  35. #endif
  36. int result = 0;
  37. int mod_err = OPENMPT_ERROR_OK;
  38. FILE * file = NULL;
  39. #if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY )
  40. int result_binary = 0;
  41. int probe_file_header_result = OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE;
  42. const char * probe_file_header_result_str = NULL;
  43. #endif
  44. #if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT )
  45. double probability = 0.0;
  46. #endif
  47. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  48. if ( wcslen( filename ) == 0 ) {
  49. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE'." );
  50. goto fail;
  51. }
  52. #else
  53. if ( strlen( filename ) == 0 ) {
  54. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE'." );
  55. goto fail;
  56. }
  57. #endif
  58. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  59. file = _wfopen( filename, L"rb" );
  60. #else
  61. file = fopen( filename, "rb" );
  62. #endif
  63. if ( !file ) {
  64. fprintf( stderr, "Error: %s\n", "fopen() failed." );
  65. goto fail;
  66. }
  67. #if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY )
  68. probe_file_header_result = openmpt_probe_file_header_from_stream( OPENMPT_PROBE_FILE_HEADER_FLAGS_DEFAULT, openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &openmpt_error_func_default, NULL, &mod_err, NULL );
  69. probe_file_header_result_str = NULL;
  70. switch ( probe_file_header_result ) {
  71. case OPENMPT_PROBE_FILE_HEADER_RESULT_SUCCESS:
  72. probe_file_header_result_str = "Success ";
  73. result_binary = 1;
  74. break;
  75. case OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE:
  76. probe_file_header_result_str = "Failure ";
  77. result_binary = 0;
  78. break;
  79. case OPENMPT_PROBE_FILE_HEADER_RESULT_WANTMOREDATA:
  80. probe_file_header_result_str = "WantMoreData";
  81. result_binary = 0;
  82. break;
  83. case OPENMPT_PROBE_FILE_HEADER_RESULT_ERROR:
  84. result_binary = 0;
  85. (void)result_binary;
  86. fprintf( stderr, "Error: %s\n", "openmpt_probe_file_header() failed." );
  87. goto fail;
  88. break;
  89. default:
  90. result_binary = 0;
  91. (void)result_binary;
  92. fprintf( stderr, "Error: %s\n", "openmpt_probe_file_header() failed." );
  93. goto fail;
  94. break;
  95. }
  96. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  97. fprintf( stdout, "%s - %ls\n", probe_file_header_result_str, filename );
  98. #else
  99. fprintf( stdout, "%s - %s\n", probe_file_header_result_str, filename );
  100. #endif
  101. if ( result_binary ) {
  102. result = 0;
  103. } else {
  104. result = 1;
  105. }
  106. #elif ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT )
  107. probability = openmpt_could_open_probability2( openmpt_stream_get_file_callbacks(), file, 0.25, &libopenmpt_example_logfunc, NULL, &openmpt_error_func_default, NULL, &mod_err, NULL );
  108. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  109. fprintf( stdout, "%s: %f - %ls\n", "Result", probability, filename );
  110. #else
  111. fprintf( stdout, "%s: %f - %s\n", "Result", probability, filename );
  112. #endif
  113. if ( probability >= 0.5 ) {
  114. result = 0;
  115. } else {
  116. result = 1;
  117. }
  118. #else
  119. #error "LIBOPENMPT_EXAMPLE_PROBE_RESULT is wrong"
  120. #endif
  121. goto cleanup;
  122. fail:
  123. result = 2;
  124. cleanup:
  125. if ( file ) {
  126. fclose( file );
  127. file = 0;
  128. }
  129. return result;
  130. }
  131. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  132. #if defined( __clang__ ) && !defined( _MSC_VER )
  133. int wmain( int argc, wchar_t * argv[] );
  134. #endif
  135. int wmain( int argc, wchar_t * argv[] ) {
  136. #else
  137. int main( int argc, char * argv[] ) {
  138. #endif
  139. int global_result = 0;
  140. if ( argc <= 1 ) {
  141. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE ...'." );
  142. goto fail;
  143. }
  144. for ( int i = 1; i < argc; ++i ) {
  145. int result = probe_file( argv[i] );
  146. if ( result > global_result ) {
  147. global_result = result;
  148. }
  149. }
  150. goto cleanup;
  151. fail:
  152. global_result = 2;
  153. cleanup:
  154. return global_result;
  155. }