filemap.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "filemap.h"
  2. #include "common.h"
  3. #ifndef _WIN32
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/fcntl.h> /* open */
  7. #include <sys/mman.h> /* mmap */
  8. #include <unistd.h> /* close */
  9. #include <stdio.h> /* perror */
  10. struct AFileMap aFileMapOpen(const char *filename) {
  11. struct AFileMap ret;
  12. ret.map = 0;
  13. ret.size = 0;
  14. ret.impl_.fd = open(filename, O_RDONLY);
  15. if (ret.impl_.fd < 0) { perror("cannot open file"); goto exit; }
  16. struct stat stat;
  17. if (fstat(ret.impl_.fd, &stat) < 0) { perror("cannot fstat file"); goto exit; }
  18. ret.size = stat.st_size;
  19. ret.map = mmap(0, ret.size, PROT_READ, MAP_PRIVATE, ret.impl_.fd, 0);
  20. if (!ret.map) perror("cannot mmap file");
  21. exit:
  22. if (!ret.map && ret.impl_.fd > 0)
  23. close(ret.impl_.fd);
  24. return ret;
  25. }
  26. void aFileMapClose(struct AFileMap *file) {
  27. if (file->map && file->impl_.fd > 0) {
  28. munmap((void*)file->map, file->size);
  29. close(file->impl_.fd);
  30. }
  31. }
  32. void aFileReset(struct AFile *file) {
  33. file->size = 0;
  34. file->impl_.fd = -1;
  35. }
  36. enum AFileResult aFileOpen(struct AFile *file, const char *filename) {
  37. file->impl_.fd = open(filename, O_RDONLY);
  38. if (file->impl_.fd < 0)
  39. return AFile_Fail;
  40. struct stat stat;
  41. fstat(file->impl_.fd, &stat);
  42. file->size = stat.st_size;
  43. return AFile_Success;
  44. }
  45. size_t aFileReadAtOffset(struct AFile *file, size_t off, size_t size, void *buffer) {
  46. ssize_t rd = pread(file->impl_.fd, buffer, size, off);
  47. if (rd < 0)
  48. perror("pread(fd)");
  49. return rd;
  50. }
  51. void aFileClose(struct AFile *file) {
  52. if (file->impl_.fd > 0) {
  53. close(file->impl_.fd);
  54. aFileReset(file);
  55. }
  56. }
  57. #else
  58. struct AFileMap aFileMapOpen(const char *filename) {
  59. (void)filename;
  60. struct AFileMap map = { 0 };
  61. return map;
  62. }
  63. void aFileMapClose(struct AFileMap *file) {
  64. (void)file;
  65. }
  66. void aFileReset(struct AFile *file) {
  67. file->size = 0;
  68. file->impl_.handle = INVALID_HANDLE_VALUE;
  69. }
  70. enum AFileResult aFileOpen(struct AFile *file, const char *filename) {
  71. const int filename_len = strlen(filename);
  72. char *slashes = _alloca(filename_len + 1);
  73. for (int i = 0; filename[i] != '\0'; ++i)
  74. slashes[i] = filename[i] != '/' ? filename[i] : '\\';
  75. slashes[filename_len] = '\0';
  76. wchar_t *filename_w;
  77. const int buf_length = MultiByteToWideChar(CP_UTF8, 0, slashes, -1, NULL, 0);
  78. filename_w = _alloca(buf_length);
  79. MultiByteToWideChar(CP_UTF8, 0, slashes, -1, filename_w, buf_length);
  80. file->impl_.handle = CreateFileW(filename_w, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  81. if (file->impl_.handle == INVALID_HANDLE_VALUE)
  82. return AFile_Fail;
  83. LARGE_INTEGER splurge_integer = { 0 };
  84. if (!GetFileSizeEx(file->impl_.handle, &splurge_integer)) {
  85. CloseHandle(file->impl_.handle);
  86. file->impl_.handle = INVALID_HANDLE_VALUE;
  87. return AFile_Fail;
  88. }
  89. file->size = (size_t)splurge_integer.QuadPart;
  90. return AFile_Success;
  91. }
  92. size_t aFileReadAtOffset(struct AFile *file, size_t off, size_t size, void *buffer) {
  93. OVERLAPPED overlapped;
  94. memset(&overlapped, 0, sizeof(overlapped));
  95. overlapped.Offset = off;
  96. DWORD read = 0;
  97. if (!ReadFile(file->impl_.handle, buffer, size, &read, &overlapped))
  98. PRINTF("Failed to read from file %p", file->impl_.handle);
  99. return read;
  100. }
  101. void aFileClose(struct AFile *file) {
  102. CloseHandle(file->impl_.handle);
  103. }
  104. #endif