Skip to content

Latest commit

 

History

History
67 lines (48 loc) · 877 Bytes

Readme.md

File metadata and controls

67 lines (48 loc) · 877 Bytes

struct

C struct style buffer views for reading and writing structures of varying types.

Installation

$ component install component/struct

Example

var struct = require('struct');

// buffer big enough for all 3 structs
var buf = new Uint8Array(3 * (32 + 5 + 16));

var Pet = struct({
  id: 'uint32',
  name: 'string',
  age: 'uint16'
});

// write

var tobi = new Pet({
  id: 1,
  name: 'Tobi',
  age: 2
});

var loki = new Pet({
  id: 2,
  name: 'Loki',
  age: 2
});

var jane = new Pet({
  id: 3,
  name: 'Jane',
  age: 6
});

var off = 0;
var pets = [tobi, loki, jane];
for (var i = 0; i < pets.length; i++) {
  off = pets[i].write(buf, off);
}

// read

var n = 3;
var off = 0;
var pet = new Pet;
while (n--) {
  off = pet.read(buf, off);
  console.log('%s (%s) is %s years old', pet.name, pet.id, pet.age);
}

API

...

License

MIT