Subversion Repositories DevTools

Rev

Rev 229 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
#! perl
2
########################################################################
3
# Copyright ( C ) 2004 ERG Limited, All rights reserved
4
#
5
# Module name   : jats.sh
6
# Module type   : Makefile system
7
# Compiler(s)   : n/a
8
# Environment(s): jats
9
#
10
# Description   : Transform a file
11
#                 This is a JATS command line utility that can be used to:
12
#                   1) Copy a file
13
#                   2) Perform DOS <-> Unix converion
14
#                   3) Perform tag substitution
15
#
16
# Usage:    jats_transform_file
17
#               -infile=xxxxx
18
#               -outfile=yyyyyyy
19
#               -dos2unix, -unix2dos
20
#               -tag=tag,value
21
#
22
#               The utilty may be used by user scripts
23
#               The utilty may be used within GenerateFiles
24
#
25
#......................................................................#
26
 
27
require 5.6.1;
28
use strict;
29
use warnings;
30
 
31
use JatsError;
32
use FileUtils;
33
 
34
use File::Copy;
35
use Getopt::Long;
36
use Pod::Usage;                             # required for help support
37
 
38
 
39
################################################################################
40
#   Option variables
41
#
42
 
43
my $VERSION = "1.0.0";                      # Update this
44
my $opt_verbose = 0;
45
my $opt_datafile = "";
46
my $opt_ofile;
47
my $opt_infile;
48
my $opt_help = 0;
49
my $opt_manual;
50
my $opt_2dos;
51
my $opt_2unix;
52
my @opt_tags;
53
my $opt_tsep = ',';
54
 
55
#   Globals
56
#
57
my @tag_order;
58
my %tags;                                      # Tags data
59
 
60
my $result = GetOptions (
61
                "help+"     => \$opt_help,          # flag, multiple use allowed
62
                "manual"    => \$opt_manual,        # flag
63
                "verbose+"  => \$opt_verbose,       # flag
64
                "outfile=s" => \$opt_ofile,         # string
65
                "infile=s"  => \$opt_infile,        # string
66
                "unix"      => \$opt_2unix,         # flag
67
                "dos"       => \$opt_2dos,          # flag
68
                "tag=s"     => \@opt_tags,          # flag
69
                "tsep=s"    => \$opt_tsep,          # flag
70
                );
71
 
72
#
73
#   Process help and manual options
74
#
75
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
76
pod2usage(-verbose => 1)  if ($opt_help == 2 );
77
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));
78
 
79
#
80
#   Configure the error reporting process now that we have the user options
81
#
82
ErrorConfig( 'name'    =>'TRANS',
83
             'verbose' => $opt_verbose );
84
 
85
#
86
#   Allow two command line argumenst to be infile and outfile
87
#
88
unless ( $opt_infile || $opt_ofile )
89
{
90
    $opt_infile = shift @ARGV;
91
    $opt_ofile =  shift @ARGV;
92
}
93
 
94
#
95
#   Sanity test the user arguemnts
96
#
97
Error ("Too many command line arguments: @ARGV" )
98
    if ( $#ARGV >= 0 );
99
 
100
Error ("No Input File specified")
101
    unless ( $opt_infile );
102
 
103
Error ("Input File is a directory")
104
    if ( -d $opt_infile );
105
 
106
Error ("Input File does not exist")
107
    unless ( -e $opt_infile );
108
 
109
Error ("No Output File specified" )
110
    unless ( $opt_ofile );
111
 
112
Error ("Input and output file are the same" )
113
    if ( $opt_infile eq $opt_ofile );
114
 
115
Error ("Select one of DOS or UNIX line endings" )
116
    if ( $opt_2unix && $opt_2dos );
117
 
118
#
119
#   Determine the output directory
120
#   Ensure that it exists
121
#
122
my $tdir = StripFileExt( $opt_ofile );
123
Verbose ("Target directory: $tdir");
124
Error ("Target directory does not exist: $tdir")
125
    if ( $tdir && ! -d $tdir );
126
 
127
Verbose ("Create DOS line endings" ) if ( $opt_2dos );
128
Verbose ("Create UNIX line endings" ) if ( $opt_2unix );
129
 
130
#
131
#   Process the TAGS and create a structure to be used later
132
#
133
my $sep = quotemeta ($opt_tsep );
134
foreach my $tag ( @opt_tags )
135
{
136
    my ($tname,$tvalue) = split ( $sep, $tag );
137
    Error ("No tag value in: $tag" ) unless ( defined $tvalue );
138
    Error ("Duplicate Tag: $tname" ) if ( exists $tags{$tname} );
139
    Verbose ("Tag: $tname :: $tvalue");
140
    push @tag_order, $tname;
141
    $tags{$tname} = $tvalue;
142
}
143
 
144
#
145
#   Is this a simple file copy
146
#   If so then don't treat the file as a text file
147
#
148
unless ( $opt_2unix || $opt_2dos || @opt_tags )
149
{
150
    simple_file_copy();
151
    exit (0);
152
}
153
 
154
#
155
#   Not a simple copy
156
#   Read each line from the input file
157
#       - Perform translations
158
#       - Perform line end fixup
159
#
160
open (FH, "<", $opt_infile ) || Error ("Cannot open input file: $opt_infile", "Reason: $!");
161
binmode(FH);
162
 
163
open (FO, ">", $opt_ofile ) || Error ("Cannot open output file: $opt_ofile", "Reason: $!");
164
binmode(FO);
165
 
166
#
167
#   Setup line ending
168
#
169
my $eol = '';
170
$eol = "\n"   if ( $opt_2unix );
171
$eol = "\r\n" if ( $opt_2dos );
172
 
173
#
174
#   Read each line form the input file
175
#
176
while ( <FH> )
177
{
178
    #
179
    #   Perform tag replacement
180
    #
181
    foreach my $tag ( @tag_order )
182
    {
183
        my $value = $tags{$tag};
184
        if ( s~$tag~$value~g )
185
        {
186
            Verbose ("Replaced: $tag with $value");
187
        }
188
    }
189
 
190
    #
191
    #   Perform Line Ending massaging
192
    #
193
    if ( $opt_2unix || $opt_2dos )
194
    {
195
        s~[\n\r]+$~$eol~;
196
    }
197
 
198
    print FO $_;
199
}
200
close FH;
201
close FO;
202
 
203
exit (0);
204
 
205
 
206
#-------------------------------------------------------------------------------
207
# Function        : simple_file_copy
208
#
209
# Description     : Simple binary file copy
210
#                   Used to copy a file, with a possible name change
211
#
212
# Inputs          : None. Use globals
213
#                         $opt_infile - does exist and is not a directory
214
#
215
# Returns         : Will not return on error
216
#
217
sub simple_file_copy
218
{
219
    Verbose ("Simple Copy: $opt_infile, $opt_ofile" );
220
    unless (File::Copy::copy($opt_infile, $opt_ofile))
221
    {
222
        Error ("File Copy Failed: $!",
223
               "Input file: $opt_infile",
224
               "Output file: $opt_ofile" );
225
    }
226
}
227
 
228
#-------------------------------------------------------------------------------
229
#   Documentation
230
#
231
 
232
=pod
233
 
234
=head1 NAME
235
 
236
jats_transform_file - Transform a file
237
 
238
=head1 SYNOPSIS
239
 
240
  jats etool jats_transform_file [options]
241
 
242
 Options:
243
    -help               - brief help message
244
    -help -help         - Detailed help message
245
    -man                - Full documentation
246
    -verbose            - Verbose operation
247
    -infile=file        - Input file
248
    -outfile=file       - Output file
249
    -dos                - Output file will have DOS line endingd
250
    -unix               - Output file will have Unix line endings
251
    -tsep=c             - Tag seperator character. Default ','
252
    -tag=name,value     - Perform tag replacement
253
 
254
 
255
=head1 OPTIONS
256
 
257
=over 8
258
 
259
=item B<-help>
260
 
261
Print a brief help message and exits.
262
 
263
=item B<-help -help>
264
 
265
Print a detailed help message with an explanation for each option.
266
 
267
=item B<-man>
268
 
269
Prints the manual page and exits.
270
 
271
=item B<-verbose>
272
 
273
Increases program output. This option may be specified mutiple times
274
 
275
=item B<-infile=xxx>
276
 
277
The name of the input file. There is no default file.
278
 
279
=item B<-outfile=xxx>
280
 
281
The name of the output file. The output file cannot be the same as
282
the input file. This utility does not do in-place transformations
283
 
284
=item B<-dos>
285
 
286
The input file is assumed to be a text file. Line endings will be replaced
287
with DOS line endings.
288
 
289
=item B<-unix>
290
 
291
The input file is assumed to be a text file. Line endings will be replaced
292
with Unix line endings.
293
 
294
=item B<-tsep=c>
295
 
296
The character specified the seperator used in the -tag option to isolate the
297
name and the tag. The default character is ','.
298
 
299
This value applies to all tags fields specified on the command line.
300
 
301
=item B<-tag=name,value>
302
 
303
The input file is assumed to be a text file. Lines will be scanned and text
304
replacement will occur.
305
 
306
Tags of the form 'name' will be replaced with 'value'.
307
 
308
The order of replacement is the same as that specified on the command line.
309
 
310
=back
311
 
312
=head1 DESCRIPTION
313
 
314
This JATS utility is used within the build system to perform a number of file
315
transformation operations.
316
 
317
If no transformation options are invoked then the utility will assume that the
318
file is a binary file and simply copy the file.
319
 
320
If neiterh DOS or UNIX line endings are specified then the line endings will
321
be preserved.
322
 
323
=cut
324