OSDN Git Service

Add Bluray support
[handbrake-jp/handbrake-jp-git.git] / gtk / src / quotestring.py
1 #! /usr/bin/python
2
3 import re
4 import getopt
5 import sys
6
7 def usage():
8         print >> sys.stderr, (
9                 "Usage: %s <input> [output]\n"
10                 "Summary:\n"
11                 "    Creates a quoted string suitable for inclusion in a C char*\n\n"
12                 "Options:\n"
13                 "    <input>   Input file to quote\n"
14                 "    <output>  Output quoted string [stdout]\n"
15                 % sys.argv[0]
16         )
17
18 def main():
19         global inc_list
20
21         OPTS = ""
22         try:
23                 opts, args = getopt.gnu_getopt(sys.argv[1:], OPTS)
24         except getopt.GetoptError, err:
25                 print >> sys.stderr, str(err)
26                 usage()
27                 sys.exit(2)
28
29         for o, a in opts:
30                 usage()
31                 assert False, "unhandled option"
32
33         if len(args) > 2 or len(args) < 1:
34                 usage()
35                 sys.exit(2)
36
37         try:
38                 infile = open(args[0])
39         except Exception, err:
40                 print >> sys.stderr, ( "Error: %s"  % str(err) )
41                 sys.exit(1)
42
43         if len(args) > 1:
44                 try:
45                         outfile = open(args[1], "w")
46                 except Exception, err:
47                         print >> sys.stderr, ( "Error: %s"  % str(err))
48                         sys.exit(1)
49         else:
50                 outfile = sys.stdout
51
52         ss = infile.read()
53         ss = re.sub("\"", "\\\"", ss)
54         pattern = re.compile("$", re.M)
55         # the replacement string below seems a bit strange, but it seems to be
56         # the only way to get the litteral chars '\' 'n' inserted into the string
57         ss = re.sub(pattern, "\\\\n\"", ss)
58         pattern = re.compile("^", re.M)
59         ss = re.sub(pattern, "\"", ss)
60         outfile.write(ss)
61
62 main()
63