]> git.meshlink.io Git - catta/blob - avahi-daemon/embedd-file.py
eaa250f7230b8f1f9d17773d56244ecedbc7d1ac
[catta] / avahi-daemon / embedd-file.py
1 #!/usr/bin/python
2
3 import os, sys
4
5 def usage(ret = 0):
6     print "%s [-s] <file> <symbol>" % sys.argv[0]
7     sys.exit(ret)
8
9 args = sys.argv[1:]
10
11 use_static = False
12
13 if len(args) >= 1 and args[0] == '-s':
14     use_static = True
15     args = args[1:]
16
17 if len(args) >= 1 and args[0] == '-h':
18     usage()
19
20 if len(args) != 2:
21     sys.stderr("Wrong number of arguments")
22     usage(1)
23
24 f = file(args[0])
25 t = f.read()
26 f.close()
27
28 out = sys.stdout
29
30 if use_static:
31     out.write("static ")
32
33 out.write('const char %s[] = \n"' % args[1]);
34
35 n = 0
36
37 for c in t:
38     if c == '\n':
39         out.write('\\n"\n"')
40         n = 0
41     elif c == '"':
42         out.write('\\"')
43         n += 2
44     elif ord(c) < 32 or ord(c) >= 127:
45         out.write('\\x%02x' % ord(c))
46         n += 4
47     else:
48         out.write(c)
49         n += 1
50
51     if n >= 76:
52         out.write('"\n"')
53         n = 0
54         
55 out.write('";\n');
56