1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# Copyright (C) 2008-2010 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
package GenTest;
use base 'Exporter';
@EXPORT = ('say', 'sayFile', 'tmpdir', 'safe_exit',
'osWindows', 'osLinux', 'osSolaris', 'osMac',
'isoTimestamp', 'isoUTCTimestamp', 'rqg_debug', 'unix2winPath');
use strict;
use Cwd;
use POSIX;
use Carp;
my $tmpdir;
1;
sub BEGIN {
foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
if (
(defined $tmp) &&
(-e $tmp)
) {
$tmpdir = $tmp;
last;
}
}
if (defined $tmpdir) {
if (
($^O eq 'MSWin32') ||
($^O eq 'MSWin64')
) {
$tmpdir = $tmpdir.'\\';
} else {
$tmpdir = $tmpdir.'/';
}
}
croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
return 1;
}
sub new {
my $class = shift;
my $args = shift;
my $obj = bless ([], $class);
my $max_arg = (scalar(@_) / 2) - 1;
foreach my $i (0..$max_arg) {
if (exists $args->{$_[$i * 2]}) {
if (defined $obj->[$args->{$_[$i * 2]}]) {
carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
} else {
$obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
}
} else {
carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
}
}
return $obj;
}
sub say {
my $text = shift;
if ($text =~ m{[\r\n]}sio) {
foreach my $line (split (m{[\r\n]}, $text)) {
print "# ".isoTimestamp()." $line\n";
}
} else {
print "# ".isoTimestamp()." $text\n";
}
}
sub sayFile {
my ($file) = @_;
say("--------- Contents of $file -------------");
open FILE,$file;
while (<FILE>) {
say("| ".$_);
}
close FILE;
say("----------------------------------");
}
sub tmpdir {
return $tmpdir;
}
sub safe_exit {
my $exit_status = shift;
POSIX::_exit($exit_status);
}
sub osWindows {
if (
($^O eq 'MSWin32') ||
($^O eq 'MSWin64')
) {
return 1;
} else {
return 0;
}
}
sub osLinux {
if ($^O eq 'linux') {
return 1;
} else {
return 0;
}
}
sub osSolaris {
if ($^O eq 'solaris') {
return 1;
} else {
return 0;
}
}
sub osMac {
if ($^O eq 'darwin') {
return 1;
} else {
return 0;
}
}
sub isoTimestamp {
my $datetime = shift;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
}
sub isoUTCTimestamp {
my $datetime = shift;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
}
# unix2winPath:
# Converts the given file path from unix style to windows native style
# by replacing all forward slashes to backslashes.
sub unix2winPath {
my $path = shift;
$path =~ s/\//\\/g; # replace "/" with "\"
return $path;
}
sub rqg_debug {
if ($ENV{RQG_DEBUG}) {
return 1;
} else {
return 0;
}
}
1;
|