IODevice.h 3.2 KB

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