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