binary_to_c_array.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 #
3 # Copyright 2020-2021 Picovoice Inc.
4 #
5 # You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
6 # file accompanying this source.
7 #
8 # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 # specific language governing permissions and limitations under the License.
11 #
12 
13 import os
14 import struct
15 import sys
16 from argparse import ArgumentParser
17 
18 
19 def main():
20  parser = ArgumentParser()
21 
22  parser.add_argument('--binary_file_path',
23  metavar='INPUT_PATH',
24  type=str,
25  help='the path to the binary file',
26  required=True)
27 
28  parser.add_argument('--array_file_path',
29  metavar='OUTPUT_PATH',
30  type=str,
31  default=os.path.join(os.path.dirname(__file__), "c_array.txt"),
32  help='the path to the output text file')
33 
34  args = parser.parse_args()
35 
36  if not os.path.exists(args.binary_file_path):
37  print("[ERROR] Please enter a valid file as the input!")
38  return
39 
40  indent = 8
41  line_width = 120
42 
43  with open(args.binary_file_path, 'rb') as f:
44  array = f.read()
45  res = list()
46 
47  array = ['0x%s' % z.hex() for z in struct.unpack('%dc' % len(array), array)]
48 
49  row = ' ' * indent
50  last_x = 0
51  for x in array:
52  if len(row) >= line_width:
53  row = row.rsplit(', ', maxsplit=1)[0] + ','
54  res.append(row)
55  row = ' ' * indent + last_x
56 
57  if row != ' ' * indent:
58  row += ', '
59  row += x
60 
61  last_x = x
62 
63  if row != ' ' * indent:
64  res.append(row)
65  res.append('')
66 
67  with open(args.array_file_path, 'w') as f_out:
68  f_out.write('\n'.join(res))
69 
70 
71 if __name__ == '__main__':
72  main()
main
Definition: main.py:1
binary_to_c_array.main
def main()
Definition: binary_to_c_array.py:19


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:47