summaryrefslogtreecommitdiffstats
path: root/scripts/quotestring.py
blob: e3712f140b947d5c7ca851552beed4ad3cabb445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/python

import re
import argparse
import sys


def main():
    parser = argparse.ArgumentParser(description='Creates a quoted string suitable for inclusion in a C char*')
    parser.add_argument('infile', metavar='<input>', type=argparse.FileType('r'), help='Input file to quote')
    parser.add_argument('outfile', metavar='<output>', type=argparse.FileType('w'), nargs='?',
                        default=sys.stdout, help='Output quoted string [stdout]')
    args = parser.parse_args()

    ss = args.infile.read()
    ss = re.sub(r'\\', r'\\\\', ss)
    ss = re.sub(r'"', r'\\"', ss)
    pattern = re.compile("$", re.M)
    ss = re.sub(pattern, r'\\n"', ss)
    pattern = re.compile("^", re.M)
    ss = re.sub(pattern, "\"", ss)
    args.outfile.write(ss)

main()