PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/gecko_api/include/nsINIParser.h

http://firefox-mac-pdf.googlecode.com/
C++ Header | 162 lines | 52 code | 22 blank | 88 comment | 0 complexity | d0b3eb5f8ebf8611732e96fde17ec31d MD5 | raw file
  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
 56class nsILocalFile;
 57
 58class NS_COM_GLUE nsINIParser
 59{
 60public:
 61    nsINIParser() { }
 62    ~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 PR_FALSE to stop enumeration, or PR_TRUE to continue.
 83     */
 84    typedef PRBool
 85    (* PR_CALLBACK INISectionCallback)(const char *aSection,
 86                                       void *aClosure);
 87
 88    /**
 89     * Enumerate the sections within the INI file.
 90     */
 91    nsresult GetSections(INISectionCallback aCB, void *aClosure);
 92
 93    /**
 94     * Callback for GetStrings
 95     * @return PR_FALSE to stop enumeration, or PR_TRUE to continue
 96     */
 97    typedef PRBool
 98    (* PR_CALLBACK INIStringCallback)(const char *aString,
 99                                      const char *aValue,
100                                      void *aClosure);
101
102    /**
103     * Enumerate the strings within a section. If the section does
104     * not exist, this function will silently return.
105     */
106    nsresult GetStrings(const char *aSection,
107                        INIStringCallback aCB, void *aClosure);
108
109    /**
110     * Get the value of the specified key in the specified section
111     * of the INI file represented by this instance.
112     *
113     * @param aSection      section name
114     * @param aKey          key name
115     * @param aResult       the value found
116     * @throws NS_ERROR_FAILURE if the specified section/key could not be
117     *                          found.
118     */
119    nsresult GetString(const char *aSection, const char *aKey, 
120                       nsACString &aResult);
121
122    /**
123     * Alternate signature of GetString that uses a pre-allocated buffer
124     * instead of a nsACString (for use in the standalone glue before
125     * the glue is initialized).
126     *
127     * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
128     *         large enough for the data. aResult will be filled with as
129     *         much data as possible.
130     *         
131     * @see GetString [1]
132     */
133    nsresult GetString(const char *aSection, const char* aKey,
134                       char *aResult, PRUint32 aResultLen);
135
136private:
137    struct INIValue
138    {
139        INIValue(const char *aKey, const char *aValue)
140            : key(aKey), value(aValue) { }
141
142        const char *key;
143        const char *value;
144        nsAutoPtr<INIValue> next;
145    };
146
147    struct GSClosureStruct
148    {
149        INISectionCallback  usercb;
150        void               *userclosure;
151    };
152
153    nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
154    nsAutoArrayPtr<char> mFileContents;    
155
156    nsresult InitFromFILE(FILE *fd);
157
158    static PLDHashOperator GetSectionsCB(const char *aKey,
159                                         INIValue *aData, void *aClosure);
160};
161
162#endif /* nsINIParser_h__ */