From a6eb611129aebfab5c7f738bb2638743ec40a3d9 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 26 Jul 2014 01:10:51 +0200 Subject: jsrrdgraph: binaryXHR: jshint style fixes Added jshint comment on top, added "use strict". Added missing semi-colons, converted to '===' (after validation). Added comment for `typeof strData == "unknown"`. Break one long line over two lines. There are no other changes except for whitespace and line ending changes. Check with `git diff -w`. --- js/binaryXHR.js | 521 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 264 insertions(+), 257 deletions(-) (limited to 'js/binaryXHR.js') diff --git a/js/binaryXHR.js b/js/binaryXHR.js index d82c808..63186b3 100644 --- a/js/binaryXHR.js +++ b/js/binaryXHR.js @@ -1,257 +1,264 @@ - -/* - * BinaryFile over XMLHttpRequest - * Part of the javascriptRRD package - * Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu - * MIT License [http://www.opensource.org/licenses/mit-license.php] - * - * Original repository: http://javascriptrrd.sourceforge.net/ - * - * Based on: - * Binary Ajax 0.1.5 - * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ - * MIT License [http://www.opensource.org/licenses/mit-license.php] - */ - -// ============================================================ -// Exception class -function InvalidBinaryFile(msg) { - this.message=msg; - this.name="Invalid BinaryFile"; -} - -// pretty print -InvalidBinaryFile.prototype.toString = function() { - return this.name + ': "' + this.message + '"'; -} - -// ===================================================================== -// BinaryFile class -// Allows access to element inside a binary stream -function BinaryFile(strData, iDataOffset, iDataLength) { - var data = strData; - var dataOffset = iDataOffset || 0; - var dataLength = 0; - // added - var doubleMantExpHi=Math.pow(2,-28); - var doubleMantExpLo=Math.pow(2,-52); - var doubleMantExpFast=Math.pow(2,-20); - - var switch_endian = false; - - this.getRawData = function() { - return data; - } - - if (typeof strData == "string") { - dataLength = iDataLength || data.length; - - this.getByteAt = function(iOffset) { - return data.charCodeAt(iOffset + dataOffset) & 0xFF; - } - } else if (typeof strData == "unknown") { - dataLength = iDataLength || IEBinary_getLength(data); - - this.getByteAt = function(iOffset) { - return IEBinary_getByteAt(data, iOffset + dataOffset); - } - } else { - throw new InvalidBinaryFile("Unsupported type " + (typeof strData)); - } - - this.getEndianByteAt = function(iOffset,width,delta) { - if (this.switch_endian) - return this.getByteAt(iOffset+width-delta-1); - else - return this.getByteAt(iOffset+delta); - } - - this.getLength = function() { - return dataLength; - } - - this.getSByteAt = function(iOffset) { - var iByte = this.getByteAt(iOffset); - if (iByte > 127) - return iByte - 256; - else - return iByte; - } - - this.getShortAt = function(iOffset) { - var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0) - if (iShort < 0) iShort += 65536; - return iShort; - } - this.getSShortAt = function(iOffset) { - var iUShort = this.getShortAt(iOffset); - if (iUShort > 32767) - return iUShort - 65536; - else - return iUShort; - } - this.getLongAt = function(iOffset) { - var iByte1 = this.getEndianByteAt(iOffset,4,0), - iByte2 = this.getEndianByteAt(iOffset,4,1), - iByte3 = this.getEndianByteAt(iOffset,4,2), - iByte4 = this.getEndianByteAt(iOffset,4,3); - - var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; - if (iLong < 0) iLong += 4294967296; - return iLong; - } - this.getSLongAt = function(iOffset) { - var iULong = this.getLongAt(iOffset); - if (iULong > 2147483647) - return iULong - 4294967296; - else - return iULong; - } - this.getStringAt = function(iOffset, iLength) { - var aStr = []; - for (var i=iOffset,j=0;i0);i++,j++) { - aStr[j] = String.fromCharCode(this.getByteAt(i)); - } - return aStr.join(""); - } - - // Added - this.getDoubleAt = function(iOffset) { - var iByte1 = this.getEndianByteAt(iOffset,8,0), - iByte2 = this.getEndianByteAt(iOffset,8,1), - iByte3 = this.getEndianByteAt(iOffset,8,2), - iByte4 = this.getEndianByteAt(iOffset,8,3), - iByte5 = this.getEndianByteAt(iOffset,8,4), - iByte6 = this.getEndianByteAt(iOffset,8,5), - iByte7 = this.getEndianByteAt(iOffset,8,6), - iByte8 = this.getEndianByteAt(iOffset,8,7); - var iSign=iByte8 >> 7; - var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); - var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4; - var iMantLo=((((iByte3) << 8) + iByte2) << 8) + iByte1; - - if (iExpRaw==0) return 0.0; - if (iExpRaw==0x7ff) return undefined; - - var iExp=(iExpRaw & 0x7FF)-1023; - - var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi); - return dDouble; - } - // added - // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa) - this.getFastDoubleAt = function(iOffset) { - var iByte5 = this.getEndianByteAt(iOffset,8,4), - iByte6 = this.getEndianByteAt(iOffset,8,5), - iByte7 = this.getEndianByteAt(iOffset,8,6), - iByte8 = this.getEndianByteAt(iOffset,8,7); - var iSign=iByte8 >> 7; - var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); - var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5; - - if (iExpRaw==0) return 0.0; - if (iExpRaw==0x7ff) return undefined; - - var iExp=(iExpRaw & 0x7FF)-1023; - - var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMant*doubleMantExpFast); - return dDouble; - } - - this.getCharAt = function(iOffset) { - return String.fromCharCode(this.getByteAt(iOffset)); - } -} - - -document.write( - "\r\n" -); - - - -// =============================================================== -// Load a binary file from the specified URL -// Will return an object of type BinaryFile -function FetchBinaryURL(url) { - var request = new XMLHttpRequest(); - request.open("GET", url,false); - try { - request.overrideMimeType('text/plain; charset=x-user-defined'); - } catch (err) { - // ignore any error, just to make both FF and IE work - } - request.send(null); - - var response=request.responseText; - try { - // for older IE versions, the value in responseText is not usable - if (IEBinary_getLength(this.responseBody)>0) { - // will get here only for older verson of IE - response=this.responseBody; - } - } catch (err) { - // not IE, do nothing - } - - var bf=new BinaryFile(response); - return bf; -} - - -// =============================================================== -// Asyncronously load a binary file from the specified URL -// -// callback must be a function with one or two arguments: -// - bf = an object of type BinaryFile -// - optional argument object (used only if callback_arg not undefined) -function FetchBinaryURLAsync(url, callback, callback_arg) { - var callback_wrapper = function() { - if(this.readyState == 4) { - var response=this.responseText; - try { - // for older IE versions, the value in responseText is not usable - if (IEBinary_getLength(this.responseBody)>0) { - // will get here only for older verson of IE - response=this.responseBody; - } - } catch (err) { - // not IE, do nothing - } - - var bf=new BinaryFile(response); - if (callback_arg!=null) { - callback(bf,callback_arg); - } else { - callback(bf); - } - } - } - - var request = new XMLHttpRequest(); - request.onreadystatechange = callback_wrapper; - request.open("GET", url,true); - try { - request.overrideMimeType('text/plain; charset=x-user-defined'); - } catch (err) { - // ignore any error, just to make both FF and IE work - } - request.send(null); - return request -} +// jshint browser:true +/* + * BinaryFile over XMLHttpRequest + * Part of the javascriptRRD package + * Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu + * MIT License [http://www.opensource.org/licenses/mit-license.php] + * + * Original repository: http://javascriptrrd.sourceforge.net/ + * + * Based on: + * Binary Ajax 0.1.5 + * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ + * MIT License [http://www.opensource.org/licenses/mit-license.php] + */ + +// ============================================================ +// Exception class +function InvalidBinaryFile(msg) { + "use strict"; + this.message = msg; + this.name = "Invalid BinaryFile"; +} + +// pretty print +InvalidBinaryFile.prototype.toString = function() { + "use strict"; + return this.name + ': "' + this.message + '"'; +}; + +// ===================================================================== +// BinaryFile class +// Allows access to element inside a binary stream +function BinaryFile(strData, iDataOffset, iDataLength) { + "use strict"; + var data = strData; + var dataOffset = iDataOffset || 0; + var dataLength = 0; + // added + var doubleMantExpHi=Math.pow(2,-28); + var doubleMantExpLo=Math.pow(2,-52); + var doubleMantExpFast=Math.pow(2,-20); + + var switch_endian = false; + + this.getRawData = function() { + return data; + }; + + if (typeof strData === "string") { + dataLength = iDataLength || data.length; + + this.getByteAt = function(iOffset) { + return data.charCodeAt(iOffset + dataOffset) & 0xFF; + }; + } else if (typeof strData === "unknown") { + // Correct. "unknown" as type. MS JScript 8 added this. + dataLength = iDataLength || IEBinary_getLength(data); + + this.getByteAt = function(iOffset) { + return IEBinary_getByteAt(data, iOffset + dataOffset); + }; + } else { + throw new InvalidBinaryFile("Unsupported type " + (typeof strData)); + } + + this.getEndianByteAt = function(iOffset,width,delta) { + if (this.switch_endian) + return this.getByteAt(iOffset+width-delta-1); + else + return this.getByteAt(iOffset+delta); + }; + + this.getLength = function() { + return dataLength; + }; + + this.getSByteAt = function(iOffset) { + var iByte = this.getByteAt(iOffset); + if (iByte > 127) + return iByte - 256; + else + return iByte; + }; + + this.getShortAt = function(iOffset) { + var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0); + if (iShort < 0) iShort += 65536; + return iShort; + }; + this.getSShortAt = function(iOffset) { + var iUShort = this.getShortAt(iOffset); + if (iUShort > 32767) + return iUShort - 65536; + else + return iUShort; + }; + this.getLongAt = function(iOffset) { + var iByte1 = this.getEndianByteAt(iOffset,4,0), + iByte2 = this.getEndianByteAt(iOffset,4,1), + iByte3 = this.getEndianByteAt(iOffset,4,2), + iByte4 = this.getEndianByteAt(iOffset,4,3); + + var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; + if (iLong < 0) iLong += 4294967296; + return iLong; + }; + this.getSLongAt = function(iOffset) { + var iULong = this.getLongAt(iOffset); + if (iULong > 2147483647) + return iULong - 4294967296; + else + return iULong; + }; + this.getStringAt = function(iOffset, iLength) { + var aStr = []; + for (var i=iOffset,j=0;i0);i++,j++) { + aStr[j] = String.fromCharCode(this.getByteAt(i)); + } + return aStr.join(""); + }; + + // Added + this.getDoubleAt = function(iOffset) { + var iByte1 = this.getEndianByteAt(iOffset,8,0), + iByte2 = this.getEndianByteAt(iOffset,8,1), + iByte3 = this.getEndianByteAt(iOffset,8,2), + iByte4 = this.getEndianByteAt(iOffset,8,3), + iByte5 = this.getEndianByteAt(iOffset,8,4), + iByte6 = this.getEndianByteAt(iOffset,8,5), + iByte7 = this.getEndianByteAt(iOffset,8,6), + iByte8 = this.getEndianByteAt(iOffset,8,7); + var iSign=iByte8 >> 7; + var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); + var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4; + var iMantLo=((((iByte3) << 8) + iByte2) << 8) + iByte1; + + if (iExpRaw === 0) return 0.0; + if (iExpRaw === 0x7ff) return undefined; + + var iExp=(iExpRaw & 0x7FF)-1023; + + var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi); + return dDouble; + }; + // added + // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa) + this.getFastDoubleAt = function(iOffset) { + var iByte5 = this.getEndianByteAt(iOffset,8,4), + iByte6 = this.getEndianByteAt(iOffset,8,5), + iByte7 = this.getEndianByteAt(iOffset,8,6), + iByte8 = this.getEndianByteAt(iOffset,8,7); + var iSign=iByte8 >> 7; + var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); + var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5; + + if (iExpRaw === 0) return 0.0; + if (iExpRaw === 0x7ff) return undefined; + + var iExp=(iExpRaw & 0x7FF)-1023; + + var dDouble = ((iSign === 1) ? -1 : 1); + dDouble *= Math.pow(2,iExp) * (1.0 + iMant*doubleMantExpFast); + return dDouble; + }; + + this.getCharAt = function(iOffset) { + return String.fromCharCode(this.getByteAt(iOffset)); + }; +} + + +document.write( + "\r\n" +); + + + +// =============================================================== +// Load a binary file from the specified URL +// Will return an object of type BinaryFile +function FetchBinaryURL(url) { + "use strict"; + var request = new XMLHttpRequest(); + request.open("GET", url,false); + try { + request.overrideMimeType('text/plain; charset=x-user-defined'); + } catch (err) { + // ignore any error, just to make both FF and IE work + } + request.send(null); + + var response=request.responseText; + try { + // for older IE versions, the value in responseText is not usable + if (IEBinary_getLength(this.responseBody)>0) { + // will get here only for older verson of IE + response=this.responseBody; + } + } catch (err) { + // not IE, do nothing + } + + var bf=new BinaryFile(response); + return bf; +} + + +// =============================================================== +// Asyncronously load a binary file from the specified URL +// +// callback must be a function with one or two arguments: +// - bf = an object of type BinaryFile +// - optional argument object (used only if callback_arg not undefined) +function FetchBinaryURLAsync(url, callback, callback_arg) { + "use strict"; + var callback_wrapper = function() { + if(this.readyState === 4) { + var response=this.responseText; + try { + // for older IE versions, the value in responseText is not usable + if (IEBinary_getLength(this.responseBody)>0) { + // will get here only for older verson of IE + response=this.responseBody; + } + } catch (err) { + // not IE, do nothing + } + + var bf=new BinaryFile(response); + if (callback_arg) { + callback(bf, callback_arg); + } else { + callback(bf); + } + } + }; + + var request = new XMLHttpRequest(); + request.onreadystatechange = callback_wrapper; + request.open("GET", url,true); + try { + request.overrideMimeType('text/plain; charset=x-user-defined'); + } catch (err) { + // ignore any error, just to make both FF and IE work + } + request.send(null); + return request; +} -- cgit v1.1