ConfigFile.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/Forward.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/OwnPtr.h>
  13. #include <AK/RefCounted.h>
  14. #include <AK/RefPtr.h>
  15. #include <AK/Vector.h>
  16. #include <LibCore/File.h>
  17. namespace Core {
  18. class ConfigFile : public RefCounted<ConfigFile> {
  19. public:
  20. enum class AllowWriting {
  21. Yes,
  22. No,
  23. };
  24. static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_lib(DeprecatedString const& lib_name, AllowWriting = AllowWriting::No);
  25. static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_app(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
  26. static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_system(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
  27. static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, AllowWriting = AllowWriting::No);
  28. static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, int fd);
  29. static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, NonnullOwnPtr<Core::File>);
  30. ~ConfigFile();
  31. bool has_group(DeprecatedString const&) const;
  32. bool has_key(DeprecatedString const& group, DeprecatedString const& key) const;
  33. Vector<DeprecatedString> groups() const;
  34. Vector<DeprecatedString> keys(DeprecatedString const& group) const;
  35. size_t num_groups() const { return m_groups.size(); }
  36. DeprecatedString read_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& default_value = {}) const
  37. {
  38. return read_entry_optional(group, key).value_or(default_value);
  39. }
  40. Optional<DeprecatedString> read_entry_optional(DeprecatedString const& group, DeprecatedString const& key) const;
  41. bool read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value = false) const;
  42. template<Integral T = int>
  43. T read_num_entry(DeprecatedString const& group, DeprecatedString const& key, T default_value = 0) const
  44. {
  45. if (!has_key(group, key))
  46. return default_value;
  47. if constexpr (IsSigned<T>)
  48. return read_entry(group, key, "").to_int<T>().value_or(default_value);
  49. else
  50. return read_entry(group, key, "").to_uint<T>().value_or(default_value);
  51. }
  52. void write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value);
  53. void write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value);
  54. template<Integral T = int>
  55. void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, T value)
  56. {
  57. write_entry(group, key, DeprecatedString::number(value));
  58. }
  59. void dump() const;
  60. bool is_dirty() const { return m_dirty; }
  61. ErrorOr<void> sync();
  62. void add_group(DeprecatedString const& group);
  63. void remove_group(DeprecatedString const& group);
  64. void remove_entry(DeprecatedString const& group, DeprecatedString const& key);
  65. DeprecatedString const& filename() const { return m_filename; }
  66. private:
  67. ConfigFile(DeprecatedString const& filename, OwnPtr<InputBufferedFile> open_file);
  68. ErrorOr<void> reparse();
  69. DeprecatedString m_filename;
  70. OwnPtr<InputBufferedFile> m_file;
  71. HashMap<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> m_groups;
  72. bool m_dirty { false };
  73. };
  74. }