1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2
9 #ifndef nsINIParser_h__
10 #define nsINIParser_h__
12 #ifdef MOZILLA_INTERNAL_API
13 # define nsINIParser nsINIParser_internal
17 #include "nsClassHashtable.h"
18 #include "mozilla/UniquePtr.h"
27 ~nsINIParser() = default;
30 * Initialize the INIParser with a nsIFile. If this method fails, no
31 * other methods should be called. This method reads and parses the file,
32 * the class does not hold a file handle open. An instance must only be
35 nsresult
Init(nsIFile
* aFile
);
37 nsresult
InitFromString(const nsCString
& aStr
);
40 * Enumerate the sections within the INI file.
42 * The callback takes the section name as an argument, and can can return
43 * false to stop enumeration.
45 nsresult
GetSections(std::function
<bool(const char*)>&& aCallback
);
48 * Enumerate the strings within a section. If the section does not exist, this
49 * function will silently return.
51 * The callback takes the key and value strings as arguments, and can return
52 * false to stop enumeration.
56 std::function
<bool(const char*, const char*)>&& aCallback
);
59 * Get the value of the specified key in the specified section
60 * of the INI file represented by this instance.
62 * @param aSection section name
63 * @param aKey key name
64 * @param aResult the value found
65 * @throws NS_ERROR_FAILURE if the specified section/key could not be
68 nsresult
GetString(const char* aSection
, const char* aKey
,
72 * Alternate signature of GetString that uses a pre-allocated buffer
73 * instead of a nsACString (for use in the standalone glue before
74 * the glue is initialized).
76 * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
77 * large enough for the data. aResult will be filled with as
78 * much data as possible.
82 nsresult
GetString(const char* aSection
, const char* aKey
, char* aResult
,
86 * Sets the value of the specified key in the specified section. The section
87 * is created if it does not already exist.
89 * @oaram aSection section name
90 * @param aKey key name
91 * @param aValue the value to set
93 nsresult
SetString(const char* aSection
, const char* aKey
,
97 * Deletes the value of the specified key in the specified section.
99 * @param aSection section name
100 * @param aKey key name
102 * @throws NS_ERROR_FAILURE if the string was not set.
104 nsresult
DeleteString(const char* aSection
, const char* aKey
);
107 * Deletes the specified section.
109 * @param aSection section name
111 * @throws NS_ERROR_FAILURE if the section did not exist.
113 nsresult
DeleteSection(const char* aSection
);
116 * Renames the specified section.
118 * @param aSection section name
119 * @param aNewName new section name
121 * @throws NS_ERROR_FAILURE if the section did not exist.
122 * @throws NS_ERROR_ILLEGAL_VALUE if the new section name already exists.
124 nsresult
RenameSection(const char* aSection
, const char* aNewName
);
127 * Writes the ini data to disk.
128 * @param aFile the file to write to
129 * @throws NS_ERROR_FAILURE on failure.
131 nsresult
WriteToFile(nsIFile
* aFile
);
133 void WriteToString(nsACString
& aOutput
);
137 INIValue(const char* aKey
, const char* aValue
)
138 : key(strdup(aKey
)), value(strdup(aValue
)) {}
145 void SetValue(const char* aValue
) {
147 value
= strdup(aValue
);
152 mozilla::UniquePtr
<INIValue
> next
;
155 nsClassHashtable
<nsCharPtrHashKey
, INIValue
> mSections
;
157 bool IsValidSection(const char* aSection
);
158 bool IsValidKey(const char* aKey
);
159 bool IsValidValue(const char* aValue
);
162 #endif /* nsINIParser_h__ */