Skip to content

Commit 4b24d24

Browse files
committed
Update docs with missing functions, add examples
1 parent 5868e3b commit 4b24d24

File tree

1 file changed

+158
-2
lines changed

1 file changed

+158
-2
lines changed

README.markdown

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ values don't exist they will be created automatically, so for instance you can
102102
assign into `dst.addr` and `dst.port` and the `dst` key in the variable stash
103103
will be `{ addr : x, port : y }` afterwards.
104104

105+
``` js
106+
var vars = binary.parse(new Buffer([5, 0, 80, 11, 184]))
107+
.word8lu('count')
108+
.word16lu('ports.src')
109+
.word16bu('ports.dst')
110+
.vars;
111+
console.log(vars)
112+
113+
//{ count: 5, ports: { src: 80, dst: 3000 } }
114+
```
115+
116+
105117
b.buffer(key, size)
106118
-------------------
107119

@@ -110,6 +122,16 @@ slice in the variable stash at `key`. If `size` is a string, use the value at
110122
`vars[size]`. The key follows the same dotted address rules as the word
111123
functions.
112124

125+
``` js
126+
var vars = binary.parse(new Buffer([4, 1, 0, 1, 0]))
127+
.word8lu('dataLength')
128+
.buffer('data', 'dataLength')
129+
.vars;
130+
console.log(vars.data)
131+
132+
//<Buffer 01 00 01 00>
133+
```
134+
113135
b.scan(key, buffer)
114136
-------------------
115137

@@ -136,32 +158,166 @@ The callback `cb` is provided with the variable stash from all the previous
136158
actions once they've all finished.
137159

138160
You can nest additional actions onto `this` inside the callback.
161+
``` js
162+
binary.parse(new Buffer([4, 1, 0, 1, 0]))
163+
.word8lu('dataLength')
164+
.tap(function (vars) {
165+
var getInt;
166+
if (vars.dataLength <= 4) {
167+
getInt = "word" + (8 * vars.dataLength) + "lu";
168+
this[getInt]('data');
169+
} else {
170+
this.skip(vars.dataLength);
171+
}
172+
})
173+
.vars;
174+
console.log(vars);
175+
176+
//{ dataLength: 4, data: 65537 }
177+
```
139178

140179
b.into(key, cb)
141180
---------------
142181

143182
Like `.tap()`, except all nested actions will assign into a `key` in the `vars`
144183
stash.
145184

185+
``` js
186+
var vars = binary.parse(new Buffer([45, 13, 8]))
187+
.word8lu('avg')
188+
.into('stdDev', function (inner) {
189+
// e.g. q8.8 formatted std dev
190+
this.word8lu('integer')
191+
.word8lu('decimal');
192+
console.log("inner", inner)
193+
})
194+
.vars;
195+
console.log("outer", vars);
196+
197+
//inner { integer: 13, decimal: 8 }
198+
//outer { avg: 45, stdDev: { integer: 13, decimal: 8 } }
199+
```
200+
201+
146202
b.loop(cb)
147203
----------
148204

149205
Loop, each time calling `cb(end, vars)` for function `end` and the variable
150206
stash with `this` set to a new chain for nested parsing. The loop terminates
151207
once `end` is called.
152208

209+
``` js
210+
var entries = [];
211+
var vars = binary.parse(new Buffer([3, 1, 97, 3, 99, 97, 116, 4, 119, 105, 110, 115]))
212+
.word8lu('wordCount')
213+
.loop(function (end, inner) {
214+
this.word8lu('length')
215+
.string('word', inner.length)
216+
.vars;
217+
entries.push(this.vars.word.toString('ascii'));
218+
if (entries.length >= inner.wordCount) {
219+
end();
220+
}
221+
console.log("inner", inner);
222+
})
223+
.vars;
224+
console.log("outer", vars);
225+
console.log("entries", entries);
226+
227+
//inner { wordCount: 3, length: 1, word: 'a' }
228+
//inner { wordCount: 3, length: 3, word: 'cat' }
229+
//inner { wordCount: 3, length: 4, word: 'wins' }
230+
//outer { wordCount: 3, length: 4, word: 'wins' }
231+
//entries [ 'a', 'cat', 'wins' ]
232+
```
233+
234+
b.string(key, size)
235+
---------
236+
237+
Read <size> bytes as a utf8 string, or read until end of buffer if size not specified
238+
239+
``` js
240+
var vars = binary.parse(new Buffer([97, 32, 99, 97, 116, 32, 119, 105, 110, 115]))
241+
.string('res')
242+
.vars;
243+
console.log(vars);
244+
245+
// { res: 'a cat wins' }
246+
```
247+
248+
b.cstring(key, size)
249+
---------
250+
251+
Read <size> bytes as a null-terminated utf8 string (slices off the null character and anything after it, or last character if no null found)
252+
253+
``` js
254+
var vars = binary.parse(new Buffer([97, 32, 99, 97, 116, 32, 119, 105, 110, 115, 0]))
255+
.cstring('res')
256+
.vars;
257+
console.log(vars);
258+
259+
// { res: 'a cat wins' }
260+
```
261+
262+
b.skip(size)
263+
---------
264+
265+
Skip <size> bytes
266+
267+
``` js
268+
var vars = binary.parse(new Buffer([5, 13, 80]))
269+
.word8lu('count')
270+
.skip(1)
271+
.word8lu('port')
272+
.vars;
273+
console.log(vars);
274+
275+
//{ count: 5, port: 80 }
276+
```
277+
278+
b.tell()
279+
---------
280+
281+
Return the current offset in the buffer
282+
283+
``` js
284+
var pos = binary.parse(new Buffer([5, 0, 80, 11, 184]))
285+
.word8lu('count')
286+
.tell();
287+
console.log(pos);
288+
289+
// 1
290+
```
291+
153292
b.flush()
154293
---------
155294

156295
Clear the variable stash entirely.
296+
``` js
297+
var pos = binary.parse(new Buffer([5, 0, 80, 11, 184]))
298+
.word8lu('preFlush')
299+
.word8lu('preFlush2)
300+
.flush()
301+
.word8lu('postFlush')
302+
.word8lu('postFlush2')
303+
.tap(function (inner) {
304+
console.log("inner", inner);
305+
})
306+
.vars;
307+
308+
console.log("outer:", vars);
309+
310+
//inner { postFlush: 80, postFlush2: 11 }
311+
//outer: { preFlush: 5 } //not sure why
312+
```
157313
158314
installation
159315
============
160316
161-
To install with [npm](http://github.com/isaacs/npm):
317+
To install with [npm](http://github.com/npm/npm):
162318
163319
```
164-
npm install binary
320+
npm install binary@Casear/node-binary#0.3.0
165321
```
166322
167323
notes

0 commit comments

Comments
 (0)