nxfile.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #ifndef WIN32_LEAN_AND_MEAN
  3. #define WIN32_LEAN_AND_MEAN
  4. #endif
  5. #include <windows.h>
  6. #include "../../nx/nxapi.h"
  7. #include <stdio.h> // for FILE
  8. #include "../../nx/nxuri.h"
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #include "../../nx/nxtime.h"
  12. #include "../../foundation/error.h"
  13. #include "../../jnetlib/jnetlib.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef struct nx_file_stat_s
  18. {
  19. nx_time_unix_64_t creation_time;
  20. nx_time_unix_64_t access_time;
  21. nx_time_unix_64_t modified_time;
  22. uint64_t file_size;
  23. } nx_file_stat_s, *nx_file_stat_t;
  24. typedef enum
  25. {
  26. nx_file_FILE_none = 0,
  27. nx_file_FILE_binary = (1 << 0),
  28. nx_file_FILE_read_text= (1 << 1),
  29. nx_file_FILE_read_binary=nx_file_FILE_read_text|nx_file_FILE_binary,
  30. nx_file_FILE_write_text=(1 << 2),
  31. nx_file_FILE_write_binary=nx_file_FILE_write_text|nx_file_FILE_binary,
  32. nx_file_FILE_update_text=(1 << 3),
  33. nx_file_FILE_update_binary=nx_file_FILE_update_text|nx_file_FILE_binary,
  34. nx_file_FILE_readwrite_text=(1 << 4),
  35. nx_file_FILE_readwrite_binary=nx_file_FILE_readwrite_text|nx_file_FILE_binary,
  36. nx_file_FILE_writable_mask = nx_file_FILE_write_text|nx_file_FILE_update_text|nx_file_FILE_readwrite_text,
  37. } nx_file_FILE_flags_t;
  38. static const int nx_file_O_BINARY=_O_BINARY;
  39. static const int nx_file_O_WRONLY=_O_WRONLY;
  40. static const int nx_file_O_RDONLY=_O_RDONLY;
  41. static FILE *NXFile_fopen(nx_uri_t filename, nx_file_FILE_flags_t flags)
  42. {
  43. if (filename)
  44. {
  45. if (flags == nx_file_FILE_read_binary)
  46. {
  47. return _wfopen(filename->string, L"rb");
  48. }
  49. else if (flags == nx_file_FILE_write_binary)
  50. {
  51. return _wfopen(filename->string, L"wb");
  52. }
  53. else if (flags == nx_file_FILE_update_binary)
  54. {
  55. return _wfopen(filename->string, L"r+b");
  56. }
  57. else if (flags == nx_file_FILE_readwrite_binary)
  58. {
  59. return _wfopen(filename->string, L"w+b");
  60. }
  61. }
  62. return 0;
  63. }
  64. /* returns a file descriptor */
  65. static int NXFile_open(nx_uri_t filename, int flags)
  66. {
  67. return _wopen(filename->string, flags);
  68. }
  69. NX_API ns_error_t NXFile_move(nx_uri_t destination, nx_uri_t source);
  70. NX_API ns_error_t NXFile_unlink(nx_uri_t filename);
  71. NX_API ns_error_t NXFile_stat(nx_uri_t filename, nx_file_stat_t file_stats);
  72. NX_API ns_error_t NXFile_statFILE(FILE *f, nx_file_stat_t file_stats);
  73. NX_API ns_error_t NXFile_fstat(int file_descriptor, nx_file_stat_t file_stats);
  74. /* --------------------------------------------------------------------------- */
  75. typedef struct nx_file_s { size_t dummy; } *nx_file_t;
  76. NX_API ns_error_t NXFileOpenFile(nx_file_t *out_file, nx_uri_t filename, nx_file_FILE_flags_t flags);
  77. NX_API ns_error_t NXFileOpenProgressiveDownloader(nx_file_t *out_file, nx_uri_t filename, nx_file_FILE_flags_t flags, jnl_http_t http, const char *user_agent);
  78. NX_API ns_error_t NXFileOpenZip(nx_file_t *out_file, nx_uri_t filename, nx_string_t extension_hint);
  79. NX_API nx_file_t NXFileRetain(nx_file_t f);
  80. NX_API void NXFileRelease(nx_file_t f);
  81. /* the implementation of this function will only return NErr_EndOfFile if 0 bytes were read.
  82. when *bytes_read < bytes_requested, it's likely that the file is at the end, but it will still return NErr_Success
  83. until the next call */
  84. NX_API ns_error_t NXFileRead(nx_file_t f, void *buffer, size_t bytes_requested, size_t *bytes_read);
  85. NX_API ns_error_t NXFileWrite(nx_file_t f, const void *buffer, size_t bytes);
  86. NX_API ns_error_t NXFileSeek(nx_file_t f, uint64_t position);
  87. NX_API ns_error_t NXFileTell(nx_file_t f, uint64_t *position);
  88. NX_API ns_error_t NXFileLockRegion(nx_file_t _f, uint64_t start_position, uint64_t end_position);
  89. NX_API ns_error_t NXFileUnlockRegion(nx_file_t _f);
  90. /* file_stats does _not_ take into account the current region */
  91. NX_API ns_error_t NXFileStat(nx_file_t f, nx_file_stat_t file_stats);
  92. /* returns the length of the file given the current region */
  93. NX_API ns_error_t NXFileLength(nx_file_t f, uint64_t *length);
  94. /* returns NErr_True, NErr_False, or possibly some error */
  95. NX_API ns_error_t NXFileEndOfFile(nx_file_t f);
  96. /* this exists as a one-off for nsmp4. hopefully we can get rid of it */
  97. NX_API ns_error_t NXFilePeekByte(nx_file_t f, uint8_t *byte);
  98. NX_API ns_error_t NXFileSync(nx_file_t f);
  99. NX_API ns_error_t NXFileTruncate(nx_file_t f);
  100. /* only valid for Progressive Downloader objects */
  101. NX_API ns_error_t NXFileProgressiveDownloaderAvailable(nx_file_t f, uint64_t size, uint64_t *available);
  102. #ifdef __cplusplus
  103. }
  104. #endif