|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 // This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2 |
|
7 |
|
8 #ifndef nsINIParser_h__ |
|
9 #define nsINIParser_h__ |
|
10 |
|
11 #ifdef MOZILLA_INTERNAL_API |
|
12 #define nsINIParser nsINIParser_internal |
|
13 #endif |
|
14 |
|
15 #include "nscore.h" |
|
16 #include "nsClassHashtable.h" |
|
17 #include "nsAutoPtr.h" |
|
18 |
|
19 #include <stdio.h> |
|
20 |
|
21 class nsIFile; |
|
22 |
|
23 class NS_COM_GLUE nsINIParser |
|
24 { |
|
25 public: |
|
26 nsINIParser() { } |
|
27 ~nsINIParser() { } |
|
28 |
|
29 /** |
|
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 |
|
33 * initialized once. |
|
34 */ |
|
35 nsresult Init(nsIFile* aFile); |
|
36 |
|
37 /** |
|
38 * Initialize the INIParser with a file path. If this method fails, no |
|
39 * other methods should be called. This method reads and parses the file, |
|
40 * the class does not hold a file handle open. An instance must only |
|
41 * be initialized once. |
|
42 */ |
|
43 nsresult Init(const char *aPath); |
|
44 |
|
45 /** |
|
46 * Callback for GetSections |
|
47 * @return false to stop enumeration, or true to continue. |
|
48 */ |
|
49 typedef bool |
|
50 (* INISectionCallback)(const char *aSection, void *aClosure); |
|
51 |
|
52 /** |
|
53 * Enumerate the sections within the INI file. |
|
54 */ |
|
55 nsresult GetSections(INISectionCallback aCB, void *aClosure); |
|
56 |
|
57 /** |
|
58 * Callback for GetStrings |
|
59 * @return false to stop enumeration, or true to continue |
|
60 */ |
|
61 typedef bool |
|
62 (* INIStringCallback)(const char *aString, const char *aValue, |
|
63 void *aClosure); |
|
64 |
|
65 /** |
|
66 * Enumerate the strings within a section. If the section does |
|
67 * not exist, this function will silently return. |
|
68 */ |
|
69 nsresult GetStrings(const char *aSection, |
|
70 INIStringCallback aCB, void *aClosure); |
|
71 |
|
72 /** |
|
73 * Get the value of the specified key in the specified section |
|
74 * of the INI file represented by this instance. |
|
75 * |
|
76 * @param aSection section name |
|
77 * @param aKey key name |
|
78 * @param aResult the value found |
|
79 * @throws NS_ERROR_FAILURE if the specified section/key could not be |
|
80 * found. |
|
81 */ |
|
82 nsresult GetString(const char *aSection, const char *aKey, |
|
83 nsACString &aResult); |
|
84 |
|
85 /** |
|
86 * Alternate signature of GetString that uses a pre-allocated buffer |
|
87 * instead of a nsACString (for use in the standalone glue before |
|
88 * the glue is initialized). |
|
89 * |
|
90 * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not |
|
91 * large enough for the data. aResult will be filled with as |
|
92 * much data as possible. |
|
93 * |
|
94 * @see GetString [1] |
|
95 */ |
|
96 nsresult GetString(const char *aSection, const char* aKey, |
|
97 char *aResult, uint32_t aResultLen); |
|
98 |
|
99 private: |
|
100 struct INIValue |
|
101 { |
|
102 INIValue(const char *aKey, const char *aValue) |
|
103 : key(aKey), value(aValue) { } |
|
104 |
|
105 const char *key; |
|
106 const char *value; |
|
107 nsAutoPtr<INIValue> next; |
|
108 }; |
|
109 |
|
110 struct GSClosureStruct |
|
111 { |
|
112 INISectionCallback usercb; |
|
113 void *userclosure; |
|
114 }; |
|
115 |
|
116 nsClassHashtable<nsDepCharHashKey, INIValue> mSections; |
|
117 nsAutoArrayPtr<char> mFileContents; |
|
118 |
|
119 nsresult InitFromFILE(FILE *fd); |
|
120 |
|
121 static PLDHashOperator GetSectionsCB(const char *aKey, |
|
122 INIValue *aData, void *aClosure); |
|
123 }; |
|
124 |
|
125 #endif /* nsINIParser_h__ */ |