1 #!/usr/bin/perl -w 2 # 3 # $RCSfile: ModifyNewLineChar.pl,v $ 4 # $Date: 2015/02/28 20:46:20 $ 5 # $Revision: 1.26 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2015 Manish Sud. All rights reserved. 10 # 11 # This file is part of MayaChemTools. 12 # 13 # MayaChemTools is free software; you can redistribute it and/or modify it under 14 # the terms of the GNU Lesser General Public License as published by the Free 15 # Software Foundation; either version 3 of the License, or (at your option) any 16 # later version. 17 # 18 # MayaChemTools is distributed in the hope that it will be useful, but without 19 # any warranty; without even the implied warranty of merchantability of fitness 20 # for a particular purpose. See the GNU Lesser General Public License for more 21 # details. 22 # 23 # You should have received a copy of the GNU Lesser General Public License 24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or 25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330, 26 # Boston, MA, 02111-1307, USA. 27 # 28 29 use strict; 30 use FindBin; use lib "$FindBin::Bin/../lib"; 31 use Getopt::Long; 32 use File::Basename; 33 use Text::ParseWords; 34 use Benchmark; 35 use FileUtil; 36 use TextUtil; 37 38 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime); 39 40 # Autoflush STDOUT 41 $| = 1; 42 43 # Starting message... 44 $ScriptName = basename $0; 45 print "\n$ScriptName:Starting...\n\n"; 46 $StartTime = new Benchmark; 47 48 # Get the options and setup script... 49 SetupScriptUsage(); 50 if ($Options{help} || @ARGV < 1) { 51 die GetUsageFromPod("$FindBin::Bin/$ScriptName"); 52 } 53 54 my( @FilesList); 55 @FilesList = ExpandFileNames(\@ARGV, ""); 56 57 # Process options... 58 print "Processing options...\n"; 59 my(%OptionsInfo); 60 ProcessOptions(); 61 62 print "Checking input file(s)...\n"; 63 my(%FilesInfo); 64 RetrieveFilesInfo(); 65 66 # Generate output files... 67 my($FileIndex); 68 if (@FilesList > 1) { 69 print "\nProcessing files...\n"; 70 } 71 for $FileIndex (0 .. $#FilesList) { 72 if ($FilesInfo{FileOkay}[$FileIndex]) { 73 print "\nProcessing file $FilesList[$FileIndex]...\n"; 74 ModifyNewLineChar($FileIndex); 75 } 76 } 77 print "\n$ScriptName:Done...\n\n"; 78 79 $EndTime = new Benchmark; 80 $TotalTime = timediff ($EndTime, $StartTime); 81 print "Total time: ", timestr($TotalTime), "\n"; 82 83 ############################################################################### 84 85 # Modify new line characters... 86 sub ModifyNewLineChar { 87 my($Index) = @_; 88 my($File, $NewFile, $Mode, $Nothing); 89 90 $File = $FilesList[$Index]; 91 $NewFile = $FilesInfo{OutFile}[$Index]; 92 93 $Mode = $OptionsInfo{Mode}; 94 95 print "Generating new $NewFile file...\n"; 96 97 open NEWFILE, ">$NewFile" or die "Error: Can't open $NewFile: !$ \n"; 98 open FILE, "$File" or die "Error: Can't open $File: $! \n"; 99 100 while (<FILE>) { 101 LINE: { 102 if ($Mode =~ /^Unix$/i) { s/(\r\n)|(\r)|(\n)/\n/g; last LINE; } 103 if ($Mode =~ /^Windows$/i) { s/(\r\n)|(\r)|(\n)/\r\n/g; last LINE; } 104 if ($Mode =~ /^Mac$/i) { s/(\r\n)|(\r)|(\n)/\r/g; last LINE; } 105 $Nothing = 1; 106 } 107 print NEWFILE; 108 } 109 110 close NEWFILE; 111 close FILE; 112 } 113 114 # Retrieve input files info... 115 sub RetrieveFilesInfo { 116 my($File, $Index, $FileDir, $FileName, $FileExt, $NewFileName); 117 118 %FilesInfo = (); 119 120 @{$FilesInfo{FileOkay}} = (); 121 @{$FilesInfo{OutFile}} = (); 122 123 FILELIST: for $Index (0 .. $#FilesList) { 124 $File = $FilesList[$Index]; 125 126 $FilesInfo{FileOkay}[$Index] = 0; 127 $FilesInfo{OutFile}[$Index] = ""; 128 129 if (!(-e $File)) { 130 warn "Warning: Ignoring file $File: It doesn't exist\n"; 131 next FILELIST; 132 } 133 134 if (!open FILE, "$File") { 135 warn "Warning: Ignoring file $File: Couldn't open it: $! \n"; 136 next FILELIST; 137 } 138 close FILE; 139 140 $FileDir = ""; $FileName = ""; $FileExt = ""; 141 ($FileDir, $FileName, $FileExt) = ParseFileName($File); 142 $NewFileName = $FileName; 143 if ($OptionsInfo{OutFileRoot} && (@FilesList == 1)) { 144 my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot}); 145 if ($RootFileName && $RootFileExt) { 146 $NewFileName = $RootFileName; 147 } 148 else { 149 $NewFileName = $OptionsInfo{OutFileRoot}; 150 } 151 } 152 else { 153 $NewFileName .= $OptionsInfo{Mode}; 154 } 155 156 if ($FileExt) { 157 $NewFileName .= ".$FileExt"; 158 } 159 160 if (!$OptionsInfo{Overwrite}) { 161 if (-e $NewFileName) { 162 warn "Warning: Ignoring file $File: New Text file, $NewFileName, already exists\n"; 163 next FILELIST; 164 } 165 } 166 $FilesInfo{FileOkay}[$Index] = 1; 167 $FilesInfo{OutFile}[$Index] = $NewFileName; 168 } 169 } 170 171 # Process option values... 172 sub ProcessOptions { 173 %OptionsInfo = (); 174 175 $OptionsInfo{Mode} = $Options{mode}; 176 177 $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : undef; 178 $OptionsInfo{Overwrite} = $Options{overwrite} ? $Options{overwrite} : undef; 179 180 } 181 182 # Setup script usage and retrieve command line arguments specified using various options... 183 sub SetupScriptUsage { 184 185 # Retrieve all the options... 186 %Options = (); 187 $Options{mode} = "Unix"; 188 189 if (!GetOptions(\%Options, "help|h", "mode|m=s", "overwrite|o", "root|r=s", "workingdir|w=s")) { 190 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"; 191 } 192 if ($Options{workingdir}) { 193 if (! -d $Options{workingdir}) { 194 die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n"; 195 } 196 chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n"; 197 } 198 if ($Options{mode} !~ /^(Unix|Windows|Mac)$/i) { 199 die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: Unix, Windows, or Mac\n"; 200 } 201 } 202