summaryrefslogtreecommitdiff
path: root/src/ini_type.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-08-24 13:50:31 +0000
committerrubidium <rubidium@openttd.org>2008-08-24 13:50:31 +0000
commitae32d158cbfc28faa48095025a2e65b00dae1d3b (patch)
treed44ea07890a61b0fc47031372302b96429a3f73c /src/ini_type.h
parenta4854aaddc68b8d06165052067254e997692c4b1 (diff)
downloadopenttd-ae32d158cbfc28faa48095025a2e65b00dae1d3b.tar.xz
(svn r14153) -Codechange: split ini file reading/saving from settings.cpp.
Diffstat (limited to 'src/ini_type.h')
-rw-r--r--src/ini_type.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/ini_type.h b/src/ini_type.h
new file mode 100644
index 000000000..34b97bd03
--- /dev/null
+++ b/src/ini_type.h
@@ -0,0 +1,102 @@
+/* $Id$ */
+
+/** @file ini_type.h Types related to reading/writing '*.ini' files. */
+
+#ifndef INI_TYPE_H
+#define INI_TYPE_H
+
+/** Types of groups */
+enum IniGroupType {
+ IGT_VARIABLES = 0, ///< values of the form "landscape = hilly"
+ IGT_LIST = 1, ///< a list of values, seperated by \n and terminated by the next group block
+};
+
+/** A single "line" in an ini file. */
+struct IniItem {
+ IniItem *next; ///< The next item in this group
+ char *name; ///< The name of this item
+ char *value; ///< The value of this item
+ char *comment; ///< The comment associated with this item
+
+ IniItem(struct IniGroup *parent, const char *name, size_t len = 0);
+ ~IniItem();
+};
+
+/** A group within an ini file. */
+struct IniGroup {
+ IniGroup *next; ///< the next group within this file
+ IniGroupType type; ///< type of group
+ IniItem *item; ///< the first item in the group
+ IniItem **last_item; ///< the last item in the group
+ char *name; ///< name of group
+ char *comment; ///< comment for group
+
+ /**
+ * Construct a new in-memory group of an Ini file.
+ * @param parent the file we belong to
+ * @param name the name of the group
+ * @param len the length of the name of the group
+ */
+ IniGroup(struct IniFile *parent, const char *name, size_t len = 0);
+
+ /** Free everything we loaded. */
+ ~IniGroup();
+
+ /**
+ * Get the item with the given name, and if it doesn't exist
+ * and create is true it creates a new item.
+ * @param name name of the item to find.
+ * @param create whether to create an item when not found or not.
+ * @return the requested item or NULL if not found.
+ */
+ IniItem *GetItem(const char *name, bool create);
+};
+
+/** The complete ini file. */
+struct IniFile {
+ IniGroup *group; ///< the first group in the ini
+ IniGroup **last_group; ///< the last group in the ini
+ char *comment; ///< last comment in file
+ const char **list_group_names; ///< NULL terminated list with group names that are lists
+
+ /**
+ * Construct a new in-memory Ini file representation.
+ * @param list_group_names A NULL terminated list with groups that should be
+ * loaded as lists instead of variables.
+ */
+ IniFile(const char **list_group_names = NULL);
+
+ /** Free everything we loaded. */
+ ~IniFile();
+
+ /**
+ * Get the group with the given name, and if it doesn't exist
+ * create a new group.
+ * @param name name of the group to find.
+ * @param len the maximum length of said name.
+ * @return the requested group.
+ */
+ IniGroup *GetGroup(const char *name, size_t len = 0);
+
+ /**
+ * Remove the group with the given name.
+ * @param name name of the group to remove.
+ */
+ void RemoveGroup(const char *name);
+
+ /**
+ * Load the Ini file's data from the disk.
+ * @param filename the file to load.
+ * @pre nothing has been loaded yet.
+ */
+ void LoadFromDisk(const char *filename);
+
+ /**
+ * Save the Ini file's data to the disk.
+ * @param filename the file to save to.
+ * @return true if saving succeeded.
+ */
+ bool SaveToDisk(const char *filename);
+};
+
+#endif /* INI_TYPE */