Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/ds/nsINIParserImpl.cpp
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
#include "nsINIParserImpl.h"
8
9
#include "nsINIParser.h"
10
#include "nsStringEnumerator.h"
11
#include "nsTArray.h"
12
#include "mozilla/Attributes.h"
13
14
class nsINIParserImpl final
15
  : public nsIINIParser
16
  , public nsIINIParserWriter
17
{
18
0
  ~nsINIParserImpl() {}
19
20
public:
21
  NS_DECL_ISUPPORTS
22
  NS_DECL_NSIINIPARSER
23
  NS_DECL_NSIINIPARSERWRITER
24
25
0
  nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
26
27
private:
28
  nsINIParser mParser;
29
  bool ContainsNull(const nsACString& aStr);
30
};
31
32
NS_IMPL_ISUPPORTS(nsINIParserFactory,
33
                  nsIINIParserFactory,
34
                  nsIFactory)
35
36
NS_IMETHODIMP
37
nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
38
                                    nsIINIParser** aResult)
39
0
{
40
0
  *aResult = nullptr;
41
0
42
0
  RefPtr<nsINIParserImpl> p(new nsINIParserImpl());
43
0
  if (!p) {
44
0
    return NS_ERROR_OUT_OF_MEMORY;
45
0
  }
46
0
47
0
  if (aINIFile) {
48
0
    nsresult rv = p->Init(aINIFile);
49
0
    if (NS_FAILED(rv)) {
50
0
      return rv;
51
0
    }
52
0
  }
53
0
54
0
   p.forget(aResult);
55
0
  return NS_OK;
56
0
}
57
58
NS_IMETHODIMP
59
nsINIParserFactory::CreateInstance(nsISupports* aOuter,
60
                                   REFNSIID aIID,
61
                                   void** aResult)
62
0
{
63
0
  if (NS_WARN_IF(aOuter)) {
64
0
    return NS_ERROR_NO_AGGREGATION;
65
0
  }
66
0
67
0
  // We are our own singleton.
68
0
  return QueryInterface(aIID, aResult);
69
0
}
70
71
NS_IMETHODIMP
72
nsINIParserFactory::LockFactory(bool aLock)
73
0
{
74
0
  return NS_OK;
75
0
}
76
77
NS_IMPL_ISUPPORTS(nsINIParserImpl,
78
                  nsIINIParser,
79
                  nsIINIParserWriter)
80
81
bool
82
0
nsINIParserImpl::ContainsNull(const nsACString& aStr) {
83
0
  return aStr.CountChar('\0') > 0;
84
0
}
85
86
static bool
87
SectionCB(const char* aSection, void* aClosure)
88
0
{
89
0
  nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
90
0
  strings->AppendElement()->Assign(aSection);
91
0
  return true;
92
0
}
93
94
NS_IMETHODIMP
95
nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
96
0
{
97
0
  nsTArray<nsCString>* strings = new nsTArray<nsCString>;
98
0
  if (!strings) {
99
0
    return NS_ERROR_OUT_OF_MEMORY;
100
0
  }
101
0
102
0
  nsresult rv = mParser.GetSections(SectionCB, strings);
103
0
  if (NS_SUCCEEDED(rv)) {
104
0
    rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
105
0
  }
106
0
107
0
  if (NS_FAILED(rv)) {
108
0
    delete strings;
109
0
  }
110
0
111
0
  return rv;
112
0
}
113
114
static bool
115
KeyCB(const char* aKey, const char* aValue, void* aClosure)
116
0
{
117
0
  nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
118
0
  strings->AppendElement()->Assign(aKey);
119
0
  return true;
120
0
}
121
122
NS_IMETHODIMP
123
nsINIParserImpl::GetKeys(const nsACString& aSection,
124
                         nsIUTF8StringEnumerator** aResult)
125
0
{
126
0
  if (ContainsNull(aSection)) {
127
0
    return NS_ERROR_INVALID_ARG;
128
0
  }
129
0
130
0
  nsTArray<nsCString>* strings = new nsTArray<nsCString>;
131
0
  if (!strings) {
132
0
    return NS_ERROR_OUT_OF_MEMORY;
133
0
  }
134
0
135
0
  nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
136
0
                                   KeyCB, strings);
137
0
  if (NS_SUCCEEDED(rv)) {
138
0
    rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
139
0
  }
140
0
141
0
  if (NS_FAILED(rv)) {
142
0
    delete strings;
143
0
  }
144
0
145
0
  return rv;
146
0
147
0
}
148
149
NS_IMETHODIMP
150
nsINIParserImpl::GetString(const nsACString& aSection,
151
                           const nsACString& aKey,
152
                           nsACString& aResult)
153
0
{
154
0
  if (ContainsNull(aSection) || ContainsNull(aKey)) {
155
0
    return NS_ERROR_INVALID_ARG;
156
0
  }
157
0
158
0
  return mParser.GetString(PromiseFlatCString(aSection).get(),
159
0
                           PromiseFlatCString(aKey).get(),
160
0
                           aResult);
161
0
}
162
163
NS_IMETHODIMP
164
nsINIParserImpl::SetString(const nsACString& aSection,
165
                           const nsACString& aKey,
166
                           const nsACString& aValue)
167
0
{
168
0
  if (ContainsNull(aSection) || ContainsNull(aKey) || ContainsNull(aValue)) {
169
0
    return NS_ERROR_INVALID_ARG;
170
0
  }
171
0
172
0
  return mParser.SetString(PromiseFlatCString(aSection).get(),
173
0
                           PromiseFlatCString(aKey).get(),
174
0
                           PromiseFlatCString(aValue).get());
175
0
}
176
177
NS_IMETHODIMP
178
nsINIParserImpl::WriteFile(nsIFile* aINIFile)
179
0
{
180
0
  return mParser.WriteToFile(aINIFile);
181
0
}