1# Reads in a text file, and performs the necessary escapes so that it
2# can be #included as a static string like:
3#    static const char string_from_textfile[] =
4#    #include "build/textfile.h"
5#    ;
6# This script simply prints the escaped string straight to stdout
7
8from __future__ import print_function
9
10import sys
11
12# Can either be set explicitly, or left blank to auto-detect
13# Except auto-detect doesn't work because the file has been passed
14# through Python text processing, which makes all EOL a \n
15line_end = "\\r\\n"
16
17if __name__ == "__main__":
18    filename = sys.argv[1]
19    for line in open(filename, "r").readlines():
20        if not line_end:
21            for ending in ("\r\n", "\r", "\n"):
22                if line.endswith(ending):
23                    line_end = ending.replace("\r", "\\r").replace("\n", "\\n")
24                    break
25            if not line_end:
26                raise Exception("Couldn't auto-detect line-ending of %s" % filename)
27        line = line.rstrip("\r\n")
28        line = line.replace("\\", "\\\\")
29        line = line.replace('"', '\\"')
30        print('"%s%s"' % (line, line_end))
31