pluie-bin/lib/header.js

54 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2016-12-18 22:50:34 +00:00
module.exports = function (ns) {
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var HeaderProperty = function(name, type, size, defaultValue)
{
this.name = name;
this.type = type;
this.size = size;
this.length = type == 'N' ? size*4 : (type == 'n' ? size*2 : size );
this.value = defaultValue;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var HeaderDef = function(mapData)
{
var map = [];
mapData.forEach(function (def) {
map.push(new HeaderProperty(def[0], def[1], def[2], def[3]));
});
this.map = map;
var keys = [];
var indexes = {};
var offset = 0;
var index = 0;
this.map.forEach(function(prop) {
prop.offset = offset;
keys[keys.length] = prop.name;
offset += prop.length;
indexes[prop.name] = index++;
});
this.indexes = indexes;
this.keys = keys;
this.length = offset;
this.getProperty = function(key) {
return this.map[this.indexes[key]];
}
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.Header = function(def) {
2016-12-18 13:44:45 +00:00
this.def = new HeaderDef(def);
this.value = function(key) {
return this.def.getProperty(key).value;
};
this.buildProperties = function(properties) {
this.def.map.forEach(function(prop) {
if (prop.name != 'signature') {
if (typeof properties[prop.name] != 'undefined') {
prop.value = properties[prop.name];
}
}
});
};
}
}