ConfigFile.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. String read_entry(const String& group, const String& key, const String& default_value = String()) const;
  26. int read_num_entry(const String& group, const String& key, int default_value = 0) const;
  27. bool read_bool_entry(const String& group, const String& key, bool default_value = false) const;
  28. void write_entry(const String& group, const String& key, const String& value);
  29. void write_num_entry(const String& group, const String& key, int value);
  30. void write_bool_entry(const String& group, const String& key, bool value);
  31. void write_color_entry(const String& group, const String& key, Color value);
  32. void dump() const;
  33. bool is_dirty() const { return m_dirty; }
  34. bool sync();
  35. void remove_group(const String& group);
  36. void remove_entry(const String& group, const String& key);
  37. String filename() const { return m_filename; }
  38. private:
  39. explicit ConfigFile(const String& filename);
  40. void reparse();
  41. String m_filename;
  42. HashMap<String, HashMap<String, String>> m_groups;
  43. bool m_dirty { false };
  44. };
  45. }