michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "parseKeyValuePairs", michael@0: "parseKeyValuePairsFromFile" michael@0: ]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: this.parseKeyValuePairs = function parseKeyValuePairs(text) { michael@0: let lines = text.split('\n'); michael@0: let data = {}; michael@0: for (let i = 0; i < lines.length; i++) { michael@0: if (lines[i] == '') michael@0: continue; michael@0: michael@0: // can't just .split() because the value might contain = characters michael@0: let eq = lines[i].indexOf('='); michael@0: if (eq != -1) { michael@0: let [key, value] = [lines[i].substring(0, eq), michael@0: lines[i].substring(eq + 1)]; michael@0: if (key && value) michael@0: data[key] = value.replace(/\\n/g, "\n").replace(/\\\\/g, "\\"); michael@0: } michael@0: } michael@0: return data; michael@0: } michael@0: michael@0: this.parseKeyValuePairsFromFile = function parseKeyValuePairsFromFile(file) { michael@0: let fstream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: fstream.init(file, -1, 0, 0); michael@0: let is = Cc["@mozilla.org/intl/converter-input-stream;1"]. michael@0: createInstance(Ci.nsIConverterInputStream); michael@0: is.init(fstream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); michael@0: let str = {}; michael@0: let contents = ''; michael@0: while (is.readString(4096, str) != 0) { michael@0: contents += str.value; michael@0: } michael@0: is.close(); michael@0: fstream.close(); michael@0: return parseKeyValuePairs(contents); michael@0: }