/[pxelator]/lib/PXElator/file.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/file.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 289 by dpavlin, Thu Aug 20 19:00:13 2009 UTC revision 290 by dpavlin, Wed Aug 26 08:43:40 2009 UTC
# Line 4  use File::Slurp; Line 4  use File::Slurp;
4  use autodie;  use autodie;
5  use Carp qw/carp confess/;  use Carp qw/carp confess/;
6  use File::Path qw//;  use File::Path qw//;
7    use Data::Dump qw/dump/;
8    
9    my $debug = 1;
10    
11  sub mkpath {  sub mkpath {
12          my $file = shift;          my $file = shift;
# Line 18  sub append { Line 21  sub append {
21                  mkpath $file;                  mkpath $file;
22                  write_file $file, $content;                  write_file $file, $content;
23                  my $size = -s $file;                  my $size = -s $file;
24                  carp "append created $size bytes in $file";                  carp "## append created $size bytes in $file";
25                  return $size;                  return $size;
26          }          }
27    
28          my $on_disk = read_file $file;          my $on_disk = read_file $file;
29    
30          my $relaxed_content =~ s{\s+}{\\s+}gs;          my $relaxed_content = $content;
31               $relaxed_content =~ s{\s+}{\\s+}gs;
32    
33          if ( $on_disk !~ m{$relaxed_content} ) {          if ( $on_disk !~ m{$relaxed_content} ) {
34    
35  #               $content =~ s{^[\n\r]+}{\n}s;  #               $content =~ s{^[\n\r]+}{\n}s;
36  #               $content =~ s{[\n\r]*$}{\n}s;  #               $content =~ s{[\n\r]*$}{\n}s;
37    
38                  open($fh, '>>', $file);                  if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) {
39                  print $fh $content;  #                       warn "# insert $file\n$on_disk";
40                  close($fh);                          write_file $file, $on_disk;
41                    } else {
42                  my $size = length($content);  #                       warn "# append $file\n$content\n";
43                            open($fh, '>>', $file);
44                            print $fh $content;
45                            close($fh);
46                    }
47    
48                  carp "append $size bytes to $file";                  carp "## append to $file";
49                  warn $content;                  return -s $file;
                 return $size;  
50          }          }
51  }  }
52    
# Line 49  sub change { Line 56  sub change {
56          my $content = read_file $file;          my $content = read_file $file;
57          if ( $content =~ s{$from}{$to}s ) {          if ( $content =~ s{$from}{$to}s ) {
58                  write_file $file, $content;                  write_file $file, $content;
59                  carp "change $file $from => $to";                  carp "## change $file $from => $to" if $debug;
60                  return 1;                  return 1;
61          } elsif ( $content !~ m{$to}s ) {          } elsif ( $content !~ m{$to}s ) {
62                  confess "can't find $from to change into $to in $file";                  confess "can't find $from to change into $to in $file in ",dump( $content );
63          }          }
64  }  }
65    
# Line 62  sub replace { Line 69  sub replace {
69          write_file $file, $content;          write_file $file, $content;
70  }  }
71    
72    sub copy_once {
73            my ( $from, $to ) = @_;
74            $to ||= $from;
75            return if -e $to;
76            mkpath $to;
77            write_file $to, read_file $from;
78    #       warn "# copy_once $to ", -s $to, " bytes";
79    }
80    
81  1;  1;

Legend:
Removed from v.289  
changed lines
  Added in v.290

  ViewVC Help
Powered by ViewVC 1.1.26