| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, the SerenityOS developers. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 17:12:53 +02:00
										 |  |  | #include <AK/Buffered.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-15 13:11:21 +01:00
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  | #include <AK/Stream.h>
 | 
					
						
							|  |  |  | #include <LibCore/File.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InputFileStream final : public InputStream { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     explicit InputFileStream(NonnullRefPtr<File> file) | 
					
						
							|  |  |  |         : m_file(file) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Result<InputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto file_result = File::open(filename, mode, permissions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (file_result.is_error()) | 
					
						
							|  |  |  |             return file_result.error(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return InputFileStream { file_result.value() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 17:12:53 +02:00
										 |  |  |     static Result<Buffered<InputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto file_result = File::open(filename, mode, permissions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (file_result.is_error()) | 
					
						
							|  |  |  |             return file_result.error(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Buffered<InputFileStream> { file_result.value() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     size_t read(Bytes bytes) override | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-09-05 16:39:56 +02:00
										 |  |  |         if (has_any_error()) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 12:24:17 +02:00
										 |  |  |         const auto buffer = m_file->read(bytes.size()); | 
					
						
							|  |  |  |         return buffer.bytes().copy_to(bytes); | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool read_or_error(Bytes bytes) override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (read(bytes) < bytes.size()) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:12:15 +02:00
										 |  |  |             set_fatal_error(); | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 12:24:17 +02:00
										 |  |  |     bool discard_or_error(size_t count) override { return m_file->seek(count, IODevice::SeekMode::FromCurrentPosition); } | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 12:24:17 +02:00
										 |  |  |     bool unreliable_eof() const override { return m_file->eof(); } | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void close() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!m_file->close()) | 
					
						
							| 
									
										
										
										
											2020-08-31 13:12:15 +02:00
										 |  |  |             set_fatal_error(); | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-09-13 12:24:17 +02:00
										 |  |  |     NonnullRefPtr<File> m_file; | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OutputFileStream : public OutputStream { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     explicit OutputFileStream(NonnullRefPtr<File> file) | 
					
						
							|  |  |  |         : m_file(file) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Result<OutputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto file_result = File::open(filename, mode, permissions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (file_result.is_error()) | 
					
						
							|  |  |  |             return file_result.error(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return OutputFileStream { file_result.value() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 17:12:53 +02:00
										 |  |  |     static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto file_result = File::open(filename, mode, permissions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (file_result.is_error()) | 
					
						
							|  |  |  |             return file_result.error(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Buffered<OutputFileStream> { file_result.value() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 19:37:11 -03:00
										 |  |  |     static OutputFileStream standard_output() | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-22 19:37:11 -03:00
										 |  |  |         return OutputFileStream { Core::File::standard_output() }; | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 17:12:53 +02:00
										 |  |  |     static Buffered<OutputFileStream> stdout_buffered() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-22 19:37:11 -03:00
										 |  |  |         return Buffered<OutputFileStream> { Core::File::standard_output() }; | 
					
						
							| 
									
										
										
										
											2020-09-02 17:12:53 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     size_t write(ReadonlyBytes bytes) override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!m_file->write(bytes.data(), bytes.size())) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:12:15 +02:00
										 |  |  |             set_fatal_error(); | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return bytes.size(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool write_or_error(ReadonlyBytes bytes) override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (write(bytes) < bytes.size()) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:12:15 +02:00
										 |  |  |             set_fatal_error(); | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void close() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!m_file->close()) | 
					
						
							| 
									
										
										
										
											2020-08-31 13:12:15 +02:00
										 |  |  |             set_fatal_error(); | 
					
						
							| 
									
										
										
										
											2020-08-28 17:53:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     NonnullRefPtr<File> m_file; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |