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

Re: [MacPerl] File conversion script problem



At 9:51 AM +0000 2/2/1999, Jay Bedsole wrote:
>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 have no idea of whether this is better or not performance-wise, but
this one does a couple of cute Toolbox things and auto-detects the current
line ending type (or at least tries to), so that kind of helps.  It can
also handle files and folders dropped on it, and it'll only try to convert
text files.  You can download a (hopefully functional) copy from
<http://www.stanford.edu/~ejalbert/TextConverter/TextConverter.sit.hqx>.
Ignore most of what's said on the page -- I haven't updated that in a
while. ;-)

BTW, in putting that downloadable copy together just now, I had some very
serious problems getting Mac::Utils::ProgressBar to be added properly to
the resource fork with RuntimeBuilder.  Switching things to
Mac::ProgressBar worked, but that would imply a fairly serious bug with
RuntimeBuilder, possibly only under MacOS 8.5 (that's the only large change
on my system since I last built TextConverter).  Thoughts?

-Eric

#!perl

use Mac::Windows;
use Mac::ProgressBar;
use Mac::Events;
use Mac::Dialogs;

MacPerl::Quit(1);

if (@ARGV) {
	foreach $file (@ARGV) {
		&getFileList($file);
	}
	$dialog=MacDialog->new(Rect->new(100, 100, 288, 265), 'Text Converter',
								1,
movableDBoxProc(), 0,

	[kButtonDialogItem(),

	Rect->new(25, 130, 84, 150), 'Convert'],

	[kButtonDialogItem(),

	Rect->new(98, 130, 157, 150), 'Quit'],

	[kRadioButtonDialogItem(),

	Rect->new(6, 46, 24, 62), 1],

	[kRadioButtonDialogItem(),

	Rect->new(6, 64, 24, 80), 1],

	[kRadioButtonDialogItem(),

	Rect->new(6, 82, 24, 100), 1],

	[kRadioButtonDialogItem(),

	Rect->new(6, 100, 24, 118), 1],

	[kRadioButtonDialogItem(),

	Rect->new(103, 46, 121, 62), 1],

	[kRadioButtonDialogItem(),

	Rect->new(103, 64, 121, 80), 1],

	[kRadioButtonDialogItem(),

	Rect->new(103, 82, 121, 100), 1],

	[kStaticTextDialogItem(),

	Rect->new(6, 6, 71, 22), 'Convert...'],

	[kStaticTextDialogItem(),

	Rect->new(6, 25, 46, 42), 'From:'],

	[kStaticTextDialogItem(),

	Rect->new(28, 46, 59, 62), 'Mac'],

	[kStaticTextDialogItem(),

	Rect->new(28, 64, 87, 81), 'Windows'],

	[kStaticTextDialogItem(),

	Rect->new(28, 82, 60, 98), 'Unix'],

	[kStaticTextDialogItem(),

	Rect->new(28, 100, 101, 116), 'AutoSelect'],

	[kStaticTextDialogItem(),

	Rect->new(103, 25, 125, 43), 'To:'],

	[kStaticTextDialogItem(),

	Rect->new(124, 46, 155, 62), 'Mac'],

	[kStaticTextDialogItem(),

	Rect->new(124, 64, 183, 81), 'Windows'],

	[kStaticTextDialogItem(),

	Rect->new(124, 82, 156, 98), 'Unix'],
								);
	%controls=(ok=>1, quit=>2, macFrom=>3, winFrom=>4, unixFrom=>5,

			autoFrom=>6, macTo=>7, winTo=>8, unixTo=>9);
	SetDialogDefaultItem($dialog->window(), $controls{ok});
	$dialog->item_hit($controls{ok}=>\&okClicked);
	$dialog->item_hit($controls{quit}=>\&quitClicked);
	foreach $i (3..6) {
		$dialog->item_hit($i=>\&fromRadioButtonsClicked);
	}
	foreach $i (7..9) {
		$dialog->item_hit($i=>\&toRadioButtonsClicked);
	}

	&fromRadioButtonsClicked($dialog, $controls{autoFrom});
	&toRadioButtonsClicked($dialog, $controls{macTo});

	while($dialog->window()) {
		WaitNextEvent();
	}
} else {
	MacPerl::Answer('Please drop the files or folders that you want to
convert on the TextConverter icon.');
}

END {
	$progressBar->dispose() if defined($progressBar);
	$dialog->dispose() if defined($dialog);
}

sub fromRadioButtonsClicked {
	my($dialog, $item)=@_;
	my($control);
	foreach $control (3..6) {
		if ($control==$item) {
			$dialog->item_value($control, 1);
			$fromButtonSelected=$control;
		} else {
			$dialog->item_value($control, 0);
		}
	}
}

sub toRadioButtonsClicked {
	my $dialog=shift;
	my $item=shift;
	my($control);
	foreach $control (7..9) {
		if ($control==$item) {
			$dialog->item_value($control, 1);
			$toButtonSelected=$control;
		} else {
			$dialog->item_value($control, 0);
		}
	}
}

sub okClicked {
	my($dialog)=shift;
	$autoSelect=0;
	if ($fromButtonSelected==3) {	#macFrom
			$/=chr(13);
	} elsif ($fromButtonSelected==4) {	#winFrom
			$/=chr(13).chr(10);
	} elsif ($fromButtonSelected==5) {	#unixFrom
			$/=chr(10);
	} else {	#auto select
			$autoSelect=1;
	}
	if ($toButtonSelected==7) {	#macTo
			$\=chr(13);
	} elsif ($toButtonSelected==8) {	#winTo
			$\=chr(13).chr(10);
	} else {		#unixTo
			$\=chr(10);
	}
	&convertAllFiles();
	return 1;
}

sub convertAllFiles {
	$dialog->dispose();
	$progressBar=new Mac::ProgressBar(-title=>'Text Converter');
	$progressBar->configure(-header=>'Converting', -text=>'', -min=>0,
-max=>scalar(@fileList));
	$currentValue=0;

	while($progressBar->inProgress()) {
		foreach $file (@fileList) {
			@pathInfo=split /:/, $file;
			$progressBar->setText($pathInfo[-1]);
			&convertFile($file);
			$progressBar->setValue(++$currentValue);
		}
	}
	$progressBar->dispose();
}

sub quitClicked {
	my($dialog)=shift;
	$dialog->dispose();
	return 1;
}

sub getFileList {
	my($fullPath)=shift;
	if (-d $fullPath) {
		opendir NEWDIR, $fullPath or warn "Unable to open directory
$file: $!";
		foreach (readdir NEWDIR) {
			&getFileList($fullPath.':'.$_);
		}
		closedir NEWDIR;
	} else {
		my($creator, $type)=MacPerl::GetFileInfo($fullPath);
		return if $type ne 'TEXT';
		push @fileList, $fullPath;
	}
}

sub convertFile {
	my($fullPath)=@_;
	if (-d $fullPath) {
		opendir NEWDIR, $fullPath or warn "Unable to open directory
$file: $!";
		foreach (readdir NEWDIR) {
			&convertFile($fullPath.':'.$_);
		}
		closedir NEWDIR;
	} else {
		my($creator, $type)=MacPerl::GetFileInfo($fullPath);
		return if $type ne 'TEXT';
		if ($autoSelect) {
			&setLineEnding($fullPath);
		}
		open FILE, $fullPath or die "Unable to open $fullPath: $!";
  my $tempFile = "@{[localtime]}.tmp";
		open NEWFILE, ">$tempFile" or die "Unable to create temp
file for $fullPath: $!";
		while(defined($line=<FILE>)) {
			if (chomp $line) {
				print NEWFILE $line;
			} else {
				my $tempEnding=$\;
				$\='';
				print NEWFILE $line;
				$\=$tempEnding;
			}
		}
		close NEWFILE;
		close FILE;
		rename "$tempFile", $fullPath or die "Unable to rename
files: $!";
		MacPerl::SetFileInfo($creator, $type, $fullPath);
	}
}

sub setLineEnding {
	my $file=shift;
	open FILE, $file or die "Unable to open $file: $!";
	my $lengthToRead=1024;		# some reasonably-sized number
	my $buffer;
	my $offset=0;
	while(sysread(FILE, $buffer, $lengthToRead, $offset)) {
		return if !length($buffer) || !defined($buffer);
		if (index($buffer, "\015\012")!=-1) {
			$/="\015\012";
			last;
		}
		my $crIndex=index($buffer, "\015");
		my $lfIndex=index($buffer, "\012");
		if ($crIndex<$lfIndex) {
			if ($crIndex==-1) {
				$/="\012";
			} else {
				$/="\015";
			}
			last;
		} else {
			if ($lfIndex==-1) {
				$/="\015";
			} else {
				$/="\012";
			}
			last;
		}
	}
	close FILE;
}

--
Eric Albert                     ejalbert@cs.stanford.edu
http://www.stanford.edu/~ejalbert/

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