2
0

NullFile.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #pragma once
  24. #include "File.h"
  25. namespace crown
  26. {
  27. /// Bit bucket file.
  28. /// Discards all data written to it and provides null data reading from it; plain and simple.
  29. ///
  30. /// @ingroup Filesystem
  31. class NullFile: public File
  32. {
  33. public:
  34. /// @copydoc File::File()
  35. NullFile(FileOpenMode mode) : File(mode) {}
  36. /// @copydoc File::~File()
  37. virtual ~NullFile() {}
  38. /// @copydoc File::seek()
  39. void seek(size_t position) { (void)position; }
  40. /// @copydoc File::seek_to_end()
  41. void seek_to_end() {}
  42. /// @copydoc File::skip()
  43. void skip(size_t bytes) { (void)bytes; }
  44. /// @copydoc File::read()
  45. /// @note
  46. /// Fills buffer with zeroes
  47. void read(void* buffer, size_t size)
  48. {
  49. for (size_t i = 0; i < size; i++)
  50. {
  51. ((uint8_t*)buffer)[i] = 0;
  52. }
  53. }
  54. /// @copydoc File::write()
  55. void write(const void* buffer, size_t size) { (void)buffer; (void)size; }
  56. /// @copydoc File::copy_to()
  57. /// @note
  58. /// Returns always true
  59. bool copy_to(File& file, size_t size = 0)
  60. {
  61. char zero = 0;
  62. file.write(&zero, size);
  63. return true;
  64. }
  65. /// @copydoc File::flush()
  66. void flush() {};
  67. /// @copydoc File::is_valid()
  68. /// @note
  69. /// Returns always true
  70. bool is_valid() { return true; }
  71. /// @copydoc File::end_of_file()
  72. /// @note
  73. /// Returns always false
  74. bool end_of_file() { return false; }
  75. /// @copydoc File::size()
  76. /// @note
  77. /// Returns always 0xFFFFFFFF
  78. size_t size() { return ~0; }
  79. /// @copydoc File::position()
  80. /// @note
  81. /// Returns always zero
  82. size_t position() { return 0; }
  83. /// @copydoc File::can_read()
  84. /// @note
  85. /// Returns always true
  86. bool can_read() { return true; }
  87. /// @copydoc File::can_write()
  88. /// @note
  89. /// Returns always true
  90. bool can_write() { return true; }
  91. /// @copydoc File::can_seek()
  92. /// @note
  93. /// Returns always true
  94. bool can_seek() { return true; }
  95. };
  96. } // namespace crown