1 ###################################################
2 # utility functions to support pidl
3 # Copyright (C) tridge@samba.org, 2001
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #####################################################################
24 # check if a string is in an array
30 if ($v eq $s) { return 1; }
35 #####################################################################
36 # flatten an array of arrays into a single array
49 #####################################################################
50 # flatten an array of hashes into a single hash
56 for my $k (keys %{$d}) {
64 #####################################################################
65 # return the modification time of a file
68 my($filename) = shift;
69 return (stat($filename))[9];
73 #####################################################################
74 # read a file into a string
77 my($filename) = shift;
79 open(INPUTFILE, $filename) || return "";
80 my($saved_delim) = $/;
82 my($data) = <INPUTFILE>;
88 #####################################################################
89 # write a string into a file
92 my($filename) = shift;
95 open(FILE, ">$filename") || die "can't open $filename";
100 #####################################################################
101 # return a filename with a changed extension
102 sub ChangeExtension($$)
106 if ($fname =~ /^(.*)\.(.*?)$/) {
109 return "$fname.$ext";
112 #####################################################################
113 # save a data structure into a file
114 sub SaveStructure($$)
116 my($filename) = shift;
118 FileSave($filename, Dumper($v));
121 #####################################################################
122 # load a data structure from a file (as saved with SaveStructure)
125 return eval FileLoad(shift);
129 ####################################################################
130 # setup for gzipped output of a web page if possible.
131 # based on cvsweb.pl method
132 # as a side effect this function adds the final line ot the HTTP headers
135 my $paths = ['/usr/bin/gzip', '/bin/gzip'];
137 my $Browser = $ENV{'HTTP_USER_AGENT'} || "";
139 # newer browsers accept gzip content encoding
140 # and state this in a header
141 # (netscape did always but didn't state it)
142 # It has been reported that these
143 # braindamaged MS-Internet Exploders claim that they
144 # accept gzip .. but don't in fact and
145 # display garbage then :-/
146 # Turn off gzip if running under mod_perl. piping does
147 # not work as expected inside the server. One can probably
148 # achieve the same result using Apache::GZIPFilter.
149 my $maycompress = (($ENV{'HTTP_ACCEPT_ENCODING'} =~ m|gzip|
150 || $Browser =~ m%^Mozilla/3%)
151 && ($Browser !~ m/MSIE/)
152 && !defined($ENV{'MOD_PERL'}));
154 if (!$maycompress) { print "\r\n"; return; }
156 for my $p (@{$paths}) {
157 if (stat($p)) { $GZIPBIN = $p; }
160 my $fh = do {local(*FH);};
162 if (stat($GZIPBIN) && open($fh, "|$GZIPBIN -1 -c")) {
163 print "Content-encoding: x-gzip\r\n";
164 print "Vary: Accept-Encoding\r\n"; #RFC 2068, 14.43
165 print "\r\n"; # Close headers
166 $| = 1; $| = 0; # Flush header output
173 ##########################################
174 # escape a string for cgi
187 ##########################################
188 # count the number of lines in a buffer
193 $count = split(/$/m, $s);
198 # display a time as days, hours, minutes
202 my $days = int($sec / (60*60*24));
203 my $hour = int($sec / (60*60)) % 24;
204 my $min = int($sec / 60) % 60;
213 return sprintf("%dd %dh %dm", $days, $hour, $min);
216 return sprintf("%dh %dm", $hour, $min);
219 return sprintf("%dm", $min);
221 return sprintf("%ds", $sec);