From 9bdcb814ab96fd81fda11c34ee7654ac544bdebf Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 14:24:36 -0400 Subject: [PATCH] weechat-protocol.js: clean code (indentation/trailing spaces) --- js/websockets.js | 2 +- js/weechat-protocol.js | 310 +++++++++++++++++++++-------------------- 2 files changed, 163 insertions(+), 149 deletions(-) diff --git a/js/websockets.js b/js/websockets.js index df62a1a..6344fa9 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -324,7 +324,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ }]); weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', function($rootScope, $log, handlers, colors) { - protocol = new Protocol(); + protocol = new WeeChatProtocol(); var websocket = null; diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index ea6dda8..19c79e5 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -1,180 +1,194 @@ -var Protocol = function() { - var self = this; +var WeeChatProtocol = function() { + var self = this; - var getInfo = function() { - var info = {}; - info.key = getString(); - info.value = getString(); - return info; + var getInfo = function() { + var info = {}; + info.key = getString(); + info.value = getString(); + + return info; + }; + + var getHdata = function() { + var paths; + var count; + var objs = []; + var hpath = getString(); + + keys = getString().split(','); + paths = hpath.split('/'); + count = getInt(); + + keys = keys.map(function(key) { + return key.split(':'); + }); + + for (var i = 0; i < count; i++) { + var tmp = {}; + + tmp.pointers = paths.map(function(path) { + return getPointer(); + }); + keys.forEach(function(key) { + tmp[key[0]] = runType(key[1]); + }); + objs.push(tmp); }; - var getHdata = function() { - var paths; - var count; - var objs = []; - var hpath = getString(); + return objs; + }; - + function getPointer() { + var l = getChar(); + var pointer = getSlice(l) + var parsed_data = new Uint8Array(pointer); - keys = getString().split(','); - paths = hpath.split('/'); - count = getInt(); + return _uiatos(parsed_data); + }; - keys = keys.map(function(key) { - return key.split(':'); - }); - var i; - for (i = 0; i < count; i++) { - var tmp = {}; + var _uiatos = function(uia) { + var _str = []; + for (var c = 0; c < uia.length; c++) { + _str[c] = String.fromCharCode(uia[c]); + } - tmp.pointers = paths.map(function(path) { - return getPointer(); - }); + return decodeURIComponent(escape(_str.join(""))); + }; - keys.forEach(function(key) { - tmp[key[0]] = runType(key[1]); - }); - objs.push(tmp); - }; - return objs; - }; + var getInt = function() { + var parsed_data = new Uint8Array(getSlice(4)); + var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); - function getPointer() { - var l = getChar(); + return i; + }; + + var getChar = function() { + var parsed_data = new Uint8Array(getSlice(1)); + + return parsed_data[0]; + }; + + var getString = function() { + var l = getInt(); + + if (l > 0) { + var s = getSlice(l); + var parsed_data = new Uint8Array(s); - var pointer = getSlice(l) - var parsed_data = new Uint8Array(pointer); return _uiatos(parsed_data); - - }; - - var _uiatos =function(uia) { - var _str = []; - for (var c = 0; c < uia.length; c++) { - _str[c] = String.fromCharCode(uia[c]); - } - return decodeURIComponent(escape(_str.join(""))); - }; - - var getInt = function() { - var parsed_data = new Uint8Array(getSlice(4)); - var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); - return i; - - }; - - var getChar = function() { - var parsed_data = new Uint8Array(getSlice(1)); - return parsed_data[0]; - }; - - var getString = function() { - var l = getInt(); - if (l > 0) { - var s = getSlice(l); - var parsed_data = new Uint8Array(s); - return _uiatos(parsed_data); - } - return ""; - }; - - var getSlice = function(length) { - var slice = self.data.slice(0,length); - self.data = self.data.slice(length); - return slice; - }; - - var getType = function() { - var t = getSlice(3); - return _uiatos(new Uint8Array(t)); - }; - - var runType = function(type) { - if (type in types) { - return types[type](); - } - 0; - }; - - var getHeader = function() { - return { - length: getInt(), - compression: getChar(), - } - }; - - var getId = function() { - return getString(); } - var getObject = function() { - var type = getType(); - if (type) { - return object = { - type: type, - content: runType(type), - } + return ""; + }; + + var getSlice = function(length) { + var slice = self.data.slice(0,length); + + self.data = self.data.slice(length); + + return slice; + }; + + var getType = function() { + var t = getSlice(3); + + return _uiatos(new Uint8Array(t)); + }; + + var runType = function(type) { + if (type in types) { + return types[type](); + } + }; + + var getHeader = function() { + var len = getInt(); + var comp = getChar(); + + return { + length: len, + compression: comp, + }; + }; + + var getId = function() { + return getString(); + } + + var getObject = function() { + var type = getType(); + + if (type) { + return object = { + type: type, + content: runType(type), } } + } - self.parse = function(data) { - self.setData(data); - var header = getHeader(); - var id = getId(); - var objects = []; - var object = getObject(); - while(object) { - objects.push(object); - object = getObject(); - } - return { - header: header, - id: id, - objects: objects, - } + self.parse = function(data) { + self.setData(data); + + var header = getHeader(); + var id = getId(); + var objects = []; + var object = getObject(); + + while(object) { + objects.push(object); + object = getObject(); } - self.setData = function (data) { - self.data = data; + return { + header: header, + id: id, + objects: objects, + }; + } + + self.setData = function (data) { + self.data = data; + }; + + function array() { + var type; + var count; + var values; + + type = getType(); + count = getInt(); + values = []; + + for (var i = 0; i < count; i++) { + values.push(runType(type)); }; - function array() { - var type; - var count; - var values; + return values; + } - type = getType(); - count = getInt(); - values = []; - var i; - for (i = 0; i < count; i++) { - values.push(runType(type)); - }; - return values; - } + var types = { + chr: getChar, + "int": getInt, + str: getString, + inf: getInfo, + hda: getHdata, + ptr: getPointer, + lon: getPointer, + tim: getPointer, + buf: getString, + arr: array + }; - var types = { - chr: getChar, - "int": getInt, - str: getString, - inf: getInfo, - hda: getHdata, - ptr: getPointer, - lon: getPointer, - tim: getPointer, - buf: getString, - arr: array - }; //TODO: IMPLEMENT THIS STUFF // chr: this.getChar, // 'int': getInt, - // hacks +// hacks - // hacks +// hacks // htb: getHashtable, // inf: Protocol.getInfo, // inl: getInfolist, // }, -} +};