Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit c8e67b453298bab48abec898f130f1df87bbfb4b @xdenser committed Sep 4, 2011
Showing with 538 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .npmignore
  3. +22 −0 LICENSE
  4. +128 −0 README.md
  5. +321 −0 index.js
  6. +24 −0 package.json
  7. +41 −0 samples/sample.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1 @@
+node_modules
22 LICENSE
@@ -0,0 +1,22 @@
+MIT License
+-----------
+
+Copyright (C) 2011 Denys Khanzhiyev aka xdenser.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
128 README.md
@@ -0,0 +1,128 @@
+Struct
+======
+NodeJS module to work with buffers as structures (or records) of various fields (like c struct declaration, or pascal record).
+Uses [node-proxy](https://github.com/samshull/node-proxy).
+
+Installation
+============
+
+To install with [npm](http://github.com/isaacs/npm):
+
+ npm install struct
+
+
+Example
+=======
+
+Define some structure:
+
+ var Struct = require('../index.js').Struct;
+
+ var Person = Struct()
+ .chars('firstName',10)
+ .chars('lastName',10)
+ .array('items',3,'chars',10)
+ .word16Sle('balance'),
+ People = Struct()
+ .word8('presentCount')
+ .array('list',2,Person);
+
+Now allocate buffer for it
+
+ People.allocate();
+ var buf = People.buffer();
+
+Clear buffer to see how it will change later:
+
+ var buf = Persons.buffer();
+ for (var i = 0; i < buf.length ; i++) {
+ buf[i] = 0;
+ }
+ console.log(buf);
+
+Output:
+ <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
+
+
+Now you can access memory as defined binary structure with `fields` property in a handy manner.
+
+ var proxy = People.fields;
+ proxy.presentCount = 2;
+ console.log(buf);
+
+Output:
+
+ <Buffer 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
+
+And so on
+
+ proxy.list[0].firstName = 'John';
+ console.log(buf);
+
+Output:
+ <Buffer 02 4a 6f 68 6e 00 00 00 00 00 00 ...
+
+Reference
+=========
+
+##Struct()
+
+creates struct object, you may define your data structure with chain calls to following methods:
+
+###word8(name)
+defines one byte unsigned field, name - always defines name of field
+
+### word8Sle(name),word8Sbe(name)
+define one byte signed field
+
+### word16Sle(name),word16Sbe(name)
+define 16 bit signed field with little-endian and big-endian byte order
+
+### word16Ule(name),word16Ube(name)
+define 16 bit unsigned field with little-endian and big-endian byte order
+
+### word32Sle(name),word32Sbe(name),word32Ule(name),word32Ube(name),word64Sle(name),word64Sbe(name),word64Ule(name),word64Ube(name)
+same for 32 and 64 bit fields
+
+### chars(name,length)
+defines array of chars in 'ascii' encoding, name - name of the field, length - length of array
+
+### array(name, length, type, ...)
+defines array of fields (internally it is Struct() object with field names set to 0,1,2,... ).
+
+- `name` - name of array field;
+- `length` - length of array;
+- `type` - `string||Struct()`, string is interpreted as name of Struct() method to call for each array element.
+
+For example `array('numbers',5,'word16Ule')` will define array of 2 byte unsigned words (x86 byte order) with 5 elements.
+Any parameters that follow type will be passed to definition function.
+So `array('someName',3,'chars',20)` defines 3 element array of 20 chars.
+You also may pass Struct() object to make array of structures.
+
+### struct(name, Struct() )
+
+defines field that itself is a structure.
+
+## Other methods
+
+### get(fieldName)
+allows access to field (reads value from it's offset in buffer)
+
+### set(fieldName, value)
+allows access to field (write value at it's offset in buffer)
+
+### allocate()
+allocates buffer for defined structure with proper size. This is used when you need to format data in buffer and send or write it out.
+
+### _setBuff(buffer)
+sets buffer reference of object to other buffer. This may be used to parse or adjust some binary data received or read from somewhere.
+
+### buffer()
+returns currently used buffer. Before you may read or write to structure you have to call either allocate() or _setBuff(buffer).
+This is not true only for Struct() objects that are fields themselves, as they are allocated automatically by parent Struct object.
+
+## fields property
+Struct().fields is a Proxy object allowing to access structure fields in a handy manner - as any other javascript object.
+
+
+
Oops, something went wrong.

0 comments on commit c8e67b4

Please sign in to comment.