1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code, released
16 : * March 31, 1998.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Samir Gehani <sgehani@netscape.com>
25 : * Benjamin Smedberg <bsmedberg@covad.net>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : // This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2
42 :
43 : #ifndef nsINIParser_h__
44 : #define nsINIParser_h__
45 :
46 : #ifdef MOZILLA_INTERNAL_API
47 : #define nsINIParser nsINIParser_internal
48 : #endif
49 :
50 : #include "nscore.h"
51 : #include "nsClassHashtable.h"
52 : #include "nsAutoPtr.h"
53 :
54 : #include <stdio.h>
55 :
56 : class nsILocalFile;
57 :
58 : class NS_COM_GLUE nsINIParser
59 : {
60 : public:
61 1214 : nsINIParser() { }
62 1214 : ~nsINIParser() { }
63 :
64 : /**
65 : * Initialize the INIParser with a nsILocalFile. If this method fails, no
66 : * other methods should be called. This method reads and parses the file,
67 : * the class does not hold a file handle open. An instance must only be
68 : * initialized once.
69 : */
70 : nsresult Init(nsILocalFile* aFile);
71 :
72 : /**
73 : * Initialize the INIParser with a file path. If this method fails, no
74 : * other methods should be called. This method reads and parses the file,
75 : * the class does not hold a file handle open. An instance must only
76 : * be initialized once.
77 : */
78 : nsresult Init(const char *aPath);
79 :
80 : /**
81 : * Callback for GetSections
82 : * @return false to stop enumeration, or true to continue.
83 : */
84 : typedef bool
85 : (* INISectionCallback)(const char *aSection, void *aClosure);
86 :
87 : /**
88 : * Enumerate the sections within the INI file.
89 : */
90 : nsresult GetSections(INISectionCallback aCB, void *aClosure);
91 :
92 : /**
93 : * Callback for GetStrings
94 : * @return false to stop enumeration, or true to continue
95 : */
96 : typedef bool
97 : (* INIStringCallback)(const char *aString, const char *aValue,
98 : void *aClosure);
99 :
100 : /**
101 : * Enumerate the strings within a section. If the section does
102 : * not exist, this function will silently return.
103 : */
104 : nsresult GetStrings(const char *aSection,
105 : INIStringCallback aCB, void *aClosure);
106 :
107 : /**
108 : * Get the value of the specified key in the specified section
109 : * of the INI file represented by this instance.
110 : *
111 : * @param aSection section name
112 : * @param aKey key name
113 : * @param aResult the value found
114 : * @throws NS_ERROR_FAILURE if the specified section/key could not be
115 : * found.
116 : */
117 : nsresult GetString(const char *aSection, const char *aKey,
118 : nsACString &aResult);
119 :
120 : /**
121 : * Alternate signature of GetString that uses a pre-allocated buffer
122 : * instead of a nsACString (for use in the standalone glue before
123 : * the glue is initialized).
124 : *
125 : * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
126 : * large enough for the data. aResult will be filled with as
127 : * much data as possible.
128 : *
129 : * @see GetString [1]
130 : */
131 : nsresult GetString(const char *aSection, const char* aKey,
132 : char *aResult, PRUint32 aResultLen);
133 :
134 : private:
135 : struct INIValue
136 : {
137 : INIValue(const char *aKey, const char *aValue)
138 : : key(aKey), value(aValue) { }
139 :
140 : const char *key;
141 : const char *value;
142 : nsAutoPtr<INIValue> next;
143 : };
144 :
145 : struct GSClosureStruct
146 : {
147 : INISectionCallback usercb;
148 : void *userclosure;
149 : };
150 :
151 : nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
152 : nsAutoArrayPtr<char> mFileContents;
153 :
154 : nsresult InitFromFILE(FILE *fd);
155 :
156 : static PLDHashOperator GetSectionsCB(const char *aKey,
157 : INIValue *aData, void *aClosure);
158 : };
159 :
160 : #endif /* nsINIParser_h__ */
|