comparison bin/ModifyNewLineChar.pl @ 0:4816e4a8ae95 draft default tip

Uploaded
author deepakjadmin
date Wed, 20 Jan 2016 09:23:18 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4816e4a8ae95
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
203 __END__
204
205 =head1 NAME
206
207 ModifyNewLineChar.pl - Modify new line char(s)
208
209 =head1 SYNOPSIS
210
211 ModifyNewLineChar.pl File(s)...
212
213 ModifyNewLineChar.pl [B<-h, --help>] [B<-m, --mode> Unix | Mac | Windows] [B<-o, --overwrite>]
214 [B<-r, --root> rootname] [B<-w, --workingdir> dirname] File(s)...
215
216 =head1 DESCRIPTION
217
218 Modify new line char(s) in ASCII files to interchange among Unix, Windows, and Mac
219 formats.
220
221 =head1 OPTIONS
222
223 =over 4
224
225 =item B<-h, --help>
226
227 Print this help message.
228
229 =item B<-m, --mode> I<Unix | Mac | Windows>
230
231 New line char(s) mode. Possible values: I<Unix, Mac, or Windows>. Default: I<Unix>. Here
232 are default values for new line char(s): I<Unix - \n; Windows: \r\n; Mac - \r>
233
234 =item B<-o, --overwrite>
235
236 Overwrite existing files.
237
238 =item B<-r, --root> I<rootname>
239
240 New text file name is generated using the root: <Root>.<Ext>. Default new file name:
241 <InitialFileName><Mode>.<InitialFileExt>. This option is ignored for multiple input files.
242
243 =item B<-w, --workingdir> I<dirname>
244
245 Location of working directory. Default: current directory.
246
247 =back
248
249 =head1 EXAMPLES
250
251 To use Unix new line char and generate NewSample1.csv file, type:
252
253 % ModifyNewLineChar.pl -m Unix -r NewSample1 -o Sample1.csv
254
255 To use Mac new line char and generate NewSample1.sdf file, type:
256
257 % ModifyNewLineChar.pl -m Mac -r NewSample1 -o Sample1.sdf
258
259 To use Windows new line chars and generate NewSample1.csv file, type:
260
261 % ModifyNewLineChar.pl -m Windows -r NewSample1 -o Sample1.csv
262
263 =head1 AUTHOR
264
265 Manish Sud <msud@san.rr.com>
266
267 =head1 SEE ALSO
268
269 ModifyTextFilesFormat.pl
270
271 =head1 COPYRIGHT
272
273 Copyright (C) 2015 Manish Sud. All rights reserved.
274
275 This file is part of MayaChemTools.
276
277 MayaChemTools is free software; you can redistribute it and/or modify it under
278 the terms of the GNU Lesser General Public License as published by the Free
279 Software Foundation; either version 3 of the License, or (at your option)
280 any later version.
281
282 =cut