1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2010
18 * the Initial Developer. All Rights Reserved.
21 * Justin Dolske <dolske@mozilla.com> (original author)
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 const Cc = Components.classes;
39 const Ci = Components.interfaces;
40 const Cr = Components.results;
41 const Cu = Components.utils;
43 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
45 function INIProcessorFactory() {
48 INIProcessorFactory.prototype = {
49 classDescription: "INIProcessorFactory",
50 contractID: "@mozilla.org/xpcom/ini-processor-factory;1",
51 classID: Components.ID("{6ec5f479-8e13-4403-b6ca-fe4c2dca14fd}"),
52 QueryInterface : XPCOMUtils.generateQI([Ci.nsIINIParserFactory]),
54 createINIParser : function (aINIFile) {
55 return new INIProcessor(aINIFile);
58 }; // end of INIProcessorFactory implementation
60 const MODE_WRONLY = 0x02;
61 const MODE_CREATE = 0x08;
62 const MODE_TRUNCATE = 0x20;
64 // nsIINIParser implementation
65 function INIProcessor(aFile) {
66 this._iniFile = aFile;
71 INIProcessor.prototype = {
72 QueryInterface : XPCOMUtils.generateQI([Ci.nsIINIParser, Ci.nsIINIParserWriter]),
74 __utfConverter : null, // UCS2 <--> UTF8 string conversion
76 if (!this.__utfConverter) {
77 this.__utfConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
78 createInstance(Ci.nsIScriptableUnicodeConverter);
79 this.__utfConverter.charset = "UTF-8";
81 return this.__utfConverter;
84 _utfConverterReset : function() {
85 this.__utfConverter = null;
92 * Reads the INI file and stores the data internally.
94 _readFile : function() {
95 // If file doesn't exist, there's nothing to do.
96 if (!this._iniFile.exists() || 0 == this._iniFile.fileSize)
99 let iniParser = Cc["@mozilla.org/xpcom/ini-parser-factory;1"]
100 .getService(Ci.nsIINIParserFactory).createINIParser(this._iniFile);
101 for (let section in XPCOMUtils.IterStringEnumerator(iniParser.getSections())) {
102 this._iniData[section] = {};
103 for (let key in XPCOMUtils.IterStringEnumerator(iniParser.getKeys(section))) {
104 this._iniData[section][key] = iniParser.getString(section, key);
111 getSections : function() {
113 for (let section in this._iniData)
114 sections.push(section);
115 return new stringEnumerator(sections);
118 getKeys : function(aSection) {
120 if (aSection in this._iniData)
121 for (let key in this._iniData[aSection])
123 return new stringEnumerator(keys);
126 getString : function(aSection, aKey) {
127 if (!(aSection in this._iniData))
128 throw Cr.NS_ERROR_FAILURE;
129 if (!(aKey in this._iniData[aSection]))
130 throw Cr.NS_ERROR_FAILURE;
131 return this._iniData[aSection][aKey];
135 // nsIINIParserWriter
137 setString : function(aSection, aKey, aValue) {
138 const isSectionIllegal = /[\0\r\n\[\]]/;
139 const isKeyValIllegal = /[\0\r\n=]/;
141 if (isSectionIllegal.test(aSection))
142 throw Components.Exception("bad character in section name",
143 Cr.ERROR_ILLEGAL_VALUE);
144 if (isKeyValIllegal.test(aKey) || isKeyValIllegal.test(aValue))
145 throw Components.Exception("bad character in key/value",
146 Cr.ERROR_ILLEGAL_VALUE);
148 if (!(aSection in this._iniData))
149 this._iniData[aSection] = {};
151 this._iniData[aSection][aKey] = aValue;
154 writeFile : function(aFile) {
156 let converter = this._utfConverter;
157 function writeLine(data) {
158 data = converter.ConvertFromUnicode(data);
159 data += converter.Finish();
161 outputStream.write(data, data.length);
165 aFile = this._iniFile;
167 let safeStream = Cc["@mozilla.org/network/safe-file-output-stream;1"].
168 createInstance(Ci.nsIFileOutputStream);
169 safeStream.init(aFile, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE,
172 var outputStream = Cc["@mozilla.org/network/buffered-output-stream;1"].
173 createInstance(Ci.nsIBufferedOutputStream);
174 outputStream.init(safeStream, 8192);
175 outputStream.QueryInterface(Ci.nsISafeOutputStream); // for .finish()
177 for (let section in this._iniData) {
178 writeLine("[" + section + "]");
179 for (let key in this._iniData[section]) {
180 writeLine(key + "=" + this._iniData[section][key]);
184 outputStream.finish();
188 function stringEnumerator(stringArray) {
189 this._strings = stringArray;
191 stringEnumerator.prototype = {
192 QueryInterface : XPCOMUtils.generateQI([Ci.nsIUTF8StringEnumerator]),
197 hasMore : function() {
198 return (this._enumIndex < this._strings.length);
201 getNext : function() {
202 return this._strings[this._enumIndex++];
206 let component = [INIProcessorFactory];
207 function NSGetModule (compMgr, fileSpec) {
208 return XPCOMUtils.generateModule(component);