Subversion Repositories DevTools

Rev

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