ConfigFile.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = DeprecatedString()) const;
  37. bool read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value = false) const;
  38. template<Integral T = int>
  39. T read_num_entry(DeprecatedString const& group, DeprecatedString const& key, T default_value = 0) const
  40. {
  41. if (!has_key(group, key))
  42. return default_value;
  43. if constexpr (IsSigned<T>)
  44. return read_entry(group, key).to_int<T>().value_or(default_value);
  45. else
  46. return read_entry(group, key).to_uint<T>().value_or(default_value);
  47. }
  48. void write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value);
  49. void write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value);
  50. template<Integral T = int>
  51. void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, T value)
  52. {
  53. write_entry(group, key, DeprecatedString::number(value));
  54. }
  55. void dump() const;
  56. bool is_dirty() const { return m_dirty; }
  57. ErrorOr<void> sync();
  58. void add_group(DeprecatedString const& group);
  59. void remove_group(DeprecatedString const& group);
  60. void remove_entry(DeprecatedString const& group, DeprecatedString const& key);
  61. DeprecatedString const& filename() const { return m_filename; }
  62. private:
  63. ConfigFile(DeprecatedString const& filename, OwnPtr<BufferedFile> open_file);
  64. ErrorOr<void> reparse();
  65. DeprecatedString m_filename;
  66. OwnPtr<BufferedFile> m_file;
  67. HashMap<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> m_groups;
  68. bool m_dirty { false };
  69. };
  70. }