VirtualIO.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "VirtualIO.h"
  2. #include "api__in_wave.h"
  3. #include <api/service/waservicefactory.h>
  4. #include "../nu/AutoWide.h"
  5. #include <assert.h>
  6. #include <windows.h>
  7. // TODO: extend this to use api_filereader
  8. // instead of just the HTTP reader directly
  9. // then we could use this is a workaround for unicode filenames
  10. sf_count_t VirtualGetFileLength( void *user_data )
  11. {
  12. svc_fileReader *reader = (svc_fileReader *)user_data;
  13. return reader->getLength();
  14. }
  15. sf_count_t VirtualSeek( sf_count_t offset, int whence, void *user_data )
  16. {
  17. svc_fileReader *reader = (svc_fileReader *)user_data;
  18. switch ( whence )
  19. {
  20. case SEEK_SET:
  21. reader->seek( offset );
  22. break;
  23. case SEEK_CUR:
  24. {
  25. uint64_t cur = reader->getPos();
  26. reader->seek( offset + cur );
  27. }
  28. break;
  29. case SEEK_END:
  30. {
  31. uint64_t total = reader->getLength();
  32. reader->seek( total + offset );
  33. }
  34. break;
  35. }
  36. return reader->getPos();
  37. }
  38. sf_count_t VirtualRead( void *ptr, sf_count_t count, void *user_data )
  39. {
  40. svc_fileReader *reader = (svc_fileReader *)user_data;
  41. return reader->read( (int8_t *)ptr, (size_t)count );
  42. }
  43. sf_count_t VirtualWrite( const void *ptr, sf_count_t count, void *user_data )
  44. {
  45. svc_fileReader *reader = (svc_fileReader *)user_data;
  46. return reader->write( (int8_t *)ptr, (size_t)count );
  47. }
  48. sf_count_t VirtualTell( void *user_data )
  49. {
  50. svc_fileReader *reader = (svc_fileReader *)user_data;
  51. return reader->getPos();
  52. }
  53. SF_VIRTUAL_IO httpIO =
  54. {
  55. VirtualGetFileLength,
  56. VirtualSeek,
  57. VirtualRead,
  58. VirtualWrite,
  59. VirtualTell
  60. };
  61. static const GUID HTTPReaderGUID =
  62. { 0xbc10fa00, 0x53f5, 0x4032, { 0xa0, 0x09, 0x2, 0x2b, 0x87, 0xec, 0x34, 0x04 } };
  63. void *CreateReader( const wchar_t *url )
  64. {
  65. static waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid( HTTPReaderGUID );
  66. if ( sf )
  67. {
  68. svc_fileReader *l_reader = (svc_fileReader *)sf->getInterface();
  69. if ( l_reader )
  70. l_reader->open( url );
  71. return l_reader;
  72. }
  73. else
  74. return 0;
  75. }
  76. void DestroyReader( void *reader )
  77. {
  78. static waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid( HTTPReaderGUID );
  79. if ( sf )
  80. sf->releaseInterface( reader );
  81. }