IODevice.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. KeepOnExec = 32,
  36. };
  37. enum class SeekMode {
  38. SetPosition,
  39. FromCurrentPosition,
  40. FromEndPosition,
  41. };
  42. AK_ENUM_BITWISE_OPERATORS(OpenMode)
  43. class IODevice : public Object {
  44. C_OBJECT_ABSTRACT(IODevice)
  45. public:
  46. virtual ~IODevice() override;
  47. int fd() const { return m_fd; }
  48. OpenMode mode() const { return m_mode; }
  49. bool is_open() const { return m_mode != OpenMode::NotOpen; }
  50. bool eof() const { return m_eof; }
  51. int error() const { return m_error; }
  52. const char* error_string() const;
  53. bool has_error() const { return m_error != 0; }
  54. int read(u8* buffer, int length);
  55. ByteBuffer read(size_t max_size);
  56. ByteBuffer read_all();
  57. String read_line(size_t max_size = 16384);
  58. bool write(const u8*, int size);
  59. bool write(const StringView&);
  60. bool truncate(off_t);
  61. bool can_read_line() const;
  62. bool can_read() const;
  63. bool seek(i64, SeekMode = SeekMode::SetPosition, off_t* = nullptr);
  64. virtual bool open(OpenMode) = 0;
  65. virtual bool close();
  66. LineIterator line_begin() & { return LineIterator(*this); }
  67. LineIterator line_end() { return LineIterator(*this, true); }
  68. protected:
  69. explicit IODevice(Object* parent = nullptr);
  70. void set_fd(int);
  71. void set_mode(OpenMode mode) { m_mode = mode; }
  72. void set_error(int error) const { m_error = error; }
  73. void set_eof(bool eof) const { m_eof = eof; }
  74. virtual void did_update_fd(int) { }
  75. private:
  76. bool populate_read_buffer() const;
  77. bool can_read_from_fd() const;
  78. int m_fd { -1 };
  79. OpenMode m_mode { OpenMode::NotOpen };
  80. mutable int m_error { 0 };
  81. mutable bool m_eof { false };
  82. mutable Vector<u8> m_buffered_data;
  83. };
  84. }