Skip to content

Commit 1adffdd

Browse files
committed
Make examples work from any directory
1 parent 6d49eb6 commit 1adffdd

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

example/bmp.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Parser } from "../lib/binary_parser";
21
import { readFile } from "fs";
2+
import { join } from "path";
3+
4+
import { Parser } from "../lib/binary_parser";
35

46
// C structure BITMAPFILEHEADER
57
// typedef struct tagBITMAPFILEHEADER {
@@ -56,6 +58,6 @@ const bmpFile = new Parser()
5658
type: bmpInfoHeader,
5759
});
5860

59-
readFile("test.bmp", (_, data) => {
61+
readFile(join(__dirname, "test.bmp"), (_, data) => {
6062
console.log(bmpFile.parse(data));
6163
});

example/classfile.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Parser } from "../lib/binary_parser";
21
import { readFile } from "fs";
2+
import { join } from "path";
33
import { inspect } from "util";
44

5+
import { Parser } from "../lib/binary_parser";
6+
57
const ConstantClassInfo = Parser.start().uint16be("name_index");
68

79
const ConstantFieldrefInfo = Parser.start()
@@ -83,6 +85,6 @@ const ClassFile = Parser.start()
8385
},
8486
});
8587

86-
readFile("Hello.class", function (_, data) {
88+
readFile(join(__dirname, "Hello.class"), (_, data) => {
8789
console.log(inspect(ClassFile.parse(data), { depth: null, colors: true }));
8890
});

example/elf32.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Parser } from "../lib/binary_parser";
21
import { readFile } from "fs";
2+
import { join } from "path";
33
import { inspect } from "util";
44

5+
import { Parser } from "../lib/binary_parser";
6+
57
const ELF32ProgramHeader = new Parser()
68
.endianess("little")
79
.uint32("type")
@@ -80,7 +82,7 @@ const ELF32Header = new Parser()
8082
},
8183
});
8284

83-
readFile("hello", function (_, data) {
85+
readFile(join(__dirname, "hello"), (_, data) => {
8486
const result = ELF32Header.parse(data);
8587
console.log(inspect(result, { depth: null, colors: true }));
8688
});

example/jpg.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Parser } from "../lib/binary_parser";
21
import { readFile } from "fs";
2+
import { join } from "path";
33
import { inspect } from "util";
44

5+
import { Parser } from "../lib/binary_parser";
6+
57
const SOI = Parser.start();
68

79
const EOI = Parser.start();
@@ -106,6 +108,6 @@ const JPEG = Parser.start().array("segments", {
106108
readUntil: "eof",
107109
});
108110

109-
readFile("test.jpg", function (_, data) {
111+
readFile(join(__dirname, "test.jpg"), (_, data) => {
110112
console.log(inspect(JPEG.parse(data), { depth: null, colors: true }));
111113
});

example/mbr.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Parser } from "../lib/binary_parser";
21
import { readFile } from "fs";
2+
import { join } from "path";
33
import { inspect } from "util";
44

5+
import { Parser } from "../lib/binary_parser";
6+
57
const chs = new Parser()
68
.uint8("head")
79
.bit2("cylinderHigh")
@@ -39,6 +41,6 @@ const mbrParser = new Parser()
3941
assert: 0x55aa,
4042
});
4143

42-
readFile("raspbian.img", function (_, data) {
44+
readFile(join(__dirname, "raspbian.img"), (_, data) => {
4345
console.log(inspect(mbrParser.parse(data), { depth: null, colors: true }));
4446
});

example/tar.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { Parser } from "../lib/binary_parser";
1+
import { join } from "path";
22
import { readFile } from "fs";
33

4+
import { Parser } from "../lib/binary_parser";
5+
46
function oct2int(s: string): number {
57
return parseInt(s, 8);
68
}
@@ -37,8 +39,6 @@ const tarArchive = new Parser().array("files", {
3739
readUntil: "eof",
3840
});
3941

40-
console.log(tarArchive.getCode());
41-
42-
readFile("test.tar", function (_, data) {
42+
readFile(join(__dirname, "test.tar"), (_, data) => {
4343
console.dir(tarArchive.parse(data), { depth: null, colors: true });
4444
});

0 commit comments

Comments
 (0)