Skip to content

Commit

Permalink
reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
xdenser committed Sep 5, 2013
1 parent f843487 commit 410922c
Showing 1 changed file with 65 additions and 62 deletions.
127 changes: 65 additions & 62 deletions index.js
@@ -1,19 +1,19 @@
function byteField(p, offset) {
this.length = 1;
this.get = function () {
this.get = function() {
return p.buf[offset];
}
this.set = function (val) {
this.set = function(val) {
p.buf[offset] = val;
}
}

function boolField(p, offset, length) {
this.length = length;
this.get = function () {
this.get = function() {
return (p.buf[offset] > 0 );
}
this.set = function (val) {
this.set = function(val) {
p.buf[offset] = val ? 1 : 0;
}
}
Expand All @@ -33,7 +33,7 @@ function intField(p, offset, length, le, signed) {

function getUVal(bor) {
var val = 0;
bor(function (i, o) {
bor(function(i, o) {
val += Math.pow(256, o) * p.buf[offset + i];
})
return val;
Expand All @@ -49,17 +49,17 @@ function intField(p, offset, length, le, signed) {
}

function setVal(bor, val) {
bor(function (i, o) {
bor(function(i, o) {
p.buf[offset + i] = Math.floor(val / Math.pow(256, o)) & 0xff;
});
}


this.get = function () {
this.get = function() {
var bor = le ? lec : bec;
return ( signed ? getSVal(bor) : getUVal(bor));
}
this.set = function (val) {
this.set = function(val) {
var bor = le ? lec : bec;
setVal(bor, val);
}
Expand All @@ -68,7 +68,7 @@ function intField(p, offset, length, le, signed) {
function charField(p, offset, length, encoding) {
this.length = length;
this.encoding = encoding;
this.get = function () {
this.get = function() {
var result = p.buf.toString(this.encoding, offset, offset + length);
var strlen = result.indexOf("\0");
if (strlen == -1) {
Expand All @@ -77,7 +77,7 @@ function charField(p, offset, length, encoding) {
return result.slice(0, strlen);
}
}
this.set = function (val) {
this.set = function(val) {
val += "\0";
if (val.length > length)
val = val.substring(0, length);
Expand All @@ -87,13 +87,13 @@ function charField(p, offset, length, encoding) {

function structField(p, offset, struct) {
this.length = struct.length();
this.get = function () {
this.get = function() {
return struct;
}
this.set = function (val) {
this.set = function(val) {
struct.set(val);
}
this.allocate = function () {
this.allocate = function() {
struct._setBuff(p.buf.slice(offset, offset + struct.length()));
}
}
Expand All @@ -103,21 +103,21 @@ function arrayField(p, offset, len, type) {
var args = [].slice.call(arguments, 4);
args.unshift(0);
for (var i = 0; i < len; i++) {
if (type instanceof Struct) {
if ( type instanceof Struct) {
as.struct(i, type.clone());
} else if (type in as) {
} else if ( type in as) {
args[0] = i;
as[type].apply(as, args);
}
}
this.length = as.length();
this.allocate = function () {
this.allocate = function() {
as._setBuff(p.buf.slice(offset, offset + as.length()));
}
this.get = function () {
this.get = function() {
return as;
}
this.set = function (val) {
this.set = function(val) {
as.set(val);
}
}
Expand All @@ -127,11 +127,11 @@ function Struct() {
return new Struct;

var priv = {
buf: {},
allocated: false,
len: 0,
fields: {},
closures: []
buf : {},
allocated : false,
len : 0,
fields : {},
closures : []
}, self = this;

function checkAllocated() {
Expand All @@ -140,20 +140,20 @@ function Struct() {
}


this.word8 = function (key) {
this.word8 = function(key) {
checkAllocated();
priv.closures.push(function (p) {
priv.closures.push(function(p) {
p.fields[key] = new byteField(p, p.len);
p.len++;
});
return this;
};

// Create handlers for various Bool Field Variants
[1, 2, 3, 4].forEach(function (n) {
self['bool' + (n == 1 ? '' : n)] = function (key) {
[1, 2, 3, 4].forEach(function(n) {
self['bool' + (n == 1 ? '' : n)] = function(key) {
checkAllocated();
priv.closures.push(function (p) {
priv.closures.push(function(p) {
p.fields[key] = new boolField(p, p.len, n);
p.len += n;
});
Expand All @@ -162,13 +162,13 @@ function Struct() {
});

// Create handlers for various Integer Field Variants
[1, 2, 3, 4, 6, 8].forEach(function (n) {
[true, false].forEach(function (le) {
[true, false].forEach(function (signed) {
[1, 2, 3, 4, 6, 8].forEach(function(n) {
[true, false].forEach(function(le) {
[true, false].forEach(function(signed) {
var name = 'word' + (n * 8) + ( signed ? 'S' : 'U') + ( le ? 'le' : 'be');
self[name] = function (key) {
self[name] = function(key) {
checkAllocated();
priv.closures.push(function (p) {
priv.closures.push(function(p) {
p.fields[key] = new intField(p, p.len, n, le, signed);
p.len += n;
});
Expand All @@ -178,18 +178,18 @@ function Struct() {
});
});

this.chars = function (key, length, encoding) {
this.chars = function(key, length, encoding) {
checkAllocated();
priv.closures.push(function (p) {
priv.closures.push(function(p) {
p.fields[key] = new charField(p, p.len, length, encoding || 'ascii');
p.len += length;
});
return this;
}

this.struct = function (key, struct) {
this.struct = function(key, struct) {
checkAllocated();
priv.closures.push(function (p) {
priv.closures.push(function(p) {
p.fields[key] = new structField(p, p.len, struct.clone());
p.len += p.fields[key].length;
});
Expand All @@ -206,12 +206,12 @@ function Struct() {
}


this.array = function (key, length, type) {
this.array = function(key, length, type) {
checkAllocated();
var args = [].slice.call(arguments, 1);
args.unshift(null);
args.unshift(null);
priv.closures.push(function (p) {
priv.closures.push(function(p) {
args[0] = p;
args[1] = p.len;
p.fields[key] = construct(arrayField, args);
Expand All @@ -225,7 +225,7 @@ function Struct() {
function applyClosures(p) {
if (beenHere)
return;
p.closures.forEach(function (el) {
p.closures.forEach(function(el) {
el(p);
});
beenHere = true;
Expand All @@ -239,47 +239,47 @@ function Struct() {
}


this._setBuff = function (buff) {
this._setBuff = function(buff) {
priv.buf = buff;
applyClosures(priv);
allocateFields();
priv.allocated = true;
}

this.allocate = function () {
this.allocate = function() {
applyClosures(priv);
priv.buf = new Buffer(priv.len);
allocateFields();
priv.allocated = true;
return this;
}

this._getPriv = function () {
this._getPriv = function() {
return priv;
}

this.clone = function () {
this.clone = function() {
var c = new Struct;
var p = c._getPriv();
p.closures = priv.closures;
return c;
}

this.length = function () {
this.length = function() {
applyClosures(priv);
return priv.len;
}

this.get = function (key) {
if (key in priv.fields) {
this.get = function(key) {
if ( key in priv.fields) {
return priv.fields[key].get();
} else
throw new Error('Can not find field ' + key);
}

this.set = function (key, val) {
this.set = function(key, val) {
if (arguments.length == 2) {
if (key in priv.fields) {
if ( key in priv.fields) {
priv.fields[key].set(val);
} else
throw new Error('Can not find field ' + key);
Expand All @@ -291,37 +291,40 @@ function Struct() {
}
}
}
this.buffer = function () {
this.buffer = function() {
return priv.buf;
}

function getFields() {
var fields = {};
Object.keys(priv.fields).forEach(function (key) {
Object.keys(priv.fields).forEach(function(key) {
Object.defineProperty(fields, key, {
get: function () {
get : function() {
var res = self.get(key);
if (res instanceof Struct) return res.fields;
else return res;
if ( res instanceof Struct)
return res.fields;
else
return res;
},
set: function (newVal) {
set : function(newVal) {
self.set(key, newVal);
},
enumerable: true
enumerable : true
});
});
return fields;
};

var _fields;
Object.defineProperty(this, 'fields', {
get: function () {
if (_fields) return _fields;
return (_fields = getFields());
get : function() {
if (_fields)
return _fields;
return ( _fields = getFields());
},
enumerable: true,
configurable: true
enumerable : true,
configurable : true
});

}

exports.Struct = Struct;

0 comments on commit 410922c

Please sign in to comment.