[glib] glib-mkenums: add --output option to write output to a file



commit ac54db2ee27d7ae105375256ac13d8f6ccaa9e59
Author: Tim-Philipp Müller <tim centricular com>
Date:   Sun Aug 21 20:03:15 2016 +0100

    glib-mkenums: add --output option to write output to a file
    
    https://bugzilla.gnome.org/show_bug.cgi?id=770175

 gobject/glib-mkenums.in |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
---
diff --git a/gobject/glib-mkenums.in b/gobject/glib-mkenums.in
index 357ef90..3d5945f 100755
--- a/gobject/glib-mkenums.in
+++ b/gobject/glib-mkenums.in
@@ -2,6 +2,9 @@
 
 use warnings;
 use File::Basename;
+use File::Copy "move";
+use File::Temp;
+use Cwd;
 use Safe;
 
 # glib-mkenums.pl 
@@ -25,6 +28,8 @@ my $firstenum = 1;            # Is this the first enumeration per file?
 my @entries;                   # [ $name, $val ] for each entry
 my $sandbox = Safe->new;        # sandbox for safe evaluation of expressions
 
+my $output_file;               # Filename to write result into
+
 sub parse_trigraph {
     my $opts = shift;
     my @opts;
@@ -154,6 +159,7 @@ sub usage {
     print "  --vtail <text>               Value tail, produced after iterating over enum values\n";
     print "  --comments <text>            Comment structure\n";
     print "  --template file              Template file\n";
+    print "  --output file                Output file\n";
     print "  -v, --version                Print version informations\n\n";
     print "Production text substitutions:\n";
     print "  \@EnumName\@            PrefixTheXEnum\n";
@@ -247,12 +253,23 @@ while ($_=$ARGV[0],/^-/) {
     elsif (/^--vprod$/)                      { $vprod = $vprod . shift }
     elsif (/^--vtail$/)                      { $vtail = $vtail . shift }
     elsif (/^--comments$/)                   { $comment_tmpl = shift }
+    elsif (/^--output$/)                     { $output = shift }
     elsif (/^--help$/ || /^-h$/ || /^-\?$/)  { usage; }
     elsif (/^--version$/ || /^-v$/)          { version; }
     else { usage; }
     last if not defined($ARGV[0]);
 }
 
+if (defined ($output)) {
+    my($out_fn, $out_dir, $out_suffix) = fileparse($output, qr{\.\w+$});
+    if ($out_dir eq '') { $out_dir = cwd(); }
+
+    $out_suffix =~ s/^\./_/;   # .foo -> _foo
+
+    $OUTPUT = File::Temp->new("$out_fn$out_suffix\_XXXXXX", DIR => $out_dir, UNLINK => 0);
+    select $OUTPUT;         # Make all print calls from here on go to OUTPUT
+}
+
 # put auto-generation comment
 {
     my $comment = $comment_tmpl;
@@ -563,3 +580,12 @@ if (length($ftail)) {
     $comment =~ s/\@comment\@/Generated data ends here/;
     print "\n" . $comment . "\n\n";
 }
+
+if (defined ($output)) {
+    select STDOUT;
+    my $tmpfilename = $OUTPUT->filename;
+    close ($OUTPUT)
+      || warn "Closing output file $tmpfilename failed: $!";
+    move ($tmpfilename, $output)
+      || die "Could not rename $tmpfilename to $output: $!";
+}


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