IODevice.h 3.1 KB

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