Browse Source

tools/hexdump: make --output work

Aqua-sama 1 month ago
parent
commit
f1501b3733
Signed by: Aqua-sama <aqua@iserlohn-fortress.net> GPG Key ID: 5378B8349C1D5ADA
1 changed files with 9 additions and 8 deletions
  1. 9
    8
      tools/hexdump.py

+ 9
- 8
tools/hexdump.py View File

@@ -1,34 +1,35 @@
1 1
 #!/usr/bin/env python3
2 2
 
3 3
 import argparse
4
+import sys
4 5
 from functools import partial
5 6
 
6 7
 parser = argparse.ArgumentParser(description='Convert a file to C array')
7 8
 parser.add_argument('input', type=str, help='Input file')
8
-parser.add_argument('output', type=str, help='Output file')
9
+parser.add_argument('--output', type=argparse.FileType('wt'), default=sys.stdout, help='Output file')
9 10
 parser.add_argument('--array-type', type=str, default='const unsigned char', help='Array type')
10 11
 parser.add_argument('--length-type', type=str, default='const unsigned int', help='Length type')
11 12
 parser.add_argument('--name', type=str, default='a', help='Array name')
12 13
 
13 14
 args=parser.parse_args()
14 15
 
15
-print("{} {}[] = {{".format(args.array_type, args.name))
16
+print("{} {}[] = {{".format(args.array_type, args.name), file=args.output)
16 17
 
17 18
 n = 0
18 19
 
19 20
 with open(args.input, "rb") as in_file:
20 21
   for c in iter(partial(in_file.read, 1), b''):
21 22
     if n % 16 == 0:
22
-      print("    ", end='')
23
+      print("    ", end='', file=args.output)
23 24
 
24
-    print("0x%02X," % ord(c), end='')
25
+    print("0x%02X," % ord(c), end='', file=args.output)
25 26
 
26 27
     n += 1
27 28
     if n % 16 == 0:
28
-      print("")
29
+      print("", file=args.output)
29 30
     else:
30
-      print(" ", end='')
31
+      print(" ", end='', file=args.output)
31 32
 
32
-print("\n};")
33
-print("{} {}_len = {};".format(args.length_type, args.name, n))
33
+print("\n};", file=args.output)
34
+print("{} {}_len = {};".format(args.length_type, args.name, n), file=args.output)
34 35
 

Loading…
Cancel
Save