Permalink
Browse files

pattern-matching

  • Loading branch information...
1 parent 119d664 commit 46abd04ba962c4a7cc7a2d6b91ea87f0af783c42 @kig committed May 22, 2012
Showing with 46 additions and 3 deletions.
  1. +37 −2 shp.html
  2. +9 −1 structdef.js
View
39 shp.html
@@ -11,10 +11,42 @@
document.querySelector('input[type="file"]').onchange = function(e) {
var reader = new FileReader();
+ var xyDef = {x: 'float64le', y: 'float64le'};
+ var polyDef = {
+ minX: 'float64le',
+ minY: 'float64le',
+ maxX: 'float64le',
+ maxY: 'float64le',
+ partCount: 'int32le',
+ pointCount: 'int32le',
+ parts: ['int32le', 'partCount'],
+ points: [xyDef, 'pointCount']
+ };
+
+ var shapeDef = [
+ {
+ number: 'int32le=0' // Null
+ },
+ {
+ number: 'int32le=1', // Point
+ content: xyDef
+ },
+ {
+ number: 'int32le=3', // Polyline
+ content: polyDef
+ },
+ {
+ number: 'int32le=5', // Polygon
+ content: polyDef
+ },
+ ['uint8', 'length*2'] // Catch-all
+ ];
+
+
var recordDef = {
number: 'int32',
length: 'int32',
- shape: ['uint8', 'length*2']
+ shape: shapeDef
};
var shpDef = {
@@ -41,7 +73,10 @@
var obj = readStruct(dv, idx, shpDef);
if (obj) {
for (var i=0; i<obj.records.length; i++) {
- obj.records[i].shape = "uint8["+obj.records[i].shape.length+"]";
+ var c = obj.records[i].shape.content;
+ if (c && c.points) {
+ c.points = "pointXY["+c.points.length+"]";
+ }
}
pre.textContent = JSON.stringify(obj, null, 4);
} else {
View
10 structdef.js
@@ -143,11 +143,19 @@ var readType = function(dataView, idx, t, struct) {
} else {
length = struct[length];
}
+ } else if (typeof length == 'object') { // branch
+ i = idx[0];
+ for (var k=0; k < t.length; k++) {
+ idx[0] = i;
+ v = readType(dataView, idx, t[k], struct);
+ if (v) break;
+ }
+ return v;
} else if (length < 0) {
length = dataView.byteLength - idx[0] + length;
}
v = new Array(length);
- for (var i=0; i<length; i++) {
+ for (i=0; i<length; i++) {
v[i] = readType(dataView, idx, ta, struct);
}
return v;

0 comments on commit 46abd04

Please sign in to comment.