MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # File: TextFilesToHTML.pl
   4 # Author: Manish Sud <msud@san.rr.com>
   5 #
   6 # Copyright (C) 2019 Manish Sud. All rights reserved.
   7 #
   8 # This file is part of MayaChemTools.
   9 #
  10 # MayaChemTools is free software; you can redistribute it and/or modify it under
  11 # the terms of the GNU Lesser General Public License as published by the Free
  12 # Software Foundation; either version 3 of the License, or (at your option) any
  13 # later version.
  14 #
  15 # MayaChemTools is distributed in the hope that it will be useful, but without
  16 # any warranty; without even the implied warranty of merchantability of fitness
  17 # for a particular purpose.  See the GNU Lesser General Public License for more
  18 # details.
  19 #
  20 # You should have received a copy of the GNU Lesser General Public License
  21 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
  22 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
  23 # Boston, MA, 02111-1307, USA.
  24 #
  25 
  26 use strict;
  27 use FindBin; use lib "$FindBin::Bin/../lib";
  28 use Getopt::Long;
  29 use File::Basename;
  30 use Text::ParseWords;
  31 use Benchmark;
  32 use FileUtil;
  33 use TextUtil;
  34 use HTMLUtil;
  35 
  36 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  37 
  38 # Autoflush STDOUT
  39 $| = 1;
  40 
  41 # Starting message...
  42 $ScriptName = basename($0);
  43 print "\n$ScriptName: Starting...\n\n";
  44 $StartTime = new Benchmark;
  45 
  46 # Get the options and setup script...
  47 SetupScriptUsage();
  48 if ($Options{help} || @ARGV < 1) {
  49   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  50 }
  51 
  52 my(@TextFilesList);
  53 @TextFilesList = ExpandFileNames(\@ARGV, "csv tsv");
  54 
  55 print "Processing options...\n";
  56 my(%OptionsInfo);
  57 ProcessOptions();
  58 
  59 print "Checking input text file(s)...\n";
  60 my(%TextFilesInfo);
  61 RetrieveTextFilesInfo();
  62 SetupCoulmnsTablesAndMiscInfo();
  63 
  64 # Generate output files...
  65 my($FileIndex);
  66 if (@TextFilesList > 1) {
  67   print "\nProcessing text files...\n";
  68 }
  69 for $FileIndex (0 .. $#TextFilesList) {
  70   if ($TextFilesInfo{FileOkay}[$FileIndex]) {
  71     print "\nProcessing file $TextFilesList[$FileIndex]...\n";
  72     GenerateHTMLTable($FileIndex);
  73   }
  74 }
  75 print "\n$ScriptName:Done...\n\n";
  76 
  77 $EndTime = new Benchmark;
  78 $TotalTime = timediff ($EndTime, $StartTime);
  79 print "Total time: ", timestr($TotalTime), "\n";
  80 
  81 ###############################################################################
  82 
  83 # Generate HTML table(s)...
  84 sub GenerateHTMLTable {
  85   my($Index) = @_;
  86 
  87   if ($TextFilesInfo{MultipleHTMLTables}[$Index]) {
  88     GenerateMultipleHTMLTable($Index);
  89   }
  90   else {
  91     GenerateOneHTMLTable($Index);
  92   }
  93 }
  94 
  95 # Generate one table...
  96 sub GenerateOneHTMLTable {
  97   my($Index) = @_;
  98   my($TextFile, $TopHTMLDir, $HTMLFile, $Line, $StartRowNum, $EndRowNum, $CSSFile, $CSSFilePath, $CSSRef);
  99 
 100   $HTMLFile = $TextFilesInfo{HTMLRoot}[$Index] . ".html";
 101   $TextFile = $TextFilesList[$Index];
 102 
 103   # Setup data directories...
 104   ($TopHTMLDir) = SetupDataDirs($Index);
 105 
 106   # Setup stylesheet file...
 107   $CSSRef = "";
 108   if ($Options{stylesheet} =~ /^new$/i) {
 109     $CSSFile = $TextFilesInfo{HTMLRoot}[$Index] . ".css"; $CSSRef = ".\/" . "$CSSFile";
 110     $CSSFilePath = "$TopHTMLDir" . "\/" . $CSSFile;
 111     GenerateStyleSheetFile($CSSFilePath);
 112   }
 113   elsif ($Options{stylesheet} =~ /^old$/i) {
 114     $CSSRef = $Options{stylesheetname};
 115   }
 116   # Set HTML file location...
 117   $HTMLFile = "$TopHTMLDir" . "\/" . $HTMLFile;
 118 
 119   print "Generating HTML file $HTMLFile...\n";
 120   open HTMLFILE, ">$HTMLFile" or die "Error: Can't open $HTMLFile: $! \n";
 121   open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n";
 122 
 123   # Write out HTML page header...
 124   print HTMLFILE SetupHTMLPageHeader($TextFilesInfo{HTMLTitle}[$Index], $CSSRef);
 125   if ($OptionsInfo{TitleDisplay}) {
 126     print HTMLFILE SetupHTMLPageTitle($TextFilesInfo{HTMLTitle}[$Index]);
 127   }
 128   else {
 129     print HTMLFILE SetupHTMLEmptyLines(1);
 130   }
 131 
 132   # Start the table...
 133   print HTMLFILE SetupHTMLAlignmentBegin("center");
 134   print HTMLFILE SetupHTMLTableHeader($OptionsInfo{TableBorder}, $OptionsInfo{TableCellPadding}, $OptionsInfo{TableCellSpacing});
 135 
 136   WriteColLabels($Index, \*TEXTFILE, \*HTMLFILE);
 137 
 138   # Skip the labels and write out all the other rows...
 139   $Line = <TEXTFILE>;
 140   $StartRowNum = 1;
 141   $EndRowNum = $TextFilesInfo{LineCount}[$Index];
 142   WriteRowValues($Index, $StartRowNum, $EndRowNum, \*TEXTFILE, \*HTMLFILE);
 143 
 144   # Finish up the table...
 145   print HTMLFILE SetupHTMLTableEnd();
 146   print HTMLFILE SetupHTMLAlignmentEnd("center");
 147 
 148   # Write out HTML page end...
 149   print HTMLFILE SetupHTMLPageEnd($OptionsInfo{Footer});
 150 
 151   close HTMLFILE;
 152   close TEXTFILE;
 153 }
 154 
 155 # Generate multiple tables...
 156 sub GenerateMultipleHTMLTable {
 157   my($Index) = @_;
 158   my($TopHTMLDir, $SubHTMLDir, $TextFile, $HTMLFile, $TableNum, $TableCount, $TableIndex, $TableStartLineNum, $TableEndLineNum, $Line, $InSubHTMLDir, $PrintMsg, $CSSFile, $CSSFilePath, $CSSRef, $NewStyleSheet);
 159 
 160   # Open text file and skip over label line...
 161   $TextFile = $TextFilesList[$Index];
 162   open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n";
 163   $Line = <TEXTFILE>;
 164 
 165   # Set up data directories to hold various html files...
 166   ($TopHTMLDir, $SubHTMLDir) = SetupDataDirs($Index);
 167 
 168   # Create stylesheet file...
 169   $CSSRef = "";
 170   $NewStyleSheet = 0;
 171   if ($Options{stylesheet} =~ /^new$/i) {
 172     $NewStyleSheet = 1;
 173     $CSSFile = $TextFilesInfo{HTMLRoot}[$Index] . ".css";
 174     $CSSFilePath = "$TopHTMLDir" . "\/" . $CSSFile;
 175     GenerateStyleSheetFile($CSSFilePath);
 176   }
 177   elsif ($Options{stylesheet} =~ /^old$/i) {
 178     $CSSRef = $Options{stylesheetname};
 179   }
 180 
 181   $PrintMsg = 1;
 182   # Generate HTML files for all the tables...
 183   $TableCount = $TextFilesInfo{TableCount}[$Index];
 184   for $TableNum (1 .. $TableCount) {
 185     $TableIndex = $TableNum - 1;
 186     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$TableIndex];
 187     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$TableIndex];
 188     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$TableIndex];
 189 
 190     # Setup file name...
 191     if ($TableNum == 1) {
 192       $HTMLFile = "$TopHTMLDir" . "\/" . $HTMLFile;
 193       print "Generating HTML file $HTMLFile...\n";
 194     }
 195     else {
 196       $HTMLFile = "$SubHTMLDir" . "\/" . $HTMLFile;
 197       if ($PrintMsg) {
 198         $PrintMsg = 0;
 199         if ($TableCount == 2) {
 200           print "Generating HTML file $HTMLFile...\n";
 201         }
 202         else {
 203           print "Generating ", ($TableCount - 1), " other HTML files: $SubHTMLDir\/$TextFilesInfo{HTMLRoot}[$Index]\*.html...\n";
 204         }
 205       }
 206     }
 207     # Setup stylesheet reference...
 208     if ($NewStyleSheet) {
 209       $CSSRef = ($TableNum == 1) ? ".\/" : "..\/";
 210       $CSSRef .= $CSSFile;
 211     }
 212 
 213     open HTMLFILE, ">$HTMLFile" or die "Error: Can't open $HTMLFile: $! \n";
 214     # Write out HTML page header...
 215     print HTMLFILE SetupHTMLPageHeader($TextFilesInfo{HTMLTitle}[$Index], $CSSRef);
 216 
 217     # Set up the navigation links for this table...
 218     if ($OptionsInfo{NavLinksAtTop}) {
 219       WriteNavigationLinks($Index, $TableNum, \*HTMLFILE);
 220     }
 221     # Setup page title...
 222     if ($OptionsInfo{TitleDisplay}) {
 223       print HTMLFILE SetupHTMLPageTitle($TextFilesInfo{HTMLTitle}[$Index]);
 224     }
 225     else {
 226       print HTMLFILE SetupHTMLEmptyLines(1);
 227     }
 228 
 229     # Start the table...
 230     print HTMLFILE SetupHTMLAlignmentBegin("center");
 231     print HTMLFILE SetupHTMLTableHeader($OptionsInfo{TableBorder}, $OptionsInfo{TableCellPadding}, $OptionsInfo{TableCellSpacing});
 232 
 233     WriteColLabels($Index, \*TEXTFILE, \*HTMLFILE);
 234 
 235     # Write out appropriate row data for this table...
 236     WriteRowValues($Index, $TableStartLineNum, $TableEndLineNum, \*TEXTFILE, \*HTMLFILE);
 237 
 238     # Finish up the table...
 239     print HTMLFILE SetupHTMLTableEnd();
 240     print HTMLFILE SetupHTMLAlignmentEnd("center");
 241 
 242     # Set up the navigation links for this table...
 243     if ($OptionsInfo{NavLinksAtBottom}) {
 244       print HTMLFILE SetupHTMLEmptyLines(1);
 245       WriteNavigationLinks($Index, $TableNum, \*HTMLFILE);
 246     }
 247 
 248     # Write out HTML page end...
 249     print HTMLFILE SetupHTMLPageEnd($OptionsInfo{Footer});
 250     close HTMLFILE;
 251   }
 252   close TEXTFILE;
 253 
 254 }
 255 
 256 # Create stylesheet file...
 257 sub GenerateStyleSheetFile {
 258   my($CSSFile) = @_;
 259     print "Generating stylesheet file $CSSFile...\n";
 260     open CSSFILE, ">$CSSFile" or die "Error: Can't open $CSSFile: $! \n";
 261     print CSSFILE SetupHTMLStyleSheetTags();
 262     close CSSFILE;
 263 }
 264 
 265 # Write out table header using column labels...
 266 sub WriteColLabels {
 267   my($Index, $TextFileRef, $HTMLFileRef) = @_;
 268   my(@ColLabels, $Label);
 269 
 270   print $HTMLFileRef $TextFilesInfo{TableRowHeaderTags};
 271 
 272   @ColLabels = @{$TextFilesInfo{ColLabels}[$Index]};
 273   for $Label (@ColLabels) {
 274     print $HTMLFileRef SetupHTMLTableRowHeaderValue($Label);
 275   }
 276   print $HTMLFileRef $TextFilesInfo{RowEndTags};
 277 }
 278 
 279 #Write out the rows value...
 280 sub WriteRowValues {
 281   my($Index, $StartRowNum, $EndRowNum, $TextFileRef, $HTMLFileRef) = @_;
 282   my($ColNum, $BackgroundColor, $FontColor, $LineCount, $Line, @RowValues, $Value, $InDelim, $LastColNum);
 283 
 284   $InDelim = $TextFilesInfo{InDelim}[$Index];
 285   $LastColNum = @{$TextFilesInfo{ColLabels}[$Index]} - 1;
 286 
 287   for $LineCount ($StartRowNum .. $EndRowNum) {
 288     $Line = GetTextLine($TextFileRef);
 289 
 290     if ($OptionsInfo{ShadeRowsStatus}) {
 291       print $HTMLFileRef ($LineCount % 2) ? $TextFilesInfo{BgFilledOddRowHeaderTags} : $TextFilesInfo{BgFilledEvenRowHeaderTags};
 292     }
 293     else {
 294       print $HTMLFileRef $TextFilesInfo{RowHeaderTags};
 295     }
 296     @RowValues = quotewords($InDelim, 0, $Line);
 297     for $ColNum (0 .. $LastColNum) {
 298       $Value = ($ColNum <= $#RowValues) ? $RowValues[$ColNum] : "";
 299       $BackgroundColor = ""; $FontColor = "";
 300       if ($OptionsInfo{HighlightStatus}) {
 301         if (exists($TextFilesInfo{HightlightColNumMap}[$Index]{$ColNum})) {
 302           ($BackgroundColor, $FontColor) = GetValueHighlightColors($Index, $ColNum, $Value);
 303         }
 304       }
 305       print $HTMLFileRef SetupHTMLTableRowDataValue($Value, $BackgroundColor, $FontColor);
 306     }
 307     print $HTMLFileRef $TextFilesInfo{RowEndTags};
 308   }
 309 }
 310 
 311 # Setup navigation link information for each table.
 312 #
 313 # All table sets besides first and last have these links: FirstTable, Previous, Current-1,Current,Current+1,  Next, and LastTable
 314 # First set: Current, Next, and LastTable
 315 # Last set: FirstTable, Previous and Current.
 316 #
 317 sub WriteNavigationLinks {
 318   my($Index, $CurTableNum, $HTMLFileRef) = @_;
 319   my($TableNum, $StartTableNum, $EndTableNum, $TableIndex, $BorderWidth, $CellPadding, $CellSpacing,$HTMLFile, $HTMLRefFile, $RelativeFileDir, $HTMLRefValue, $FirstTableNum, $FirstTableIndex, $LastTableNum, $LastTableIndex, $TableStartLineNum, $TableEndLineNum, $LastLineNum, $BGColor, $LinksOffSet);
 320 
 321   $LinksOffSet = 10;
 322 
 323   $FirstTableNum = 1; $FirstTableIndex = $FirstTableNum - 1;
 324   $LastTableNum = $TextFilesInfo{TableCount}[$Index]; $LastTableIndex = $LastTableNum - 1;
 325   $LastLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$LastTableIndex];
 326 
 327   # Figure out which links to display for a particular table...
 328   $StartTableNum = $CurTableNum - $LinksOffSet + 1;
 329   $StartTableNum = ($StartTableNum < $FirstTableNum) ? $FirstTableNum : $StartTableNum;
 330   if ($CurTableNum < $LinksOffSet) {
 331     $EndTableNum = $LinksOffSet;
 332   }
 333   else {
 334     $EndTableNum = $CurTableNum + $LinksOffSet - 1;
 335   }
 336   $EndTableNum = ($EndTableNum > $LastTableNum) ? $LastTableNum : $EndTableNum;
 337 
 338   my($InactiveLinkNumColor, $InactiveLinkFontBold) = ("#8e2323", "1");
 339   my($LinkTextColor, $LinkBGColor, $LinkFontBold) = ("", "", "1");
 340 
 341   # Start link table...
 342   $BorderWidth = 0; $CellPadding = 2; $CellSpacing = 2;
 343   print $HTMLFileRef SetupHTMLAlignmentBegin("center");
 344   print $HTMLFileRef SetupHTMLDivBegin("tablenav");
 345   print $HTMLFileRef  SetupHTMLTableHeader($BorderWidth, $CellPadding, $CellSpacing);
 346   print $HTMLFileRef $TextFilesInfo{RowHeaderTags};
 347 
 348   if ($OptionsInfo{NavLinksTableInfo} && $OptionsInfo{NavLinksLineInfo}) {
 349     print $HTMLFileRef SetupHTMLTableRowDataValue("Showing table $CurTableNum of $LastTableNum");
 350     print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
 351     print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
 352   }
 353 
 354   print $HTMLFileRef SetupHTMLTableRowDataValue("Tables: ");
 355   # Setup a link to first table...
 356   if ($StartTableNum != $FirstTableNum) {
 357     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$FirstTableIndex];
 358     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $FirstTableNum, $FirstTableNum) . $HTMLFile;
 359     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$FirstTableIndex];
 360     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$FirstTableIndex];
 361     $HTMLRefValue = SetupHTMLHRef("First", $HTMLRefFile, "First Table Containing Lines $TableStartLineNum To $TableEndLineNum");
 362     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
 363   }
 364 
 365   # Setup link to previous table...
 366   if ($CurTableNum != $FirstTableNum) {
 367     my($PreviousTableNum, $PreviousTableIndex);
 368     $PreviousTableNum = $CurTableNum - 1; $PreviousTableIndex = $PreviousTableNum - 1;
 369     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$PreviousTableIndex];
 370     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $PreviousTableNum, $FirstTableNum) . $HTMLFile;
 371     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$PreviousTableIndex];
 372     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$PreviousTableIndex];
 373     $HTMLRefValue = SetupHTMLHRef("Previous", $HTMLRefFile, "Previous Table Containing Lines $TableStartLineNum To $TableEndLineNum");
 374     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
 375   }
 376 
 377   for $TableNum ($StartTableNum .. $EndTableNum) {
 378     $TableIndex = $TableNum - 1;
 379     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$TableIndex];
 380     if ($TableNum == $CurTableNum) {
 381       print $HTMLFileRef SetupHTMLTableRowDataValue($TableNum, $LinkBGColor, $InactiveLinkNumColor, $InactiveLinkFontBold);
 382     }
 383     else {
 384       # Setup the link...
 385       my($RefTitle);
 386       $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$TableIndex];
 387       $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$TableIndex];
 388       $RefTitle = AddNumberSuffix($TableNum) . " Table Containing Lines $TableStartLineNum To $TableEndLineNum";
 389       $HTMLRefFile = GetRelativeFileDir($CurTableNum, $TableNum, $FirstTableNum) . $HTMLFile;
 390       $HTMLRefValue = SetupHTMLHRef($TableNum, $HTMLRefFile, $RefTitle);
 391       print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue);
 392     }
 393   }
 394 
 395   # Setup link to next table...
 396   if ($CurTableNum != $LastTableNum) {
 397     my($NextTableNum, $NextTableIndex);
 398     $NextTableNum = $CurTableNum + 1; $NextTableIndex = $NextTableNum - 1;
 399     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$NextTableIndex];
 400     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $NextTableNum, $FirstTableNum) . $HTMLFile;
 401     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$NextTableIndex];
 402     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$NextTableIndex];
 403     $HTMLRefValue = SetupHTMLHRef("Next", $HTMLRefFile, "Next Table Containing Lines $TableStartLineNum To $TableEndLineNum");
 404     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
 405   }
 406 
 407   # Setup link to last table...
 408   if ($EndTableNum != $LastTableNum) {
 409     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$LastTableIndex];
 410     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $LastTableNum, $FirstTableNum) . $HTMLFile;
 411     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$LastTableIndex];
 412     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$LastTableIndex];
 413     $HTMLRefValue = SetupHTMLHRef("Last", $HTMLRefFile, "Last Table Containing Lines $TableStartLineNum To $TableEndLineNum");
 414     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
 415   }
 416   # Setup current table info text....
 417   print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
 418   print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
 419   $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$CurTableNum - 1];
 420   $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$CurTableNum - 1];
 421   if ($OptionsInfo{NavLinksLineInfo}) {
 422     print $HTMLFileRef SetupHTMLTableRowDataValue("Showing lines $TableStartLineNum to $TableEndLineNum of $LastLineNum");
 423   }
 424   else {
 425     print $HTMLFileRef SetupHTMLTableRowDataValue("Showing table $CurTableNum of $LastTableNum");
 426   }
 427 
 428   print $HTMLFileRef $TextFilesInfo{RowEndTags};
 429   # End link table...
 430   print $HTMLFileRef SetupHTMLTableEnd();
 431   print $HTMLFileRef SetupHTMLDivEnd();
 432   print $HTMLFileRef SetupHTMLAlignmentEnd("center");
 433 }
 434 
 435 # Generate relative directory path...
 436 sub GetRelativeFileDir {
 437   my($FromTableNum, $ToTableNum, $FirstTableNum) = @_;
 438   my($RelativeFileDir) = "";
 439 
 440   if ($FromTableNum == $FirstTableNum) {
 441     $RelativeFileDir = ($ToTableNum == $FirstTableNum) ? ".\/" : ".\/html\/";
 442   }
 443   else {
 444     $RelativeFileDir = ($ToTableNum == $FirstTableNum) ? "..\/" : ".\/";
 445   }
 446   return $RelativeFileDir;
 447 }
 448 
 449 # Based on hightlight stype, return appropriate colors for background or text...
 450 sub GetValueHighlightColors {
 451   my($FileIndex, $ColNum, $Value) = @_;
 452   my($DataType, $Criterion, $CriterionValue, $BgColor, $FontColor, $ValueOk, $Nothing);
 453 
 454   $BgColor = ""; $FontColor = "";
 455   $DataType = ${$TextFilesInfo{HightlightDataMap}[$FileIndex]{$ColNum}}[0];
 456   $Criterion = ${$TextFilesInfo{HightlightDataMap}[$FileIndex]{$ColNum}}[1];
 457   $CriterionValue = ${$TextFilesInfo{HightlightDataMap}[$FileIndex]{$ColNum}}[2];
 458 
 459   $ValueOk = 0;
 460   if ($DataType =~ /^numeric$/i) {
 461   NUMSWITCH: {
 462       if ($Criterion =~ /^ge$/i) { $ValueOk = ($Value >= $CriterionValue) ? 1 : 0; last NUMSWITCH; }
 463       if ($Criterion =~ /^le$/i) { $ValueOk = ($Value <= $CriterionValue) ? 1 : 0; last NUMSWITCH; }
 464       if ($Criterion =~ /^eq$/i) { $ValueOk = ($Value == $CriterionValue) ? 1 : 0; last NUMSWITCH; }
 465       $Nothing = 1;
 466     }
 467   }
 468   else {
 469   TEXTSWITCH: {
 470       if ($Criterion =~ /^ge$/i) { $ValueOk = ($Value ge $CriterionValue) ? 1 : 0; last TEXTSWITCH; }
 471       if ($Criterion =~ /^le$/i) { $ValueOk = ($Value le $CriterionValue) ? 1 : 0; last TEXTSWITCH; }
 472       if ($Criterion =~ /^eq$/i) { $ValueOk = ($Value eq $CriterionValue) ? 1 : 0; last TEXTSWITCH; }
 473       $Nothing = 1;
 474     }
 475   }
 476   $BgColor = $ValueOk ? $OptionsInfo{ValueOkColor} : $OptionsInfo{ValueNotOkColor};
 477   if ($Options{highlightstyle} =~ /^text$/i) {
 478     $BgColor = "";
 479     $FontColor = $ValueOk ? $OptionsInfo{ValueOkColor} : $OptionsInfo{ValueNotOkColor};
 480   }
 481   return ($BgColor, $FontColor);
 482 }
 483 
 484 # Setup columns, tables and other information...
 485 sub SetupCoulmnsTablesAndMiscInfo {
 486   SetupColumnsToHighlightInfo();
 487   SetupMultipleTablesInfo();
 488   SetupHTMLTagsInfo();
 489 }
 490 
 491 # Setup columns to highlight information...
 492 sub SetupColumnsToHighlightInfo {
 493   my($ColID, $DataType, $Criterion, $Value, $Index, $ColNum, $ColLabel, $ColIndex);
 494 
 495   @{$TextFilesInfo{HightlightColNumMap}} = ();
 496   @{$TextFilesInfo{HightlightDataMap}} = ();
 497 
 498   for $Index (0 .. $#TextFilesList) {
 499     %{$TextFilesInfo{HightlightColNumMap}[$Index]} = ();
 500     %{$TextFilesInfo{HightlightDataMap}[$Index]} = ();
 501     if ($TextFilesInfo{FileOkay}[$Index]) {
 502       SPECIFIEDCOLS: for $ColIndex (0 .. $#{$OptionsInfo{SpecifiedColIds}}) {
 503         $ColID = $OptionsInfo{SpecifiedColIds}[$ColIndex];
 504         $DataType = $OptionsInfo{SpecifiedColDataTypes}[$ColIndex];
 505         $Criterion = $OptionsInfo{SpecifiedColCriteria}[$ColIndex];
 506         $Value = $OptionsInfo{SpecifiedColValues}[$ColIndex];
 507         if (!$OptionsInfo{HighlightStatus}) {
 508           next SPECIFIEDCOLS;
 509         }
 510         if ($Options{highlightby} =~ /^colnum$/i) {
 511           $ColNum = $ColID;
 512           if ($ColNum > 0 && $ColNum <= $TextFilesInfo{ColCount}[$Index]) {
 513             $ColNum -= 1;
 514           }
 515           else {
 516             warn "Warning: Ignoring column number, $ColID, specifed in quartet, \"$ColID,$DataType,$Criterion,$Value\", using \"--highlight\" option for $TextFilesList[$Index]: it doesn't exists \n";
 517             next SPECIFIEDCOLS;
 518           }
 519         }
 520         else {
 521           $ColLabel = $ColID;
 522           if (exists($TextFilesInfo{ColLabelToNumMap}[$Index]{$ColLabel})) {
 523             $ColNum = $TextFilesInfo{ColLabelToNumMap}[$Index]{$ColLabel};
 524           } else {
 525             warn "Warning: Ignoring column label, $ColID, specifed in quartet, \"$ColID,$DataType,$Criterion,$Value\", using \"--highlight\" option for $TextFilesList[$Index]: it doesn't exists \n";
 526             next SPECIFIEDCOLS;
 527           }
 528         }
 529         $TextFilesInfo{HightlightColNumMap}[$Index]{$ColNum} = $ColNum;
 530         @{$TextFilesInfo{HightlightDataMap}[$Index]{$ColNum}} =();
 531         push @{$TextFilesInfo{HightlightDataMap}[$Index]{$ColNum}}, ($DataType, $Criterion, $Value);
 532       }
 533     }
 534   }
 535 }
 536 
 537 # Setup navigation link information for multiple tables...
 538 sub SetupMultipleTablesInfo {
 539   my($Index, $LinesPerTable);
 540 
 541   $LinesPerTable = $Options{numrows};
 542   @{$TextFilesInfo{TableCount}} = ();
 543   @{$TextFilesInfo{TableHTMLFiles}} = ();
 544   @{$TextFilesInfo{TableStartLineNum}} = ();
 545   @{$TextFilesInfo{TableEndLineNum}} = ();
 546 
 547   for $Index (0 .. $#TextFilesList) {
 548     $TextFilesInfo{TableCount}[$Index] = 1;
 549     @{$TextFilesInfo{TableHTMLFiles}[$Index]} = ();
 550     @{$TextFilesInfo{TableStartLineNum}[$Index]} = ();
 551     @{$TextFilesInfo{TableEndLineNum}[$Index]} = ();
 552 
 553     if ($TextFilesInfo{FileOkay}[$Index]) {
 554       if ($TextFilesInfo{MultipleHTMLTables}[$Index]) {
 555         my($TableIndex, $TotalLines, $TableCount, $TableStartLineNum, $TableEndLineNum, $Name);
 556 
 557         $TotalLines = $TextFilesInfo{LineCount}[$Index];
 558         $TableCount = ($TotalLines % $LinesPerTable) ? (int($TotalLines/$LinesPerTable) + 1) : ($TotalLines/$LinesPerTable);
 559         $TextFilesInfo{TableCount}[$Index] = $TableCount;
 560         for $TableIndex (1 .. $TableCount) {
 561           $TableStartLineNum = ($TableIndex - 1) * $LinesPerTable + 1;
 562           $TableEndLineNum = ($TableIndex == $TableCount) ? $TotalLines : ($TableIndex * $LinesPerTable);
 563           push @{$TextFilesInfo{TableStartLineNum}[$Index]}, $TableStartLineNum;
 564           push @{$TextFilesInfo{TableEndLineNum}[$Index]}, $TableEndLineNum;
 565 
 566           # Setup HTML file names for all the tables...
 567           $Name = "Lines" . "$TableStartLineNum" . "To" . "$TableEndLineNum";
 568           if ($TableIndex == 1) {
 569             $Name = "";
 570           }
 571           $Name = $TextFilesInfo{HTMLRoot}[$Index] . $Name . ".html";
 572           push @{$TextFilesInfo{TableHTMLFiles}[$Index]}, $Name;
 573         }
 574         #print "$TextFilesList[$Index]: $TableCount -  @{$TextFilesInfo{TableStartLineNum}[$Index]} - @{$TextFilesInfo{TableEndLineNum}[$Index]} -  @{$TextFilesInfo{TableHTMLFiles}[$Index]}\n";
 575       }
 576     }
 577   }
 578 }
 579 
 580 # Setup HTML tags information...
 581 sub SetupHTMLTagsInfo {
 582   # Setup row tags...
 583   $TextFilesInfo{RowHeaderTags} = "";
 584   $TextFilesInfo{RowEndTags} = "";
 585   $TextFilesInfo{BgFilledOddRowHeaderTags} = "";
 586   $TextFilesInfo{BgFilledEvenRowHeaderTags} = "";
 587   $TextFilesInfo{TableRowHeaderTags} = "";
 588 
 589   $TextFilesInfo{RowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{RowHAlignment}, "", $OptionsInfo{RowVAlignment});
 590   $TextFilesInfo{RowEndTags} = SetupHTMLTableRowEnd();
 591 
 592   if ($OptionsInfo{ShadeRowsStatus}) {
 593     $TextFilesInfo{BgFilledOddRowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{RowHAlignment}, $OptionsInfo{OddRowsShadeColor}, $OptionsInfo{RowVAlignment});
 594     $TextFilesInfo{BgFilledEvenRowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{RowHAlignment}, $OptionsInfo{EvenRowsShadeColor}, $OptionsInfo{RowVAlignment});
 595   }
 596 
 597   $TextFilesInfo{TableRowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{TableHeaderRowHAlignment}, $OptionsInfo{TableHeaderRowColor}, $OptionsInfo{TableHeaderRowVAlignment});
 598 
 599 }
 600 
 601 #Make sure appropriate mode specific option values are specified...
 602 sub ProcessOptions {
 603 
 604   %OptionsInfo = ();
 605 
 606   $OptionsInfo{RowHAlignment} = "left"; $OptionsInfo{RowVAlignment} = "middle";
 607   if (exists($Options{align})) {
 608     my (@AlignValues) = split ",", $Options{align};
 609     if (@AlignValues == 2) {
 610       $OptionsInfo{RowHAlignment} = $AlignValues[0];
 611       $OptionsInfo{RowVAlignment} = $AlignValues[1];
 612     }
 613     elsif (@AlignValues == 1) {
 614       $OptionsInfo{RowHAlignment} = $AlignValues[0];
 615     }
 616     else {
 617       die "Error: Invalid number of values, ", scalar(@AlignValues) , ", specified by \"-a --align\" option.\nIt must contain only one or two value.\n";
 618     }
 619     if ($OptionsInfo{RowHAlignment} !~ /^(left|center|right)$/i) {
 620       die "Error: The horizontal alignment value specified, $Options{align}, for option \"-a --align\" is not valid. Allowed values: left, center, or right\n";
 621     }
 622     if ($OptionsInfo{RowVAlignment} !~ /^(top|middle|bottom)$/i) {
 623       die "Error: The horizontal alignment value specified, $Options{align}, for option \"-a --align\" is not valid. Allowed values: top, middle, or bottom\n";
 624     }
 625   }
 626 
 627   $OptionsInfo{TableHeaderRowHAlignment} = "center"; $OptionsInfo{TableHeaderRowVAlignment} = "middle";
 628   if (exists($Options{headeralign})) {
 629     my (@AlignValues) = split ",", $Options{headeralign};
 630     if (@AlignValues == 2) {
 631       $OptionsInfo{TableHeaderRowHAlignment} = $AlignValues[0];
 632       $OptionsInfo{TableHeaderRowVAlignment} = $AlignValues[1];
 633     }
 634     elsif (@AlignValues == 1) {
 635       $OptionsInfo{TableHeaderRowHAlignment} = $AlignValues[0];
 636     }
 637     else {
 638       die "Error: Invalid number of values, ", scalar(@AlignValues) , ", specified by \"--headeralign\" option.\nIt must contain only one or two value.\n";
 639     }
 640     if ($OptionsInfo{TableHeaderRowHAlignment} !~ /^(left|center|right)$/i) {
 641       die "Error: The horizontal alignment value specified, $Options{headeralign}, for option \"--headeralign\" is not valid. Allowed values: left, center, or right\n";
 642     }
 643     if ($OptionsInfo{TableHeaderRowVAlignment} !~ /^(top|middle|bottom)$/i) {
 644       die "Error: The horizontal alignment value specified, $Options{headeralign}, for option \"-a --headeralign\" is not valid. Allowed values: top, middle, or bottom\n";
 645     }
 646   }
 647 
 648   $OptionsInfo{TitleDisplay} = ($Options{titledisplay} =~ /^yes$/i) ? 1 : 0;
 649 
 650   if (exists($Options{border})) {
 651     $OptionsInfo{TableBorder} = $Options{border};
 652   }
 653   else {
 654     $OptionsInfo{TableBorder} = ($Options{mode} =~ /^(plain|highlight)$/i) ? 1 : 0;
 655   }
 656   $OptionsInfo{TableCellPadding} = $Options{cellpadding};
 657   $OptionsInfo{TableCellSpacing} = $Options{cellspacing};
 658   $OptionsInfo{Footer} = $Options{footer} ? $Options{footer} : "";
 659 
 660   if ($Options{headercolor}) {
 661     $OptionsInfo{TableHeaderRowColor} = $Options{headercolor};
 662   }
 663   else {
 664     $OptionsInfo{TableHeaderRowColor} = ($Options{mode} =~ /^plain$/i) ? "" : "#ccccff";
 665   }
 666 
 667   $OptionsInfo{NavLinksAtBottom} = 1; $OptionsInfo{NavLinksAtTop} = 0;
 668   if ($Options{displaylinks} =~ /^(both|top)$/i) {
 669     $OptionsInfo{NavLinksAtTop} = 1;
 670   }
 671   $OptionsInfo{NavLinksTableInfo} = 1; $OptionsInfo{NavLinksLineInfo} = 0;
 672   if ($Options{displaylinksinfo} =~ /^both$/i) {
 673     $OptionsInfo{NavLinksLineInfo} = 1;
 674     $OptionsInfo{NavLinksTableInfo} = 1;
 675   }
 676   elsif ($Options{displaylinksinfo} =~ /^line$/i) {
 677     $OptionsInfo{NavLinksLineInfo} = 1;
 678     $OptionsInfo{NavLinksTableInfo} = 0;
 679   }
 680 
 681   if ($Options{stylesheet} =~ /^old$/i ) {
 682     if (!$Options{stylesheetname}) {
 683       die "Error: No stylesheet name specified using \"--stylesheetname\" option: It is required for \"old\" value of \"-s --stylesheet\" option. \n";
 684     }
 685   }
 686 
 687   my(@ColorValues);
 688   $OptionsInfo{OddRowsShadeColor} = ""; $OptionsInfo{EvenRowsShadeColor} = ""; $OptionsInfo{ShadeRowsStatus} = 0;
 689   if ($Options{mode} =~ /^(shade|shadedhighlight)$/i) {
 690     $OptionsInfo{OddRowsShadeColor} = "#ffffff";
 691     $OptionsInfo{EvenRowsShadeColor} = "#e0e0eb";
 692     $OptionsInfo{ShadeRowsStatus} = 1;
 693     if ($Options{shadecolor}) {
 694       # Make sure only two value are specified...
 695       @ColorValues = split ",", $Options{shadecolor};
 696       if (@ColorValues == 2) {
 697         $OptionsInfo{OddRowsShadeColor} = $ColorValues[0];
 698         $OptionsInfo{EvenRowsShadeColor} = $ColorValues[1];
 699       }
 700       else {
 701         die "Error: Invalid number of values, ", scalar(@ColorValues) , ", specified by \"--shadecolor\" option.\nIt must contain only two values.\n";
 702       }
 703     }
 704   }
 705   $OptionsInfo{ValueOkColor} = ""; $OptionsInfo{ValueNotOkColor} = ""; $OptionsInfo{HighlightStatus} = 0;
 706   if ($Options{mode} =~ /^(highlight|shadedhighlight)$/i) {
 707     my($HighlightMode, $HighlightBy);
 708     $HighlightMode = $Options{mode}; $HighlightBy = $Options{highlightby};
 709 
 710     $OptionsInfo{HighlightStatus} = 1;
 711     $OptionsInfo{ValueOkColor} = "#0fff0f";
 712     $OptionsInfo{ValueNotOkColor} = "#ff0f0f";
 713     if ($Options{highlightstyle} =~ /^text$/i) {
 714       $OptionsInfo{ValueOkColor} = "#0fbb0f";
 715       $OptionsInfo{ValueNotOkColor} = "#ff0f0f";
 716     }
 717     if ($Options{highlightcolor}) {
 718       # Make sure two values are specified...
 719       @ColorValues = split ",", $Options{highlightcolor};
 720       if (@ColorValues == 2) {
 721         $OptionsInfo{ValueOkColor} = $ColorValues[0];
 722         $OptionsInfo{ValueNotOkColor} = $ColorValues[1];
 723       }
 724       else {
 725         die "Error: Invalid number of values, ", scalar(@ColorValues), ", specified by \"--highlightcolor\" option.\nIt must contain only two value for $HighlightMode value specified using \"-m --mode\" option.\n";
 726       }
 727     }
 728     if (!$Options{highlight}) {
 729       die "Error: Specify columns to be highlighted using \"--hightlight\" option\n";
 730     }
 731     # Retrieve quartet values from "hightlight" option...
 732     my(@HighlightValueQuartets);
 733 
 734     @HighlightValueQuartets = ();
 735     @HighlightValueQuartets = split ",", $Options{highlight};
 736     if ((@HighlightValueQuartets % 4)) {
 737       die "Error: Quartets not found in values specified using \"--highlight\" option for $HighlightMode \"-m --mode\"\n";
 738     }
 739     # Process quartets...
 740     my($Index, $Col, $DataType, $Criterion, $Value);
 741 
 742     @{$OptionsInfo{SpecifiedColIds}} = ();
 743     @{$OptionsInfo{SpecifiedColDataTypes}} = ();
 744     @{$OptionsInfo{SpecifiedColCriteria}} = ();
 745     @{$OptionsInfo{SpecifiedColValues}} = ();
 746     for ($Index = 0; $Index < @HighlightValueQuartets; $Index = $Index + 4) {
 747       $Col = $HighlightValueQuartets[$Index];
 748       $DataType = $HighlightValueQuartets[$Index + 1];
 749       $Criterion = $HighlightValueQuartets[$Index + 2];
 750       $Value = $HighlightValueQuartets[$Index + 3];
 751       if ($Options{highlightby} =~ /^colnum$/i ) {
 752         if (!IsPositiveInteger($Col)) {
 753           die "Error: Invalid column id, $Col, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: It must be an integer value > 0 for $HighlightMode \"-m --mode\" and $HighlightBy \"--highlightby\" option values.\n";
 754         }
 755       }
 756       if ($DataType !~ /^(numeric|text)$/i) {
 757         die "Error: Invalid column data type, $DataType, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: Valid values: numeric or text\n";
 758       }
 759       if ($Criterion !~ /^(eq|le|ge)$/i) {
 760         die "Error: Invalid criterion value, $Criterion, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: Valid values: le, ge, or eq\n";
 761       }
 762       if ($DataType =~ /^numeric$/i) {
 763         if (!IsFloat($Value)) {
 764           die "Error: Invalid criterion value, $Value, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: numeric value required for numeric data type\n";
 765         }
 766       }
 767       push @{$OptionsInfo{SpecifiedColIds}}, $Col;
 768       push @{$OptionsInfo{SpecifiedColDataTypes}}, $DataType;
 769       push @{$OptionsInfo{SpecifiedColCriteria}}, $Criterion;
 770       push @{$OptionsInfo{SpecifiedColValues}}, $Value;
 771     }
 772   }
 773 }
 774 
 775 # Retrieve information about input text files...
 776 sub RetrieveTextFilesInfo {
 777   my($LineCount, $TextFile, $FileDir, $FileName, $HTMLFile, $CSSFile, $HTMLRoot, $HTMLTitle, $FileExt, $Index, $ColIndex, $ColNum, $ColLabel, $LinesCount, $InDelim, $Line, @LineWords, @ColLabels, $TopHTMLDir);
 778 
 779   %TextFilesInfo = ();
 780 
 781   @{$TextFilesInfo{FileOkay}} = ();
 782   @{$TextFilesInfo{ColCount}} = ();
 783   @{$TextFilesInfo{ColLabels}} = ();
 784   @{$TextFilesInfo{ColLabelToNumMap}} = ();
 785   @{$TextFilesInfo{LineCount}} = ();
 786   @{$TextFilesInfo{InDelim}} = ();
 787 
 788   @{$TextFilesInfo{HTMLRoot}} = ();
 789   @{$TextFilesInfo{HTMLTitle}} = ();
 790   @{$TextFilesInfo{MultipleHTMLTables}} = ();
 791 
 792   @{$TextFilesInfo{TopHTMLDir}} = ();
 793   @{$TextFilesInfo{SubHTMLDir}} = ();
 794 
 795   FILELIST: for $Index (0 .. $#TextFilesList) {
 796     $TextFile = $TextFilesList[$Index];
 797 
 798     $TextFilesInfo{FileOkay}[$Index] = 0;
 799     $TextFilesInfo{ColCount}[$Index] = 0;
 800     $TextFilesInfo{LineCount}[$Index] = 0;
 801     $TextFilesInfo{InDelim}[$Index] = "";
 802     $TextFilesInfo{HTMLRoot}[$Index] = "";
 803     $TextFilesInfo{HTMLTitle}[$Index] = "";
 804     $TextFilesInfo{MultipleHTMLTables}[$Index] = 0;
 805 
 806     @{$TextFilesInfo{ColLabels}[$Index]} = ();
 807     %{$TextFilesInfo{ColLabelToNumMap}[$Index]} = ();
 808 
 809     if (!(-e $TextFile)) {
 810       warn "Warning: Ignoring file $TextFile: It doesn't exist\n";
 811       next FILELIST;
 812     }
 813     if (!CheckFileType($TextFile, "csv tsv")) {
 814       warn "Warning: Ignoring file $TextFile: It's not a csv or tsv file\n";
 815       next FILELIST;
 816     }
 817     ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile);
 818     if ($FileExt =~ /^tsv$/i) {
 819       $InDelim = "\t";
 820     }
 821     else {
 822       $InDelim = "\,";
 823       if ($Options{indelim} !~ /^(comma|semicolon)$/i) {
 824         warn "Warning: Ignoring file $TextFile: The value specified, $Options{indelim}, for option \"--indelim\" is not valid for csv files\n";
 825         next FILELIST;
 826       }
 827       if ($Options{indelim} =~ /^semicolon$/i) {
 828         $InDelim = "\;";
 829       }
 830     }
 831 
 832     if (!open TEXTFILE, "$TextFile") {
 833       warn "Warning: Ignoring file $TextFile: Couldn't open it: $! \n";
 834       next FILELIST;
 835     }
 836 
 837     $Line = GetTextLine(\*TEXTFILE);
 838     @ColLabels = quotewords($InDelim, 0, $Line);
 839     $LineCount = 0;
 840     while (<TEXTFILE>) {
 841       $LineCount++;
 842     }
 843     close TEXTFILE;
 844 
 845     $FileDir = ""; $FileName = ""; $FileExt = "";
 846     ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile);
 847     $HTMLRoot = $FileName;
 848     if ($Options{root} && (@TextFilesList == 1)) {
 849       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root});
 850       if ($RootFileName && $RootFileExt) {
 851         $HTMLRoot = $RootFileName;
 852       }
 853       else {
 854         $HTMLRoot = $Options{root};
 855       }
 856     }
 857     $HTMLTitle = $HTMLRoot;
 858     if ($Options{title} && (@TextFilesList == 1)) {
 859       $HTMLTitle = $Options{title};
 860     }
 861     $HTMLFile = lc($HTMLRoot) . "-html";
 862     if (!$Options{overwrite}) {
 863       if (-d $HTMLFile) {
 864         warn "Warning: Ignoring file $TextFile: The directory $HTMLFile already exists\n";
 865         next FILELIST;
 866       }
 867     }
 868 
 869     $TextFilesInfo{FileOkay}[$Index] = 1;
 870     $TextFilesInfo{InDelim}[$Index] = $InDelim;
 871     $TextFilesInfo{HTMLRoot}[$Index] = "$HTMLRoot";
 872     $TextFilesInfo{HTMLTitle}[$Index] = "$HTMLTitle";
 873 
 874     $TextFilesInfo{ColCount}[$Index] = @ColLabels;
 875     push @{$TextFilesInfo{ColLabels}[$Index]}, @ColLabels;
 876     for $ColNum (0 .. $#ColLabels) {
 877       $ColLabel = $ColLabels[$ColNum];
 878       $TextFilesInfo{ColLabelToNumMap}[$Index]{$ColLabel} = $ColNum;
 879     }
 880     $TextFilesInfo{LineCount}[$Index] = $LineCount;
 881 
 882     if ($Options{numrows} == 0 || $LineCount <= $Options{numrows}) {
 883       $TextFilesInfo{MultipleHTMLTables}[$Index] = 0;
 884     }
 885     else {
 886       $TextFilesInfo{MultipleHTMLTables}[$Index] = 1;
 887     }
 888     # Setup HTML data directories paths...
 889     $TopHTMLDir = lc($TextFilesInfo{HTMLRoot}[$Index]) . "-html";
 890     $TextFilesInfo{TopHTMLDir}[$Index] = "$TopHTMLDir";
 891     $TextFilesInfo{SubHTMLDir}[$Index] = "$TopHTMLDir\/html";
 892   }
 893 }
 894 
 895 # Setup various data directories to hold HTML and other related files...
 896 sub SetupDataDirs {
 897   my($Index) = @_;
 898   my($TopHTMLDir, $SubHTMLDir, $CreateTopHTMLDir, $CreateSubHTMLDir);
 899 
 900   $TopHTMLDir = $TextFilesInfo{TopHTMLDir}[$Index];
 901   $SubHTMLDir = $TextFilesInfo{SubHTMLDir}[$Index];
 902 
 903   # Clean up existing directories...
 904   if (-d $TopHTMLDir) {
 905     unlink "<$TopHTMLDir/*.html>";
 906     unlink "<$TopHTMLDir/*.css>";
 907   }
 908   if (-d $SubHTMLDir) {
 909     unlink "<$SubHTMLDir/*.html>";
 910   }
 911   # What directories need to be created...
 912   $CreateTopHTMLDir = (-d $TopHTMLDir) ? 0 : 1;
 913 
 914   $CreateSubHTMLDir = 0;
 915   if ($TextFilesInfo{MultipleHTMLTables}[$Index]) {
 916     $CreateSubHTMLDir = (-d $SubHTMLDir) ? 0 : 1;
 917   }
 918 
 919   # Create appropriate directories...
 920   if ($CreateTopHTMLDir) {
 921     mkdir $TopHTMLDir or die "Couldn't mkdir $TopHTMLDir: $! \n";
 922   }
 923   if ($CreateSubHTMLDir) {
 924     mkdir $SubHTMLDir or die "Error: Couldn't mkdir $SubHTMLDir: $! \n";
 925   }
 926   else {
 927     unlink <$SubHTMLDir/*.html>;
 928   }
 929   return ($TopHTMLDir, $SubHTMLDir);
 930 }
 931 
 932 # Setup script usage  and retrieve command line arguments specified using various options...
 933 sub SetupScriptUsage {
 934 
 935   # Retrieve all the options...
 936   %Options = ();
 937   $Options{indelim} = "comma";
 938   $Options{numrows} = 50;
 939 
 940   $Options{mode} = "shade";
 941   $Options{highlightby} = "colnum";
 942   $Options{highlightstyle} = "background";
 943 
 944   $Options{cellpadding} = 2;
 945   $Options{cellspacing} = 1;
 946 
 947   $Options{displaylinks} = "both";
 948   $Options{displaylinksinfo} = "both";
 949   $Options{stylesheet} = "new";
 950 
 951   $Options{titledisplay} = "yes";
 952 
 953   if (!GetOptions(\%Options, "align|a=s", "border|b=i", "cellpadding=i", "cellspacing=i", "color|c=s", "footer=s", "displaylinks|d=s", "displaylinksinfo=s", "help|h", "headeralign=s", "headercolor=s", "highlight=s", "highlightby=s", "highlightcolor=s", "highlightstyle=s", "indelim=s", "mode|m=s", "numrows|n=i", "overwrite|o", "root|r=s", "shadecolor=s", "stylesheet=s", "stylesheetname=s", "title|t=s", "titledisplay=s", "workingdir|w=s")) {
 954     die "\nTo get a list of valid options and their values, use \"$ScriptName -h\" or\n\"perl -S $ScriptName -h\" command and try again...\n";
 955   }
 956 
 957   if ($Options{workingdir}) {
 958     if (! -d $Options{workingdir}) {
 959       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 960     }
 961     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 962   }
 963   if ($Options{displaylinks} !~ /^(top|bottom|both)$/i) {
 964     die "Error: The value specified, $Options{displaylinks}, for option \"-d --displaylinks\" is not valid. Allowed values: top, bottom, or both\n";
 965   }
 966   if ($Options{displaylinksinfo} !~ /^(line|table|both)$/i) {
 967     die "Error: The value specified, $Options{displaylinksinfo}, for option \"--displaylinksinfo\" is not valid. Allowed values: line, table, or both\n";
 968   }
 969   if ($Options{indelim} !~ /^(comma|semicolon)$/i) {
 970     die "Error: The value specified, $Options{indelim}, for option \"--indelim\" is not valid. Allowed values: comma or semicolon\n";
 971   }
 972   if ($Options{highlightby} !~ /^(colnum|collabel)$/i) {
 973     die "Error: The value specified, $Options{highlightby}, for option \"--highlightby\" is not valid. Allowed values: colnum or collabel\n";
 974   }
 975   if ($Options{highlightstyle} !~ /^(background|text)$/i) {
 976     die "Error: The value specified, $Options{highlightstyle}, for option \"--highlightstyle\" is not valid. Allowed values: background or text\n";
 977   }
 978   if ($Options{mode} !~ /^(plain|shade|highlight|shadedhighlight)$/i) {
 979     die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: plain, shade, hightlight, or shadedhighlight\n";
 980   }
 981   if ($Options{stylesheet} !~ /^(old|new|none)$/i) {
 982     die "Error: The value specified, $Options{stylesheet}, for option \"-s --stylesheet\" is not valid. Allowed values: old, new, or none\n";
 983   }
 984   if ($Options{numrows} < 0) {
 985     die "Error: The value specified, $Options{numrows},  for option \"-n --numrows\" is not valid. Allowed values: >= 0 \n";
 986   }
 987   if ($Options{titledisplay} !~ /^(yes|no)$/i) {
 988     die "Error: The value specified, $Options{titledisplay}, for option \"--titledisplay\" is not valid. Allowed values: yes or no\n";
 989   }
 990   if (exists($Options{border})) {
 991     if ($Options{border} < 0) {
 992       die "Error: The value specified, $Options{border},  for option \"--border\" is not valid. Allowed values: >= 0 \n";
 993     }
 994   }
 995   if ($Options{cellpadding} < 0) {
 996     die "Error: The value specified, $Options{cellpadding},  for option \"--cellpadding\" is not valid. Allowed values: >= 0 \n";
 997   }
 998   if ($Options{cellspacing} < 0) {
 999     die "Error: The value specified, $Options{cellspacing},  for option \"--cellspacing\" is not valid. Allowed values: >= 0 \n";
1000   }
1001 }
1002