[goffice] tools: copy Gnumeric's embedder here



commit 57ff9482e6a7e9d9081eaec9d1cdaed3db49067a
Author: Morten Welinder <terra gnome org>
Date:   Thu Dec 1 15:23:27 2011 -0500

    tools: copy Gnumeric's embedder here

 tools/Makefile.am |    2 +-
 tools/embedder    |  134 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 1 deletions(-)
---
diff --git a/tools/Makefile.am b/tools/Makefile.am
index e1179d4..83c6877 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -1 +1 @@
-EXTRA_DIST = dumpdef.pl
+EXTRA_DIST = dumpdef.pl embedder
diff --git a/tools/embedder b/tools/embedder
new file mode 100644
index 0000000..4c033fa
--- /dev/null
+++ b/tools/embedder
@@ -0,0 +1,134 @@
+#!/usr/bin/perl
+
+use strict;
+use Getopt::Long;
+use IO::Compress::Gzip qw(gzip $GzipError);
+
+my $myself = $0;
+$myself =~ s|^.*/||;
+
+my $WIDTH = 70;
+my $regfunc = undef;
+my $regfuncstatic = 0;
+my $regfilefunc = undef;
+my @includes;
+
+&GetOptions("register-function=s" => \$regfunc,
+	    "register-file-function=s" => \$regfilefunc,
+	    "static" => \$regfuncstatic,
+	    "include=s" => \ includes,
+    ) or die "$0: invalid usage -- inquire within\n";
+
+# -----------------------------------------------------------------------------
+
+print "/* Generated by $myself -- do not edit! */\n\n";
+print "#include <gnumeric-config.h>\n";
+print "#include <gnm-rsm.h>\n\n";
+foreach (@includes) {
+    print "#include \"$_\"\n";
+}
+
+my $fileno = 0;
+my $reg = "";
+my $docompress = 0;
+print "static " if $regfuncstatic;
+print "void\n";
+print "$regfunc (void)\n";
+print "{\n";
+foreach my $file (@ARGV) {
+    if ($file eq 'COMPRESS') {
+	$docompress = 1;
+	next;
+    }
+    if ($file eq 'NOCOMPRESS') {
+	$docompress = 0;
+	next;
+    }
+    &embed ($file, $docompress);
+}
+print $reg;
+print "}\n";
+
+sub embed {
+    my ($file, $docompress) = @_;
+
+    print "  /* Embedded file $file */\n";
+
+    my $data;
+    {
+	local (*FIL);
+	local ($/);
+	$/ = undef;
+	open (*FIL, "<$file") or die "$myself: cannot read $file: $!\n";
+	$data = <FIL>;
+    }
+
+    if ($docompress) {
+	my $zdata;
+	gzip \$data => \$zdata
+	    or die "gzip failed: $GzipError\n";
+	$data = $zdata;
+    }
+
+    my $id = "data$fileno";
+    $fileno++;
+
+    &embed_data ($data, $id);
+
+    my $len = length ($data);
+    $reg .= "  $regfilefunc (\"$file\", $id, $len);\n";
+}
+
+sub embed_data {
+    my ($data,$id) = @_;
+
+    my $len = length ($data);
+    if ($len == 0) {
+	print "  static const char ${id}[] = \"\";\n";
+	return;
+    }
+
+    print "  static const char ${id}[] =\n";
+    my $linelen = 0;
+    my $nohex = 0;
+    foreach my $c (split (//, $data)) {
+	if ($linelen > $WIDTH) {
+	    print "\"\n";
+	    $linelen = 0;
+	}
+	if ($linelen == 0) {
+	    print "    \"";
+	    $linelen += 5;
+	} 
+
+	my $thisnohex = $nohex;
+	$nohex = 0;
+
+	my $ci = ord ($c);
+	if ($c eq "\n") {
+	    print "\\n";
+	    $linelen += 2;
+	} elsif ($c eq "\t") {
+	    print "\\t";
+	    $linelen += 2;
+	} elsif ($c eq '"') {
+	    print "\\\"";
+	    $linelen += 2;
+	} elsif ($c eq "\\") {
+	    print "\\\\";
+	    $linelen += 2;
+	} elsif ($ci >= 32 && $ci < 128) {
+	    if ($thisnohex && $c =~ /[a-fA-f0-9]/) {
+		print "\"\"";
+		$linelen += 2;
+	    }
+	    print $c;
+	    $linelen += 1;
+	} else {
+	    printf ("\\x%02x", $ci);
+	    $linelen += 4;
+	    $nohex = 1;
+	}
+    }
+    print "\";\n\n";
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]