Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/nsINIParser.h
Line
Count
Source (jump to first uncovered line)
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
0
#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
0
  nsINIParser() {}
28
0
  ~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
   * Callback for GetSections
40
   * @return false to stop enumeration, or true to continue.
41
   */
42
  typedef bool (*INISectionCallback)(const char* aSection, void* aClosure);
43
44
  /**
45
   * Enumerate the sections within the INI file.
46
   */
47
  nsresult GetSections(INISectionCallback aCB, void* aClosure);
48
49
  /**
50
   * Callback for GetStrings
51
   * @return false to stop enumeration, or true to continue
52
   */
53
  typedef bool (*INIStringCallback)(const char* aString, const char* aValue,
54
                                    void* aClosure);
55
56
  /**
57
   * Enumerate the strings within a section. If the section does
58
   * not exist, this function will silently return.
59
   */
60
  nsresult GetStrings(const char* aSection,
61
                      INIStringCallback aCB, void* aClosure);
62
63
  /**
64
   * Get the value of the specified key in the specified section
65
   * of the INI file represented by this instance.
66
   *
67
   * @param aSection      section name
68
   * @param aKey          key name
69
   * @param aResult       the value found
70
   * @throws NS_ERROR_FAILURE if the specified section/key could not be
71
   *                          found.
72
   */
73
  nsresult GetString(const char* aSection, const char* aKey,
74
                     nsACString& aResult);
75
76
  /**
77
   * Alternate signature of GetString that uses a pre-allocated buffer
78
   * instead of a nsACString (for use in the standalone glue before
79
   * the glue is initialized).
80
   *
81
   * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
82
   *         large enough for the data. aResult will be filled with as
83
   *         much data as possible.
84
   *
85
   * @see GetString [1]
86
   */
87
  nsresult GetString(const char* aSection, const char* aKey,
88
                     char* aResult, uint32_t aResultLen);
89
90
  /**
91
   * Sets the value of the specified key in the specified section. The section
92
   * is created if it does not already exist.
93
   *
94
   * @oaram aSection      section name
95
   * @param aKey          key name
96
   * @param aValue        the value to set
97
   */
98
  nsresult SetString(const char* aSection, const char* aKey, const char* aValue);
99
100
  /**
101
   * Deletes the value of the specified key in the specified section.
102
   *
103
   * @param aSection      section name
104
   * @param aKey          key name
105
   *
106
   * @throws NS_ERROR_FAILURE if the string was not set.
107
   */
108
  nsresult DeleteString(const char* aSection, const char* aKey);
109
110
  /**
111
   * Deletes the specified section.
112
   *
113
   * @param aSection      section name
114
   *
115
   * @throws NS_ERROR_FAILURE if the section did not exist.
116
   */
117
  nsresult DeleteSection(const char* aSection);
118
119
  /**
120
   * Writes the ini data to disk.
121
   * @param aFile         the file to write to
122
   * @throws NS_ERROR_FAILURE on failure.
123
   */
124
  nsresult WriteToFile(nsIFile *aFile);
125
126
private:
127
  struct INIValue
128
  {
129
    INIValue(const char* aKey, const char* aValue)
130
      : key(strdup(aKey))
131
      , value(strdup(aValue))
132
    {
133
    }
134
135
    ~INIValue()
136
    {
137
      delete key;
138
      delete value;
139
    }
140
141
    void SetValue(const char* aValue) {
142
      delete value;
143
      value = strdup(aValue);
144
    }
145
146
    const char* key;
147
    const char* value;
148
    mozilla::UniquePtr<INIValue> next;
149
  };
150
151
  nsClassHashtable<nsCharPtrHashKey, INIValue> mSections;
152
153
  nsresult InitFromString(const nsCString& aStr);
154
155
  bool IsValidSection(const char* aSection);
156
  bool IsValidKey(const char* aKey);
157
  bool IsValidValue(const char* aValue);
158
};
159
160
#endif /* nsINIParser_h__ */