file32.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "file32.h"
  17. int Cfile32::open(const Cwin_handle& h)
  18. {
  19. m_h = h;
  20. m_p = 0;
  21. return !is_open();
  22. }
  23. int Cfile32::open(const string& name, int access)
  24. {
  25. return access & GENERIC_WRITE ? open(name, access, CREATE_ALWAYS, 0) : open(name, access, OPEN_EXISTING, FILE_SHARE_READ);
  26. }
  27. int Cfile32::open(const string& name, int access, int creation, int share)
  28. {
  29. m_h = Cwin_handle(CreateFileA(name.c_str(), access, share, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL));
  30. m_p = 0;
  31. return !is_open();
  32. }
  33. FILETIME Cfile32::get_creation_time() const
  34. {
  35. assert(is_open());
  36. FILETIME time;
  37. int r = GetFileTime(h(), &time, NULL, NULL);
  38. assert(r);
  39. return time;
  40. }
  41. FILETIME Cfile32::get_last_access_time() const
  42. {
  43. assert(is_open());
  44. FILETIME time;
  45. int r = GetFileTime(h(), NULL, &time, NULL);
  46. assert(r);
  47. return time;
  48. }
  49. FILETIME Cfile32::get_last_write_time() const
  50. {
  51. assert(is_open());
  52. FILETIME time;
  53. int r = GetFileTime(h(), NULL, NULL, &time);
  54. assert(r);
  55. return time;
  56. }
  57. int Cfile32::open_read(const string& name)
  58. {
  59. return open(name, GENERIC_READ, OPEN_EXISTING, FILE_SHARE_READ);
  60. }
  61. int Cfile32::open_edit(const string& name)
  62. {
  63. return open(name, GENERIC_READ | GENERIC_WRITE, OPEN_ALWAYS, 0);
  64. }
  65. int Cfile32::open_write(const string& name)
  66. {
  67. return open(name, GENERIC_WRITE, CREATE_ALWAYS, 0);
  68. }
  69. long long Cfile32::size() const
  70. {
  71. assert(is_open());
  72. LARGE_INTEGER v;
  73. return GetFileSizeEx(h(), &v) ? v.QuadPart : -1;
  74. }
  75. int Cfile32::read(void* data, int size)
  76. {
  77. assert(is_open());
  78. if (SetFilePointer(h(), m_p, 0, FILE_BEGIN) == -1)
  79. return 1;
  80. DWORD cb_read;
  81. if (!ReadFile(h(), data, size, &cb_read, 0) || cb_read != size)
  82. return 1;
  83. m_p += size;
  84. return 0;
  85. }
  86. int Cfile32::write(const void* data, int size)
  87. {
  88. assert(is_open());
  89. if (SetFilePointer(h(), m_p, 0, FILE_BEGIN) == -1)
  90. return 1;
  91. DWORD cb_write;
  92. if (!WriteFile(h(), data, size, &cb_write, 0) || cb_write != size)
  93. return 1;
  94. m_p += size;
  95. return 0;
  96. }
  97. int Cfile32::write(data_ref v)
  98. {
  99. return write(v.data(), v.size());
  100. };
  101. int Cfile32::set_eof()
  102. {
  103. assert(is_open());
  104. if (SetFilePointer(h(), m_p, 0, FILE_BEGIN) == -1)
  105. return 1;
  106. return !SetEndOfFile(h());
  107. }
  108. void Cfile32::close()
  109. {
  110. m_h.clear();
  111. }
  112. Cvirtual_binary Cfile32::get_mm()
  113. {
  114. if (!size())
  115. return Cvirtual_binary();
  116. Cwin_handle mh(CreateFileMapping(h(), NULL, PAGE_READONLY, 0, 0, NULL));
  117. void* d = mh ? MapViewOfFile(mh, FILE_MAP_READ, 0, 0, 0) : NULL;
  118. return d ? Cvirtual_binary(d, size(), std::shared_ptr<void>(d, UnmapViewOfFile)) : Cvirtual_binary();
  119. }
  120. Cvirtual_binary file32_read(const string& name)
  121. {
  122. Cfile32 f;
  123. return f.open_read(name) ? Cvirtual_binary() : f.get_mm();
  124. }
  125. int file32_write(const string& name, const void* s, int cb_s)
  126. {
  127. Cfile32 f;
  128. if (int error = f.open_write(name))
  129. return error;
  130. return f.write(s, cb_s);
  131. }