From dcfb1307cc7f54f956a4a5191cc218e66b828284 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Sat, 27 Oct 2012 18:04:50 -0400 Subject: [PATCH] FIXED - DMAPType --- resources/js/models/dmap-type.js | 203 ++++++++++++++++++------------- 1 file changed, 120 insertions(+), 83 deletions(-) diff --git a/resources/js/models/dmap-type.js b/resources/js/models/dmap-type.js index 9960e0c..d372dc3 100644 --- a/resources/js/models/dmap-type.js +++ b/resources/js/models/dmap-type.js @@ -1,70 +1,15 @@ +/*jslint laxbreak:true */ +/*jslint laxcomma:true */ +/*jslint loopfunc:true */ +/*jslint strict:true */ define([ - "toolbox" + "underscore" + , "toolbox" ] -, function (Toolbox) { +, function (_, Toolbox) { + "use strict"; + var that - , typeMap = { - 1: { - length: 1 - , unpack: Types.getInt8 - } - - , 2: { - length: 1 - , unpack: Types.getUint8 - } - - , 3: { - length: 2 - , unpack: Types.getInt16 - } - - , 4: { - length: 2 - , unpack: Types.getUint16 - } - - , 5: { - length: 4 - , unpack: Types.getInt32 - } - - , 6: { - length: 4 - , unpack: Types.getUint32 - } - - , 7: { - length: 8 - , unpack: Types.getFloat64 - } - - // There is no unsigned 64bit integer in JS. - , 8: { - length: 8 - , unpack: Types.getFloat64 - } - - , 9: { - length: null - , unpack: Types.getUTFString - } - - , 10: { - length: 4 - , unpack: Types.getDate - } - - , 11: { - length: 2 - , unpack: Types.getVersion - } - - , 12: { - length: null - , unpack: Types.getCollection - } - } , Types = Toolbox.Base.extend({ constructor: function (code) { @@ -73,8 +18,8 @@ define([ ; if (_.isNumber(code)) { - if (!_.isUndefined(typeMap[code])) { - _.extend(this, typeMap[code]); + if (!_.isUndefined(Types.typeMap[code])) { + _.extend(this, Types.typeMap[code]); } else { } @@ -90,45 +35,135 @@ define([ , getCode: function() { return this.code; } - + , getByteLength: function() { return this.length; } + } , { - getInt8: function (chunk) { - return new DataView(chunk.slice(0,typeMap[1].length)).getInt8(0, false); + + + typeMap: { + 1: { + length: 1 + , unpack: function (chunk) { + Types.getInt8(chunk); + } + } + + , 2: { + length: 1 + , unpack: function (chunk) { + Types.getUint8(chunk); + } + } + + , 3: { + length: 2 + , unpack: function (chunk) { + Types.getInt16(chunk); + } + } + + , 4: { + length: 2 + , unpack: function (chunk) { + Types.getUint16(chunk); + } + } + + , 5: { + length: 4 + , unpack: function (chunk) { + Types.getInt32(chunk); + } + } + + , 6: { + length: 4 + , unpack: function (chunk) { + Types.getUint32(chunk); + } + } + + , 7: { + length: 8 + , unpack: function (chunk) { + Types.getFloat64(chunk); + } + } + + // There is no unsigned 64bit integer in JS. + , 8: { + length: 8 + , unpack: function (chunk) { + Types.getFloat64(chunk); + } + } + + , 9: { + length: null + , unpack: function (chunk, length) { + Types.getUTFString(chunk, length); + } + } + + , 10: { + length: 4 + , unpack: function (chunk) { + Types.getDate(chunk); + } + } + + , 11: { + length: 2 + , unpack: function (chunk) { + Types.getVersion(chunk); + } + } + + , 12: { + length: null + , unpack: function (chunk, length) { + Types.getModel(chunk, length); + } + } + } + + , getInt8: function (chunk) { + return new DataView(chunk.slice(0,Types.typeMap[1].length)).getInt8(0, false); } , getUint8: function (chunk) { - return new DataView(chunk.slice(0,typeMap[2].length)).getUint8(0, false); + return new DataView(chunk.slice(0,Types.typeMap[2].length)).getUint8(0, false); } , getInt16: function (chunk) { - return new DataView(chunk.slice(0,typeMap[3].length)).getInt16(0, false); + return new DataView(chunk.slice(0,Types.typeMap[3].length)).getInt16(0, false); } , getUint16: function (chunk) { - return new DataView(chunk.slice(0,typeMap[4].length)).getUint16(0, false); + return new DataView(chunk.slice(0,Types.typeMap[4].length)).getUint16(0, false); } , getInt32: function (chunk) { - return new DataView(chunk.slice(0,typeMap[5].length)).getInt32(0, false); + return new DataView(chunk.slice(0,Types.typeMap[5].length)).getInt32(0, false); } , getUint32: function (chunk) { - return new DataView(chunk.slice(0,typeMap[6].length)).getUint32(0, false); + return new DataView(chunk.slice(0,Types.typeMap[6].length)).getUint32(0, false); } , getFloat64: function (chunk) { - return new DataView(chunk.slice(0,typeMap[7].length)).getFloat64(0, false); + return new DataView(chunk.slice(0,Types.typeMap[7].length)).getFloat64(0, false); } , getString: function(chunk, length) { if (!_.isNumber(length) || length > chunk.byteLength) { - var length = chunk.byteLength; + length = chunk.byteLength; } - + return String.fromCharCode.apply(null, new Uint8Array(chunk.slice(0, length))); } @@ -136,19 +171,19 @@ define([ , getUTFString: function (chunk, length) { var str = ''; if (!_.isNumber(length) || length > chunk.byteLength) { - var length = chunk.byteLength; + length = chunk.byteLength; } - + for (var i = 0; i < length; i++) { var slice = chunk.slice(i, i+1); var num = new DataView(slice).getUint8(0, false); - + str += num <= 0x7F ? num === 0x25 ? "%25" : String.fromCharCode(num) : "%" + num.toString(16); } - + return decodeURIComponent(str); } @@ -159,14 +194,16 @@ define([ , getVersion: function (chunk) { return Types.getInt8(chunk.slice(0,1)) + "." + Types.getInt8(chunk.slice(1,2)); } - + , getModel: function (chunk) { return chunk; } , getUnknown: function (chunk) { // Unimplemented - /*switch (chunk.byteLength) { + throw "ERROR_UNIMPLEMENTED"; + /* + switch (chunk.byteLength) { // We can tring a string default: