Column Fitting: don't go higher than 15 significant digits by default.
[gnumeric.git] / tools / embedder
blob875fc6d50a49fd0748a81c9f76cfe313512467bd
1 #!/usr/bin/perl
3 # Copyright (C) 2011 Morten Welinder <terra@gnome.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) version 3.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18 # USA
20 use strict;
21 use Getopt::Long;
22 use IO::Compress::Gzip qw(gzip $GzipError);
24 my $myself = $0;
25 $myself =~ s|^.*/||;
27 my $WIDTH = 70;
28 my $regfunc = undef;
29 my $unregfunc = undef;
30 my $regfuncstatic = 0;
31 my $regfilefunc = "go_rsm_register_file";
32 my $unregfilefunc = "go_rsm_unregister_file";
33 my $id_prefix = "";
34 my @includes;
36 &GetOptions("register-function=s" => \$regfunc,
37 "unregister-function=s" => \$unregfunc,
38 "register-file-function=s" => \$regfilefunc,
39 "unregister-file-function=s" => \$unregfilefunc,
40 "static" => \$regfuncstatic,
41 "id-prefix=s" => \$id_prefix,
42 "include=s" => \@includes,
43 ) or die "$0: invalid usage -- inquire within\n";
45 # -----------------------------------------------------------------------------
47 print "/* Generated by $myself -- do not edit! */\n\n";
48 foreach my $include (@includes) {
49 $include = "\"$include\"" unless $include =~ /^[<""]/;
50 print "#include $include\n";
53 my $fileno = 0;
54 my $reg = "";
55 my $unreg = "";
56 my $docompress = 0;
57 print "static " if $regfuncstatic;
58 print "void\n";
59 print "$regfunc (void)\n";
60 print "{\n";
61 foreach my $file (@ARGV) {
62 if ($file eq 'COMPRESS') {
63 $docompress = 1;
64 next;
66 if ($file eq 'NOCOMPRESS') {
67 $docompress = 0;
68 next;
70 &embed ($file, $docompress);
72 print $reg;
73 print "}\n";
75 if (defined $unregfunc) {
76 print "\n";
77 print "static " if $regfuncstatic;
78 print "void\n";
79 print "$unregfunc (void)\n";
80 print "{\n";
81 print $unreg;
82 print "}\n";
85 sub embed {
86 my ($file, $docompress) = @_;
88 print " /* Embedded file $file */\n";
90 my $data;
92 local (*FIL);
93 local ($/);
94 $/ = undef;
95 open (*FIL, "<$file") or die "$myself: cannot read $file: $!\n";
96 $data = <FIL>;
99 if ($docompress) {
100 my $zdata;
101 gzip \$data => \$zdata, TIME => 0,
102 or die "gzip failed: $GzipError\n";
103 $data = $zdata;
106 my $id = "data$fileno";
107 $fileno++;
109 &embed_data ($data, $id);
111 my $res = "$id_prefix$file";
112 my $len = length ($data);
113 $reg .= " $regfilefunc (\"$res\", $id, $len);\n";
114 $unreg .= " $unregfilefunc (\"$res\");\n";
117 sub embed_data {
118 my ($data,$id) = @_;
120 my $len = length ($data);
121 if ($len == 0) {
122 print " static const char ${id}[] = \"\";\n";
123 return;
126 print " static const char ${id}[] =\n";
127 my $linelen = 0;
128 my $nohex = 0;
129 foreach my $c (split (//, $data)) {
130 if ($linelen > $WIDTH) {
131 print "\"\n";
132 $linelen = 0;
134 if ($linelen == 0) {
135 print " \"";
136 $linelen += 5;
137 $nohex = 0;
140 my $thisnohex = $nohex;
141 $nohex = 0;
143 my $ci = ord ($c);
144 if ($c eq "\n") {
145 print "\\n";
146 $linelen += 2;
147 } elsif ($c eq "\t") {
148 print "\\t";
149 $linelen += 2;
150 } elsif ($c eq '"') {
151 print "\\\"";
152 $linelen += 2;
153 } elsif ($c eq "\\") {
154 print "\\\\";
155 $linelen += 2;
156 } elsif ($ci >= 32 && $ci < 127) {
157 if ($thisnohex && $c =~ /[a-fA-F0-9]/) {
158 print "\"\"";
159 $linelen += 2;
161 print $c;
162 $linelen += 1;
163 } else {
164 printf ("\\x%02x", $ci);
165 $linelen += 4;
166 $nohex = 1;
169 print "\";\n\n";