Line data Source code
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/. */
6 :
7 : // This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2
8 :
9 : #ifndef nsINIParser_h__
10 : #define nsINIParser_h__
11 :
12 : #ifdef MOZILLA_INTERNAL_API
13 : #define nsINIParser nsINIParser_internal
14 : #endif
15 :
16 : #include "nscore.h"
17 : #include "nsClassHashtable.h"
18 : #include "mozilla/UniquePtr.h"
19 :
20 : #include <stdio.h>
21 :
22 : class nsIFile;
23 :
24 : class nsINIParser
25 : {
26 : public:
27 2 : nsINIParser() {}
28 2 : ~nsINIParser() {}
29 :
30 : /**
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
34 : * initialized once.
35 : */
36 : nsresult Init(nsIFile* aFile);
37 :
38 : /**
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.
43 : */
44 : nsresult Init(const char* aPath);
45 :
46 : /**
47 : * Callback for GetSections
48 : * @return false to stop enumeration, or true to continue.
49 : */
50 : typedef bool (*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 (*INIStringCallback)(const char* aString, const char* aValue,
62 : void* aClosure);
63 :
64 : /**
65 : * Enumerate the strings within a section. If the section does
66 : * not exist, this function will silently return.
67 : */
68 : nsresult GetStrings(const char* aSection,
69 : INIStringCallback aCB, void* aClosure);
70 :
71 : /**
72 : * Get the value of the specified key in the specified section
73 : * of the INI file represented by this instance.
74 : *
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
79 : * found.
80 : */
81 : nsresult GetString(const char* aSection, const char* aKey,
82 : nsACString& aResult);
83 :
84 : /**
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).
88 : *
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.
92 : *
93 : * @see GetString [1]
94 : */
95 : nsresult GetString(const char* aSection, const char* aKey,
96 : char* aResult, uint32_t aResultLen);
97 :
98 : private:
99 8 : struct INIValue
100 : {
101 8 : INIValue(const char* aKey, const char* aValue)
102 8 : : key(aKey)
103 8 : , value(aValue)
104 : {
105 8 : }
106 :
107 : const char* key;
108 : const char* value;
109 : mozilla::UniquePtr<INIValue> next;
110 : };
111 :
112 : nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
113 : mozilla::UniquePtr<char[]> mFileContents;
114 :
115 : nsresult InitFromFILE(FILE* aFd);
116 : };
117 :
118 : #endif /* nsINIParser_h__ */
|