use correct locations on git.samba.org
[metze/old/samba4-sync/samba4-sync.scripts/.git] / util.pm
1 ###################################################
2 # utility functions to support the build farm
3 # Copyright (C) tridge@samba.org, 2001
4 #
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.
9 #   
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.
14 #   
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.
18
19 package util;
20
21 use Data::Dumper;
22
23 ##############################################
24 # load a list from a file, using : to separate
25 sub load_list($)
26 {
27         my $fname = shift;
28         my @lines = split('\n', `grep -v ^# $fname`);
29         return @lines;
30 }
31
32 ##############################################
33 # load a hash from a file, using : to separate
34 sub load_hash($)
35 {
36         my $fname = shift;
37         my @lines = load_list($fname);
38         my %ret;
39         for my $l (@lines) {
40                 if ($l =~ /^([\w\-]*)\s*:\s*(.*)$/) {
41                         $ret{$1} = $2;
42                 }
43         }
44         return %ret;
45 }
46
47 #####################################################################
48 # check if a string is in an array
49 sub InArray($$)
50 {
51     my ($s, $a) = @_;
52     for my $v (@{$a}) {
53                 return 1 if ($v eq $s);
54     }
55     return 0;
56 }
57
58 #####################################################################
59 # flatten an array of arrays into a single array
60 sub FlattenArray($) 
61
62     my $a = shift;
63     my @b;
64     for my $d (@{$a}) {
65                 push(@b, $_) foreach (@{$d});
66     }
67     return \@b;
68 }
69
70 #####################################################################
71 # flatten an array of hashes into a single hash
72 sub FlattenHash($) 
73
74     my $a = shift;
75     my %b;
76     for my $d (@{$a}) {
77                 for my $k (keys %{$d}) {
78                         $b{$k} = $d->{$k};
79                 }
80     }
81     return \%b;
82 }
83
84 #####################################################################
85 # return the modification time of a file
86 sub FileModtime($)
87 {
88     my($filename) = shift;
89     return (stat($filename))[9];
90 }
91
92 #####################################################################
93 # read a file into a string
94 sub FileLoad($)
95 {
96     my($filename) = shift;
97     local(*INPUTFILE);
98     open(INPUTFILE, $filename) || return "";
99     my($saved_delim) = $/;
100     undef $/;
101     my($data) = <INPUTFILE>;
102     close(INPUTFILE);
103     $/ = $saved_delim;
104     return $data;
105 }
106
107 #####################################################################
108 # write a string into a file
109 sub FileSave($$)
110 {
111     my($filename) = shift;
112     my($v) = shift;
113     local(*FILE);
114     open(FILE, ">$filename") || die "can't open $filename";    
115     print FILE $v;
116     close(FILE);
117 }
118
119 #####################################################################
120 # return a filename with a changed extension
121 sub ChangeExtension($$)
122 {
123     my($fname,$ext) = @_;
124         return "$1.$ext" if ($fname =~ /^(.*)\.(.*?)$/);
125     return "$fname.$ext";
126 }
127
128 #####################################################################
129 # save a data structure into a file
130 sub SaveStructure($$)
131 {
132     my($filename) = shift;
133     my($v) = shift;
134     FileSave($filename, Dumper($v));
135 }
136
137 #####################################################################
138 # load a data structure from a file (as saved with SaveStructure)
139 sub LoadStructure($)
140 {
141     return eval FileLoad(shift);
142 }
143
144 ##########################################
145 # count the number of lines in a buffer
146 sub count_lines($)
147 {
148     my $s = shift;
149     my $count;
150     $count++ while $s =~ /$/gm;
151     return $count;
152 }
153
154 ################
155 # display a time as days, hours, minutes
156 sub dhm_time($)
157 {
158         my $sec = shift;
159         my $days = int($sec / (60*60*24));
160         my $hour = int($sec / (60*60)) % 24;
161         my $min = int($sec / 60) % 60;
162
163         my $ret = "";
164
165         if ($sec < 0) { 
166                 return "-";
167         }
168
169         if ($days != 0) { 
170                 return sprintf("%dd %dh %dm", $days, $hour, $min);
171         }
172         if ($hour != 0) {
173                 return sprintf("%dh %dm", $hour, $min);
174         }
175         if ($min != 0) {
176                 return sprintf("%dm", $min);
177         }
178         return sprintf("%ds", $sec);
179 }
180
181 ##############################################
182 # simple html markup stripper
183 sub strip_html($) {
184         my $string = shift;
185
186         # get rid of comments
187         $string =~ s/<!\-\-(.*?)\-\->/$2/g;
188
189         # and remove tags.
190         while ($string =~ s&<(\w+).*?>(.*?)</\1>&$2&) {
191                 ;
192         }
193
194         return $string;
195 }
196
197 1;