xpcom/glue/nsINIParser.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/nsINIParser.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +// This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2
    1.10 +
    1.11 +#ifndef nsINIParser_h__
    1.12 +#define nsINIParser_h__
    1.13 +
    1.14 +#ifdef MOZILLA_INTERNAL_API
    1.15 +#define nsINIParser nsINIParser_internal
    1.16 +#endif
    1.17 +
    1.18 +#include "nscore.h"
    1.19 +#include "nsClassHashtable.h"
    1.20 +#include "nsAutoPtr.h"
    1.21 +
    1.22 +#include <stdio.h>
    1.23 +
    1.24 +class nsIFile;
    1.25 +
    1.26 +class NS_COM_GLUE nsINIParser
    1.27 +{
    1.28 +public:
    1.29 +    nsINIParser() { }
    1.30 +    ~nsINIParser() { }
    1.31 +
    1.32 +    /**
    1.33 +     * Initialize the INIParser with a nsIFile. If this method fails, no
    1.34 +     * other methods should be called. This method reads and parses the file,
    1.35 +     * the class does not hold a file handle open. An instance must only be
    1.36 +     * initialized once.
    1.37 +     */
    1.38 +    nsresult Init(nsIFile* aFile);
    1.39 +
    1.40 +    /**
    1.41 +     * Initialize the INIParser with a file path. If this method fails, no
    1.42 +     * other methods should be called. This method reads and parses the file,
    1.43 +     * the class does not hold a file handle open. An instance must only
    1.44 +     * be initialized once.
    1.45 +     */
    1.46 +    nsresult Init(const char *aPath);
    1.47 +
    1.48 +    /**
    1.49 +     * Callback for GetSections
    1.50 +     * @return false to stop enumeration, or true to continue.
    1.51 +     */
    1.52 +    typedef bool
    1.53 +    (* INISectionCallback)(const char *aSection, void *aClosure);
    1.54 +
    1.55 +    /**
    1.56 +     * Enumerate the sections within the INI file.
    1.57 +     */
    1.58 +    nsresult GetSections(INISectionCallback aCB, void *aClosure);
    1.59 +
    1.60 +    /**
    1.61 +     * Callback for GetStrings
    1.62 +     * @return false to stop enumeration, or true to continue
    1.63 +     */
    1.64 +    typedef bool
    1.65 +    (* INIStringCallback)(const char *aString, const char *aValue,
    1.66 +                          void *aClosure);
    1.67 +
    1.68 +    /**
    1.69 +     * Enumerate the strings within a section. If the section does
    1.70 +     * not exist, this function will silently return.
    1.71 +     */
    1.72 +    nsresult GetStrings(const char *aSection,
    1.73 +                        INIStringCallback aCB, void *aClosure);
    1.74 +
    1.75 +    /**
    1.76 +     * Get the value of the specified key in the specified section
    1.77 +     * of the INI file represented by this instance.
    1.78 +     *
    1.79 +     * @param aSection      section name
    1.80 +     * @param aKey          key name
    1.81 +     * @param aResult       the value found
    1.82 +     * @throws NS_ERROR_FAILURE if the specified section/key could not be
    1.83 +     *                          found.
    1.84 +     */
    1.85 +    nsresult GetString(const char *aSection, const char *aKey, 
    1.86 +                       nsACString &aResult);
    1.87 +
    1.88 +    /**
    1.89 +     * Alternate signature of GetString that uses a pre-allocated buffer
    1.90 +     * instead of a nsACString (for use in the standalone glue before
    1.91 +     * the glue is initialized).
    1.92 +     *
    1.93 +     * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
    1.94 +     *         large enough for the data. aResult will be filled with as
    1.95 +     *         much data as possible.
    1.96 +     *         
    1.97 +     * @see GetString [1]
    1.98 +     */
    1.99 +    nsresult GetString(const char *aSection, const char* aKey,
   1.100 +                       char *aResult, uint32_t aResultLen);
   1.101 +
   1.102 +private:
   1.103 +    struct INIValue
   1.104 +    {
   1.105 +        INIValue(const char *aKey, const char *aValue)
   1.106 +            : key(aKey), value(aValue) { }
   1.107 +
   1.108 +        const char *key;
   1.109 +        const char *value;
   1.110 +        nsAutoPtr<INIValue> next;
   1.111 +    };
   1.112 +
   1.113 +    struct GSClosureStruct
   1.114 +    {
   1.115 +        INISectionCallback  usercb;
   1.116 +        void               *userclosure;
   1.117 +    };
   1.118 +
   1.119 +    nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
   1.120 +    nsAutoArrayPtr<char> mFileContents;    
   1.121 +
   1.122 +    nsresult InitFromFILE(FILE *fd);
   1.123 +
   1.124 +    static PLDHashOperator GetSectionsCB(const char *aKey,
   1.125 +                                         INIValue *aData, void *aClosure);
   1.126 +};
   1.127 +
   1.128 +#endif /* nsINIParser_h__ */

mercurial