FileStream.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Buffered.h>
  28. #include <AK/ByteBuffer.h>
  29. #include <AK/Stream.h>
  30. #include <LibCore/File.h>
  31. namespace Core {
  32. class InputFileStream final : public InputStream {
  33. public:
  34. explicit InputFileStream(NonnullRefPtr<File> file)
  35. : m_file(file)
  36. {
  37. }
  38. static Result<InputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644)
  39. {
  40. ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  41. auto file_result = File::open(filename, mode, permissions);
  42. if (file_result.is_error())
  43. return file_result.error();
  44. return InputFileStream { file_result.value() };
  45. }
  46. static Result<Buffered<InputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644)
  47. {
  48. ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  49. auto file_result = File::open(filename, mode, permissions);
  50. if (file_result.is_error())
  51. return file_result.error();
  52. return Buffered<InputFileStream> { file_result.value() };
  53. }
  54. size_t read(Bytes bytes) override
  55. {
  56. if (has_any_error())
  57. return 0;
  58. const auto buffer = m_file->read(bytes.size());
  59. return buffer.bytes().copy_to(bytes);
  60. }
  61. bool read_or_error(Bytes bytes) override
  62. {
  63. if (read(bytes) < bytes.size()) {
  64. set_fatal_error();
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool discard_or_error(size_t count) override { return m_file->seek(count, IODevice::SeekMode::FromCurrentPosition); }
  70. bool unreliable_eof() const override { return m_file->eof(); }
  71. void close()
  72. {
  73. if (!m_file->close())
  74. set_fatal_error();
  75. }
  76. private:
  77. NonnullRefPtr<File> m_file;
  78. };
  79. class OutputFileStream : public OutputStream {
  80. public:
  81. explicit OutputFileStream(NonnullRefPtr<File> file)
  82. : m_file(file)
  83. {
  84. }
  85. static Result<OutputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
  86. {
  87. ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  88. auto file_result = File::open(filename, mode, permissions);
  89. if (file_result.is_error())
  90. return file_result.error();
  91. return OutputFileStream { file_result.value() };
  92. }
  93. static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
  94. {
  95. ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  96. auto file_result = File::open(filename, mode, permissions);
  97. if (file_result.is_error())
  98. return file_result.error();
  99. return Buffered<OutputFileStream> { file_result.value() };
  100. }
  101. static OutputFileStream stdout()
  102. {
  103. return OutputFileStream { Core::File::stdout() };
  104. }
  105. static Buffered<OutputFileStream> stdout_buffered()
  106. {
  107. return Buffered<OutputFileStream> { Core::File::stdout() };
  108. }
  109. size_t write(ReadonlyBytes bytes) override
  110. {
  111. if (!m_file->write(bytes.data(), bytes.size())) {
  112. set_fatal_error();
  113. return 0;
  114. }
  115. return bytes.size();
  116. }
  117. bool write_or_error(ReadonlyBytes bytes) override
  118. {
  119. if (write(bytes) < bytes.size()) {
  120. set_fatal_error();
  121. return false;
  122. }
  123. return true;
  124. }
  125. void close()
  126. {
  127. if (!m_file->close())
  128. set_fatal_error();
  129. }
  130. private:
  131. NonnullRefPtr<File> m_file;
  132. };
  133. }