MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # File: ModifyNewLineChar.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 
  35 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  36 
  37 # Autoflush STDOUT
  38 $| = 1;
  39 
  40 # Starting message...
  41 $ScriptName = basename $0;
  42 print "\n$ScriptName:Starting...\n\n";
  43 $StartTime = new Benchmark;
  44 
  45 # Get the options and setup script...
  46 SetupScriptUsage();
  47 if ($Options{help} || @ARGV < 1) {
  48   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  49 }
  50 
  51 my( @FilesList);
  52 @FilesList = ExpandFileNames(\@ARGV, "");
  53 
  54 # Process options...
  55 print "Processing options...\n";
  56 my(%OptionsInfo);
  57 ProcessOptions();
  58 
  59 print "Checking input file(s)...\n";
  60 my(%FilesInfo);
  61 RetrieveFilesInfo();
  62 
  63 # Generate output files...
  64 my($FileIndex);
  65 if (@FilesList > 1) {
  66   print "\nProcessing files...\n";
  67 }
  68 for $FileIndex (0 .. $#FilesList) {
  69   if ($FilesInfo{FileOkay}[$FileIndex]) {
  70     print "\nProcessing file $FilesList[$FileIndex]...\n";
  71     ModifyNewLineChar($FileIndex);
  72   }
  73 }
  74 print "\n$ScriptName:Done...\n\n";
  75 
  76 $EndTime = new Benchmark;
  77 $TotalTime = timediff ($EndTime, $StartTime);
  78 print "Total time: ", timestr($TotalTime), "\n";
  79 
  80 ###############################################################################
  81 
  82 # Modify new line characters...
  83 sub ModifyNewLineChar {
  84   my($Index) = @_;
  85   my($File, $NewFile, $Mode, $Nothing);
  86 
  87   $File = $FilesList[$Index];
  88   $NewFile = $FilesInfo{OutFile}[$Index];
  89 
  90   $Mode = $OptionsInfo{Mode};
  91 
  92   print "Generating new $NewFile file...\n";
  93 
  94   open NEWFILE, ">$NewFile" or die "Error: Can't open $NewFile: !$ \n";
  95   open FILE, "$File" or die "Error: Can't open $File: $! \n";
  96 
  97   while (<FILE>) {
  98     LINE: {
  99       if ($Mode =~ /^Unix$/i) { s/(\r\n)|(\r)|(\n)/\n/g; last LINE; }
 100       if ($Mode =~ /^Windows$/i) { s/(\r\n)|(\r)|(\n)/\r\n/g; last LINE; }
 101       if ($Mode =~ /^Mac$/i) { s/(\r\n)|(\r)|(\n)/\r/g; last LINE; }
 102       $Nothing = 1;
 103     }
 104     print NEWFILE;
 105   }
 106 
 107   close NEWFILE;
 108   close FILE;
 109 }
 110 
 111 # Retrieve input files info...
 112 sub RetrieveFilesInfo {
 113   my($File, $Index, $FileDir, $FileName, $FileExt, $NewFileName);
 114 
 115   %FilesInfo = ();
 116 
 117   @{$FilesInfo{FileOkay}} = ();
 118   @{$FilesInfo{OutFile}} = ();
 119 
 120   FILELIST: for $Index (0 .. $#FilesList) {
 121     $File = $FilesList[$Index];
 122 
 123     $FilesInfo{FileOkay}[$Index] = 0;
 124     $FilesInfo{OutFile}[$Index] = "";
 125 
 126     if (!(-e $File)) {
 127       warn "Warning: Ignoring file $File: It doesn't exist\n";
 128       next FILELIST;
 129     }
 130 
 131     if (!open FILE, "$File") {
 132       warn "Warning: Ignoring file $File: Couldn't open it: $! \n";
 133       next FILELIST;
 134     }
 135     close FILE;
 136 
 137     $FileDir = ""; $FileName = ""; $FileExt = "";
 138     ($FileDir, $FileName, $FileExt) = ParseFileName($File);
 139     $NewFileName = $FileName;
 140     if ($OptionsInfo{OutFileRoot} && (@FilesList == 1)) {
 141       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot});
 142       if ($RootFileName && $RootFileExt) {
 143         $NewFileName = $RootFileName;
 144       }
 145       else {
 146         $NewFileName = $OptionsInfo{OutFileRoot};
 147       }
 148     }
 149     else {
 150       $NewFileName .= $OptionsInfo{Mode};
 151     }
 152 
 153     if ($FileExt) {
 154       $NewFileName .= ".$FileExt";
 155     }
 156 
 157     if (!$OptionsInfo{Overwrite}) {
 158       if (-e $NewFileName) {
 159         warn "Warning: Ignoring file $File: New Text file, $NewFileName, already exists\n";
 160         next FILELIST;
 161       }
 162     }
 163     $FilesInfo{FileOkay}[$Index] = 1;
 164     $FilesInfo{OutFile}[$Index] = $NewFileName;
 165   }
 166 }
 167 
 168 # Process option values...
 169 sub ProcessOptions {
 170   %OptionsInfo = ();
 171 
 172   $OptionsInfo{Mode} = $Options{mode};
 173 
 174   $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : undef;
 175   $OptionsInfo{Overwrite} = $Options{overwrite} ? $Options{overwrite} : undef;
 176 
 177 }
 178 
 179 # Setup script usage  and retrieve command line arguments specified using various options...
 180 sub SetupScriptUsage {
 181 
 182   # Retrieve all the options...
 183   %Options = ();
 184   $Options{mode} = "Unix";
 185 
 186   if (!GetOptions(\%Options, "help|h", "mode|m=s", "overwrite|o", "root|r=s", "workingdir|w=s")) {
 187     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";
 188   }
 189   if ($Options{workingdir}) {
 190     if (! -d $Options{workingdir}) {
 191       die "Error: The value specified, $Options{workingdir},  for option \"-w --workingdir\" is not a directory name.\n";
 192     }
 193     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 194   }
 195   if ($Options{mode} !~ /^(Unix|Windows|Mac)$/i) {
 196     die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: Unix, Windows, or Mac\n";
 197   }
 198 }
 199