Subversion Repositories DevTools

Rev

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