OsFile.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "OsFile.h"
  24. #include "Assert.h"
  25. #include "OS.h"
  26. #include "tchar.h"
  27. namespace crown
  28. {
  29. OsFile::OsFile(const char* path, FileOpenMode mode) :
  30. m_eof(false)
  31. {
  32. m_file_handle = CreateFile(path,
  33. mode == FOM_READ ? GENERIC_READ : GENERIC_WRITE,
  34. 0,
  35. NULL,
  36. OPEN_ALWAYS,
  37. FILE_ATTRIBUTE_NORMAL,
  38. NULL);
  39. CE_ASSERT(m_file_handle != INVALID_HANDLE_VALUE, "Unable to open file: %s", path);
  40. m_mode = mode;
  41. }
  42. OsFile::~OsFile()
  43. {
  44. close();
  45. }
  46. void OsFile::close()
  47. {
  48. if (is_open())
  49. {
  50. CloseHandle(m_file_handle);
  51. m_file_handle = NULL;
  52. }
  53. }
  54. bool OsFile::is_open() const
  55. {
  56. return m_file_handle != NULL;
  57. }
  58. size_t OsFile::size() const
  59. {
  60. DWORD size;
  61. size = GetFileSize(m_file_handle, NULL);
  62. return size;
  63. }
  64. FileOpenMode OsFile::mode()
  65. {
  66. return m_mode;
  67. }
  68. size_t OsFile::read(void* data, size_t size)
  69. {
  70. DWORD bytes_read;
  71. BOOL result = ReadFile(m_file_handle, data, size, &bytes_read, NULL);
  72. CE_ASSERT(result == TRUE, "Unable to read from file");
  73. if (result && bytes_read == 0)
  74. {
  75. m_eof = true;
  76. }
  77. return bytes_read;
  78. }
  79. size_t OsFile::write(const void* data, size_t size)
  80. {
  81. DWORD bytes_written;
  82. bool write = WriteFile(m_file_handle, data, size, &bytes_written, NULL);
  83. CE_ASSERT(size == bytes_written, "Cannot read from file\n");
  84. return size;
  85. }
  86. void OsFile::seek(size_t position)
  87. {
  88. DWORD seek_result = SetFilePointer(m_file_handle, position, NULL, FILE_BEGIN);
  89. CE_ASSERT(seek_result != INVALID_SET_FILE_POINTER, "Failed to seek");
  90. }
  91. void OsFile::seek_to_end()
  92. {
  93. DWORD seek_result = SetFilePointer(m_file_handle, 0, NULL, FILE_END);
  94. CE_ASSERT(seek_result != INVALID_SET_FILE_POINTER, "Failed to seek to end");
  95. }
  96. void OsFile::skip(size_t bytes)
  97. {
  98. DWORD seek_result = SetFilePointer(m_file_handle, bytes, NULL, FILE_CURRENT);
  99. CE_ASSERT(seek_result != INVALID_SET_FILE_POINTER, "Failed to skip");
  100. }
  101. size_t OsFile::position() const
  102. {
  103. DWORD position = SetFilePointer(m_file_handle, 0, NULL, FILE_CURRENT);
  104. CE_ASSERT(position != INVALID_SET_FILE_POINTER, "Failed to get position");
  105. return position;
  106. }
  107. bool OsFile::eof() const
  108. {
  109. return m_eof;
  110. }
  111. } // namespace crown