summaryrefslogtreecommitdiff
path: root/src/wasm2c_tmpl.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/wasm2c_tmpl.py')
-rwxr-xr-xsrc/wasm2c_tmpl.py86
1 files changed, 43 insertions, 43 deletions
diff --git a/src/wasm2c_tmpl.py b/src/wasm2c_tmpl.py
index 6f9c8225..b854d887 100755
--- a/src/wasm2c_tmpl.py
+++ b/src/wasm2c_tmpl.py
@@ -18,64 +18,64 @@
from __future__ import print_function
import argparse
try:
- from cStringIO import StringIO
+ from cStringIO import StringIO
except ImportError:
- from io import StringIO
+ from io import StringIO
import os
import sys
def EscapeCString(s):
- out = ''
- for b in bytearray(s.encode('utf-8')):
- if b in (34, 92):
- # " or \
- out += '\\' + chr(b)
- elif b == 10:
- # newline
- out += '\\n'
- elif 32 <= b <= 127:
- # printable char
- out += chr(b)
- else:
- # non-printable; write as \xab
- out += '\\x%02x' % b
+ out = ''
+ for b in bytearray(s.encode('utf-8')):
+ if b in (34, 92):
+ # " or \
+ out += '\\' + chr(b)
+ elif b == 10:
+ # newline
+ out += '\\n'
+ elif 32 <= b <= 127:
+ # printable char
+ out += chr(b)
+ else:
+ # non-printable; write as \xab
+ out += '\\x%02x' % b
- return out
+ return out
def main(args):
- arg_parser = argparse.ArgumentParser()
- arg_parser.add_argument('-o', '--output', metavar='PATH',
- help='output file.')
- arg_parser.add_argument('file', help='input file.')
- options = arg_parser.parse_args(args)
+ arg_parser = argparse.ArgumentParser()
+ arg_parser.add_argument('-o', '--output', metavar='PATH',
+ help='output file.')
+ arg_parser.add_argument('file', help='input file.')
+ options = arg_parser.parse_args(args)
- section_name = None
- output = StringIO()
+ section_name = None
+ output = StringIO()
- output.write('/* Generated from \'%s\' by wasm2c_tmpl.py, do not edit! */\n' %
- os.path.basename(options.file))
+ output.write('/* Generated from \'%s\' by wasm2c_tmpl.py, do not edit! */\n' %
+ os.path.basename(options.file))
- with open(options.file) as f:
- for line in f.readlines():
- if line.startswith('%%'):
- if section_name is not None:
- output.write(';\n\n')
- section_name = line[2:-1]
- output.write('const char SECTION_NAME(%s)[] =\n' % section_name)
- else:
- output.write('"%s"\n' % EscapeCString(line))
+ with open(options.file) as f:
+ for line in f.readlines():
+ if line.startswith('%%'):
+ if section_name is not None:
+ output.write(';\n\n')
+ section_name = line[2:-1]
+ output.write('const char SECTION_NAME(%s)[] =\n' % section_name)
+ else:
+ output.write('"%s"\n' % EscapeCString(line))
- output.write(';\n')
- if options.output:
- with open(options.output, 'w') as outf:
- outf.write(output.getvalue())
- else:
- sys.stdout.write(output.getvalue())
+ output.write(';\n')
+ if options.output:
+ with open(options.output, 'w') as outf:
+ outf.write(output.getvalue())
+ else:
+ sys.stdout.write(output.getvalue())
- return 0
+ return 0
if __name__ == '__main__':
- sys.exit(main(sys.argv[1:]))
+ sys.exit(main(sys.argv[1:]))