File: insert-file-as-string.pl

package info (click to toggle)
xmlroff 0.6.2-1.4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 37,208 kB
  • sloc: ansic: 178,246; xml: 109,155; sh: 8,973; makefile: 1,332; perl: 30
file content (78 lines) | stat: -rwxr-xr-x 1,611 bytes parent folder | download | duplicates (5)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl

# Fo
# insert-file-as-string.pl: Insert a file into a C program as a string
#
# Copyright (C) 2006 Sun Microsystems
#
# $Id: insert-file-as-string.pl,v 1.1 2006/04/16 20:27:09 tonygraham Exp $
#
# See COPYING for the status of this software.
#

# Reads a text file, converts it into a form that is safe to include
# in a C source file, then reads a C source code file and replaces all
# occurrences of a string ('REPLACE_ME' by default) with the string
# form of the text file.  Result is printed to STDOUT.

############################################################
# Library modules
use Getopt::Long;

############################################################
# Constants
#
# Usage statement
$cUsage = <<"EndOfUsage";
Usage:
perl $0 --file <file> <c-file>

where:
  file   = File to insert
  c-file = C source code file into which to insert file
EndOfUsage

############################################################
# Global variables

# File
$gFile = '';
# Token to replace
$gToken = 'REPLACE_ME';


############################################################
# Main program

&GetOptions("file=s" => \$gFile,
	    "token:s"      => \$gToken);

if ($gToken eq '') {
    $gToken = "REPLACE_ME";
}

# Die if we don't have any files
if (@ARGV != 1 || $gFile eq '') {
    die $cUsage;
}

open(FILE, $gFile) or die "Couldn't open file: \"", $gFile, "\"";

undef $/;

$gFileString = <FILE>;

$/ = "\n";
chomp $gFileString;

$gFileString =~ s/"/\\"/g;

$gFileString =~ s/^/"/mg;
$gFileString =~ s/$/\\n"/mg;

#print $gFileString;

while (<>) {
    s/$gToken/$gFileString/g;
    print $_
}