home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!munnari.oz.au!uniwa!cujo!peter
- From: peter@cujo.curtin.edu.au (Peter N Lewis)
- Subject: Re: Wanted: Perl script to convert file names from Unix to DOS
- Message-ID: <1992Dec13.071718.24757@cujo.curtin.edu.au>
- Organization: Curtin University of Technology
- References: <1992Dec9.205342.20218@zip.eecs.umich.edu> <1992Dec10.064406.1201@netcom.com>
- Date: Sun, 13 Dec 1992 07:17:18 GMT
- Lines: 71
-
- jfh@netcom.com (Jack Hamilton) writes:
-
- >>It would be nice if the algorithm used can be
- >>inverted, so that the Unix filenames can be extracted back from their
- >>DOS translation.
-
- >In general, I don't think that will be possible unless the Unix filenames
- >are already in 8x3 format with no unacceptable characters. The only way
- >you could do it reliably would be to save a table and use it for the
- >conversion back.
-
- I did more or less the same thing for converting Mac file names in to
- unix file names. I wanted to guarentee that each unique Mac name was
- converted to exactly one unix name (and thus could be inverted back to
- the Mac file name). I did this by using an associative array thing,
- using the ndbm features in Perl, the code looks like this:
-
- $filenamelen = 25;
-
- sub fixname {
- local ($macname) = @_[0];
- local ($name);
- return $name if ($name = $MACNAMES{$macname});
- $name = $macname;
- $name = substr($name,0,$filenamelen);
- $name =~ tr/A-Z/a-z/; # lower case
- $name =~ tr/a-z0-9_./_/c; # improve this...
- $name =~ s/^\./_/;
- $name = ":" . $name;
- if ($MACNAMES{$name}) {
- $name .= "~";
- $extra = "aa";
- while ($MACNAMES{$name . $extra}) { $extra++; }
- $name .= $extra;
- }
- $MACNAMES{$name} = $macname;
- $name = substr($name,1);
- $MACNAMES{$macname} = $name;
- # it'd be nice to reset the cache now, but I don't want to reset
- # everything starting with an M :-(
- $name;
- }
-
- dbmopen(MACNAMES,"macnames",0600) || &mydie("DBMOpen failed");
- while ($line=<STDIN>) {
- print $line, " ", &fixname($line), "\n";
- }
- dbmclose(MACNAMES);
-
-
- Note: The above is just a fragment of the code, it probably won't work
- on its own, but might give you some ideas.
-
- Note2: The associative array has two kinds of mappings:
- :unixname --> macname
- macname --> unixname
- So it can map the names in either direction.
-
- Note3: Because Mac names don't traditionally have extensions, I didn't
- make any attempt to convert extensions, but with unix->msdos
- translation, you'd probably want to do that. All that requires is a
- slight change to the fixname subroutine... The latter half of fixname
- will guarentee uniqueness and invertability.
-
- Have fun all,
- Peter.
- Just a newbie Perl hacker :-)
-
- --
- _______________________________________________________________________
- Peter N Lewis <peter@ncrpda.curtin.edu.au> Ph: +61 9 368 2055
-