diff src/gearsTools/lib/Gears/Util.pm @ 110:8c7c1ea49f21

impl auto gen context tools
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Wed, 27 Nov 2019 21:21:42 +0900
parents b84aac4ab529
children b9df8ea87b42
line wrap: on
line diff
--- a/src/gearsTools/lib/Gears/Util.pm	Thu Nov 21 21:10:25 2019 +0900
+++ b/src/gearsTools/lib/Gears/Util.pm	Wed Nov 27 21:21:42 2019 +0900
@@ -41,7 +41,7 @@
   my ($file,$code_verbose) = @_;
   my $ir  = {};
 
-  _file_checking($file);
+  Gears::Util->file_checking($file);
   open my $fh, '<', $file;
   my $line = <$fh>;
 
@@ -63,26 +63,21 @@
       next;
     }
     next if ($line =~ /^\s+$/);
-    next if ($line =~ m[//|}]);
+    next if ($line =~ m[^//]);
+    next if ($line =~ m[^\}\s*$ir->{name};]);
 
     if ($line =~ m|__code (\w+)\(([()\.\*\s\w,_]+)\)|) {
-      unless ($code_verbose) {
-        push(@{$ir->{codes}},$1);
-        next;
-      }
-      push(@{$ir->{codes}}, [$1,$2]);
-      next;
+      $line = "enum Code $1;\n";
     }
 
-    $line =~ s/\s*([\w\s\*]+);\s*/$1/;
-    push(@{$ir->{data}},$1);
+    push(@{$ir->{content}},$line);
   }
 
   return $ir;
 }
 
-sub _file_checking {
-  my $file_name = shift;
+sub file_checking {
+  my ($class, $file_name) = @_;
   unless (-f $file_name) {
     croak "invalid filepath :$file_name\n";
   }
@@ -116,4 +111,41 @@
   return $header_file;
 }
 
+sub find_headers_path {
+  my $class = shift;
+  my $find_path = shift // ".";
+
+  my @files;
+  find( { wanted => sub { push @files, $_ if /\.h/ }, no_chdir => 1 }, $find_path);
+
+  return \@files;
+}
+
+sub h2context_str {
+  my ($class, $h2context) = @_;
+  my $context = '';
+  my $space = '    ';
+
+  $context =  "${space}struct $h2context->{name} {\n";
+  my $content_space;
+  if (exists $h2context->{content}){
+    my @chars = split //, $h2context->{content}->[0];
+    for my $w (@chars) {
+      last if ($w !~ /\s/);
+      $content_space .= $w;
+    }
+  }
+
+  unless (defined $content_space) {
+    $content_space = "";
+  }
+
+  for my $c (@{$h2context->{content}}) {
+    $c =~ s/$content_space//;
+    $context .= "${space}${space}$c";
+  }
+  $context .= "${space}} $h2context->{name};\n";
+  return $context;
+}
+
 1;