OsFile.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <stdio.h>
  24. #include "Assert.h"
  25. #include "OS.h"
  26. #include "OsFile.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. OsFile::OsFile(const char* path, FileOpenMode mode) :
  31. m_file_handle(NULL)
  32. {
  33. m_file_handle = fopen(path, (mode == FOM_READ) ? "rb" : "wb");
  34. CE_ASSERT(m_file_handle != NULL, "Unable to open file: %s", path);
  35. m_mode = mode;
  36. }
  37. //-----------------------------------------------------------------------------
  38. OsFile::~OsFile()
  39. {
  40. close();
  41. }
  42. //-----------------------------------------------------------------------------
  43. void OsFile::close()
  44. {
  45. if (m_file_handle != NULL)
  46. {
  47. fclose(m_file_handle);
  48. m_file_handle = NULL;
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. bool OsFile::is_open() const
  53. {
  54. return m_file_handle != NULL;
  55. }
  56. //-----------------------------------------------------------------------------
  57. FileOpenMode OsFile::mode()
  58. {
  59. return m_mode;
  60. }
  61. //-----------------------------------------------------------------------------
  62. size_t OsFile::size() const
  63. {
  64. size_t pos = position();
  65. int fseek_result = fseek(m_file_handle, 0, SEEK_END);
  66. CE_ASSERT(fseek_result == 0, "Failed to seek");
  67. size_t size = position();
  68. fseek_result = fseek(m_file_handle, (long) pos, SEEK_SET);
  69. CE_ASSERT(fseek_result == 0, "Failed to seek");
  70. return size;
  71. }
  72. //-----------------------------------------------------------------------------
  73. size_t OsFile::read(void* data, size_t size)
  74. {
  75. CE_ASSERT(data != NULL, "Data must be != NULL");
  76. return fread(data, 1, size, m_file_handle);
  77. }
  78. //-----------------------------------------------------------------------------
  79. size_t OsFile::write(const void* data, size_t size)
  80. {
  81. CE_ASSERT(data != NULL, "Data must be != NULL");
  82. return fwrite(data, 1, size, m_file_handle);
  83. }
  84. //-----------------------------------------------------------------------------
  85. void OsFile::seek(size_t position)
  86. {
  87. int fseek_result = fseek(m_file_handle, (long) position, SEEK_SET);
  88. CE_ASSERT(fseek_result == 0, "Failed to seek");
  89. }
  90. //-----------------------------------------------------------------------------
  91. void OsFile::seek_to_end()
  92. {
  93. int fseek_result = fseek(m_file_handle, 0, SEEK_END);
  94. CE_ASSERT(fseek_result == 0, "Failed to seek");
  95. }
  96. //-----------------------------------------------------------------------------
  97. void OsFile::skip(size_t bytes)
  98. {
  99. int fseek_result = fseek(m_file_handle, bytes, SEEK_CUR);
  100. CE_ASSERT(fseek_result == 0, "Failed to seek");
  101. }
  102. //-----------------------------------------------------------------------------
  103. size_t OsFile::position() const
  104. {
  105. return (size_t) ftell(m_file_handle);
  106. }
  107. //-----------------------------------------------------------------------------
  108. bool OsFile::eof() const
  109. {
  110. return feof(m_file_handle) != 0;
  111. }
  112. } // namespace crown