rubidium@9996: /* $Id$ */ rubidium@9996: rubidium@9996: /** @file ini_type.h Types related to reading/writing '*.ini' files. */ rubidium@9996: rubidium@9996: #ifndef INI_TYPE_H rubidium@9996: #define INI_TYPE_H rubidium@9996: rubidium@9996: /** Types of groups */ rubidium@9996: enum IniGroupType { rubidium@9996: IGT_VARIABLES = 0, ///< values of the form "landscape = hilly" rubidium@9996: IGT_LIST = 1, ///< a list of values, seperated by \n and terminated by the next group block rubidium@9996: }; rubidium@9996: rubidium@9996: /** A single "line" in an ini file. */ rubidium@9996: struct IniItem { rubidium@9996: IniItem *next; ///< The next item in this group rubidium@9996: char *name; ///< The name of this item rubidium@9996: char *value; ///< The value of this item rubidium@9996: char *comment; ///< The comment associated with this item rubidium@9996: rubidium@10005: /** rubidium@10005: * Construct a new in-memory item of an Ini file. rubidium@10005: * @param parent the group we belong to rubidium@10005: * @param name the name of the item rubidium@10005: * @param len the length of the name of the item rubidium@10005: */ rubidium@9996: IniItem(struct IniGroup *parent, const char *name, size_t len = 0); rubidium@10005: rubidium@10005: /** Free everything we loaded. */ rubidium@9996: ~IniItem(); rubidium@10005: rubidium@10005: /** rubidium@10005: * Replace the current value with another value. rubidium@10005: * @param value the value to replace with. rubidium@10005: */ rubidium@10005: void SetValue(const char *value); rubidium@9996: }; rubidium@9996: rubidium@9996: /** A group within an ini file. */ rubidium@9996: struct IniGroup { rubidium@9996: IniGroup *next; ///< the next group within this file rubidium@9996: IniGroupType type; ///< type of group rubidium@9996: IniItem *item; ///< the first item in the group rubidium@9996: IniItem **last_item; ///< the last item in the group rubidium@9996: char *name; ///< name of group rubidium@9996: char *comment; ///< comment for group rubidium@9996: rubidium@9996: /** rubidium@9996: * Construct a new in-memory group of an Ini file. rubidium@9996: * @param parent the file we belong to rubidium@9996: * @param name the name of the group rubidium@9996: * @param len the length of the name of the group rubidium@9996: */ rubidium@9996: IniGroup(struct IniFile *parent, const char *name, size_t len = 0); rubidium@9996: rubidium@9996: /** Free everything we loaded. */ rubidium@9996: ~IniGroup(); rubidium@9996: rubidium@9996: /** rubidium@9996: * Get the item with the given name, and if it doesn't exist rubidium@9996: * and create is true it creates a new item. rubidium@9996: * @param name name of the item to find. rubidium@9996: * @param create whether to create an item when not found or not. rubidium@9996: * @return the requested item or NULL if not found. rubidium@9996: */ rubidium@9996: IniItem *GetItem(const char *name, bool create); rubidium@9996: }; rubidium@9996: rubidium@9996: /** The complete ini file. */ rubidium@9996: struct IniFile { rubidium@9996: IniGroup *group; ///< the first group in the ini rubidium@9996: IniGroup **last_group; ///< the last group in the ini rubidium@9996: char *comment; ///< last comment in file rubidium@9996: const char **list_group_names; ///< NULL terminated list with group names that are lists rubidium@9996: rubidium@9996: /** rubidium@9996: * Construct a new in-memory Ini file representation. rubidium@9996: * @param list_group_names A NULL terminated list with groups that should be rubidium@9996: * loaded as lists instead of variables. rubidium@9996: */ rubidium@9996: IniFile(const char **list_group_names = NULL); rubidium@9996: rubidium@9996: /** Free everything we loaded. */ rubidium@9996: ~IniFile(); rubidium@9996: rubidium@9996: /** rubidium@9996: * Get the group with the given name, and if it doesn't exist rubidium@9996: * create a new group. rubidium@9996: * @param name name of the group to find. rubidium@9996: * @param len the maximum length of said name. rubidium@9996: * @return the requested group. rubidium@9996: */ rubidium@9996: IniGroup *GetGroup(const char *name, size_t len = 0); rubidium@9996: rubidium@9996: /** rubidium@9996: * Remove the group with the given name. rubidium@9996: * @param name name of the group to remove. rubidium@9996: */ rubidium@9996: void RemoveGroup(const char *name); rubidium@9996: rubidium@9996: /** rubidium@9996: * Load the Ini file's data from the disk. rubidium@9996: * @param filename the file to load. rubidium@9996: * @pre nothing has been loaded yet. rubidium@9996: */ rubidium@9996: void LoadFromDisk(const char *filename); rubidium@9996: rubidium@9996: /** rubidium@9996: * Save the Ini file's data to the disk. rubidium@9996: * @param filename the file to save to. rubidium@9996: * @return true if saving succeeded. rubidium@9996: */ rubidium@9996: bool SaveToDisk(const char *filename); rubidium@9996: }; rubidium@9996: smatz@9999: #endif /* INI_TYPE_H */