summaryrefslogtreecommitdiffstats
path: root/TOOLS/file2string.pl
blob: 341bb06fd6be1b86729ceb3d57f06817f4c66524 (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
#! /usr/bin/env perl

use strict;
use warnings;

# 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.
# FIXME: why not a char array?

# treat only alphanumeric and punctuations (excluding " and ?) as safe
my $unsafe_chars = qr{[^][A-Za-z0-9!#%&'()*+,./:;<=>^_{|}~ -]};

for my $file (@ARGV) {
    open my $fh, '<:raw', $file or next;
    print "/* Generated from $file */\n";
    while (<$fh>) {
        # replace unsafe chars with their equivalent octal escapes
        s/($unsafe_chars)/\\@{[sprintf '%03o', ord($1)]}/gos;
        print "\"$_\"\n"
    }
    close $fh;
}