Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Many changes

  • Loading branch information...
commit 68278067b71f9ba1b795950658bc059d9cb22483 1 parent 0c1863b
@vjeux authored
Showing with 30 additions and 15 deletions.
  1. +30 −15 src/jparser.js
View
45 src/jparser.js
@@ -1,5 +1,10 @@
(function () {
+if (typeof jDataView === 'undefined' && typeof require !== 'undefined') {
+ jDataView = require('jDataView');
+}
+
+
// Extend code from underscorejs
var extend = function (obj) {
for (var i = 1; i < arguments.length; ++i) {
@@ -17,11 +22,21 @@ function jParser(view, structure) {
if (!(this instanceof arguments.callee)) {
throw new Error("Constructor may not be called as a function");
}
+ if (!(view instanceof jDataView)) {
+ view = new jDataView(view);
+ }
this.view = view;
this.view.seek(0);
this.structure = extend({}, jParser.prototype.structure, structure);
}
+function toInt(val) {
+ if (typeof val === 'function') {
+ val = val.call(this);
+ }
+ return val;
+}
+
jParser.prototype.structure = {
uint8: function () { return this.view.getUint8(); },
uint16: function () { return this.view.getUint16(); },
@@ -32,11 +47,11 @@ jParser.prototype.structure = {
float32: function () { return this.view.getFloat32(); },
float64: function () { return this.view.getFloat64(); },
char: function () { return this.view.getChar(); },
- string: function (length) { return this.view.getString(length); },
+ string: function (length) {
+ return this.view.getString(toInt.call(this, length));
+ },
array: function (type, length) {
- if (typeof length === 'function') {
- length = length();
- }
+ length = toInt.call(this, length);
var results = [];
for (var i = 0; i < length; ++i) {
results.push(this.parse(type));
@@ -44,31 +59,30 @@ jParser.prototype.structure = {
return results;
},
seek: function (position, block) {
- if (typeof position === 'function') {
- position = position();
- }
+ position = toInt.call(this, position);
if (block instanceof Function) {
var old_position = this.view.tell();
this.view.seek(position);
- block();
- return this.view.seek(old_position);
+ var result = block.call(this);
+ this.view.seek(old_position);
+ return result;
} else {
return this.view.seek(position);
}
},
tell: function () {
return this.view.tell();
+ },
+ skip: function (offset) {
+ offset = toInt.call(this, offset);
+ this.view.seek(this.view.tell() + offset);
+ return offset;
}
};
jParser.prototype.seek = jParser.prototype.structure.seek;
jParser.prototype.tell = jParser.prototype.structure.tell;
-jParser.prototype.skip = function (offset) {
- if (typeof offset === 'function') {
- offset = offset();
- }
- return this.view.seek(this.view.tell() + offset);
-};
+jParser.prototype.skip = jParser.prototype.structure.skip;
jParser.prototype.parse = function (structure) {
// f, 1, 2 means f(1, 2)
@@ -94,6 +108,7 @@ jParser.prototype.parse = function (structure) {
if (typeof structure === 'object') {
var output = {};
for (var key in structure) {
+ this.current = output;
output[key] = this.parse(structure[key]);
}
return output;
Please sign in to comment.
Something went wrong with that request. Please try again.