xref: /aoo41x/main/solenv/bin/leconvert.pl (revision 7e90fac2)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#**************************************************************
5#
6#  Licensed to the Apache Software Foundation (ASF) under one
7#  or more contributor license agreements.  See the NOTICE file
8#  distributed with this work for additional information
9#  regarding copyright ownership.  The ASF licenses this file
10#  to you under the Apache License, Version 2.0 (the
11#  "License"); you may not use this file except in compliance
12#  with the License.  You may obtain a copy of the License at
13#
14#    http://www.apache.org/licenses/LICENSE-2.0
15#
16#  Unless required by applicable law or agreed to in writing,
17#  software distributed under the License is distributed on an
18#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19#  KIND, either express or implied.  See the License for the
20#  specific language governing permissions and limitations
21#  under the License.
22#
23#**************************************************************
24
25
26my $target_format = "";
27my @filelist;
28#my $debug=1;
29my $debug=0;
30
31parameter_parse(@ARGV);
32print "@filelist\n" if ( $debug );
33foreach my $onefile ( @filelist ) {
34    convert_file( $onefile );
35}
36
37
38sub convert_file
39{
40    my $filename = shift;
41    if ( $target_format eq "dos" ) {
42        $lineend = "\r\n";
43    } else {
44        $lineend = "\n";
45    }
46    open INFILE, "$filename"or die "ERROR: Couldn\'t open $filename for reading.\n";
47    my @lines = <INFILE>;
48    close INFILE;
49
50    foreach my $oneline ( @lines ) {
51        $oneline =~ s/\r*\n*$/$lineend/;
52    }
53
54    open OUTFILE, ">$filename" or die "ERROR: Couldn\'t open $filename for writing.\n";
55    syswrite OUTFILE, join "", @lines;
56    close OUTFILE;
57
58}
59
60sub parameter_parse
61{
62    if ( $target_format eq "" ) {
63        $target_format = shift ;
64        usage() if ( $target_format ne "unix" && $target_format ne "dos" );
65        usage() if ( $#_ == -1 );
66    }
67    foreach my $param ( @_ ) {
68        if ( $param =~ "^@" ) {
69            my $filename = $param;
70            $filename =~ s/^@//;
71            open CMDFILE, "$filename" or die "ERROR: Couldn\'t open $filename for reading.\n";
72            my @filelist = <CMDFILE>;
73            close CMDFILE;
74            parameter_parse( @filelist );
75        } else {
76            push @filelist, $param;
77        }
78    }
79}
80
81sub usage
82{
83    print "Convert text files to the desired lineend convention:\n";
84    print "$0 <unix|dos> <FILE|\@filelist> [more files/lists]\n";
85    exit 1;
86}
87
88