Added options to make Robodoc more customizable.
[robodoc.git] / dos2unix.pl
1 #!/usr/bin/perl -w
2
3 # $Id: dos2unix.pl,v 1.2 2007/04/02 20:29:20 gumpu Exp $
4 #
5 # Convert all files to the unix-line-end convention.
6 # This ensures that all files in the ROBODoc package 
7 # follow the same convention.
8 #
9 use strict;
10 use IO::File;
11 use IO::Dir;
12
13
14 sub fix 
15 {
16     my $path = shift;
17     my $mode = shift;
18     my $file;
19
20     if ( -T $path ) {
21         if ( !( $file = IO::File->new($path, "r") ) ) {
22             print "Can't open $path to read : $!\n"; 
23         } else {
24             # Open in binmode otherwise Perl will do the cr/lf for
25             # us while reading.
26             binmode( $file );
27             my @file_data = <$file>;
28             $file->close();
29             if ( !( grep { /\r/ } @file_data ) ) {
30                 print "$path is OK\n";
31                 # File is OK
32             } else {
33                 print "$path contains CR/LF\n";
34                 if ( $mode eq "test" ) {
35                     print "$path contains CR/LF\n";
36                 } else {
37                     print "Fixing: $path\n";
38                     map { s/\r//g; } (@file_data);
39                     $file = IO::File->new("> $path") or die "Can't open $path to write : $!";
40                     binmode( $file );
41                     print $file @file_data;
42                     $file->close();
43                 }
44             }
45         }
46     }
47 }
48
49 sub scan_directory
50 {
51     my $dirname = shift;
52     my $mode    = shift;
53     my $path;
54     my $dir = IO::Dir->new($dirname) or die "Can't open $dirname : $!";
55     my @files = $dir->read();
56     $dir->close();
57
58     foreach my $filename ( sort @files ) {
59         $path = "$dirname/$filename";
60         if ( -f $path ) {
61             fix( $path, $mode );
62         }
63     }
64
65     # Also fix any subdirectories.
66     foreach my $subdirname ( sort @files ) {
67         $path = "$dirname/$subdirname";
68         if ( -d $path ) {
69             unless ( $subdirname =~ m/^\.+$/ ) {
70                 scan_directory( $path, $mode );
71             }
72         }
73     }
74 }
75
76 sub main
77 {
78     my $out = IO::File->new(">fl.txt") or die "can't open fl.txt : $!";
79     scan_directory( ".", "test" );
80     $out->close();
81 }
82
83 main;
84