From b7acf9fab6d78a370b598681e82afea544194309 Mon Sep 17 00:00:00 2001 From: ketmar Date: Fri, 7 Aug 2015 13:54:28 +0000 Subject: [PATCH] moved some utility functions to "basic.js" FossilOrigin-Name: bf9a8e78c13714055e0aeabc817b1e5cdea21b9e51555683f925e47a98358254 --- includes/basic.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ includes/utils.js | 62 +------------------------------------------------- modules/concmd.js | 2 +- modules/scriptcache.js | 20 ---------------- 4 files changed, 57 insertions(+), 82 deletions(-) diff --git a/includes/basic.js b/includes/basic.js index 5fac87e..daa4393 100644 --- a/includes/basic.js +++ b/includes/basic.js @@ -164,4 +164,59 @@ function logException (msg, e) { addExport("getLocale", global.getLocale); addExport("addExport", global.addExport); addExport("require", global.require); + + global.alert = function (str) { + let ps = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService); + ps.alert(null, "Guerilla", ""+str); + }; + addExport("alert", global.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.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+"]"); + method = obj[method]; + } else if (typeof(method) == "function") { + obj = obj||{}; + } else { + throw new Error("invalid arguments to hitch()"); + } + let stargs = Array.prototype.splice.call(arguments, 2, arguments.length); + return function() { + // make a copy of stargs (don't modify it because it gets reused for every invocation) + let args = Array.prototype.slice.call(stargs); + // add all the new arguments + Array.prototype.push.apply(args, arguments); + // invoke the original function with the correct this obj and the combined list of static and dynamic arguments + return method.apply(obj, args); + }; + }; + addExport("hitch", global.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.fileReadText); })(this); diff --git a/includes/utils.js b/includes/utils.js index 3644a77..c4f74fa 100644 --- a/includes/utils.js +++ b/includes/utils.js @@ -26,48 +26,6 @@ * copies or substantial portions of the Software. */ //////////////////////////////////////////////////////////////////////////////// -function alert (str) { - let ps = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService); - ps.alert(null, "Guerilla", ""+str); -} -addExport("alert", alert); - - -//////////////////////////////////////////////////////////////////////////////// -let dirSvcProps = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); - - -// get profile directory -function getProfileDir () { - return dirSvcProps.get("ProfD", Ci.nsIFile); -} -addExport("getProfileDir", getProfileDir); - - -//////////////////////////////////////////////////////////////////////////////// -function hitch (obj, method) { - if (obj && method && typeof(method) == "string") { - if (!obj[method]) throw new Error("method ["+method+"] doesn't exist in object ["+obj+"]"); - method = obj[method]; - } else if (typeof(method) == "function") { - obj = obj||{}; - } else { - throw new Error("invalid arguments to hitch()"); - } - let stargs = Array.prototype.splice.call(arguments, 2, arguments.length); - return function() { - // make a copy of stargs (don't modify it because it gets reused for every invocation) - let args = Array.prototype.slice.call(stargs); - // add all the new arguments - Array.prototype.push.apply(args, arguments); - // invoke the original function with the correct this obj and the combined list of static and dynamic arguments - return method.apply(obj, args); - }; -} -addExport("hitch", hitch); - - -//////////////////////////////////////////////////////////////////////////////// // 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 @@ -118,22 +76,4 @@ function calcFileSha512 (fname) { let hash = chan.finish(false); return [("0"+hash.charCodeAt(i).toString(16)).slice(-2) for (i in hash)].join(""); } - - -//////////////////////////////////////////////////////////////////////////////// -function fileReadText (file) { - 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 = "UTF-8"; - let res = converter.ConvertToUnicode(output); - if (typeof(res) != "string") throw new Error("fucked file '"+file.path+"'"); - // fuck BOM - if (res.length >= 3 && res.substr(0, 3) == "\u00EF\u00BB\u00BF") res = res.substr(3); - return res; -} +addExport("calcFileSha512", calcFileSha512); diff --git a/modules/concmd.js b/modules/concmd.js index 5365e9a..4a2408e 100644 --- a/modules/concmd.js +++ b/modules/concmd.js @@ -45,7 +45,7 @@ function openEditor (fname, chromeWin, newfile) { } let text; try { - text = scacheAPI.fileReadText(fl); + text = fileReadText(fl); newfile = false; } catch (e) { if (!newfile) { diff --git a/modules/scriptcache.js b/modules/scriptcache.js index 56457be..b5d010c 100644 --- a/modules/scriptcache.js +++ b/modules/scriptcache.js @@ -30,25 +30,6 @@ //////////////////////////////////////////////////////////////////////////////// -function fileReadText (file) { - 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 = "UTF-8"; - let res = converter.ConvertToUnicode(output); - if (typeof(res) != "string") throw new Error("fucked file '"+file.path+"'"); - // fuck BOM - if (res.length >= 3 && res.substr(0, 3) == "\u00EF\u00BB\u00BF") res = res.substr(3); - return res; -} - - -//////////////////////////////////////////////////////////////////////////////// function uri2re (uri) { uri = uri.replace(/^\s+/, "").replace(/\s+$/, ""); if (uri == "") return null; @@ -644,5 +625,4 @@ scacheAPI.getScripts = function () { //////////////////////////////////////////////////////////////////////////////// -scacheAPI.fileReadText = fileReadText; exports = scacheAPI; -- 2.11.4.GIT