| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  |  * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:58:45 +02:00
										 |  |  | #include <AK/RefCounted.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | #include <AK/RefPtr.h>
 | 
					
						
							|  |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/Color.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ConfigFile : public RefCounted<ConfigFile> { | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  |     enum class AllowWriting { | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |         No, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 16:33:54 +02:00
										 |  |  |     static NonnullRefPtr<ConfigFile> open_for_lib(String const& lib_name, AllowWriting = AllowWriting::No); | 
					
						
							|  |  |  |     static NonnullRefPtr<ConfigFile> open_for_app(String const& app_name, AllowWriting = AllowWriting::No); | 
					
						
							|  |  |  |     static NonnullRefPtr<ConfigFile> open_for_system(String const& app_name, AllowWriting = AllowWriting::No); | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  |     static NonnullRefPtr<ConfigFile> open(String const& filename, AllowWriting = AllowWriting::No); | 
					
						
							|  |  |  |     static NonnullRefPtr<ConfigFile> open(String const& filename, int fd); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     ~ConfigFile(); | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 22:28:45 +02:00
										 |  |  |     bool has_group(String const&) const; | 
					
						
							|  |  |  |     bool has_key(String const& group, String const& key) const; | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Vector<String> groups() const; | 
					
						
							| 
									
										
										
										
											2021-08-19 22:28:45 +02:00
										 |  |  |     Vector<String> keys(String const& group) const; | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-27 20:04:11 -06:00
										 |  |  |     size_t num_groups() const { return m_groups.size(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 22:28:45 +02:00
										 |  |  |     String read_entry(String const& group, String const& key, String const& default_value = String()) const; | 
					
						
							|  |  |  |     int read_num_entry(String const& group, String const& key, int default_value = 0) const; | 
					
						
							|  |  |  |     bool read_bool_entry(String const& group, String const& key, bool default_value = false) const; | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 22:28:45 +02:00
										 |  |  |     void write_entry(String const& group, String const& key, String const& value); | 
					
						
							|  |  |  |     void write_num_entry(String const& group, String const& key, int value); | 
					
						
							|  |  |  |     void write_bool_entry(String const& group, String const& key, bool value); | 
					
						
							|  |  |  |     void write_color_entry(String const& group, String const& key, Color value); | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     void dump() const; | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_dirty() const { return m_dirty; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     bool sync(); | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 22:28:45 +02:00
										 |  |  |     void remove_group(String const& group); | 
					
						
							|  |  |  |     void remove_entry(String const& group, String const& key); | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  |     String filename() const { return m_file->filename(); } | 
					
						
							| 
									
										
										
										
											2019-05-24 21:10:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  |     explicit ConfigFile(String const& filename, AllowWriting); | 
					
						
							|  |  |  |     explicit ConfigFile(String const& filename, int fd); | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void reparse(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 01:23:31 +02:00
										 |  |  |     NonnullRefPtr<File> m_file; | 
					
						
							| 
									
										
										
										
											2019-04-15 02:22:08 +02:00
										 |  |  |     HashMap<String, HashMap<String, String>> m_groups; | 
					
						
							|  |  |  |     bool m_dirty { false }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |