#!/usr/bin/perl -w
###########################################################################
# p2hall03.pl - Geoff McLane - geoffair.net/mperl
#
# 12/10/2013 - Seems the Godaddy server does NOT download filename.pl.txt! Change to filename.txt
# 02/02/2010 - 1. Somehow MISSED adding 'bm_top' to ALL the samples!!!
# 2. since using
do NOT need to ADD ' ' all over the place,
# but this seems like too drastic a change for now. Maybe add using 'Tidy'???,
# but have a tidyall.bat to do this as a separate process
#
# This is the MOST recent, in a group
# update 07 June, 2007
# This is an update on p2hall, 01, 02, ... of April, 2006.
# Major update: 28 August, 2006
# Its purpose is to read a FOLDER, and CONVERT ALL perl scripts found
# into a HTML file, of the same name, in an OUTPUT folder. It will
# NOT overwrite any EXISTING file of the same name found in the
# OUTPUT folder unless the source is LATER/NEWER ...
# This p2hallNN.pl series took over from the p2htmlNN.pl series.
#
# Further the CSS coding comes from an perl.css file, which is
# generates if NOT already in existance ...
#
# Another attempt at 'converting' perl scripts to a colour coded HTML page.
# The previous attempt got too unwieldy - abandoned at p2html12.pl ...
# This works on a line by line, character by character, decode,
# and colour encode ... a modest file can grow to 4 or more times
# its original size ... adding colour coding COSTS!
#
# NOTE: While this conversion to coloured HTML produces a 'pretty
# picture' of the original perl script file, often it MAY NOT be copied
# exactly by others. Asside from some big spacing differences, entities
# such as $tok .= '&'; MAY NOT translate correctly. In a copy-and-
# paste operation, this MAY be 'translated' as $tok .= '&';, which
# produces ERRANT perl code! Other ERRORS are $ln =~ s/</g;
# MAY become $ln =~ s/';
my $wrap = 5; # column wrap in file tables
# set the CLASS and COLOUR strings
my $a_class = 'a'; # built-in function (red)
my $b_class = 'b'; # comments (#006666)
my $c_class = 'c'; # reserved words (blue)
my $d_class = 'd'; # inside qw(...)
my $e_class = 'e'; # $scalar (#9400d3)
my $f_class = 'f'; # in <...
block
# this option REALLY adds weight to certain files
my $add_uvars = 1; # colour code user variables
my $wrap_menu = 20; # insert jump islands, into table
my $menu_wrap = 0;
my $maxcelltxt = (8 * 12 * 12 * 2);
# special DEBUG variables
my $debug_on = 0; # heavy DEBUG ONLY output
my $out_lists = 0; # output the lists in qw form
my $dbg1 = 0;
my $dbg2 = 1;
my $dbg3 = 0;
my $dbg4 = 0; # show 'Add metas to handle'
my $dbg5 = 0; # show ALL email changes
my $dbg6 = 0; # show ALL DISCARDED:
my $verb3 = 0;
my $verb4 = 0;
my $verb5 = 0; # debug add_2_used sub
my $verb6 = 0; # output each file processed ...
#####################
# PROGRAM VARIABLES #
#####################
my %HFuncsFnd = (); # set of FOUND builtin functions
my %HResWdFnd = (); # reserved words used
my @AFileNames = (); # for each output file, with hash of functions
my @AFileHashs = (); # for each output file, with hash of functions
my %HUsedpack = (); # used packages
my $out_file = ''; # out file for HTML
my $in_file = ''; # current in begin processed
my $bs_name = ''; # base file name of current
my $in_date = '';
my $in_size = '';
# for log file
my ($LF, $OF);
# reserved words, and build-ins
my @ResWords = ();
my @BuiltIns = ();
# load perl.stx file, or use this local list
if ($use_local) {
@ResWords = qw/ continue do else elsif for foreach goto if last local lock map my next package redo
require return sub unless until use while STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG TRUE FALSE __FILE__
__LINE__ __PACKAGE__ __END__ __DATA__ lt gt le ge eq ne cmp x not and or xor q qq qx qw $ @ % /;
@BuiltIns = qw(abs accept alarm atan2 bind binmode bless caller chdir chmod chomp chop chown chr
chroot close closedir connect cos crypt dbmclose dbmopen defined delete die dump each eof eval exec exists
exit exp fcntl fileno flock fork format formline getc getlogin getpeername getpgrp getppid getpriority
getpwnam getgrnam gethostbyname getnetbyname getprotobyname getpwuid getgrgid getservbyname gethostbyaddr
getnetbyaddr getprotobynumber getservbyport getpwent getgrent gethostent getnetent getprotoent
getservent setpwent setgrent sethostent setnetent setprotoent setservent endpwent endgrent endhostent
endnetent endprotoent endservent getsockname getsockopt glob gmtime grep hex import index int ioctl
join keys kill lc lcfirst length link listen localtime log lstat mkdir msgctl msgget msgsnd msgrcv no oct
open opendir ord pack pipe pop pos print printf prototype push quotemeta rand read readdir readlink recv
ref rename reset reverse rewinddir rindex rmdir scalar seek seekdir select semctl semget semop send setpgrp
setpriority setsockopt shift shmctl shmget shmread shmwrite shutdown sin sleep socket socketpair sort
splice split sprintf sqrt srand stat study substr symlink syscall sysopen sysread sysseek system syswrite
tell telldir tie tied time times truncate uc ucfirst umask undef unlink unpack untie unshift utime values
vec wait waitpid wantarray warn write );
}
my $perlcss = <<"PEOF";
/* Style Definitions - updated 2007.06.12 - 2006.07.13 */
body {
background-image : url('cldsp.jpg');
margin : 0 1cm 0 1cm;
text-align : justify;
}
h1 {
background : #efefef;
border-style : solid solid solid solid;
border-color : #d9e2e2;
border-width : 1px;
padding : 2px 2px 2px 2px;
font-size : 200%;
text-align : center;
}
h2 {
font-size : 16pt;
font-weight : bold;
background-color : #ccccff;
}
p.top {
margin : 0;
border-style : none;
padding : 0;
text-align : center;
}
p.nom {
margin : 0cm;
margin-bottom : .0001pt;
color : red;
}
p.code {
margin: 0cm 0.5cm 0cm 0.5cm;
font-size:10.0pt;
font-family:"Courier New";
}
.bld { font-weight: bold; }
.cn { font-family:"Courier New"; }
.ctr { text-align: center; }
.nob {
margin : 0 0 0 0;
border-style : none;
padding : 0 0 0 0;
}
.red { color:red; }
.blue { color:blue; }
.green { color:#006600 }
.brown { color:#a52a2a }
.a { color:red; }
.b { color:#006666; }
.c { color:blue; }
.d { color:#a52a2a; }
.e { color:#9400d3; }
.f { color:#666666; }
.o { color:#008b8b; }
.v { color:#a52a2a; }
.t { color:#006600; }
.cd {
/* top, right, bottom, left */
padding: 0px 10px 0px 10px;
margin: 1px 10px 1px 10px;
background: #f0f8ff;
border-width: 1px;
border-style: solid solid solid solid;
border-color: #cccccc;
width: 90%;
font-family:"Courier New";
}
.out {
padding: 0px 10px 0px 10px;
margin: 1px 10px 1px 10px;
background: #2f2f2f;
color: #ffffff;
border-width: 1px;
border-style: solid solid solid solid;
border-color: #cccccc;
width: 90%;
font-family:"Courier New";
}
/* reserved words */
.rw { color: #0000cd; }
/* built-in functions */
.bif { color: #ff0000; }
/* scalar variables */
.sca { color: #9400d3; }
/* array variables */
.arr { color: #008b8b; }
/* hash variables */
.has { color: #a52a2a; }
/* comments after # */
.com { color: #008000; }
/* quoted items */
.qot { color: #009900; }
hr {
margin : 0px 0px 0px 0px;
border-style : none;
padding: 0px 0px 0px 0px;
}
hr.mini {
margin : 0;
border-style : none;
padding : 0;
width : 20%;
text-align : center;
}
/* eof - perl.css */
PEOF
my @lines = (); # final output line gathered here
my $line = '';
my $date = '';
my $sz = '';
my $last_builtin = '';
my $last_resword = '';
my $doc_total = 0;
my $out_total = 0;
# these are really just DEBUG counters
my $a_cnt = 0;
my $b_cnt = 0;
my $c_cnt = 0;
my $d_cnt = 0;
my $e_cnt = 0;
my $f_cnt = 0;
my $o_cnt = 0;
my $v_cnt = 0;
my $q_cnt = 0;
# TIME VARIABLES
my ($t0, $t1, $elapsed);
my @in_files = (); # set of perl files gathered from FOLDER search
my $ind_file = ''; # file name for INDEX list
my $txt_file = ''; # file name for TEXT file (copy of perl)
my @ind_files = (); # array of files for INDEX generation
my $sb; # stat of current file
my $latest = 0;
my $earliest = time();
my $dbg20 = 0;
my $dbg21 = 0; # collect from alphabetic table
my $dbg22 = 0; # collect from alphabetic table
my $dbg23 = 0;
my $dbg24 = 0;
my $dbg25 = 0;
# get the old index.htm - D NOT lose information in update ...
my $in_index = "$out_folder/$indexhtm"; # = something line 'index.htm';
my $tbl_num = 1; # want the first table
my @tbl_arr = ();
my $tacnt = 0;
my @tbl_set = ();
## push(@tbl_set, [$hrf, $fil, $dt, $sz, $yr, $mt, $dy, 0]);
my $tbl_num3 = 3; # and we want the third table
my @tbl_arr3 = ();
my $tacnt3 = 0;
my @tbl_set3 = ();
## push(@tbl_set3, [$bif, $files]);
my @hrefs = ();
my %HOldbifs = ();
#################
my $lncnt = 0;
my $tblcnt = 0;
my $indcnt = 0;
my @larr = ();
my @larr2 = ();
my $ln = '';
# get_existing_files( $out_folder ); # put existing in @existing
my @existing = (); # push(@existing, $dfile);
my @dir_list = (); # if/when I want to be recursive...
my $tot_dirs = 0;
my $tot_files = 0;
my @warnings = ();
# forward references
sub get_old_index($);
#####################################################################
# This is the small MAIN part of the script
$t0 = [gettimeofday];
# logging file, if possible
my $out_log = 1;
if (open $LF, ">$log_file") {
$out_log = 1;
prt( "Output also being written to LOG file $log_file ... \n" );
} else {
$out_log = 0;
prtw( "WARNING: Unable to create LOG file $log_file ... \n" );
}
sub pgm_exit($$) {
my ($val,$msg) = @_;
if (length($msg)) {
$msg .= "\n" if (!($msg =~ /\n$/));
prt($msg);
}
###show_warnings($val);
###close_log($outfile,$load_log);
exit($val);
}
if ( ! $use_local) { load_stx_file( $perlstx ); }
prt( "Got ".scalar @ResWords." Reserved Words, and ".scalar @BuiltIns." Built-in functions ...\n" );
# get existing files, and fix missing files in destination.
get_existing_files( $out_folder ); # put existing in @existing
get_input_files( $in_folder ); # find perl scripts, and put in @in_files
# PROCESS THE IN FILES FOUND
#############################################################################
# Get the DATE and TIME,
# Process the file lines - process_file
# If the out file already exists, only overwrite it if the in file is NEWER
# Read in the OLD index, if any, extracting information from the tables.
# Then GENERATE the NEW index file, with lots of information.
#############################################################################
my $fcnt = scalar @in_files;
my $skipped = 0;
my $newercnt = 0;
my $donecnt = 0;
my $tmsg = '';
if ($fcnt) {
prt( "Processing $fcnt files from folder $in_folder ...\n" );
for (my $i = 0; $i < $fcnt; $i++ ) {
reset_variables();
# push(@in_files, [$df, $dfile]); # store full, and base names
$in_file = $in_files[$i][0];
$bs_name = $in_files[$i][1];
$sb = stat($in_file);
###$in_date = YYYYMMDD($sb->mtime);
###$in_size = get_nn($sb->size);
$in_size = $sb->size;
$in_date = $sb->mtime; # keep DATE unchanged, so a SORT can be done
if ($sb->mtime > $latest) { $latest = $sb->mtime; }
if ($sb->mtime < $earliest) { $earliest = $sb->mtime; }
$ind_file = my_file_name( $bs_name );
$txt_file = $ind_file . ".txt";
$ind_file .= '.htm'; # make the sample file name
push(@ind_files, [$ind_file,$in_date,$in_size]); # array of files for INDEX generation
$out_file = "$out_folder/$ind_file";
process_file( $in_file, $bs_name ); # main processing of the file lines
##push(@AFileNames, [$ind_file, \%HFuncsFnd]); # store the functions used ...
push(@AFileNames, $ind_file );
my %th = %HFuncsFnd;
my @tar = keys %th;
prt("Pushing HASH with ".scalar @tar." keys ...\n") if ($verb6 || $debug_on);
push(@AFileHashs, \%th); # store the functions used ...
$tmsg = 'NEW';
if ( -f $out_file) {
my $sb2 = stat($out_file);
if ($sb->mtime < $sb2->mtime) {
prt( "Skipping [$out_file] since it already exists ...\n" ) if ($dbg3);
$out_file = ''; # kill the new output
$skipped++; # older or same
$tmsg = 'Skip';
} else {
$newercnt++;
$tmsg = 'Upd';
}
}
if (length($out_file)) {
my $clncnt = sprintf("%5d", scalar @lines);
prt( "$tmsg: Put $clncnt lines to [$out_file] ... from [$in_file] ...\n" );
write_out_file( $out_file ); # write out results, using HTML format ...
my $txtfil = $out_folder.'/txt/'.$txt_file;
copy($in_file, $txtfil) or pgm_exit(1,"Failed to copy file [$in_file] to [$txtfil]!\n");
$donecnt++;
}
}
if ($skipped > 0) {
prt( "Skipped $skipped already existing, and where time is not later ...\n" );
}
prt( "Processed $donecnt files, $newercnt were newer, ".($donecnt - $newercnt)." as new... skipped $skipped\n" );
get_old_index( $in_index ); # load HTML table, and get set of files ...
###generate_index() if (($write_index > 0)||($no_index > 0)); # output @ind_files - array of files to index.htm
generate_index() if ($write_index > 0); # output @ind_files - array of files to index.htm
} else {
prt( "FAILED to find any perl files in [$in_folder] ...\n" );
}
if ($efix_cnt) {
my $eff = scalar @efix_files;
prt("Note: $efix_cnt email changes were made, in $eff files ...\n");
if ($dbg5) {
##foreach my $fxf (@efix_files) {
for (my $i = 0; $i < $eff; $i++) {
my $fxf = $efix_files[$i][0];
my $sm = $efix_files[$i][1];
my $mm = $efix_files[$i][2];
prt( "In [$fxf] found [$sm], to [$mm] ...\n" ) ;
}
}
}
show_warnings();
$t1 = [gettimeofday];
$elapsed = tv_interval ( $t0, $t1 );
prt( "$pgmname - processing took $elapsed seconds ...\n" );
if ($out_log) {
close($LF);
system($log_file);
}
exit 0;
#####################################################################
#######################
### only subs below ###
#######################
sub mycmp_decend2 {
my $off = 1;
if (${$a}[$off] < ${$b}[$off]) {
prt( "+[".${$a}[$off]."] < [".${$b}[$off]."]\n" ) if $verb3;
return 1;
}
if (${$a}[$off] > ${$b}[$off]) {
prt( "-[".${$a}[$off]."] < [".${$b}[$off]."]\n" ) if $verb3;
return -1;
}
prt( "=[".${$a}[$off]."] == [".${$b}[$off]."]\n" ) if $verb3;
return 0;
}
sub is_valid_link($) {
my ($f_l) = shift;
my $ff = $out_folder . "\\" . $f_l;
if (($f_l =~ /\./) && ( -f $ff )) {
return 1;
}
return 0;
}
sub has_valid_files($) {
my ($tx) = shift;
my @atmp = split(/\s/,$tx); # get a list
my $f = '';
foreach $f (@atmp) {
if (is_valid_link($f)) {
return 1;
}
}
return 0;
}
# push(@AFileNames, $ind_file); store the file
# push(@AFileHashs, \%hr); # and store the functions used ...
sub add_jump_table($) {
my ($oh) = shift;
my $acnt = scalar @AFileNames;
my $bcnt = scalar @AFileHashs;
my ($itm, $bi, $b3, $msg);
my @bijumps = ();
my %nh = ();
my @b2 = ();
%nh = %HOldbifs; # get any OLD, from the OLD index
@b2 = keys %nh;
$b3 = scalar @b2;
if ($acnt != $bcnt) {
prtw("WARNING: THESE TWO COUNTS SHOULD BE THE SAME!!! $acnt vs $bcnt ???\n");
}
prt("Adding jump table for $acnt ($bcnt) new hashes ... plus $b3 from OLD index ...\n");
for ($itm = 0; $itm < $acnt; $itm++) {
my $fl = $AFileNames[$itm];
my $hr = $AFileHashs[$itm];
my $nfl = '';
prt("Processing file [$fl] ...\n") if ($verb4);
###my @kys = keys %{$hr}; # get built-ins for this file
my @kys = keys %{$AFileHashs[$itm]}; # get built-ins for this file
if (@kys) {
foreach my $ky (@kys) {
$nfl = '';
if (exists $nh{$ky} ) {
$nfl = $nh{$ky};
}
if ( $nfl =~ /$fl/i ) {
prt("$ky - File [$fl] aready in [$nfl] ...\n") if ($verb4);
} else {
if (length($nfl) && !($nfl =~ /\s$/) ) {
$nfl .= ' '; # add space
}
prt("$ky - Adding [$fl] to [$nfl] ...\n") if ($verb4);
$nfl .= $fl; # add file with this built-in
$nh{$ky} = $nfl; # store or create built-in with this, these files
}
}
} else {
prt("Failed ... NO KEYS for file [$fl] ... check ...\n");
}
}
# done generation of a set of built in, with each file that contains that built-in
@b2 = sort keys %nh;
$b3 = scalar @b2;
if ( @b2 && ($b3 > 0)) {
my $allbi = ' '.join(' ', @BuiltIns).' '; # ensure begin and end with space
prt("Adding 3rd table with $b3 built-ins ...\n");
out_link_line($oh, 4); # avoid jumptable
$msg = "\n";
$msg .= "
This is a link/jump table for built-in functions. ";
$msg .= "The link is to file(s) using that function. Enjoy ;=))
\n";
print $oh $msg;
$menu_wrap = 0;
my $mwrap = $wrap_menu;
# *TABLE* FUNCTION TABLE - jumptable
print $oh '
'."\n";
print $oh '
Jump index to Perl samples
'."\n";
print $oh '
Built-In
Jump file(s)
'."\n";
foreach $bi (@b2) {
my $v = $nh{$bi};
# check if the 'value' has any valid file to link to
if (has_valid_files($v)) {
$msg = "
\n";
$allbi =~ s/\s+$bi\s+/ /; # delete this entry
# set to 1 to add anchor names (bookmarks) to index table 3 ...
$msg .= '
';
$msg .= "" if ($add_link);
$msg .= $bi."
\n";
push(@bijumps, $bi);
###$msg .= '
'.$v."
\n";
my @tmpa = split(/\s/,$v);
$msg .= "
\n";
###foreach my $tmpf (@tmpa) {
foreach my $tmpf (sort @tmpa) { # put jumps in alphabetic order
if (is_valid_link($tmpf)) {
$msg .= " $tmpf\n";
} else {
# this needs to be FIXED - I think it is from the reading of the OLD
prt( "DISCARDED: [$bi] file [$tmpf] ...\n" ) if ($dbg6);
}
}
if (length($msg) > $maxcelltxt ) { # = (8 * 12 * 12 * 2)
$msg .= "\npage links: ";
$msg .= get_link_txt(0);
$menu_wrap = 0;
}
$msg .= "
\n";
$msg .= "
\n";
print $oh $msg; # PRINT EACH LINE, but text in 2nd cell can mean MANY lines ...
$menu_wrap++;
if ($mwrap && ($menu_wrap > $mwrap)) {
$msg = get_menu_row( 2 );
print $oh $msg;
$menu_wrap = 0;
}
} else {
prt( "DISCARD: bif=[$bi] has no valid files [$v]!!!\n" );
}
}
$allbi = trim_line($allbi);
if (length($allbi)) {
$msg = "
\n";
$msg .= "
missed
\n";
$msg .= "
$allbi
\n";
$msg .= "
\n";
print $oh $msg;
}
print $oh "
\n";
if (@bijumps) {
$msg = '';
$msg .= '
';
$msg .= get_link_txt( 0 );
$msg .= "
\n";
print $oh $msg;
$msg = '';
$msg .= '
';
$msg .= "";
$msg .= "Alphabetic link list, into the Function Table. ";
foreach $bi (@bijumps) {
$msg .= '' . $bi . ' ' . "\n";
}
$msg .= "
\n";
print $oh $msg;
}
} else {
prt( "Failed ... no keys in %nh ... NO JUMP TABLE DONE!\n" );
}
}
###########################################################################
# out link line
# This navigation line is pepered throughout the index file
#
# out_link_line($OF, $num);
# name=""
# get_link_txt(0); # get em all
# get_link_txt(1); # avoid adding top
# get_link_txt(2); # avoid adding alphabetc
# get_link_txt(3); # avoid dateindex
# get_link_txt(4); # avoid jumptable
# get_link_txt(5); # avoid packages
# get_link_txt(6); # avoid end
# get_link_txt(7); # avoid functions
###########################################################################
sub get_link_txt {
my ($num) = shift;
my $msg = '';
$msg .= '[ Back ]'."\n";
$msg .= '[ top ]'."\n" if ($num != 1);
$msg .= '[ Alphabetic ]'."\n" if ($num != 2);
$msg .= '[ Date Order ]'."\n" if ($num != 3);
$msg .= '[ Functions ]'."\n" if ($num != 4);
$msg .= '[ Sorted Func. ]'."\n" if ($num != 7);
$msg .= '[ Packages ]'."\n" if ($num != 5);
$msg .= '[ end ]'."\n" if ($num != 6);
return $msg;
}
# out_link_line($OF, 0); # avoid none
sub out_link_line($$) {
my ($oh, $num) = @_;
my $txt = get_link_txt( $num );
print $oh '
';
print $oh $txt;
print $oh "
\n";
}
sub get_menu_row {
my ($wp) = shift;
my $ms = '';
$ms .= "
\n";
if ($wp) {
$ms .= "
";
$ms .= get_link_txt( 0 ); # get ALL
$ms .= "
";
$ms .= "
\n";
return $ms;
}
sub add_new_table($) {
my ($oh) = shift;
my @ind_sort = sort mycmp_decend2 @ind_files;
my $icnt = scalar @ind_sort;
my $cnt = 0;
my $i = 0;
my $line = '';
my $date = '';
my $sz = 0;
my $msg = '';
prt("Adding 2nd table ...\n");
out_link_line($oh, 3); # avoid dateindex
$msg = "\n";
$msg .= "
This is a repeated table in date order, with the latest, most recent listed first.";
$msg .= " Enjoy ;=))
\n";
print $oh $msg;
# *TABLE* FILES - DATE INDEX TABLE - dateindex
print $oh '
'."\n";
print $oh '
Date index to Perl samples
'."\n";
# actual output of SORTED generated lines
$cnt = 0;
my $mwrap = $wrap_menu; # get default wrap
$menu_wrap = 0; # start line counter
for ($i = 0; $i < $icnt; $i++) {
$line = $ind_sort[$i][0];
$date = YYYYMMDD($ind_sort[$i][1]);
$sz = get_nn($ind_sort[$i][2]);
$msg = '';
if ($cnt == 0) {
$msg = "
\n";
out_link_line($oh, 0); # avoid none
}
}
#########################################################################
# generate index
#
# This index contains lots of information about what is in each specific
# file. It provides a sort of cross reference to all the perl samples,
# showing -
# table file list - alphabetic
# table file list - date order
# table of the built-in functions used, and an alphabetic link set.
# table of used packages
########################################################################
sub generate_index { # output @ind_files - array of files to index.htm
my $icnt = scalar @ind_files;
my $cnt = 0;
my $msg = '';
my $i = 0;
my $dcnt = 0;
my $ocnt = 0;
my $acnt = 0; # added to index.htm
if ($icnt == 0) {
prt( "No index.htm generated - no files to list ...\n" );
return;
}
my $slatest = YYYYMMDD($latest);
my $searly = YYYYMMDD($earliest);
my $of = $in_index; ## "$out_folder/$indexhtm"; # = something line 'index.htm';
open $OF, ">$of" or mydie("ERROR: Unable to generate index file ...aborting ...\n");
prt( "\nWriting [$of] HTML with $icnt files ...\n" );
print $OF "$m_doctype\n";
print $OF <<"EOF";
Index to Perl HTML Samples
EOF
add_metas($OF, 0);
print $OF <<"EOF";
EOF
print $OF <<"EOF";
This is a rather random sample of the Perl scripts I have generated over the last few years
($searly - $slatest), $icnt files. Some represent complete Perl applications, aimed at a particular
purpose, while others are just samples, sometimes not functional! And some, like logfile.pl, are
only 'include' files, ie require 'logfile.pl'. A small amount of script has been scraped from
various web site, to test some suggested functionality, but most are largely my own fun and games
with Perl.
Most are for command line use, rather than as CGI, on the WWW, but a few CGI are included.
When there is a series numbered 01, 02, 03, etc, this usually means the latest is the largest
number, but sometimes they are different samples. However, the date following each file name
link is a further indication of the age of the sample. And the original file size, in bytes, follows
that.
Each of these HTML files are generated from the Perl script, p2hall[series].pl, with colour coding added,
and, as can be read in the preamble to $pgmname, this means sometimes a simple
copy and paste will fail, due mainly to a 'translation' of certain characters.
But most of the time it should be ok, or only require minor fixes.
As always, *** USE AT OWN RISK ***. These are in the
'public domain' thus there is no 'licence' to worry about. Of course you MUST have a Perl runtime
installed, and in some special cases, additional Perl 'libraries' installed, to run those
particular files.
The file table is repeated. The first should be more or less in file name alphabetic order,
the second is in date order table, with the latest listed first. Then
there is a 'jump' table, where each Perl built-in function is listed,
with links to the file(s) that use that built-in. Then a sorted used
'function' list, and a
'package' list. Enjoy ;=))
\n";
prt( "Done primary table ".($acnt + $dcnt)." ... now to do date sorted table ...\n" );
add_new_table($OF); # add new table sorted by time
add_jump_table($OF); # put a jump table of build-in function
out_link_line($OF, 0) if (!$add_used); # avoid none, if not inserted before the table
add_use_table($OF) if ($add_used); # add packages
# add 4.01 validation ...
print $OF <<"EOF";
EOF
$msg = "\n";
$msg .= "\n";
print $OF $msg;
print $OF "\n";
print $OF "\n";
close($OF);
prt( "Done file [$of] with $icnt files, plus $dcnt of $tsc from previous ...\n" );
###system($of); # load the INDEX
}
##########################################################################
# The main file OUTPUT - that is the HTML file.
# It establishes the HTML header, which includes the CSS style
# information. then outputs each of the 'converted' lines ...
# this is what it is all about - to generate a HTML document
##########################################################################
sub write_out_file {
my ($of) = shift;
open $OF, ">$of" or mydie( "ERROR: Unable to create $of ... aborting ...\n" );
print $OF "$m_doctype\n";
print $OF <<"EOF";
$in_file to HTML
EOF
add_metas($OF, 1);
print $OF <<"EOF";
EOF
print $OF "\n";
print $OF "
EOF
print $OF ''."\n";
print $OF "\n";
print $OF "\n";
close($OF);
}
#########################################################
# A small set of 9 services which add in the CSS class,
# using .thetext.
#
# Each one does a different class, and the class
# is extracted to variables set above. This means
# they can easily be adjusted to new, different
# values ...
#
# They also accumulate statistic information on how
# many time each is used ...
#########################################################
# built-in functions
sub add_red {
my ($t) = shift;
$a_cnt++;
return (''.$t.'');
}
# perl comments
sub add_class_b {
my ($t) = shift;
$b_cnt++;
return (''.$t.'');
}
# perl reserved words
sub add_blue {
my ($t) = shift;
$c_cnt++;
return (''.$t.'');
}
# perl qw set
sub add_class_d {
my ($t) = shift;
$d_cnt++;
return (''.$t.'');
}
sub add_class_e {
my ($t) = shift;
$e_cnt++;
return (''.$t.'');
}
sub add_class_f {
my ($t) = shift;
$f_cnt++;
return (''.$t.'');
}
sub add_class_o {
my ($t) = shift;
$o_cnt++;
return (''.$t.'');
}
sub add_class_v {
my ($t) = shift;
$v_cnt++;
return (''.$t.'');
}
sub add_quote {
my ($t) = shift;
$q_cnt++;
return (''.$t.'');
}
#########################################################
# search the @ResWord array for an entry
sub in_res_words {
my ($t) = shift;
foreach my $rw (@ResWords) {
if ($t eq $rw) {
$last_resword = $rw;
if (exists $HResWdFnd{$rw}) {
$HResWdFnd{$rw}++; # another count
} else {
$HResWdFnd{$rw} = 1; # start count
}
return 1;
}
}
return 0;
}
# search the @BuiltIns array for an entry
sub is_built_in {
my ($t) = shift;
foreach my $rw (@BuiltIns) {
if ($t eq $rw) {
return 1;
}
}
return 0;
}
sub in_built_in {
my ($t) = shift;
if (is_built_in($t)) {
$last_builtin = $t;
if (exists $HFuncsFnd{$t}) {
### prt ( "Bumped Funcs [$t] ...\n" );
$HFuncsFnd{$t}++; # another count
} else {
### prt ( "Created Funcs [$t] ...\n" );
$HFuncsFnd{$t} = 1; # start count
}
return 1;
}
return 0;
}
sub in_built_in_ok {
my ($t) = shift;
foreach my $rw (@BuiltIns) {
if ($t eq $rw) {
$last_builtin = $rw;
if (exists $HFuncsFnd{$rw}) {
### prt ( "Bumped Funcs $rw ...\n" );
$HFuncsFnd{$rw}++; # another count
} else {
### prt ( "Created Funcs $rw ...\n" );
$HFuncsFnd{$rw} = 1; # start count
}
return 1;
}
}
return 0;
}
sub is2lt {
my $t = shift;
$t =~ s/</= 2 ) && ( $t =~ /<<$/ ) ) {
return 1;
}
return 0;
}
sub sans_quotes {
my $t = shift;
$t =~ s/"//g;
$t =~ s/'//g;
return $t;
}
######################################################
# Converting SPACES to ' '
# Of course this could be done just using perl's
# powerful search and replace, but this handles
# any number of spaces, only converting the number
# minus 1 to ... not sure how to have
# this level of control with regex replacement
######################################################
sub conv_spaces {
my $t = shift;
my ($c, $i, $nt, $ln, $sc, $sp);
$nt = ''; # accumulate new line here
$ln = length($t);
for ($i = 0; $i < $ln; $i++) {
$c = substr($t,$i,1);
if ($c eq ' ') {
$i++; # bump to next
$sc = 0;
$sp = '';
for ( ; $i < $ln; $i++) {
$c = substr($t,$i,1);
if ($c ne ' ') {
last; # exit
}
$sc++;
$sp .= $c;
}
if ($sc) {
$sp =~ s/ / /g;
$nt .= $sp;
}
$i--; # back up one
$c = ' '; # add back the 1 space
}
$nt .= $c;
}
prt( "conv_space: from [$t] to [$nt] ...\n" ) if $debug_on;
return $nt;
}
###########################################################################
# VERY IMPORTANT SERVICE
# This converts the 'text' into HTML text, but only does a partial job!
# 1. Convert '&' to '&' to avoid interpreting as replacement
# 2. Convert '<' to '<' to avoid interpreting as HTML
# 3. Convert '"' to '"'
# 4. Convert '\t' to SPACES
# 5. Finally, if there are double or more SPACES, convert to ' '
###########################################################################
sub html_line {
my $t = shift;
my $ot = $t;
$t =~ s/&/&/g; # all '&' become '&'
$t =~ s/</g; # make sure all '<' is/are swapped out
$t =~ s/\"/"/g; # and all quotes become "
$t =~ s/\t/$tab_space/g; # tabs to spaces
if ($t =~ /\s\s/) { # if any two consecutive white space
return conv_spaces($t);
}
prt( "html_line: from [$ot] to [$t] ...\n" ) if $debug_on;
return $t;
}
##########################################################
# The following two functions 'convert' scalar variables
# to colour codes spans, in the print < 4)&&(length($ind_file))) {
$lne = substr($lne,0,$i2);
###print "[$lne]\n";
if ($lne =~ /^use\s+(.+)/) {
$pkg = trimall($1);
###print "[$pkg]\n";
if ( defined $HUsedpack{$pkg} ) {
$v = $HUsedpack{$pkg};
if ( $v =~ /$ind_file/ ) {
$dn = 3;
} else {
$v .= ' ' . $ind_file;
$HUsedpack{$pkg} = $v;
$dn = 2;
}
} else {
$HUsedpack{$pkg} = $ind_file;
$dn = 1;
}
}
}
if ($dn) {
if ($dn == 1) {
prt( "New USE [$pkg] in [$in_file] out [$ind_file] ... [$_[0]]\n" ) if ($verb5);
} elsif ($dn == 3) {
prt( "Repeat USE [$pkg] in [$in_file] out [$ind_file] ...[$_[0]]\n" ) if ($verb5);
} else {
prt( "Added USE [$pkg] in [$in_file] out [$ind_file] ...[$_[0]]\n" ) if ($verb5);
}
} else {
prtw( "WARNING: failed USE with $xlne ... [$in_file] out [$ind_file]\n" );
}
}
#################################################################
# The MAIN file processing
# The input file is openned, and all the lines read
# into an array @lns, then each line is processed,
# cheracter by character ...
# It does it mainly via a state, $st
# $st == 0 - processing white space
# $st == 1 - processing alphanumeric, plus _
# $st == 2 - processing nither space nor alphanumeric, here
# referred to as 'an_' ...
# $st == 3 - Locked in one of << thingies, until the end
# token located, or until end of file ...
# $st == 4 - Processing a qw(...) function, of qw/.../ if
# enabled.
#
# Generally the 'tokens' are stored in $tok, as the line
# is processed, added to the $nline at various change
# points, and finally the $nline is stored in the array
# @lines, for later output ...
#
# Setting $debug_on will give a BIG TRACE of where the
# code is handling something ...
#
# Setting $add_uvars to on will add colour code user variables
# but this adds a lot of extra weight to the file.
#
# At this time, the global variable $ind_file contains the
# HTML output file name ...
#
# At this time, the global variable $txt_file contains the
# TEXT copy file name ...
#################################################################
sub process_file {
my ($in_file, $bn) = @_; # input and base name
my ($IF);
my ($ch1,$ch2,$ch3,$ch4);
open $IF, "<$in_file" or die "ERROR: Unable to open $in_file ... aborting ...\n";
my @lns = <$IF>; # slurp into line array
close($IF);
my $max = scalar @lns;
prt( "\nGot $max lines to process from $in_file ...\n" ) if ($verb6 || $debug_on);
my $st = 0; # current status
my $nst = 0;
my $pc = '';
my $pc2 = '';
my $ch = '';
my $tok = '';
my $ltok = ''; # last token
my $ltok1 = '';
my $ltok2 = '';
my $qtok = ''; # print <<"EOF" or ANY <<'until_end', token
my $end_qw = '/';
my $i = 0;
my ($x);
# foreach my $ln (@lns) {
for ($x = 0; $x < $max; $x++) {
my $ln = $lns[$x];
$doc_total += length($ln);
chomp $ln;
$ln =~ s/\r$//; # and remove CR, if present
$ln = fix_email($ln, $bn); # keep list where email is present
my $len = length($ln);
my $nline = '';
prt( "\nline=[$ln] ...\n" ) if $debug_on;
$pc = '';
$pc2 = '';
$tok = '';
$ltok = ''; # last token
$ltok1 = ''; # token stack
$ltok2 = '';
$i = 0;
$nst = 0; # if fall through, next status is IN space
add_2_used($ln, $x, \@lns) if ($ln =~ /^use\s+/); # if line BEGINS with 'use '
if ($st == 3) {
# locked in a 'print' string to end token
if ($add_uvars) {
$nline = get_uform( $ln );
} else {
$nline = add_class_f(html_line($ln));
}
add_2_lines($nline);
if ($ln =~ /^$qtok/) {
$st = 0;
}
next; # next LINE of file
} elsif ($st == 4) {
# processing a 'qw' block - only if $brown_qw is ON
$tok = '';
for ( ; $i < $len; $i++) {
$ch = substr($ln, $i, 1);
if ($ch eq $end_qw) { # either '/' or ')' depending on start
$nline .= add_class_d(html_line($tok)) if (length($tok));
$tok = '';
last;
}
$tok .= $ch;
}
if ($i < $len) {
$nst = 2; # fall through to continue line
} else {
$nline = add_class_d(html_line($ln));
add_2_lines($nline);
next;
}
}
$st = $nst;
for ( ; $i < $len; $i++) {
$ch = substr($ln, $i, 1);
# make a BIG exception of '<' ...
if (($ch eq '&') && (($i + 3) < $len)) {
$ch1 = substr($ln, $i, 4);
if ($ch1 eq '<') {
$tok .= $ch1;
$i += 3;
$st = 2;
$pc = ';';
next;
}
}
if ($st == 0) {
# IN white space territory
if ($ch =~ /\S/) {
prt( "IN ws, changed to NOT with [$ch] ".
"\$tok=[$tok] \$ltok[$ltok] \$ltok1[$ltok1] \$ltok2[$ltok2] html\n" ) if $debug_on;
$nline .= html_line($tok); # add any white space to new line
$ltok2 = $ltok1;
$ltok1 = $ltok;
$ltok = $tok;
$tok = '';
# if NOT escape, or escaped escape character
if ( ($pc ne '\\') || (($pc eq '\\') && ($pc2 eq '\\')) ){
if ($ch eq '#') {
# start of a COMMENT
prt( "start of a COMMENT [$ch] ".
"tok=[$tok] ltok[$ltok] ltok1[$ltok1] ltok2[$ltok2] html\n" ) if $debug_on;
$tok = substr($ln, $i);
$nline .= add_class_b(html_line($tok));
$tok = '';
$st = 0;
last;
} elsif (($ch eq '"')||($ch eq "'")) {
my $bch = $ch;
prt( "start of a QUOTE [$ch] ".
"tok=[$tok] ltok[$ltok] ltok1[$ltok1] ltok2[$ltok2]\n" ) if $debug_on;
$tok = $ch;
$i++;
$pc2 = '';
for ( ; $i < $len; $i++ ) {
$ch = substr($ln, $i, 1);
# if the PREVIOUS is NOT an ESCAPE, OR the previous and previous ARE
# that is a ESCAPED ESCAPE character, which is NOT an escape at all ;=))
if ( ($pc ne '\\') || (($pc eq '\\')&&($pc2 eq '\\')) ) {
if ($ch eq $bch) {
$tok .= $ch;
prt( "End of a QUOTE [$ch] ".
"tok=[$tok] ltok[$ltok] ltok1[$ltok1] ltok2[$ltok2] html\n" ) if $debug_on;
if ($add_uvars && ($bch eq '"')) {
$nline .= add_quote2($tok);
} else {
$nline .= add_quote(html_line($tok));
}
$tok = '';
$pc2 = $pc;
$pc = $ch;
last;
}
}
$tok .= $ch;
$pc2 = $pc;
$pc = $ch;
}
$pc = $ch;
next;
}
}
$tok = $ch;
if ($ch =~ /\w/) {
prt( "Start tok with $ch ... sw st [$st] to 1\n" ) if $debug_on;
$st = 1;
} else {
prt( "Start tok with $ch ... sw st [$st] to 2\n" ) if $debug_on;
$st = 2;
}
$pc2 = $pc;
$pc = $ch;
next;
} else {
# staying in white space
$tok .= $ch;
$pc2 = $pc;
$pc = $ch;
next;
}
} elsif ($st == 1) {
# dealing with alphanumberic + _
if ($ch =~ /\w/) {
$tok .= $ch;
$pc2 = $pc;
$pc = $ch;
next; # continue alphanumeric + _
}
prt( "IN an_, no longer an_ with [$ch] ... tok=[$tok]\n" ) if $debug_on;
if (length($tok)) {
if (in_res_words($tok) ) {
$nline .= add_blue(html_line($tok));
if ($brown_qw && (($ch eq '(')||($ch eq '/')) && ($last_resword eq 'qw')) {
# entering a qw list
$end_qw = '/';
$end_qw = ')' if ($ch eq '(');
prt( "Excepting a qw list ... Begin $ch, End $end_qw ...\n" ) if $debug_on;
$i++;
$nline .= $ch;
$tok = ''; # no token
for ( ; $i < $len ; $i++) {
$ch = substr($ln,$i,1);
if ($ch eq $end_qw) { # end on '/' or ')' depending on start
$nline .= add_class_d(html_line($tok)) if (length($tok));
$nline .= $ch;
$tok = '';
last;
}
$tok .= $ch;
}
if ($i < $len) {
next; # get next character
} # else, we have ended the line, still in a 'qw' ...
$nline .= add_class_d(html_line($tok)) if (length($tok));
$tok = '';
$st = 4;
last; # end of THIS line
}
} elsif (in_built_in($tok)) {
$nline .= add_red(html_line($tok));
} else {
if ($add_uvars) { # colour code user variables
$ch1 = substr($tok,0,1);
if ($ch1 eq '$') {
$nline .= add_class_e(html_line($tok));
} elsif ($ch1 eq '@') {
$nline .= add_class_o(html_line($tok));
} elsif ($ch1 eq '%') {
$nline .= add_class_v(html_line($tok));
} else {
$nline .= html_line($tok);
}
} else {
$nline .= html_line($tok);
}
}
$ltok2 = $ltok1;
$ltok1 = $ltok;
$ltok = $tok;
$tok = '';
}
$tok = $ch;
if ($ch =~ /\s/) {
$st = 0; # goto SPACE mode
} elsif ($ch =~ /\w/) {
$st = 1; # goto AN_ mode
} else {
$st = 2; # goto NOT SPACE or AN_ mode
}
$pc2 = $pc;
$pc = $ch;
next;
} elsif ($st == 2) {
# not IN space or IN an_
if ($ch =~ /\s/) {
prt( "IN 2 - change back to space with [$ch] ... tok=[$tok]\n" ) if $debug_on;
if ( is2lt($tok) ) {
$ch1 = get_balance(substr($ln,$i)); # get balance of line
$ch1 =~ s/\s+$//; # remove any trailing white space
##if ( ($ch1 =~ /;$/) && ($ltok =~ /=/) ) { mod20060829 add ltok2 also
if ( ($ch1 =~ /;$/) && (($ltok =~ /=/)||($ltok1 =~ /=/)) ) {
$ch1 =~ s/^\s+//; # remove any leading spaces
$ch1 =~ s/;$//; # remove colon
$ch1 =~ s/\s+$//; # now again remove any trailing white space
if ( !($ch1 =~ /\s/) ) {
$qtok = sans_quotes($ch1); # STORE THE END MARKER !!!
prt( "Got <= 2) &&
is2lt($ltok) &&
length($qtok) ) {
$qtok = sans_quotes($qtok); # strip any DOUBLE/SINGLE quotes
prt( "Got print [$last_builtin] ltok[$ltok] qtok[$qtok] ...\n" ) if $debug_on;
$i++;
$nline .= html_line(substr($ln,$i));
$tok = '';
$st = 3;
last; # done this line
}
$pc2 = $pc;
$pc = $ch;
next;
}
}
if ($add_uvars && (($ch eq '$')||($ch eq '@')||($ch eq '%'))) {
prt( "In add_uvars and got \$\@\% [$ch] add tok 2 line ... reset tok\n" ) if $debug_on;
$nline .= html_line($tok); # add in current token
$ltok2 = $ltok1;
$ltok1 = $ltok;
$ltok = $tok;
$tok = '';
} else {
prt( "NOT space or alphanumeric, including _, or special, or \$\@\% [$ch] add2tok ...\n" ) if $debug_on;
}
$tok .= $ch;
}
$pc2 = $pc;
$pc = $ch;
}
$nline .= html_line($tok);
add_2_lines($nline); # push(@lines, $nline); after appending EOL
}
}
####################################
# Reducing a line to bare bones
# used when loading
# the EditPlus 2 perl.stx file.
# and getting used packages
####################################
sub trim_line($) {
my ($l) = shift;
chomp $l; # remove LF
$l =~ s/\r$//; # and remove CR, if present
$l =~ s/\t/ /g; # tabs to a space
$l =~ s/\s\s/ /g while ($l =~ /\s\s/); # duplicate space to single
$l = substr($l,1) while ($l =~ /^\s/); # each off leading space
$l = substr($l,0,length($l)-1) while (($l =~ /\s$/)&&(length($l))); # and trailing space
return $l;
}
sub trimall($) {
return( trim_line($_[0]) );
}
########################################
# Loading the reserved words, and
# perl built-in functions from a
# special EditPlus 2, perl.stx file,
# but there are arrays already included
# if you do not have this file.
########################################
sub load_stx_file {
my ($infil) = shift;
my ($IF);
my @stx = ();
my %dchk = ();
open $IF, "<$infil" or mydie( "ERROR: Unable to open $infil ... aborting ...\n" );
@stx = <$IF>; # slurp entire file into array
close($IF);
my $scnt = scalar @stx;
prt( "Got $scnt lines in $infil to process ...\n" );
my $st = 0;
foreach my $ln (@stx) {
my $tln = trim_line($ln);
my $ll = length($tln);
next if ($ll == 0);
if( $tln =~ /^\#KEYWORD=Reserved words/ ) {
$st = 1;
next;
} elsif ($tln =~ /^\#KEYWORD=Built-in functions/ ) {
$st = 2;
next;
} elsif (($tln =~ /^\#/) || ($tln =~ /^;/)) {
$st = 0;
next;
}
if (exists $dchk{$tln}) {
prt( "Warning: Avoiding duplicate of [$tln] ...\n" );
next;
}
$dchk{$tln} = 1;
if( $st == 1 ) {
push(@ResWords, $tln);
} elsif ($st == 2) {
push(@BuiltIns, $tln);
}
}
# this was ONLY used to get the internal list
# so this file becomes unneccessary ...
if ($out_lists) {
my $max = 85;
my $cnt = 20;
prt( '@ResWords = qw(' );
foreach my $ln (@ResWords) {
prt( $ln.' ' );
$cnt += length($ln);
if ($cnt > $max) {
prt("\n");
$cnt = 0;
}
}
prt( ");\n" );
$cnt = 20;
prt( '@BuiltIns = qw(' );
foreach my $ln (@BuiltIns) {
prt( $ln.' ' );
$cnt += length($ln);
if ($cnt > $max) {
prt("\n");
$cnt = 0;
}
}
prt( ");\n" );
}
}
sub in_efix_list {
my ($fn) = shift;
my $cc = scalar @efix_files;
###foreach my $n (@efix_files) {
for (my $i = 0; $i < $cc; $i++) {
my $n = $efix_files[$i][0];
if ($n eq $fn) {
return 1;
}
}
return 0;
}
################################################
# sadly, this is to mangle my email, so
# it does not 'appear' to web scrapers
################################################
sub fix_email {
my ($eml, $bfn) = @_; # get line, and base name
my $nem = $eml;
if ($eml =~ /$emreg/i) {
my $nm = $1.$2;
my $sm = mangled_email($nm);
my $ind = index($eml, $nm);
if (!($ind == 1)) {
$nem = substr($eml,0,$ind);
$nem .= $sm;
$nem .= substr($eml, $ind+length($nm));
$efix_cnt++;
push(@efix_files, [$bfn, $nm, $sm]) if (!in_efix_list($bfn));
}
print "got [$nm] ... now [$sm] ... ind $ind ...\n" if $dbgem;
} else {
print "failed\n" if $dbgem;
}
return $nem;
}
sub mangled_email {
my ($em) = shift;
$em =~ s/geoffmclane/geoffair/i;
$em =~ s/\./ _dot_ /;
$em =~ s/\@/ _at_ /;
return $em;
}
################################################
# My particular time 'translation'
sub YYYYMMDD {
# 0 1 2 3 4 5 6 7 8
my ($tm) = shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tm);
$year += 1900;
$mon += 1;
my $ymd = "$year/";
if ($mon < 10) {
$ymd .= '0'.$mon.'/';
} else {
$ymd .= "$mon/";
}
if ($mday < 10) {
$ymd .= '0'.$mday;
} else {
$ymd .= "$mday";
}
return $ymd;
}
##################################################
# My particular 'nice number'
sub get_nn { # perl nice number nicenum add commas
my ($n) = shift;
if (length($n) > 3) {
my $mod = length($n) % 3;
my $ret = (($mod > 0) ? substr( $n, 0, $mod ) : '');
my $mx = int( length($n) / 3 );
for (my $i = 0; $i < $mx; $i++ ) {
if (($mod == 0) && ($i == 0)) {
$ret .= substr( $n, ($mod+(3*$i)), 3 );
} else {
$ret .= ',' . substr( $n, ($mod+(3*$i)), 3 );
}
}
return $ret;
}
return $n;
}
##################################################
# My particular bytes to K, M, G
sub b2KMG($) {
my ($d) = shift;
if ($d < 1000) {
return $d;
}
my $oss;
my $kss;
my $lg = 0;
my $ks = ($d / 1024); #// get Ks
my $div = 1;
if( $ks < 1000 ) {
$div = 1;
$oss = "KB";
} elsif ( $ks < 1000000 ) {
$div = 1000;
$oss = "MB";
} elsif ( $ks < 1000000000 ) {
$div = 1000000;
$oss = "GB";
} else {
$div = 1000000000;
$oss = "TB";
}
$kss = $ks / $div;
$kss += 0.05;
$kss *= 10;
$lg = int($kss);
return( ($lg / 10) . " " . $oss );
###return( ($lg / 10) . $oss );
}
################################################
# A small 'print' service, that not only
# sends the output to STDOUT, but also
# directs it to a LOG file. I find it
# quite difficult to watch the console
# messages FLASH by ... Of course the
# output can be command line RE-DRIECTED,
# IF you are running it from the command
# line ... most of the time I run it
# from withing the Editor tool, thus thus
# provides a convenient look-back at what
# happend ... this is especially true when
# $debug_on is set ...
################################################
sub prt {
my ($m) = shift;
print $m;
print $LF $m if $out_log;
}
sub mydie {
my ($m) = shift;
prt($m);
die "Got above error ... aborting ...\n";
}
sub write2file {
my ($txt,$fil) = @_;
open WOF, ">$fil" or mydie("ERROR: Unable to open $fil!!!\n");
print WOF $txt;
close WOF;
}
#########################################################
######## keep the OLD index
### this is needed IF files have been DELETED ...
# ===============================================
sub get_old_index($) {
my ($ind) = shift;
$tacnt = 0;
$tacnt3 = 0;
if (open IF, "<$ind") {
@larr = ; # slurp it all in ...
close(IF);
$lncnt = scalar @larr;
prt( "Got $lncnt lines to process ... from [$ind]\n" );
###write2file( join('',@larr), 'tempout.txt');
$ln = tag2newline( join('',@larr), 'td' );
###$ln = tag2newline( $ln, 'br' );
@larr2 = split(/\n/, $ln);
###write2file( join("\n",@larr2), 'tempout3.txt');
if (get_table_array()) {
$tacnt = scalar @tbl_arr;
$tacnt3 = scalar @tbl_arr3;
prt( "Got $tacnt and $tacnt3 lines to process ... from [$ind]...\n" );
} else {
prt( "Failed to find table $tbl_num or $tbl_num3 ... in [$ind]...\n" );
}
} else {
prtw( "Warning: Failed to open $ind ...\n" );
### $no_index = 1;
}
if ($tacnt > 0) {
my $cc = 0;
for (my $i = 0; $i < $tacnt ; $i++) {
$ln = $tbl_arr[$i]; # extract a line
if ($ln =~ //i) {
while ( !($ln =~ /<\/td>/i) ) {
$i++;
if ($i < $tacnt) {
$ln .= ' '.$tbl_arr[$i]; # extract a line
} else {
last;
}
}
# got begin and end of
...
block
if ($ln =~ /()(.*)(<\/td>)/i) {
my $tds = $1;
my $inb = $2;
my $tde = $3;
# like Line [