IODevice.h 2.7 KB

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