changeset 276:793a266bf3c7

add overwrrite mode at trans_impl
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 07 Feb 2020 14:34:14 +0900
parents 173753022721
children 9cbd1ecac10d 6d96bba13d5d
files src/gearsTools/trans_impl.pl
diffstat 1 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/gearsTools/trans_impl.pl	Thu Feb 06 20:31:43 2020 +0900
+++ b/src/gearsTools/trans_impl.pl	Fri Feb 07 14:34:14 2020 +0900
@@ -23,12 +23,19 @@
 my $stdout    = *STDOUT;
 
 if ($opt{w}) {
+    if(-f $output_file) {
+      update_file($output_file, $inter_ir, $impl_ir, $impl_file);
+      exit 0;
+    }
     open $stdout, '>', $output_file;
 } elsif ($opt{o}) {
+    if(-f $opt{o}) {
+      update_file($opt{o}, $inter_ir, $impl_ir, $impl_file);
+      exit 0;
+    }
     open $stdout, '>', $opt{o};
 }
 
-
 emit_include_part($stdout, $inter_ir->{name});
 emit_impl_header_in_comment($stdout, $impl_file);
 emit_constracutor($stdout,$impl_ir,$inter_ir);
@@ -177,3 +184,29 @@
   }
   print $out "}\n\n";
 }
+
+sub update_file {
+    my ($output_file, $inter_ir, $impl_ir, $impl_file) = @_;
+    my $under_code = collection_save_code_gears($output_file,$inter_ir->{name});
+    open my $fh, '>', $output_file;
+    emit_include_part($fh, $inter_ir->{name});
+    emit_impl_header_in_comment($fh, $impl_file);
+    emit_constracutor($fh,$impl_ir,$inter_ir);
+    map { print $fh $_ } @{$under_code};
+    close $fh;
+}
+
+sub collection_save_code_gears {
+  my ($output_file,$interface_name) = @_;
+  open my $fh, '<', $output_file;
+  while (my $line = <$fh>) {
+    if ($line =~ /\s*return $interface_name;\s*/) {
+      $line = <$fh>; # } skip...
+      last;
+    }
+  }
+
+  my @res;
+  push(@res, <$fh>);
+  return \@res;
+}