1 #!/usr/bin/perl -w 2 # 3 # $RCSfile: ModifyNewLineChar.pl,v $ 4 # $Date: 2008/01/30 21:44:49 $ 5 # $Revision: 1.12 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2004-2008 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 5.006; 30 use strict; 31 use FindBin; use lib "$FindBin::Bin/../lib"; 32 use Getopt::Long; 33 use File::Basename; 34 use Text::ParseWords; 35 use Benchmark; 36 use FileUtil; 37 use TextUtil; 38 39 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime); 40 41 # Autoflush STDOUT 42 $| = 1; 43 44 # Starting message... 45 $ScriptName = basename $0; 46 print "\n$ScriptName:Starting...\n\n"; 47 $StartTime = new Benchmark; 48 49 # Get the options and setup script... 50 SetupScriptUsage(); 51 if ($Options{help} || @ARGV < 1) { 52 die GetUsageFromPod("$FindBin::Bin/$ScriptName"); 53 } 54 55 my( @FilesList); 56 @FilesList = ExpandFileNames(\@ARGV, ""); 57 58 my($Mode) = $Options{mode}; 59 60 print "Checking input file(s)...\n"; 61 my(@FilesOkay, @FilesOutFile); 62 RetrieveFileInfo(); 63 64 my($Index, $File, $NewFile, $Nothing); 65 if (@FilesList > 1) { 66 print "Processing files...\n"; 67 } 68 FILELIST: for $Index (0 .. $#FilesList) { 69 if ($FilesOkay[$Index]) { 70 $File = $FilesList[$Index]; 71 $NewFile = $FilesOutFile[$Index]; 72 if (@FilesList > 1) { 73 print "\nProcessing file $File...\n"; 74 } 75 else { 76 print "Processing file $File...\n"; 77 } 78 79 open NEWFILE, ">$NewFile" or die "Error: Can't open $NewFile: !$ \n"; 80 print "Generating $NewFile file\n"; 81 open FILE, "$File" or die "Error: Can't open $File: $! \n"; 82 while (<FILE>) { 83 LINE: { 84 if ($Mode =~ /^Unix$/i) { s/(\r\n)|(\r)|(\n)/\n/g; last LINE; } 85 if ($Mode =~ /^Windows$/i) { s/(\r\n)|(\r)|(\n)/\r\n/g; last LINE; } 86 if ($Mode =~ /^Mac$/i) { s/(\r\n)|(\r)|(\n)/\r/g; last LINE; } 87 $Nothing = 1; 88 } 89 print NEWFILE; 90 } 91 close NEWFILE; 92 close FILE; 93 } 94 } 95 96 print "$ScriptName:Done...\n\n"; 97 98 $EndTime = new Benchmark; 99 $TotalTime = timediff ($EndTime, $StartTime); 100 print "Total time: ", timestr($TotalTime), "\n"; 101 102 ############################################################################### 103 104 # Retrieve input files info... 105 sub RetrieveFileInfo { 106 my($File, $Index, $FileDir, $FileName, $FileExt, $NewFileName); 107 108 @FilesOkay = (); 109 @FilesOutFile = (); 110 FILELIST: for $Index (0 .. $#FilesList) { 111 $File = $FilesList[$Index]; 112 113 $FilesOkay[$Index] = 0; 114 if (!(-e $File)) { 115 warn "Warning: Ignoring file $File: It doesn't exist\n"; 116 next FILELIST; 117 } 118 if (!open FILE, "$File") { 119 warn "Warning: Ignoring file $File: Couldn't open it: $! \n"; 120 next FILELIST; 121 } 122 close FILE; 123 124 $FileDir = ""; $FileName = ""; $FileExt = ""; 125 ($FileDir, $FileName, $FileExt) = ParseFileName($File); 126 $NewFileName = $FileName; 127 if ($Options{root} && (@FilesList == 1)) { 128 my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root}); 129 if ($RootFileName && $RootFileExt) { 130 $NewFileName = $RootFileName; 131 } 132 else { 133 $NewFileName = $Options{root}; 134 } 135 $NewFileName .= ".$FileExt"; 136 } 137 else { 138 $NewFileName .= "$Mode" . ".$FileExt"; 139 } 140 if (!$Options{overwrite}) { 141 if (-e $NewFileName) { 142 warn "Warning: Ignoring file $File: New Text file, $NewFileName, already exists\n"; 143 next FILELIST; 144 } 145 } 146 $FilesOkay[$Index] = 1; 147 $FilesOutFile[$Index] = $NewFileName; 148 } 149 } 150 151 # Setup script usage and retrieve command line arguments specified using various options... 152 sub SetupScriptUsage { 153 154 # Retrieve all the options... 155 %Options = (); 156 $Options{mode} = "Unix"; 157 if (!GetOptions(\%Options, "help|h", "mode|m=s", "overwrite|o", "root|r=s", "workingdir|w=s")) { 158 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"; 159 } 160 if ($Options{workingdir}) { 161 if (! -d $Options{workingdir}) { 162 die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n"; 163 } 164 chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n"; 165 } 166 if ($Options{mode} !~ /^(Unix|Windows|Mac)$/i) { 167 die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: Unix, Windows, or Mac\n"; 168 } 169 } 170