Subversion Repositories DevTools

Rev

Rev 3967 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
#! perl
2
########################################################################
315 dpurdie 3
# Copyright ( C ) 2004-2010 ERG Limited, All rights reserved
227 dpurdie 4
#
5
# Module name   : jats.sh
315 dpurdie 6
# Module type   : Jats Perl Module
227 dpurdie 7
# Environment(s): jats
8
#
315 dpurdie 9
# Description:  Class to simply the process of creating and writing
10
#               a new configuration file
227 dpurdie 11
#
315 dpurdie 12
#               Used to create files used by JATS in the build process
227 dpurdie 13
#
315 dpurdie 14
# Usage:       $fh = ConfigurationFile::New( 'SomeName' );
15
#              $fh->HeaderSimple();
16
#              $fh->Header();
17
#              $fh->Write();
18
#              $fh->WriteLn();
19
#              $fh->Comment()
20
#              $fh->Dump()
21
#              $fh->DumpData()
22
#              $fh->Close();
23
#               
227 dpurdie 24
#......................................................................#
25
 
255 dpurdie 26
require 5.006_001;
227 dpurdie 27
use strict;
28
use warnings;
29
 
30
################################################################################
31
#
32
#   Package to manage the creation of configuration files
33
#
34
#
35
package ConfigurationFile;
36
 
37
#-------------------------------------------------------------------------------
38
# Function        : ConfigurationFile::New
39
#
40
# Description     : Create and open a configuration file
41
#
285 dpurdie 42
# Inputs          : $name   - Name of the file to create.
43
#                   @opts   - List of options
44
#                               --NoEof     - Supress EOF at the end of the file
45
#                               --Type=xxx  - Control the comment markers
46
#                               --NoTime    - Supress the time stamp
227 dpurdie 47
#
48
# Returns         : Package handle
49
#
50
sub New
51
{
52
    my ($name, @opts) = @_;
53
 
54
    #
55
    #   Package variables
56
    #
57
    my ($self) = {
58
            FH          => *CONFIG,                 # File Handle
59
            NAME        => $name,                   # Name of the file
60
            EOF         => 1,                       # Print EOF on closure
315 dpurdie 61
            FTYPE       => 'perl',                  # vi style file type
227 dpurdie 62
            CMT         => '#',                     # Line comments begin with
63
            TIME        => '',                      # Inserted timestamp
285 dpurdie 64
            DO_TIME     => 1,                       # Insert timestamp in header
227 dpurdie 65
       };
66
 
67
    #
68
    #   Parse arguments
69
    #
70
    foreach ( @opts )
71
    {
72
        if ( /--NoEof/ ) {
73
            $self->{'EOF'} = 0;
74
 
75
        } elsif ( /--Type=(.*)/ ) {
311 dpurdie 76
            $self->{'FTYPE'} = $1;
227 dpurdie 77
            $self->{'CMT'} = 'REM' if ( $1 eq 'bat' );
78
            $self->{'CMT'} = '//'  if ( $1 eq 'CSharp' );
79
            $self->{'CMT'} = '//'  if ( $1 eq 'C++' );
289 dpurdie 80
            $self->{'CMT'} = '//'  if ( $1 eq 'Delphi' );
315 dpurdie 81
            $self->{'CMT'} = '\''  if ( $1 eq 'Basic' );
227 dpurdie 82
 
285 dpurdie 83
        } elsif ( /^--NoTime/ ) {
84
            $self->{'DO_TIME'} = 0;
85
 
227 dpurdie 86
        } else {
87
            ::Error("ConfigurationFile::New: Bad option: $_");
88
        }
89
    }
90
 
91
    #
92
    #   Insert a timestamp
93
    #
94
    $self->{TIME} = $::CurrentTime ?  $::CurrentTime : localtime;
95
 
96
    #
97
    #   Create the file
98
    #   Save the file handle
99
    #
100
    open( $self->{'FH'}, ">", $name ) || ::Error( "Cannot create '$name'");
287 dpurdie 101
    binmode ( $self->{'FH'} )
311 dpurdie 102
        if ( $self->{'FTYPE'} eq 'sh' );
227 dpurdie 103
 
104
    #
105
    #   Bless my self and return a handle
106
    #
107
    return bless $self, __PACKAGE__;
108
}
109
 
110
#-------------------------------------------------------------------------------
111
# Function        : Write
289 dpurdie 112
#                   WriteLn
227 dpurdie 113
#                   Comment
114
#
115
# Description     : print a line to the file
116
#                   Called write to avoid massivce confusion with print
117
#
118
# Inputs          : $self   - package handle
119
#                   $*      - print arguments
289 dpurdie 120
#                             WriteLn will write each one with a newline
121
#                             Arrays are allowed
227 dpurdie 122
#
123
# Returns         :
124
#
125
sub Write
126
{
127
    my $self = shift;
128
    print {$self->{FH}} ( @_ );
129
}
130
 
289 dpurdie 131
sub WriteLn
132
{
133
    my $self = shift;
134
 
135
    foreach my $entry ( @_ ) {
136
        if ( ref ($entry ) eq 'ARRAY'  ) {
137
            print {$self->{FH}}  $_ . "\n" foreach  ( @$entry );
138
        } else {
139
            print {$self->{FH}}  $entry . "\n"
140
        }
141
    }
142
}
143
 
227 dpurdie 144
sub Comment
145
{
146
    my $self = shift;
147
    print {$self->{FH}} ( $self->{'CMT'}, ' ', @_ );
148
}
149
 
150
 
151
#-------------------------------------------------------------------------------
152
# Function        : Dump
153
#
154
# Description     : Write out a data structure to the configuration file.
155
#                   Raw version. No header, no frills
156
#
157
# Inputs          : $varref     - Array of stuff to dump
158
#                   $name       - Array of names to use
159
#
160
# Returns         :
161
#
162
sub Dump
163
{
164
    my( $self, $varref, $name ) = @_;
165
 
166
    $Data::Dumper::Indent  = 1;         # Use 2 for readability, 1 or 0 for production
167
    $Data::Dumper::Sortkeys = 1;        # Sort the output for readability
168
#    $Data::Dumper::Purity  = 1;
289 dpurdie 169
    $Data::Dumper::Deepcopy = 1;        # Need to get @LIBS, @MLIBS into .cfg
227 dpurdie 170
 
171
    $self->Write (Data::Dumper->Dump ($varref, $name));
172
}
173
 
174
#-------------------------------------------------------------------------------
175
# Function        : DumpData
176
#
177
# Description     : Write out a data structure with header
178
#
179
# Inputs          :
180
#
181
# Returns         :
182
#
183
sub DumpData             # Will also work with a hash
184
{
185
    my ($self, $desc, $name, $array) = @_;
186
    my ($sep) = "";
187
 
188
    $self->Write($desc) if ($desc);
189
    $self->Dump( [$array], ["*$name"] );
190
    $self->Write("\n\n");
191
}
192
 
193
#-------------------------------------------------------------------------------
194
# Function        : Header
195
#
196
# Description:    : Generate a "standard" configuration file.
197
#..
198
 
199
sub Header
200
{
201
    my ($self, $by, $desc, $trailing) = @_;
202
 
203
    $desc = "" if ( !defined( $desc ) );
204
    $trailing = "" if ( !defined( $trailing ) );
205
 
206
    my ($diff);
207
    $diff = 0 if (($diff = ((80-6) - length($self->{'CMT'}) - length($desc))) < 0);
208
    $desc .= " " . ("-" x $diff);
209
 
210
    #
211
    #   Process HERE document to remove leading write space
212
    #   Simply to make the source look nice
213
    #
285 dpurdie 214
 
215
    my $ts = '';
216
    if ( $self->{'DO_TIME'} )
217
    {
218
        $ts = <<HERE_TARGET;
219
        $self->{'CMT'}         on $self->{'TIME'}
220
HERE_TARGET
221
    }
222
 
227 dpurdie 223
    my $var;
224
    ($var = <<HERE_TARGET) =~ s/^\s+//gm;
225
 
226
    $self->{'CMT'} -- $desc
227
    $self->{'CMT'}
228
    $self->{'CMT'}                   -- Please do not edit this file. --
229
    $self->{'CMT'}
230
    $self->{'CMT'} WARNING:
231
    $self->{'CMT'}       This file is used internally by JATS to maintain information
232
    $self->{'CMT'}       about the current sandbox.  You not must modify, nor move or
233
    $self->{'CMT'}       delete this file.  To do so may result in a system failure,
234
    $self->{'CMT'}       in additional to any changes made shall be overwritten.
235
    $self->{'CMT'}
236
    $self->{'CMT'} Created by $by
285 dpurdie 237
    $ts
227 dpurdie 238
    $self->{'CMT'}
239
    $trailing
240
 
241
HERE_TARGET
242
 
243
    $self->Write ($var);
244
}
245
 
246
#-------------------------------------------------------------------------------
247
# Function        : HeaderSimple
248
#
249
# Description:    : Generate a "simple" configuration file.
250
#..
251
 
252
sub HeaderSimple
253
{
254
    my ($self, $by, $desc, $trailing) = @_;
255
 
256
    $desc = "" if ( !defined( $desc ) );
257
    $trailing = "" if ( !defined( $trailing ) );
258
 
259
    my ($diff);
260
    $diff = 0 if (($diff = ((80-6) - length($self->{'CMT'}) - length($desc))) < 0);
261
    $desc .= " " . ("-" x $diff);
262
 
263
    #
264
    #   Process HERE document to remove leading write space
265
    #   Simply to make the source look nice
266
    #
285 dpurdie 267
    my $ts = '';
268
    if ( $self->{'DO_TIME'} )
269
    {
270
        $ts = <<HERE_TARGET;
271
        $self->{'CMT'}         on $self->{'TIME'}
272
HERE_TARGET
273
    }
274
 
227 dpurdie 275
    my $var;
276
    ($var = <<HERE_TARGET) =~ s/^\s+//gm;
277
 
278
    $self->{'CMT'} -- $desc
279
    $self->{'CMT'}
280
    $self->{'CMT'} -- Do not edit this file. --
281
    $self->{'CMT'}
282
    $self->{'CMT'} Created by $by
285 dpurdie 283
    $ts
227 dpurdie 284
    $self->{'CMT'}
285
    $trailing
286
 
287
HERE_TARGET
288
 
289
    $self->Write ($var);
290
}
291
 
292
 
293
#-------------------------------------------------------------------------------
294
# Function        : Close
295
#
296
# Description     : Generate EOF markers and close the file
297
#
298
# Inputs          : $file       - File handle
299
#                   $noeof      - TRUE: Don't generate an EOF
300
#
301
# Returns         :
302
#
303
sub Close
304
{
305
    my ($self, $noeof ) = @_;
306
    $self->{'EOF'} = 0 if ( $noeof );
307
 
308
    if ( $self->{'EOF'} )
309
    {
311 dpurdie 310
        if ( $self->{'FTYPE'} eq 'perl' )
227 dpurdie 311
        {
312
            my $var;
313
            ($var = <<HERE_TARGET) =~ s/^\s+//gm;
314
 
315
            #-EOF-
316
            1;
317
HERE_TARGET
318
        $self->Write( $var);
319
        }
320
        else
321
        {
322
            $self->Comment("End of File\n" );
323
        }
324
    }
325
 
326
    close $self->{FH};
327
    $self->{FH} = '';
328
}
329
 
330
#-------------------------------------------------------------------------------
331
# Function        : DESTROY
332
#
333
# Description     : Object destructor
334
#                   Sanity test. Generate an error if the object is destroyed
335
#                   without being closed.
336
#
337
# Inputs          : $self
338
#
339
# Returns         :
340
#
341
 
342
sub DESTROY
343
{
344
    my $self = shift;
345
 
346
    ::Error("ConfigurationFile not closed: $self->{NAME}")
347
        if ( $self->{FH} );
348
}
349
################################################################################
350
 
351
1;