1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/KeyValueParser.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,49 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.9 + 1.10 +this.EXPORTED_SYMBOLS = [ 1.11 + "parseKeyValuePairs", 1.12 + "parseKeyValuePairsFromFile" 1.13 +]; 1.14 + 1.15 +const Cc = Components.classes; 1.16 +const Ci = Components.interfaces; 1.17 + 1.18 +this.parseKeyValuePairs = function parseKeyValuePairs(text) { 1.19 + let lines = text.split('\n'); 1.20 + let data = {}; 1.21 + for (let i = 0; i < lines.length; i++) { 1.22 + if (lines[i] == '') 1.23 + continue; 1.24 + 1.25 + // can't just .split() because the value might contain = characters 1.26 + let eq = lines[i].indexOf('='); 1.27 + if (eq != -1) { 1.28 + let [key, value] = [lines[i].substring(0, eq), 1.29 + lines[i].substring(eq + 1)]; 1.30 + if (key && value) 1.31 + data[key] = value.replace(/\\n/g, "\n").replace(/\\\\/g, "\\"); 1.32 + } 1.33 + } 1.34 + return data; 1.35 +} 1.36 + 1.37 +this.parseKeyValuePairsFromFile = function parseKeyValuePairsFromFile(file) { 1.38 + let fstream = Cc["@mozilla.org/network/file-input-stream;1"]. 1.39 + createInstance(Ci.nsIFileInputStream); 1.40 + fstream.init(file, -1, 0, 0); 1.41 + let is = Cc["@mozilla.org/intl/converter-input-stream;1"]. 1.42 + createInstance(Ci.nsIConverterInputStream); 1.43 + is.init(fstream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); 1.44 + let str = {}; 1.45 + let contents = ''; 1.46 + while (is.readString(4096, str) != 0) { 1.47 + contents += str.value; 1.48 + } 1.49 + is.close(); 1.50 + fstream.close(); 1.51 + return parseKeyValuePairs(contents); 1.52 +}