IODevice.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/EnumBits.h>
  9. #include <AK/Forward.h>
  10. #include <AK/Stream.h>
  11. #include <LibCore/Object.h>
  12. namespace Core {
  13. class IODevice;
  14. // This is not necessarily a valid iterator in all contexts,
  15. // if we had concepts, this would be InputIterator, not Copyable, Movable.
  16. class LineIterator {
  17. AK_MAKE_NONCOPYABLE(LineIterator);
  18. public:
  19. explicit LineIterator(IODevice&, bool is_end = false);
  20. bool operator==(LineIterator const& other) const { return &other == this || (at_end() && other.is_end()) || (other.at_end() && is_end()); }
  21. bool is_end() const { return m_is_end; }
  22. bool at_end() const;
  23. LineIterator& operator++();
  24. StringView operator*() const { return m_buffer; }
  25. private:
  26. NonnullRefPtr<IODevice> m_device;
  27. bool m_is_end { false };
  28. DeprecatedString m_buffer;
  29. };
  30. class LineRange {
  31. public:
  32. LineRange() = delete;
  33. explicit LineRange(IODevice& device)
  34. : m_device(device)
  35. {
  36. }
  37. LineIterator begin();
  38. LineIterator end();
  39. private:
  40. IODevice& m_device;
  41. };
  42. enum class OpenMode : unsigned {
  43. NotOpen = 0,
  44. ReadOnly = 1,
  45. WriteOnly = 2,
  46. ReadWrite = 3,
  47. Append = 4,
  48. Truncate = 8,
  49. MustBeNew = 16,
  50. KeepOnExec = 32,
  51. };
  52. AK_ENUM_BITWISE_OPERATORS(OpenMode)
  53. class IODevice : public Object {
  54. C_OBJECT_ABSTRACT(IODevice)
  55. public:
  56. virtual ~IODevice() override = default;
  57. int fd() const { return m_fd; }
  58. OpenMode mode() const { return m_mode; }
  59. bool is_open() const { return m_mode != OpenMode::NotOpen; }
  60. bool eof() const { return m_eof; }
  61. int error() const { return m_error; }
  62. char const* error_string() const;
  63. bool has_error() const { return m_error != 0; }
  64. int read(u8* buffer, int length);
  65. ByteBuffer read(size_t max_size);
  66. ByteBuffer read_all();
  67. DeprecatedString read_line(size_t max_size = 16384);
  68. bool write(u8 const*, int size);
  69. bool write(StringView);
  70. bool truncate(off_t);
  71. bool can_read_line() const;
  72. bool can_read() const;
  73. bool can_read_only_from_buffer() const { return !m_buffered_data.is_empty() && !can_read_from_fd(); }
  74. bool seek(i64, SeekMode = SeekMode::SetPosition, off_t* = nullptr);
  75. virtual bool open(OpenMode) = 0;
  76. virtual bool close();
  77. LineIterator line_begin() & { return LineIterator(*this); }
  78. LineIterator line_end() { return LineIterator(*this, true); }
  79. LineRange lines()
  80. {
  81. return LineRange(*this);
  82. }
  83. protected:
  84. explicit IODevice(Object* parent = nullptr);
  85. void set_fd(int);
  86. void set_mode(OpenMode mode) { m_mode = mode; }
  87. void set_error(int error) const { m_error = error; }
  88. void set_eof(bool eof) const { m_eof = eof; }
  89. virtual void did_update_fd(int) { }
  90. private:
  91. bool populate_read_buffer(size_t size = 1024) const;
  92. bool can_read_from_fd() const;
  93. int m_fd { -1 };
  94. OpenMode m_mode { OpenMode::NotOpen };
  95. mutable int m_error { 0 };
  96. mutable bool m_eof { false };
  97. mutable Vector<u8> m_buffered_data;
  98. };
  99. }