ConfigFile.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/HashMap.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/String.h>
  11. #include <AK/Vector.h>
  12. #include <LibGfx/Color.h>
  13. namespace Core {
  14. class ConfigFile : public RefCounted<ConfigFile> {
  15. public:
  16. static NonnullRefPtr<ConfigFile> get_for_lib(const String& lib_name);
  17. static NonnullRefPtr<ConfigFile> get_for_app(const String& app_name);
  18. static NonnullRefPtr<ConfigFile> get_for_system(const String& app_name);
  19. static NonnullRefPtr<ConfigFile> open(const String& path);
  20. ~ConfigFile();
  21. bool has_group(const String&) const;
  22. bool has_key(const String& group, const String& key) const;
  23. Vector<String> groups() const;
  24. Vector<String> keys(const String& group) const;
  25. size_t num_groups() const { return m_groups.size(); }
  26. String read_entry(const String& group, const String& key, const String& default_value = String()) const;
  27. int read_num_entry(const String& group, const String& key, int default_value = 0) const;
  28. bool read_bool_entry(const String& group, const String& key, bool default_value = false) const;
  29. void write_entry(const String& group, const String& key, const String& value);
  30. void write_num_entry(const String& group, const String& key, int value);
  31. void write_bool_entry(const String& group, const String& key, bool value);
  32. void write_color_entry(const String& group, const String& key, Color value);
  33. void dump() const;
  34. bool is_dirty() const { return m_dirty; }
  35. bool sync();
  36. void remove_group(const String& group);
  37. void remove_entry(const String& group, const String& key);
  38. String filename() const { return m_filename; }
  39. private:
  40. explicit ConfigFile(const String& filename);
  41. void reparse();
  42. String m_filename;
  43. HashMap<String, HashMap<String, String>> m_groups;
  44. bool m_dirty { false };
  45. };
  46. }