[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] File conversion script problem



Ero wrote:

> I wrote the following script to convert files from
> one line ending format to another (i.e., Mac2Unix,
> Dos2Mac, etc.).
> 
> 1) Please let me know if you know of a better script
>    to dothis sort of thing.


Well, I won't claim it's better, but it's a slightly
different solution. I check in the first 150 bytes
for a unix or DOS record separator and modify $/
accordingly. Then I read the file as normal, chomp'ing
off the record terminator and reprinting the line with
the Mac newline. This works great on everything I've
tried except for one peculiar file (this was the source
of my "afraid to ask" question a couple of days ago).
I've since decided that there is something distinctly odd
about this one file, because another 45MB file that I
created, that should be of the same format, works just
fine. On the other hand, I ran this on a unix system and
that seemed to work fine on my problem file...

Anyway, here's my script for anyone who cares. Save it
as a droplet and you can drop one file at a time on it.
I intend to make it work with multiple files dropped,
but haven't gotten around to it yet.

WARNING! This version overwrites the original!

Advice, criticisms, and witticisms welcome...

.........................................................

#!perl -w

# Utility to translate unix and DOS end of lines into Macintosh
# carriage returns.
# 
# No warrantees, expressed or implicit!


use File::Copy;

$msg1 = "Is already a MacOS text file.\nNo changes were made.";
$msg2 = "Is an unknown file type.\nNo changes were made.";
$msg3 = "Is not a text file.\nNo changes were made.";

$infnam = $ARGV[0];

# Don't do anything to files that don't pass the basic -T test
if (! -T $infnam) {
    MacPerl::Answer("\n$infnam\n\n$msg3");
    exit 0;                    # Not text, do nothing
}

open(IN, $infnam) || die "Can't open input file!\n";
read(IN, $chunk, 150);      # Read the first 150 bytes
close(IN);

if ($chunk =~ m/\x0d\x0a/) {   # Check for DOS file
    $ftyp = "DOS";
    $/ = "\x0d\x0a";
  }
elsif ($chunk =~ m/\x0a/) {    # Check for unix file
    $ftyp = "unix";
    $/ = "\x0a";
  }
elsif ($chunk =~ m/\x0d/) {    # Check for MacOS file
    MacPerl::Answer("\n$infnam\n\n$msg1");
    exit 0;                    # Mac file type, do nothing
  }
else {
    MacPerl::Answer("\n$infnam\n\n$msg2");
    exit 0;                    # Unknown file type, do nothing
}


$tempfnam = $infnam . ".fxtmp";
copy($infnam,$tempfnam);
open(IN, $tempfnam) || die "Can't open copy!\n";
open(OUT,">$infnam") || die "Can't rewrite original!\n";


# Read one line at a time, chomp off it's record terminator
# and tack on a proper Macintosh line terminator (CR)
while (<IN>) {
    chomp;
    print OUT "$_\n";
}

unlink $tempfnam;

MacPerl::Answer("\n$. lines fixed in $ftyp file:\n\n$infnam");

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch