2020-08-12 15:21:49 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-12-17 12:24:05 +00:00
|
|
|
|
|
|
|
# Convert the contents of a file into a C string constant.
|
|
|
|
# Note that the compiler will implicitly add an extra 0 byte at the end
|
|
|
|
# of every string, so code using the string may need to remove that to get
|
|
|
|
# the exact contents of the original file.
|
|
|
|
|
2017-06-24 08:07:48 +00:00
|
|
|
#
|
|
|
|
# This file is part of mpv.
|
|
|
|
#
|
|
|
|
# mpv is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# mpv is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2024-09-21 17:21:02 +00:00
|
|
|
import os
|
2016-12-17 12:24:05 +00:00
|
|
|
import sys
|
|
|
|
|
2016-12-17 16:12:56 +00:00
|
|
|
def file2string(infilename, infile, outfile):
|
|
|
|
outfile.write("// Generated from %s\n\n" % infilename)
|
|
|
|
|
2020-11-22 14:27:02 +00:00
|
|
|
conv = ["\\%03o" % c for c in range(256)]
|
2016-12-17 12:24:05 +00:00
|
|
|
safe_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" \
|
2020-11-22 14:27:02 +00:00
|
|
|
"0123456789!#%&'()*+,-./:;<=>[]^_{|}~ "
|
2016-12-17 16:12:56 +00:00
|
|
|
|
2016-12-17 12:24:05 +00:00
|
|
|
for c in safe_chars:
|
|
|
|
conv[ord(c)] = c
|
|
|
|
for c, esc in ("\nn", "\tt", r"\\", '""'):
|
2024-09-15 00:55:28 +00:00
|
|
|
conv[ord(c)] = "\\" + esc
|
2016-12-17 12:24:05 +00:00
|
|
|
for line in infile:
|
2024-09-15 00:55:28 +00:00
|
|
|
outfile.write('"' + "".join(conv[c] for c in line) + '"\n')
|
2016-12-17 12:24:05 +00:00
|
|
|
|
2016-12-17 16:12:56 +00:00
|
|
|
if __name__ == "__main__":
|
2024-09-15 00:55:28 +00:00
|
|
|
with open(sys.argv[1], "rb") as infile, open(sys.argv[2], "w") as outfile:
|
2024-09-21 17:21:02 +00:00
|
|
|
file2string(os.path.relpath(sys.argv[1], sys.argv[3]), infile, outfile)
|