michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2 michael@0: michael@0: #ifndef nsINIParser_h__ michael@0: #define nsINIParser_h__ michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: #define nsINIParser nsINIParser_internal michael@0: #endif michael@0: michael@0: #include "nscore.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: #include michael@0: michael@0: class nsIFile; michael@0: michael@0: class NS_COM_GLUE nsINIParser michael@0: { michael@0: public: michael@0: nsINIParser() { } michael@0: ~nsINIParser() { } michael@0: michael@0: /** michael@0: * Initialize the INIParser with a nsIFile. If this method fails, no michael@0: * other methods should be called. This method reads and parses the file, michael@0: * the class does not hold a file handle open. An instance must only be michael@0: * initialized once. michael@0: */ michael@0: nsresult Init(nsIFile* aFile); michael@0: michael@0: /** michael@0: * Initialize the INIParser with a file path. If this method fails, no michael@0: * other methods should be called. This method reads and parses the file, michael@0: * the class does not hold a file handle open. An instance must only michael@0: * be initialized once. michael@0: */ michael@0: nsresult Init(const char *aPath); michael@0: michael@0: /** michael@0: * Callback for GetSections michael@0: * @return false to stop enumeration, or true to continue. michael@0: */ michael@0: typedef bool michael@0: (* INISectionCallback)(const char *aSection, void *aClosure); michael@0: michael@0: /** michael@0: * Enumerate the sections within the INI file. michael@0: */ michael@0: nsresult GetSections(INISectionCallback aCB, void *aClosure); michael@0: michael@0: /** michael@0: * Callback for GetStrings michael@0: * @return false to stop enumeration, or true to continue michael@0: */ michael@0: typedef bool michael@0: (* INIStringCallback)(const char *aString, const char *aValue, michael@0: void *aClosure); michael@0: michael@0: /** michael@0: * Enumerate the strings within a section. If the section does michael@0: * not exist, this function will silently return. michael@0: */ michael@0: nsresult GetStrings(const char *aSection, michael@0: INIStringCallback aCB, void *aClosure); michael@0: michael@0: /** michael@0: * Get the value of the specified key in the specified section michael@0: * of the INI file represented by this instance. michael@0: * michael@0: * @param aSection section name michael@0: * @param aKey key name michael@0: * @param aResult the value found michael@0: * @throws NS_ERROR_FAILURE if the specified section/key could not be michael@0: * found. michael@0: */ michael@0: nsresult GetString(const char *aSection, const char *aKey, michael@0: nsACString &aResult); michael@0: michael@0: /** michael@0: * Alternate signature of GetString that uses a pre-allocated buffer michael@0: * instead of a nsACString (for use in the standalone glue before michael@0: * the glue is initialized). michael@0: * michael@0: * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not michael@0: * large enough for the data. aResult will be filled with as michael@0: * much data as possible. michael@0: * michael@0: * @see GetString [1] michael@0: */ michael@0: nsresult GetString(const char *aSection, const char* aKey, michael@0: char *aResult, uint32_t aResultLen); michael@0: michael@0: private: michael@0: struct INIValue michael@0: { michael@0: INIValue(const char *aKey, const char *aValue) michael@0: : key(aKey), value(aValue) { } michael@0: michael@0: const char *key; michael@0: const char *value; michael@0: nsAutoPtr next; michael@0: }; michael@0: michael@0: struct GSClosureStruct michael@0: { michael@0: INISectionCallback usercb; michael@0: void *userclosure; michael@0: }; michael@0: michael@0: nsClassHashtable mSections; michael@0: nsAutoArrayPtr mFileContents; michael@0: michael@0: nsresult InitFromFILE(FILE *fd); michael@0: michael@0: static PLDHashOperator GetSectionsCB(const char *aKey, michael@0: INIValue *aData, void *aClosure); michael@0: }; michael@0: michael@0: #endif /* nsINIParser_h__ */