# Conflicts: # regression/regression/result.txt # src/aircraft_cmd.cpp # src/airport_gui.cpp # src/articulated_vehicles.cpp # src/console_cmds.cpp # src/date_gui.cpp # src/engine.cpp # src/genworld_gui.cpp # src/gfx_layout_fallback.cpp # src/group_gui.cpp # src/hotkeys.cpp # src/network/core/tcp_connect.cpp # src/network/core/tcp_listen.h # src/newgrf.cpp # src/newgrf.h # src/newgrf_engine.cpp # src/newgrf_gui.cpp # src/newgrf_station.cpp # src/openttd.cpp # src/order_gui.cpp # src/os/macosx/osx_main.cpp # src/pathfinder/yapf/yapf_node_rail.hpp # src/rail_gui.cpp # src/saveload/afterload.cpp # src/saveload/cargopacket_sl.cpp # src/saveload/linkgraph_sl.cpp # src/saveload/station_sl.cpp # src/script/api/script_industrytype.cpp # src/settings.cpp # src/settings_gui.cpp # src/settings_table.cpp # src/settingsgen/settingsgen.cpp # src/station.cpp # src/station_cmd.cpp # src/strings.cpp # src/timer/timer_game_calendar.cpp # src/timer/timer_game_calendar.h # src/timer/timer_manager.h # src/timer/timer_window.cpp # src/timetable_cmd.cpp # src/toolbar_gui.cpp # src/town_cmd.cpp # src/town_gui.cpp # src/train_gui.cpp # src/vehicle_cmd.h # src/vehicle_gui.cpp # src/viewport.cpp # src/widgets/dropdown.cpp # src/window_func.h # src/window_gui.h
		
			
				
	
	
		
			101 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * This file is part of OpenTTD.
 | 
						|
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 | 
						|
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 | 
						|
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 | 
						|
 */
 | 
						|
 | 
						|
/** @file ini_type.h Types related to reading/writing '*.ini' files. */
 | 
						|
 | 
						|
#ifndef INI_TYPE_H
 | 
						|
#define INI_TYPE_H
 | 
						|
 | 
						|
#include "fileio_type.h"
 | 
						|
#include <list>
 | 
						|
#include <string>
 | 
						|
#include <optional>
 | 
						|
 | 
						|
/** Types of groups */
 | 
						|
enum IniGroupType {
 | 
						|
	IGT_VARIABLES = 0, ///< Values of the form "landscape = hilly".
 | 
						|
	IGT_LIST      = 1, ///< A list of values, separated by \n and terminated by the next group block.
 | 
						|
	IGT_SEQUENCE  = 2, ///< A list of uninterpreted lines, terminated by the next group block.
 | 
						|
};
 | 
						|
 | 
						|
/** A single "line" in an ini file. */
 | 
						|
struct IniItem {
 | 
						|
	std::string name;                 ///< The name of this item
 | 
						|
	std::optional<std::string> value; ///< The value of this item
 | 
						|
	std::string comment;              ///< The comment associated with this item
 | 
						|
 | 
						|
	IniItem(const std::string &name);
 | 
						|
 | 
						|
	void SetValue(const std::string_view value);
 | 
						|
};
 | 
						|
 | 
						|
/** A group within an ini file. */
 | 
						|
struct IniGroup {
 | 
						|
	std::list<IniItem> items; ///< all items in the group
 | 
						|
	IniGroupType type;   ///< type of group
 | 
						|
	std::string name;    ///< name of group
 | 
						|
	std::string comment; ///< comment for group
 | 
						|
 | 
						|
	IniGroup(const std::string &name, IniGroupType type);
 | 
						|
 | 
						|
	const IniItem *GetItem(const std::string &name) const;
 | 
						|
	IniItem &GetOrCreateItem(const std::string &name);
 | 
						|
	IniItem &CreateItem(const std::string &name);
 | 
						|
	void RemoveItem(const std::string &name);
 | 
						|
	void Clear();
 | 
						|
};
 | 
						|
 | 
						|
/** Ini file that only supports loading. */
 | 
						|
struct IniLoadFile {
 | 
						|
	using IniGroupNameList = std::initializer_list<std::string_view>;
 | 
						|
 | 
						|
	std::list<IniGroup> groups; ///< all groups in the ini
 | 
						|
	std::string comment;                  ///< last comment in file
 | 
						|
	const IniGroupNameList list_group_names; ///< list of group names that are lists
 | 
						|
	const IniGroupNameList seq_group_names;  ///< list of group names that are sequences.
 | 
						|
 | 
						|
	IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {});
 | 
						|
	virtual ~IniLoadFile() { }
 | 
						|
 | 
						|
	const IniGroup *GetGroup(const std::string &name) const;
 | 
						|
	IniGroup *GetGroup(const std::string &name);
 | 
						|
	IniGroup &GetOrCreateGroup(const std::string &name);
 | 
						|
	IniGroup &CreateGroup(const std::string &name);
 | 
						|
	void RemoveGroup(const std::string &name);
 | 
						|
 | 
						|
	void LoadFromDisk(const std::string &filename, Subdirectory subdir, std::string *save = nullptr);
 | 
						|
 | 
						|
	/**
 | 
						|
	 * Open the INI file.
 | 
						|
	 * @param filename Name of the INI file.
 | 
						|
	 * @param subdir The subdir to load the file from.
 | 
						|
	 * @param[out] size Size of the opened file.
 | 
						|
	 * @return File handle of the opened file, or \c nullptr.
 | 
						|
	 */
 | 
						|
	virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) = 0;
 | 
						|
 | 
						|
	/**
 | 
						|
	 * Report an error about the file contents.
 | 
						|
	 * @param pre    Prefix text of the \a buffer part.
 | 
						|
	 * @param buffer Part of the file with the error.
 | 
						|
	 * @param post   Suffix text of the \a buffer part.
 | 
						|
	 */
 | 
						|
	virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post) = 0;
 | 
						|
};
 | 
						|
 | 
						|
/** Ini file that supports both loading and saving. */
 | 
						|
struct IniFile : IniLoadFile {
 | 
						|
	IniFile(const IniGroupNameList &list_group_names = {});
 | 
						|
 | 
						|
	bool SaveToDisk(const std::string &filename);
 | 
						|
 | 
						|
	FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) override;
 | 
						|
	void ReportFileError(const char * const pre, const char * const buffer, const char * const post) override;
 | 
						|
};
 | 
						|
 | 
						|
#endif /* INI_TYPE_H */
 |