Subversion Repositories DevTools

Rev

Rev 285 | Rev 289 | 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
53
            TYPE        => 'perl',                  # vi style file type
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=(.*)/ ) {
68
            $self->{'TYPE'} = $1;
69
            $self->{'CMT'} = 'REM' if ( $1 eq 'bat' );
70
            $self->{'CMT'} = '//'  if ( $1 eq 'CSharp' );
71
            $self->{'CMT'} = '//'  if ( $1 eq 'C++' );
72
 
285 dpurdie 73
        } elsif ( /^--NoTime/ ) {
74
            $self->{'DO_TIME'} = 0;
75
 
227 dpurdie 76
        } else {
77
            ::Error("ConfigurationFile::New: Bad option: $_");
78
        }
79
    }
80
 
81
    #
82
    #   Insert a timestamp
83
    #
84
    $self->{TIME} = $::CurrentTime ?  $::CurrentTime : localtime;
85
 
86
    #
87
    #   Create the file
88
    #   Save the file handle
89
    #
90
    open( $self->{'FH'}, ">", $name ) || ::Error( "Cannot create '$name'");
287 dpurdie 91
    binmode ( $self->{'FH'} )
92
        if ( $self->{'TYPE'} eq 'sh' );
227 dpurdie 93
 
94
    #
95
    #   Bless my self and return a handle
96
    #
97
    return bless $self, __PACKAGE__;
98
}
99
 
100
#-------------------------------------------------------------------------------
101
# Function        : Write
102
#                   Comment
103
#
104
# Description     : print a line to the file
105
#                   Called write to avoid massivce confusion with print
106
#
107
# Inputs          : $self   - package handle
108
#                   $*      - print arguments
109
#
110
# Returns         :
111
#
112
sub Write
113
{
114
    my $self = shift;
115
    print {$self->{FH}} ( @_ );
116
}
117
 
118
sub Comment
119
{
120
    my $self = shift;
121
    print {$self->{FH}} ( $self->{'CMT'}, ' ', @_ );
122
}
123
 
124
 
125
#-------------------------------------------------------------------------------
126
# Function        : Dump
127
#
128
# Description     : Write out a data structure to the configuration file.
129
#                   Raw version. No header, no frills
130
#
131
# Inputs          : $varref     - Array of stuff to dump
132
#                   $name       - Array of names to use
133
#
134
# Returns         :
135
#
136
sub Dump
137
{
138
    my( $self, $varref, $name ) = @_;
139
 
140
    $Data::Dumper::Indent  = 1;         # Use 2 for readability, 1 or 0 for production
141
    $Data::Dumper::Sortkeys = 1;        # Sort the output for readability
142
#    $Data::Dumper::Purity  = 1;
143
 
144
    $self->Write (Data::Dumper->Dump ($varref, $name));
145
}
146
 
147
#-------------------------------------------------------------------------------
148
# Function        : DumpData
149
#
150
# Description     : Write out a data structure with header
151
#
152
# Inputs          :
153
#
154
# Returns         :
155
#
156
sub DumpData             # Will also work with a hash
157
{
158
    my ($self, $desc, $name, $array) = @_;
159
    my ($sep) = "";
160
 
161
    $self->Write($desc) if ($desc);
162
    $self->Dump( [$array], ["*$name"] );
163
    $self->Write("\n\n");
164
}
165
 
166
#-------------------------------------------------------------------------------
167
# Function        : Header
168
#
169
# Description:    : Generate a "standard" configuration file.
170
#..
171
 
172
sub Header
173
{
174
    my ($self, $by, $desc, $trailing) = @_;
175
 
176
    $desc = "" if ( !defined( $desc ) );
177
    $trailing = "" if ( !defined( $trailing ) );
178
 
179
    my ($diff);
180
    $diff = 0 if (($diff = ((80-6) - length($self->{'CMT'}) - length($desc))) < 0);
181
    $desc .= " " . ("-" x $diff);
182
 
183
    #
184
    #   Process HERE document to remove leading write space
185
    #   Simply to make the source look nice
186
    #
285 dpurdie 187
 
188
    my $ts = '';
189
    if ( $self->{'DO_TIME'} )
190
    {
191
        $ts = <<HERE_TARGET;
192
        $self->{'CMT'}         on $self->{'TIME'}
193
HERE_TARGET
194
    }
195
 
227 dpurdie 196
    my $var;
197
    ($var = <<HERE_TARGET) =~ s/^\s+//gm;
198
 
199
    $self->{'CMT'} -*- mode: $self->{'TYPE'}; tabs: 8; -*-
200
    $self->{'CMT'} -- $desc
201
    $self->{'CMT'}
202
    $self->{'CMT'}                   -- Please do not edit this file. --
203
    $self->{'CMT'}
204
    $self->{'CMT'} WARNING:
205
    $self->{'CMT'}       This file is used internally by JATS to maintain information
206
    $self->{'CMT'}       about the current sandbox.  You not must modify, nor move or
207
    $self->{'CMT'}       delete this file.  To do so may result in a system failure,
208
    $self->{'CMT'}       in additional to any changes made shall be overwritten.
209
    $self->{'CMT'}
210
    $self->{'CMT'} Created by $by
285 dpurdie 211
    $ts
227 dpurdie 212
    $self->{'CMT'}
213
    $trailing
214
 
215
HERE_TARGET
216
 
217
    $self->Write ($var);
218
}
219
 
220
#-------------------------------------------------------------------------------
221
# Function        : HeaderSimple
222
#
223
# Description:    : Generate a "simple" configuration file.
224
#..
225
 
226
sub HeaderSimple
227
{
228
    my ($self, $by, $desc, $trailing) = @_;
229
 
230
    $desc = "" if ( !defined( $desc ) );
231
    $trailing = "" if ( !defined( $trailing ) );
232
 
233
    my ($diff);
234
    $diff = 0 if (($diff = ((80-6) - length($self->{'CMT'}) - length($desc))) < 0);
235
    $desc .= " " . ("-" x $diff);
236
 
237
    #
238
    #   Process HERE document to remove leading write space
239
    #   Simply to make the source look nice
240
    #
285 dpurdie 241
    my $ts = '';
242
    if ( $self->{'DO_TIME'} )
243
    {
244
        $ts = <<HERE_TARGET;
245
        $self->{'CMT'}         on $self->{'TIME'}
246
HERE_TARGET
247
    }
248
 
227 dpurdie 249
    my $var;
250
    ($var = <<HERE_TARGET) =~ s/^\s+//gm;
251
 
252
    $self->{'CMT'} -*- mode: $self->{'TYPE'}; tabs: 8; -*-
253
    $self->{'CMT'} -- $desc
254
    $self->{'CMT'}
255
    $self->{'CMT'} -- Do not edit this file. --
256
    $self->{'CMT'}
257
    $self->{'CMT'} Created by $by
285 dpurdie 258
    $ts
227 dpurdie 259
    $self->{'CMT'}
260
    $trailing
261
 
262
HERE_TARGET
263
 
264
    $self->Write ($var);
265
}
266
 
267
 
268
#-------------------------------------------------------------------------------
269
# Function        : Close
270
#
271
# Description     : Generate EOF markers and close the file
272
#
273
# Inputs          : $file       - File handle
274
#                   $noeof      - TRUE: Don't generate an EOF
275
#
276
# Returns         :
277
#
278
sub Close
279
{
280
    my ($self, $noeof ) = @_;
281
    $self->{'EOF'} = 0 if ( $noeof );
282
 
283
    if ( $self->{'EOF'} )
284
    {
285
        if ( $self->{'TYPE'} eq 'perl' )
286
        {
287
            my $var;
288
            ($var = <<HERE_TARGET) =~ s/^\s+//gm;
289
 
290
            #-EOF-
291
            1;
292
HERE_TARGET
293
        $self->Write( $var);
294
        }
295
        else
296
        {
297
            $self->Comment("End of File\n" );
298
        }
299
    }
300
 
301
    close $self->{FH};
302
    $self->{FH} = '';
303
}
304
 
305
#-------------------------------------------------------------------------------
306
# Function        : DESTROY
307
#
308
# Description     : Object destructor
309
#                   Sanity test. Generate an error if the object is destroyed
310
#                   without being closed.
311
#
312
# Inputs          : $self
313
#
314
# Returns         :
315
#
316
 
317
sub DESTROY
318
{
319
    my $self = shift;
320
 
321
    ::Error("ConfigurationFile not closed: $self->{NAME}")
322
        if ( $self->{FH} );
323
}
324
################################################################################
325
 
326
1;