summaryrefslogtreecommitdiffstats
path: root/avahi-daemon/embedd-file.py
blob: eaa250f7230b8f1f9d17773d56244ecedbc7d1ac (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python

import os, sys

def usage(ret = 0):
    print "%s [-s] <file> <symbol>" % sys.argv[0]
    sys.exit(ret)

args = sys.argv[1:]

use_static = False

if len(args) >= 1 and args[0] == '-s':
    use_static = True
    args = args[1:]

if len(args) >= 1 and args[0] == '-h':
    usage()

if len(args) != 2:
    sys.stderr("Wrong number of arguments")
    usage(1)

f = file(args[0])
t = f.read()
f.close()

out = sys.stdout

if use_static:
    out.write("static ")

out.write('const char %s[] = \n"' % args[1]);

n = 0

for c in t:
    if c == '\n':
        out.write('\\n"\n"')
        n = 0
    elif c == '"':
        out.write('\\"')
        n += 2
    elif ord(c) < 32 or ord(c) >= 127:
        out.write('\\x%02x' % ord(c))
        n += 4
    else:
        out.write(c)
        n += 1

    if n >= 76:
        out.write('"\n"')
        n = 0
        
out.write('";\n');