1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 Components.utils.import("resource://gre/modules/Services.jsm");
7 this.EXPORTED_SYMBOLS = [
9 "parseKeyValuePairsFromFile"
12 const Cc = Components.classes;
13 const Ci = Components.interfaces;
15 this.parseKeyValuePairs = function parseKeyValuePairs(text) {
16 let lines = text.split('\n');
18 for (let i = 0; i < lines.length; i++) {
22 // can't just .split() because the value might contain = characters
23 let eq = lines[i].indexOf('=');
25 let [key, value] = [lines[i].substring(0, eq),
26 lines[i].substring(eq + 1)];
28 data[key] = value.replace(/\\n/g, "\n").replace(/\\\\/g, "\\");
34 this.parseKeyValuePairsFromFile = function parseKeyValuePairsFromFile(file) {
35 let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
36 createInstance(Ci.nsIFileInputStream);
37 fstream.init(file, -1, 0, 0);
38 let is = Cc["@mozilla.org/intl/converter-input-stream;1"].
39 createInstance(Ci.nsIConverterInputStream);
40 is.init(fstream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
43 while (is.readString(4096, str) != 0) {
44 contents += str.value;
48 return parseKeyValuePairs(contents);