nxfile-callbacks.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <nx/nxfile.h>
  2. #include <libopenmpt/libopenmpt.h>
  3. #include <assert.h>
  4. static size_t openmpt_nxfile_read(void *stream, void *dst, size_t bytes)
  5. {
  6. nx_file_t f = (nx_file_t)stream;
  7. size_t bytes_read;
  8. ns_error_t err = NXFileRead(f, dst, bytes, &bytes_read);
  9. if (err != NErr_Success) {
  10. return 0;
  11. }
  12. return bytes_read;
  13. }
  14. static int openmpt_nxfile_seek(void *stream, int64_t offset, int whence)
  15. {
  16. nx_file_t f = (nx_file_t)stream;
  17. uint64_t position;
  18. if (whence == OPENMPT_STREAM_SEEK_SET) {
  19. position = offset;
  20. } else if (whence == OPENMPT_STREAM_SEEK_CUR) {
  21. ns_error_t err = NXFileTell(f, &position);
  22. if (err != NErr_Success) {
  23. return -1;
  24. }
  25. position += offset;
  26. } else if (whence == OPENMPT_STREAM_SEEK_END) {
  27. assert(0);
  28. } else {
  29. return -1;
  30. }
  31. ns_error_t err = NXFileSeek(f, position);
  32. if (err = NErr_Success) {
  33. return 0;
  34. } else {
  35. return -1;
  36. }
  37. }
  38. static int64_t openmpt_nxfile_tell(void *stream)
  39. {
  40. nx_file_t f = (nx_file_t)stream;
  41. uint64_t position;
  42. if (NXFileTell(f, &position) == NErr_Success) {
  43. return (int64_t)position;
  44. } else {
  45. return -1;
  46. }
  47. }
  48. openmpt_stream_callbacks openmpt_stream_get_nxfile_callbacks(void)
  49. {
  50. openmpt_stream_callbacks retval;
  51. memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
  52. retval.read = openmpt_nxfile_read;
  53. retval.seek = openmpt_nxfile_seek;
  54. retval.tell = openmpt_nxfile_tell;
  55. return retval;
  56. }
  57. openmpt_module *OpenMod(const wchar_t *filename)
  58. {
  59. openmpt_module * mod = 0;
  60. nx_string_t nx_filename=0;
  61. nx_uri_t nx_uri=0;
  62. NXStringCreateWithUTF16(&nx_filename, filename);
  63. NXURICreateWithNXString(&nx_uri, nx_filename);
  64. NXStringRelease(nx_filename);
  65. nx_file_t f=0;
  66. ns_error_t nserr;
  67. nserr = NXFileOpenZip(&f, nx_uri, NULL);
  68. if (nserr != NErr_Success) {
  69. nserr = NXFileOpenFile(&f, nx_uri, nx_file_FILE_read_binary);
  70. }
  71. NXURIRelease(nx_uri);
  72. if (nserr != NErr_Success) {
  73. return 0;
  74. }
  75. mod = openmpt_module_create(openmpt_stream_get_nxfile_callbacks(), f, NULL, NULL, NULL);
  76. NXFileRelease(f);
  77. return mod;
  78. }