From 1dfb56bc243ddf288c9b46c98bbddd5d950670c2 Mon Sep 17 00:00:00 2001 From: ketmar Date: Sun, 9 Aug 2015 13:43:14 +0000 Subject: [PATCH] moved some functions from "basic.js" to "utils.js" FossilOrigin-Name: 4586f745c411d6beb6586a9d8115c351e0bdf40b827634678a5bffc644163b24 --- basic.js | 48 --------------------------------------------- includes/utils.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/basic.js b/basic.js index d230f82..f52b2d0 100644 --- a/basic.js +++ b/basic.js @@ -32,7 +32,6 @@ Cu.import("resource://gre/modules/Services.jsm", this); //////////////////////////////////////////////////////////////////////////////// const isXPCShell = false; -//let srvPrefs = Services.prefs; //////////////////////////////////////////////////////////////////////////////// @@ -100,12 +99,6 @@ function logException (msg, e) { const Cu = global.Cu; Cu.import("resource://gre/modules/Services.jsm", scp); - let loc = null; - global.getLocale = function () { - if (loc === null) loc = global.Cc["@mozilla.org/chrome/chrome-registry;1"].getService(global.Ci.nsIXULChromeRegistry).getSelectedLocale("global"); - return loc; - }; - // list of symbols exported to modules let exportList = {}; @@ -142,7 +135,6 @@ function logException (msg, e) { addExport("Ci"); addExport("Cr"); addExport("Services"); - //addExport("srvPrefs"); addExport("logException"); addExport("logError"); @@ -180,7 +172,6 @@ function logException (msg, e) { return scope.exports; }; - addExport("getLocale"); addExport("addExport"); addExport("require"); @@ -190,14 +181,6 @@ function logException (msg, e) { }; addExport("alert"); - let dirSvcProps = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); - - // get profile directory - global.getProfileDir = function () { - return dirSvcProps.get("ProfD", Ci.nsIFile); - }; - addExport("getProfileDir"); - global.hitch = function (obj, method) { if (obj && method && typeof(method) == "string") { if (!obj[method]) throw new Error("method ["+method+"] doesn't exist in object ["+obj+"]"); @@ -218,35 +201,4 @@ function logException (msg, e) { }; }; addExport("hitch"); - - global.fileReadText = function (file, charset) { - if (typeof(charset) !== "string") charset = "UTF-8"; - charset = charset||"UTF-8"; - let inputStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); - inputStream.init(file, -1, -1, null); - let scInputStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream); - scInputStream.init(inputStream); - let output = scInputStream.read(-1); - scInputStream.close(); - inputStream.close(); - let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter); - converter.charset = charset; - let res = converter.ConvertToUnicode(output); - if (typeof(res) != "string") throw new Error("invalid file '"+file.path+"'"); - // fuck BOM - if (charset == "UTF-8" && res.length >= 3 && res.substr(0, 3) == "\u00EF\u00BB\u00BF") res = res.substr(3); - return res; - }; - addExport("fileReadText"); - - global.fileReadBinary = function (fl) { - let istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); - istream.init(fl, -1, -1, false); - let bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream); - bstream.setInputStream(istream); - let bytes = bstream.readBytes(bstream.available()); - bstream.close(); - return bytes; - }; - addExport("fileReadBinary"); })(this); diff --git a/includes/utils.js b/includes/utils.js index 39f5648..6c66ed7 100644 --- a/includes/utils.js +++ b/includes/utils.js @@ -26,6 +26,64 @@ * copies or substantial portions of the Software. */ //////////////////////////////////////////////////////////////////////////////// +// getLocale () +(function (global) { + let loc = null; + global.getLocale = function () { + if (loc === null) loc = global.Cc["@mozilla.org/chrome/chrome-registry;1"].getService(global.Ci.nsIXULChromeRegistry).getSelectedLocale("global"); + return loc; + }; + addExport("getLocale", global.getLocale); +})(this); + + +//////////////////////////////////////////////////////////////////////////////// +function fileReadText (file, charset) { + if (typeof(charset) !== "string") charset = "UTF-8"; + charset = charset||"UTF-8"; + let inputStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); + inputStream.init(file, -1, -1, null); + let scInputStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream); + scInputStream.init(inputStream); + let output = scInputStream.read(-1); + scInputStream.close(); + inputStream.close(); + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter); + converter.charset = charset; + let res = converter.ConvertToUnicode(output); + if (typeof(res) != "string") throw new Error("invalid file '"+file.path+"'"); + // fuck BOM + if (charset == "UTF-8" && res.length >= 3 && res.substr(0, 3) == "\u00EF\u00BB\u00BF") res = res.substr(3); + return res; +} +addExport("fileReadText", fileReadText); + + +//////////////////////////////////////////////////////////////////////////////// +function fileReadBinary (fl) { + let istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); + istream.init(fl, -1, -1, false); + let bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream); + bstream.setInputStream(istream); + let bytes = bstream.readBytes(bstream.available()); + bstream.close(); + return bytes; +} +addExport("fileReadBinary", fileReadBinary); + + +//////////////////////////////////////////////////////////////////////////////// +// getProfileDir() +(function (global) { + let dirSvcProps = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); + + // get profile directory + global.getProfileDir = function () dirSvcProps.get("ProfD", Ci.nsIFile); + addExport("getProfileDir", global.getProfileDir); +})(this); + + +//////////////////////////////////////////////////////////////////////////////// // string aString: A string of data to be hashed. // string aAlg: optional; the hash algorithm to be used; possible values are: MD2, MD5, SHA1, SHA256, SHA384, and SHA512; defaults to SHA1 // string aCharset: optional; the charset used by the passed string; defaults to UTF-8 -- 2.11.4.GIT