Subversion Repositories DevTools

Rev

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