<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!perl -w
############################################################
## editLargefile
## Written By: Rohit Mishra ( rohit[at]rohitmishra.com)
## Modified for Win32 by Bob Friedman (friedmar[at]biol.sc.edu)
############################################################
## Keywords: BEGIN, INC, tempfile, FileHandle, stat, tempdir,
##           select, TextUndo, bind, configure, sysread,
##           syswrite, update, numberChanges, messageBox
############################################################

### TO SAVE A FILE, CHOOSE MENU ITEM "SAVE THIS BUFFER" BEFORE CHOOSING TO SAVE FILE

### Use following modules
use strict;
use Tk;
use File::stat;
use FileHandle;
use File::Path;
use File::Spec;
use File::Temp qw/ tempfile tempdir /;
require Tk::TextUndo;
require Tk::FileSelect;
require Tk::Table;

### Global Defines
###
my $NO = 0;
my $YES = 1;
my $CANCEL = -1;
my $ver = '1.0w';
my $bg_color = "white";
my $dir = "./";
my $template = ".largefile.XXXX";
my @tmpfile_array = ();
my $current_file;
my $no_page = $YES;
my $open_status = 0;
my $FILENAME = undef;

### To save the split file
###
my $split_dir = ".";
my $split_file = "file";
my $extn = 0;

### Temporary Directory functions
###
my $tempdir = tempdir ( $template, DIR =&gt; $dir , CLEANUP =&gt; 1 );

### Number of pages
###
my $HOME = ".";
my $number_of_pages = 10;
my $tmpfile_size;
my $xsize=600;
my $ysize=900;
my $font="-adobe-courier-medium-r-normal--14-100-100-100-m-90-iso8859-1";
if( ! -e "$HOME/.largefile.rc" ){
	open( RC,"&gt;$HOME/.largefile.rc" ) || die("$!");
	print RC "VERSION=1.5win\n";
	print RC "PAGES=10\n";
	print RC "XSIZE=600\n";
	print RC "YSIZE=900\n";
	print RC "FONT=-adobe-courier-medium-r-normal--14-100-100-100-m-90-iso8859-1\n";	
	print RC "BACKGROUND=white\n";
	print RC "FOREGROUND=black\n";
	### TOOLBAR CODE REMOVED FROM ALL SOURCE CODE
	# print RC "TOOLBARDIR=./toolbar\n";
	close(RC);
} else {
	open( RC,"$HOME/.largefile.rc" ) || die("$!");
	my @setup = &lt;RC&gt;;
	close(RC);
	foreach my $line ( @setup ){
		if( $line =~ /VERSION\s*=\s*(\S+)/ ){
			$ver = $1; 
		} 
		if( $line =~ /PAGES\s*=\s*(\S+)/ ){
			$number_of_pages = $1; 
		} 
		if( $line =~ /XSIZE\s*=\s*(\S+)/ ){
			$xsize = $1;
		}
		if( $line =~ /YSIZE\s*=\s*(\S+)/ ){
			$ysize = $1;
		}
		if( $line =~ /BACKGROUND\s*=\s*(\S+)/ ){
			$bg_color = $1;
		}
		if( $line =~ /FONT\s*=\s*(\S+)/ ){
			$font = $1;
		}
		# if( $line =~ /TOOLBARDIR\s*=\s*(\S+)/ ){
		#	$toolbardir = $1;
		# }
	}
}


### Main Code Begins
###
my $mw = MainWindow-&gt;new( -title =&gt; "LargeFile Editor $ver" );
$mw-&gt;minsize( $ysize,$xsize );

my $menubar1 = $mw-&gt;Frame(-relief =&gt; 'raised', -borderwidth =&gt; 2);
my $menubar2 = $mw-&gt;Frame(-relief =&gt; 'raised', -borderwidth =&gt; 2);

## File Menu
##
$menubar1-&gt;Menubutton(qw/-text File -underline 0 -tearoff 0 -menuitems/ =&gt;
  [
	  [Button =&gt; 'Open ..', -command =&gt; 
				sub{ 
					if( $open_status == 0 ){
						$open_status = 1;
						$FILENAME = &amp;Open_LargeFile;
						$open_status = 0;
					} 
				}],
	  [Button =&gt; 'Save this buffer ..', -command =&gt;
                                sub{
					&amp;On_Save( $FILENAME );
                                }],
	  [Button =&gt; 'Save Largefile ..', -command =&gt;
                                sub{
                                        &amp;Save_LargeFile( $FILENAME );
                                }],
	  [Button =&gt; 'Save Largefile As ..', -command =&gt;
                                sub{
					if( defined $FILENAME ){
						if( $open_status == 0 ){
							$open_status = 1;
							&amp;On_SaveAs;
							$open_status = 0;
						}
					} else {
						my $dialog = $mw-&gt;Dialog(-title =&gt; 'Warning', -text =&gt; 'No File opened!!');
                                                $dialog-&gt;Show('-global');
					}
                                }],
	  [Separator =&gt; ''],
	  [Button =&gt; 'Close ..', -command =&gt;
                                sub{
                                        &amp;On_Close;
                                }],
	  [Separator =&gt; ''],
	  [Button =&gt; 'Quit', -command =&gt; 
				sub{ 
					&amp;On_Exit( $FILENAME );
				}],
  ])-&gt;pack(-side=&gt;'left');

## Edit Menu
##
$menubar1-&gt;Menubutton(qw/-text Edit -underline 0 -tearoff 0 -menuitems/ =&gt;
  [
          [Button =&gt; 'Undo', -command =&gt;
                                sub{
					&amp;On_Undo;
                                }],
          [Button =&gt; 'Redo', -command =&gt;
                                sub{
					&amp;On_Redo;
                                }],
          [Separator =&gt; ''],
          [Button =&gt; 'Select All', -command =&gt;
                                sub{
					&amp;On_SelectAll;
                                }],
	  [Button =&gt; 'Unselect All', -command =&gt;
                                sub{
                                        &amp;On_UnselectAll;
                                }],
  ])-&gt;pack(-side=&gt;'left');

## View Menu
##
$menubar1-&gt;Menubutton(qw/-text View -underline 0 -tearoff 0 -menuitems/ =&gt;
  [
          [Button =&gt; 'Previous Page', -command =&gt;
                                sub{
                                        &amp;Traverse_To_Prev;
                                }],
          [Button =&gt; 'Next Page', -command =&gt;
                                sub{
                                        &amp;Traverse_To_Next
                                }],
          [Separator =&gt; ''],
          [Button =&gt; 'Go To Page', -command =&gt;
                                sub{
                                        &amp;Open_Page;
                                }],
  ])-&gt;pack(-side=&gt;'left');


my $InputText = $mw-&gt;Scrolled('TextUndo', -height =&gt; '1', -width =&gt; '1', -scrollbars =&gt; 'osoe', -background=&gt;$bg_color,-font =&gt; $font,) ;

my $Basemenu = $mw-&gt;Frame(-relief =&gt; 'raised', -borderwidth =&gt; 2);

my $Lbl_Page_No = $Basemenu-&gt;Label( -text =&gt; 'No Page Open' );

### Pack everything
###
$menubar1-&gt;form( -top=&gt; '%0', -left =&gt; '%0', -right =&gt; '%100' );
$menubar2-&gt;form( -top=&gt; $menubar1, -left =&gt; '%0', -right =&gt; '%100' );
$InputText-&gt;form( -top=&gt; $menubar2, -left =&gt; '%0', -right =&gt; '%100', -bottom =&gt; $Basemenu );
$Basemenu-&gt;form( -left=&gt; '%0', -right =&gt; '%100', -bottom =&gt; '%100' );
$Lbl_Page_No-&gt;pack( -side =&gt; 'right' );

&amp;MainLoop;


### User Defined Subroutines
###

### File menu callbacks

sub Open_LargeFile(){
   ## MODIFIED OPEN FILE DIALOG BOX ROUTINE	
   # my $filename = &amp;Open_File( $mw );
   my $main = shift;
   my $filename = $mw-&gt;getOpenFile( -title=&gt;'Select File' ); 

   my $sb;
   my $size;
   if( ( !defined $filename ) &amp;&amp; ( ! -f $filename ) ){
	return;
   } else {
       	$sb = stat( $filename );
	$size = $sb-&gt;size;
        my $line;
	my $i = 0;
	my $var = 33;
	my $pages;
	my $time_left;
	my $total_time;
	my $dis_time_left;
	my $time_for_one_page = 1.0; # seconds
	my $title = "Opening File: $filename";

	# ADDED CODE TO ERROR CHECK FOR SMALL FILES
	# if &lt;200Kb file size then force on 1 page
	if($size &lt; 200000){
		$number_of_pages = 1;
	}
	
	if( $number_of_pages != 0 ){
		$tmpfile_size = $size/$number_of_pages;
	} else {
		print "Number of pages cannot be less than 1\n";
		exit(0);
	}


	# REMOVED THIS IF CONDITION
	### if ( $size &lt;= $tmpfile_size ){
	#       $InputText-&gt;Load( $filename );
	#	$no_page = $YES;
	#	$current_file = 0;
	#	$Lbl_Page_No-&gt;configure( -text =&gt; "Page No: $current_file" );
	#	$mw-&gt;configure( -title =&gt; "LargeFile Editor $ver: $filename" );
	### } else {
	
	$pages = $size/$tmpfile_size;	
	$total_time =  sprintf("%.2f",( $pages * $time_for_one_page ) );

	my $top = $mw-&gt;Toplevel( -title =&gt; "Please Wait...",-popover =&gt; $mw);
	$top-&gt;resizable('no','no');

	$top-&gt;Label(-textvariable =&gt; \$title )-&gt;pack;
   	$top-&gt;Label(-textvariable =&gt; \$dis_time_left )-&gt;pack;

   	my $m = $top-&gt;Frame(-height =&gt; 10, -border =&gt; 2, -relief =&gt; 'sunken')-&gt;pack(-fill =&gt; 'x');
   	my $s = $m-&gt;Frame(-background =&gt; 'blue', -relief =&gt; 'raised', -border =&gt; 2);

	&amp;Busy;

	open( FILE, "&lt; $filename" ) || die( "Could not open file $filename" );
	$var = sysread( FILE, $line, $tmpfile_size );
	while( $var != $NO ){
	( undef, $tmpfile_array[$i] ) = tempfile( $template, DIR =&gt; $tempdir, OPEN =&gt; 0 );
	open( TMP,"&gt;$tmpfile_array[$i]" ) || die( "Could not open file $tmpfile_array[$i]" );
	if( $ARGV[0] =~ m|^\-s| ){
		open( SPLIT,"&gt;$split_dir/$split_file$extn" ) || 
		die( "Could not open file $split_dir/$split_file$extn" );
	}
	syswrite( TMP, $line );	
	if( $ARGV[0] =~ m|^\-s| ){
		syswrite( SPLIT, $line );	
		close( SPLIT );
		$extn++;
	}
	close( TMP );
	$i++;

	$time_left = sprintf("%.2f",( $total_time  - ( $i * $time_for_one_page )) );
	$dis_time_left = sprintf("Time Left: %.0f mins %.0f seconds",$time_left/60,$time_left%60 );

	$s-&gt;place('-x' =&gt; 0, '-y' =&gt; 0, -relheight =&gt; 1, 
	-relwidth =&gt; ($total_time - $time_left)/$total_time );
	$top-&gt;update;
	$var = sysread( FILE, $line, $tmpfile_size ); 
	}
	$top-&gt;destroy;
	close( FILE );

	$current_file = 0;

	$InputText-&gt;Load( $tmpfile_array[0] );
	$mw-&gt;configure( -title =&gt; "LargeFile Editor $ver: $filename" );
	$Lbl_Page_No-&gt;configure( -text =&gt; "Page No: $current_file" );
	&amp;Unbusy;

	$no_page = $NO;

	### }
	return ( $filename );
   }
}


sub Traverse_To_Next(){
	my $var = scalar ( @tmpfile_array ) - 1;
	if( $current_file &lt; $var ){
		&amp;main::Tk::TextUndo::ConfirmDiscard( $InputText );
		$current_file++;
		$InputText-&gt;Load( $tmpfile_array[$current_file] );
		$Lbl_Page_No-&gt;configure( -text =&gt; "Page No: $current_file" );
	} else {
		$current_file = $current_file;
	} 
}

sub Traverse_To_Prev(){
	if( $current_file != 0 ){
		&amp;main::Tk::TextUndo::ConfirmDiscard( $InputText );
		$current_file--;
		$InputText-&gt;Load( $tmpfile_array[$current_file] );
		$Lbl_Page_No-&gt;configure( -text =&gt; "Page No: $current_file" );
	} else {
		$current_file = $current_file;
	}
}

sub Open_Button_Page(){
	my $top = $mw-&gt;Toplevel( -title =&gt; "Select Page" );
	$top-&gt;minsize( qw/300 100/);
	$top-&gt;maxsize( qw/300 300/);
	my $page_container = $top-&gt;Table( -rows =&gt; '20', -columns =&gt; '20',-scrollbars =&gt; 'se' );
	my $manubar = $top-&gt;Frame( -relief =&gt; 'sunken' );
	my $cancel = $manubar-&gt;Button( -text =&gt; "Cancel", -command =&gt; 
			sub{
				$top-&gt;destroy; 
			});
	$page_container-&gt;form( -left =&gt; '%0', -top =&gt; '%0', -right =&gt; '%100', -bottom =&gt; $manubar );
	$manubar-&gt;form( -left =&gt; '%0', -right =&gt; '%100', -bottom =&gt; '%100', );
	$cancel-&gt;pack( -side =&gt; 'left', -fill =&gt; 'x', -expand =&gt; '1' );

	my $no_of_pages = scalar( @tmpfile_array ) - 1 ;	
	my @buttons = ();

	for( my $i = 0; $i &lt;= $no_of_pages; $i++ ){
		$buttons[$i] = $page_container-&gt;Button( -text =&gt; "$i" ); 	
		$page_container-&gt;put( $i%10, $i/10, $buttons[$i] );
	}
	return $top, @buttons;
}

sub Open_Page (){
	my $page_no;
	my ($w, @buttons) = &amp;Open_Button_Page();
	foreach my $button ( @buttons ){
		$button-&gt;bind( '&lt;Button-1&gt;' =&gt; sub{ 
			&amp;main::Tk::TextUndo::ConfirmDiscard( $InputText );
			$page_no = $button-&gt;cget(-text); 
			$Lbl_Page_No-&gt;configure( -text =&gt; "Page no: $page_no" );
			$w-&gt;destroy;
			$InputText-&gt;Load( $tmpfile_array[$page_no] ); 
			$current_file = $page_no;
		}); 
	}
}

sub On_SaveAs(){
	### CHANGED METHOD FOR SAVING FILE DIALOG BOX
	my $name = $InputText-&gt;getSaveFile(-title =&gt; 'File Save As');
	# my $name = $InputText-&gt;CreateFileSelect( 'getSaveFile',-title =&gt; 'File Save As');
 	return &amp;Save_LargeFile( $name ) if defined($name) and length($name);
	return 0;
}

sub On_Undo(){
	$InputText-&gt;undo;	
}

sub On_Redo(){
	$InputText-&gt;redo;	
}

sub On_SelectAll(){
 	$InputText-&gt;tagAdd('sel','1.0','end');

}

sub On_UnselectAll(){
	$InputText-&gt;tagRemove('sel','1.0','end');
}

sub On_Save(){
	my $filename = shift;

	if( defined $filename ){
		$InputText-&gt;Save;
	} else {
		my $dialog = $mw-&gt;Dialog(-title =&gt; 'Warning', -text =&gt; 'No File opened!!');
                $dialog-&gt;Show('-global');
	}
}

sub Save_LargeFile(){
	my $filename = shift;
	my $line;

	if( defined $filename ){
		open( FILE,"&gt;$filename" ) || $mw-&gt;Dialog(-title =&gt; 'Warning', -text =&gt; 'File not writable.')-&gt;Show('-global');
		foreach my $tmpfile ( @tmpfile_array ){
			open( TMP,"$tmpfile" ) || die( "Could not open swap file $tmpfile\n" );
			if( sysread( TMP, $line, $tmpfile_size ) != $NO ){
				chomp( $line );
				syswrite( FILE, $line );
			}
			close( TMP );
		}
		close( FILE );
	} else {
		my $dialog = $mw-&gt;Dialog(-title =&gt; 'Warning', -text =&gt; 'No File opened!!');
                $dialog-&gt;Show('-global');
	}
}

sub On_Close(){
	if( $InputText-&gt;ConfirmDiscard ){
		$FILENAME = undef;
		$InputText-&gt;EmptyDocument;
		@tmpfile_array = ();
                $mw-&gt;configure( -title =&gt; "LargeFile Editor $ver" );
                $Lbl_Page_No-&gt;configure( -text =&gt; "No Page Open" );
                $no_page = $YES;
	} else {
		return;
	}	
}

sub On_Exit (){
	my $filename = shift;

	if( defined $filename ){
		if( -w $filename ){
			### Check Sticky bit here
 			if ( $InputText-&gt;numberChanges ){
   				my $ans = $mw-&gt;messageBox(-icon =&gt; 'warning', -type =&gt; 'YesNoCancel', -default =&gt; 'Yes', -message =&gt; 
				"The text has been modified without being saved.  Save edits?");
   				if( $ans eq 'Cancel' or $ans eq 'No' ){
					exit(0);
				} elsif( $ans eq 'Yes' ){
					$InputText-&gt;Save( $tmpfile_array[$current_file] );
					&amp;Save_LargeFile( $filename );
					exit(0);
				}
  			} else {
				exit(0);
			}
		} else {
			if ( $InputText-&gt;numberChanges ){
				my $ans = $mw-&gt;messageBox(-icon =&gt; 'warning', -type =&gt; 'YesNoCancel', -default =&gt; 'Yes', -message =&gt; 
				"File not writable. Quit?");
				if( $ans eq 'Cancel' or $ans eq 'No' ){
       	                		 return;
       		         	} elsif( $ans eq 'Yes' ){
       	                		 exit(0);
       		         	}
			} else {
				exit(0);
			}
		}
	} else {
		exit(0);
	}
	
}

sub Busy (){
	$mw-&gt;Busy;
        $InputText-&gt;Busy;
}

sub Unbusy (){
        $mw-&gt;Unbusy;
        $InputText-&gt;Unbusy;

}


__END__

=head1 NAME

editLargefile.pl - Edit files of size greater than 2.8 GB

=head1 SYNOPSIS

editLargefile.pl

=head1 STATUS

editLargefile.pl is in its beta version.

=head1 DESCRIPTION

B&lt;editLargefile.pl&gt; is a window based editor with all the editing 
capabilities a TextUndo text widget has. This editor is 
recomended for editing a file of size more than 2.8 GB.

=head1 FEATURES

B&lt;editLargefile.pl&gt; uses sysread and syswrite system calls which makes 
it very efficient.
At startup, it creates a .largefile.rc file which contains information about
the default settings which a user wishes to use. The opened file is split into
smaller chunks of data, which are displayed on a TextUndo widget. When the 
editing is complete, the user only has to save the chunk which has been modified
and not the whole file, which drastically reduces the file saving time.
The user can browse through the pages(chunks) serially or can jump directly 
to the page to be edited.
Additionally, all the editing can be un-done or re-done at any point. 

=head1 BUGS

When the editor crashes, it leaves temporary directories with the split 
files in them.

=head1 PREREQUISITES

Tk
File::stat
FileHandle
File::Path
File::Spec
File::Temp
Tk::TextUndo
Tk::Table
Tk::FileSelect

=head1 OSNAMES
Win32

=head1 AUTHOR

Rohit Mishra E&lt;lt&gt;F&lt;rohit@rohitmishra.com&gt;E&lt;gt&gt;
modified for Win32 by Bob Friedman (friedmar[at]biol.sc.edu)

=head1 COPYRIGHT

Copyright (c) 2005 Rohit Mishra &lt;rohit@rohitmishra.com&gt; &amp; [Bob Friedman for Win32 version only]. All rights reserved This program is a free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=head1 SCRIPT CATEGORIES
Win32
Educational

=cut
</pre></body></html>