File: resource-embedder

package info (click to toggle)
goffice 0.10.57-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,136 kB
  • sloc: ansic: 114,820; sh: 4,993; makefile: 1,241; perl: 235; xml: 232
file content (102 lines) | stat: -rwxr-xr-x 2,530 bytes parent folder | download | duplicates (6)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl

# Copyright (C) 2011 Morten Welinder <terra@gnome.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) version 3.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

# This program is fairly dumb: we do search-and-replace on the text of
# an xml file.  Don't expect anything fancy to work.

use strict;
use Getopt::Long;
use English;

my $myself = $0;
$myself =~ s|^.*/||;

my $filedir = ".";

&GetOptions("file-dir=s" => \$filedir,
    ) or die "$0: invalid usage -- inquire within\n";

my $srcfile = shift @ARGV;
my $dstfile = shift @ARGV;

my $data = &read_file ($srcfile, undef);

while ($data =~ m{(<service\s+type="resource"\s+id=".*")(\s+)file="(.*)"(\s*/>)}) {
    my $filename = $3;
    print STDERR "Embedding $filename\n";
    my $file_data = &read_file ($filename, $filedir);
    my $encoded_data = &xml_encode ($file_data);
    $data = $PREMATCH . $1 . $2 . "data=\"$encoded_data\"" . $4 . $POSTMATCH;
}

&write_file ($dstfile, $data);


sub read_file
{
    my ($filename,$dir) = @_;

    if ($filename !~ m|^/| && defined $dir) {
	$filename = "$dir/$filename";
    }

    local (*FIL);
    local $/ = undef;
    open (*FIL, "<$filename") or die "$myself: cannot read $filename: $!\n";
    my $data = <FIL>;
    close (*FIL);
    return $data;
}

sub write_file
{
    my ($filename,$data) = @_;

    local (*FIL);
    open (*FIL, ">$filename") or die "$myself: cannot write $filename: $!\n";
    print FIL $data;
    close (*FIL);
}

sub xml_encode {
    my ($s) = @_;

    my $res = "";

    foreach my $c (split (//, $s)) {
	my $ci = ord ($c);
	if ($c eq "&") {
	    $res .= '&amp;';
	} elsif ($c eq "<") {
	    $res .= '&lt;';
	} elsif ($c eq ">") {
	    $res .= '&gt;';
	} elsif ($c eq '"') {
	    $res .= '&quot;';
	} elsif ($ci >= 32 && $ci < 127) {
	    $res .= $c;
	} elsif ($ci == 7) {
	    die "$myself: cannot embed character 7\n";
	} else  {
	    $res .= '&#' . $ci . ";";
	}
    }

    return $res;
}