On Tue, 26 Mar 2002, Jarkko Hietaniemi wrote:

> On Mon, Mar 25, 2002 at 07:04:37PM -0800, William R Ward wrote:
> > [EMAIL PROTECTED] (Neil Bowers) writes:
> > > Recently I had need of a unix2dos and dos2unix. I'd guess we've all
> > > written multiple of versions of these, since there's not much to them.
> > >
> > > This time I had a look at scripts section on CPAN, figuring there'd
> > > be versions there. But there aren't.
> > >
> > > So having knocked up them up, are they worth putting on CPAN?
> > > And if so, what Category should they be given - Text?
> >
> > What, you mean "perl -pe 's/\015\012/\n/g'" and "perl -pe 's/\n/\015\012/g'"?
> > Doesn't seem like it's necessary to me to even bother uploading such a thing.
>
> So you think, but try working around non-UNIX and non-Perl people for
> a while, and the above are deep magic that bestows upon you the aura
> of a mighty guru.
>
> > If you're in the neighborhood though, how about mac2dos, mac2unix, unix2mac,
> > and dos2mac?  (Mac uses \015 only)
>
> What would be cool is to have *one* script that sniffs which CR/LF is
> being used in the source file(s).  The destination CR/LF would be by
> default the execution platform's, but could be forced to be something
> else.  "crlf foo.txt", "crlf -t cr bar.txt".
>
> > --Bill.
> >
> > --
> > William R Ward            [EMAIL PROTECTED]          http://www.wards.net/~bill/
> > -----------------------------------------------------------------------------
> >      If you're not part of the solution, you're part of the precipitate.
>
> --

Apologies, I consider myself an intermediate Perl programmer at best, but
would something like this fit into this category. I realize it has a U*ix
bent.

A Perl script called crlf with the following.

#!/usr/bin/perl -w

use strict;
use File::Basename;

my $dos        = "\015\012";
my $mac        = "\015";
my $unix       = "\n";
my $myPlatform = "$unix";
my $iam        = basename($0);
my $regexp     = "";

if( $iam eq 'crlf' )     {
  while(<STDIN>) {
     chop;
     print "$_" . $myPlatform;
  }
} elsif( $iam eq 'dos2unix' ) { $regexp = "s/$dos/$unix/g"; }
  elsif( $iam eq 'unix2dos' ) { $regexp = "s/$unix/$dos/g"; }
  elsif( $iam eq 'mac2dos'  ) { $regexp = "s/$mac/$dos/g";  }
  elsif( $iam eq 'mac2unix' ) { $regexp = "s/$mac/$unix/g";  }
  elsif( $iam eq 'dos2mac'  ) { $regexp = "s/$dos/$mac/g"; }
  elsif( $iam eq 'unix2mac' ) { $regexp = "s/$unix/$mac/g";
} else {
  die "I do not recognize my own name.";
}

while(<STDIN>) {
   eval $regexp;
   print;
}

exit(0);

Rather than sniffing which eol gets fed in, I assume that if I don't
know, I call the program as 'crlf' and chop the eol character regardless
and print $myPlatform. It would be nice to automatically collect the
platform.

And a set of symbolic links, i.e.,

  $ ls -l crlf dos2unix dos2mac unix2dos unix2mac mac2dos mac2unix
  -rwxr-xr-x   1 uid  gid       654 Mar 26 10:00 crlf
  lrwxrwxrwx   1 uid  gid         4 Mar 26 10:01 dos2mac -> crlf
  lrwxrwxrwx   1 uid  gid         4 Mar 26 10:01 dos2unix -> crlf
  lrwxrwxrwx   1 uid  gid         4 Mar 26 10:01 mac2dos -> crlf
  lrwxrwxrwx   1 uid  gid         4 Mar 26 10:01 mac2unix -> crlf
  lrwxrwxrwx   1 uid  gid         4 Mar 26 10:01 unix2dos -> crlf
  lrwxrwxrwx   1 uid  gid         4 Mar 26 10:01 unix2mac -> crlf

Peace,
Josh


Reply via email to