/* * 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"/** Types of groups */enumIniGroupType{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. */structIniItem{std::stringname;///< The name of this itemstd::optional<std::string>value;///< The value of this itemstd::stringcomment;///< The comment associated with this itemIniItem(conststd::string&name);voidSetValue(conststd::string_viewvalue);};/** A group within an ini file. */structIniGroup{std::list<IniItem>items;///< all items in the groupIniGroupTypetype;///< type of groupstd::stringname;///< name of groupstd::stringcomment;///< comment for groupIniGroup(conststd::string&name,IniGroupTypetype);constIniItem*GetItem(conststd::string&name)const;IniItem&GetOrCreateItem(conststd::string&name);IniItem&CreateItem(conststd::string&name);voidRemoveItem(conststd::string&name);voidClear();};/** Ini file that only supports loading. */structIniLoadFile{usingIniGroupNameList=std::initializer_list<std::string_view>;std::list<IniGroup>groups;///< all groups in the inistd::stringcomment;///< last comment in fileconstIniGroupNameListlist_group_names;///< list of group names that are listsconstIniGroupNameListseq_group_names;///< list of group names that are sequences.IniLoadFile(constIniGroupNameList&list_group_names={},constIniGroupNameList&seq_group_names={});virtual~IniLoadFile(){}constIniGroup*GetGroup(conststd::string&name)const;IniGroup*GetGroup(conststd::string&name);IniGroup&GetOrCreateGroup(conststd::string&name);IniGroup&CreateGroup(conststd::string&name);voidRemoveGroup(conststd::string&name);voidLoadFromDisk(conststd::string&filename,Subdirectorysubdir);/** * 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. */virtualFILE*OpenFile(conststd::string&filename,Subdirectorysubdir,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. */virtualvoidReportFileError(constchar*constpre,constchar*constbuffer,constchar*constpost)=0;};/** Ini file that supports both loading and saving. */structIniFile:IniLoadFile{IniFile(constIniGroupNameList&list_group_names={});boolSaveToDisk(conststd::string&filename);FILE*OpenFile(conststd::string&filename,Subdirectorysubdir,size_t*size)override;voidReportFileError(constchar*constpre,constchar*constbuffer,constchar*constpost)override;};#endif /* INI_TYPE_H */