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 "nsAutoPtr.h"
31 * Initialize the INIParser with a nsIFile. If this method fails, no
32 * other methods should be called. This method reads and parses the file,
33 * the class does not hold a file handle open. An instance must only be
36 nsresult
Init(nsIFile
* aFile
);
39 * Initialize the INIParser with a file path. If this method fails, no
40 * other methods should be called. This method reads and parses the file,
41 * the class does not hold a file handle open. An instance must only
42 * be initialized once.
44 nsresult
Init(const char* aPath
);
47 * Callback for GetSections
48 * @return false to stop enumeration, or true to continue.
50 typedef bool (*INISectionCallback
)(const char* aSection
, void* aClosure
);
53 * Enumerate the sections within the INI file.
55 nsresult
GetSections(INISectionCallback aCB
, void* aClosure
);
58 * Callback for GetStrings
59 * @return false to stop enumeration, or true to continue
61 typedef bool (*INIStringCallback
)(const char* aString
, const char* aValue
,
65 * Enumerate the strings within a section. If the section does
66 * not exist, this function will silently return.
68 nsresult
GetStrings(const char* aSection
,
69 INIStringCallback aCB
, void* aClosure
);
72 * Get the value of the specified key in the specified section
73 * of the INI file represented by this instance.
75 * @param aSection section name
76 * @param aKey key name
77 * @param aResult the value found
78 * @throws NS_ERROR_FAILURE if the specified section/key could not be
81 nsresult
GetString(const char* aSection
, const char* aKey
,
85 * Alternate signature of GetString that uses a pre-allocated buffer
86 * instead of a nsACString (for use in the standalone glue before
87 * the glue is initialized).
89 * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
90 * large enough for the data. aResult will be filled with as
91 * much data as possible.
95 nsresult
GetString(const char* aSection
, const char* aKey
,
96 char* aResult
, uint32_t aResultLen
);
101 INIValue(const char* aKey
, const char* aValue
)
109 nsAutoPtr
<INIValue
> next
;
112 struct GSClosureStruct
114 INISectionCallback usercb
;
118 nsClassHashtable
<nsDepCharHashKey
, INIValue
> mSections
;
119 nsAutoArrayPtr
<char> mFileContents
;
121 nsresult
InitFromFILE(FILE* aFd
);
123 static PLDHashOperator
GetSectionsCB(const char* aKey
,
124 INIValue
* aData
, void* aClosure
);
127 #endif /* nsINIParser_h__ */