[11682] | 1 | ###########################################################################
|
---|
| 2 | # A module with simple functions
|
---|
| 3 | #
|
---|
| 4 | # Copyright (C) 2016-2017 Andrey Ponomarenko's ABI Laboratory
|
---|
| 5 | #
|
---|
| 6 | # Written by Andrey Ponomarenko
|
---|
| 7 | #
|
---|
| 8 | # This program is free software: you can redistribute it and/or modify
|
---|
| 9 | # it under the terms of the GNU General Public License or the GNU Lesser
|
---|
| 10 | # General Public License as published by the Free Software Foundation.
|
---|
| 11 | #
|
---|
| 12 | # This program is distributed in the hope that it will be useful,
|
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | # GNU General Public License for more details.
|
---|
| 16 | #
|
---|
| 17 | # You should have received a copy of the GNU General Public License
|
---|
| 18 | # and the GNU Lesser General Public License along with this program.
|
---|
| 19 | # If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | ###########################################################################
|
---|
| 21 | use strict;
|
---|
| 22 | use Digest::MD5 qw(md5_hex);
|
---|
| 23 | use File::Spec::Functions qw(abs2rel);
|
---|
| 24 | use Config;
|
---|
| 25 |
|
---|
| 26 | my %Cache;
|
---|
| 27 |
|
---|
| 28 | my $MD5_LEN = 8;
|
---|
| 29 |
|
---|
| 30 | sub getOSgroup()
|
---|
| 31 | {
|
---|
| 32 | my $N = $Config{"osname"};
|
---|
| 33 | my $G = undef;
|
---|
| 34 |
|
---|
| 35 | if($N=~/macos|darwin|rhapsody/i) {
|
---|
| 36 | $G = "macos";
|
---|
| 37 | }
|
---|
| 38 | elsif($N=~/freebsd|openbsd|netbsd/i) {
|
---|
| 39 | $G = "bsd";
|
---|
| 40 | }
|
---|
| 41 | elsif($N=~/haiku|beos/i) {
|
---|
| 42 | $G = "beos";
|
---|
| 43 | }
|
---|
| 44 | elsif($N=~/symbian|epoc/i) {
|
---|
| 45 | $G = "symbian";
|
---|
| 46 | }
|
---|
| 47 | elsif($N=~/win/i) {
|
---|
| 48 | $G = "windows";
|
---|
| 49 | }
|
---|
| 50 | elsif($N=~/solaris/i) {
|
---|
| 51 | $G = "solaris";
|
---|
| 52 | }
|
---|
| 53 | else
|
---|
| 54 | { # linux, unix-like
|
---|
| 55 | $G = "linux";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return $G;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | sub getArExt($)
|
---|
| 62 | {
|
---|
| 63 | my $Target = $_[0];
|
---|
| 64 | if($Target eq "windows") {
|
---|
| 65 | return "zip";
|
---|
| 66 | }
|
---|
| 67 | return "tar.gz";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | sub getMd5(@)
|
---|
| 71 | {
|
---|
| 72 | my $Md5 = md5_hex(@_);
|
---|
| 73 | return substr($Md5, 0, $MD5_LEN);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | sub writeFile($$)
|
---|
| 77 | {
|
---|
| 78 | my ($Path, $Content) = @_;
|
---|
| 79 |
|
---|
| 80 | if(my $Dir = getDirname($Path)) {
|
---|
| 81 | mkpath($Dir);
|
---|
| 82 | }
|
---|
| 83 | open (FILE, ">".$Path) || die ("can't open file \'$Path\': $!\n");
|
---|
| 84 | print FILE $Content;
|
---|
| 85 | close(FILE);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | sub readFile($)
|
---|
| 89 | {
|
---|
| 90 | my $Path = $_[0];
|
---|
| 91 |
|
---|
| 92 | open (FILE, $Path);
|
---|
| 93 | my $Content = join("", <FILE>);
|
---|
| 94 | close(FILE);
|
---|
| 95 |
|
---|
| 96 | $Content=~s/\r//g;
|
---|
| 97 |
|
---|
| 98 | return $Content;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | sub appendFile($$)
|
---|
| 102 | {
|
---|
| 103 | my ($Path, $Content) = @_;
|
---|
| 104 |
|
---|
| 105 | if(my $Dir = getDirname($Path)) {
|
---|
| 106 | mkpath($Dir);
|
---|
| 107 | }
|
---|
| 108 | open(FILE, ">>".$Path) || die ("can't open file \'$Path\': $!\n");
|
---|
| 109 | print FILE $Content;
|
---|
| 110 | close(FILE);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | sub readLineNum($$)
|
---|
| 114 | {
|
---|
| 115 | my ($Path, $Num) = @_;
|
---|
| 116 |
|
---|
| 117 | open (FILE, $Path);
|
---|
| 118 | foreach (1 ... $Num) {
|
---|
| 119 | <FILE>;
|
---|
| 120 | }
|
---|
| 121 | my $Line = <FILE>;
|
---|
| 122 | close(FILE);
|
---|
| 123 |
|
---|
| 124 | return $Line;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | sub readAttributes($$)
|
---|
| 128 | {
|
---|
| 129 | my ($Path, $Num) = @_;
|
---|
| 130 |
|
---|
| 131 | my %Attributes = ();
|
---|
| 132 | if(readLineNum($Path, $Num)=~/<!--\s+(.+)\s+-->/)
|
---|
| 133 | {
|
---|
| 134 | foreach my $AttrVal (split(/;/, $1))
|
---|
| 135 | {
|
---|
| 136 | if($AttrVal=~/(.+):(.+)/)
|
---|
| 137 | {
|
---|
| 138 | my ($Name, $Value) = ($1, $2);
|
---|
| 139 | $Attributes{$Name} = $Value;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | return \%Attributes;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | sub getFilename($)
|
---|
| 147 | { # much faster than basename() from File::Basename module
|
---|
| 148 | if(defined $Cache{"getFilename"}{$_[0]}) {
|
---|
| 149 | return $Cache{"getFilename"}{$_[0]};
|
---|
| 150 | }
|
---|
| 151 | if($_[0] and $_[0]=~/([^\/\\]+)[\/\\]*\Z/) {
|
---|
| 152 | return ($Cache{"getFilename"}{$_[0]}=$1);
|
---|
| 153 | }
|
---|
| 154 | return ($Cache{"getFilename"}{$_[0]}="");
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | sub getDirname($)
|
---|
| 158 | { # much faster than dirname() from File::Basename module
|
---|
| 159 | if(defined $Cache{"getDirname"}{$_[0]}) {
|
---|
| 160 | return $Cache{"getDirname"}{$_[0]};
|
---|
| 161 | }
|
---|
| 162 | if($_[0] and $_[0]=~/\A(.*?)[\/\\]+[^\/\\]*[\/\\]*\Z/) {
|
---|
| 163 | return ($Cache{"getDirname"}{$_[0]}=$1);
|
---|
| 164 | }
|
---|
| 165 | return ($Cache{"getDirname"}{$_[0]}="");
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | sub sepPath($) {
|
---|
| 169 | return (getDirname($_[0]), getFilename($_[0]));
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | sub checkCmd($)
|
---|
| 173 | {
|
---|
| 174 | my $Cmd = $_[0];
|
---|
| 175 |
|
---|
| 176 | foreach my $Path (sort {length($a)<=>length($b)} split(/:/, $ENV{"PATH"}))
|
---|
| 177 | {
|
---|
| 178 | if(-x $Path."/".$Cmd) {
|
---|
| 179 | return 1;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | return 0;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | sub isAbsPath($) {
|
---|
| 187 | return ($_[0]=~/\A(\/|\w+:[\/\\])/);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | sub cutPrefix($$)
|
---|
| 191 | {
|
---|
| 192 | my ($Path, $Prefix) = @_;
|
---|
| 193 | $Prefix=~s/[\/\\]+\Z//;
|
---|
| 194 | $Path=~s/\A\Q$Prefix\E([\/\\]+|\Z)//;
|
---|
| 195 | return $Path;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | sub showPos($)
|
---|
| 199 | {
|
---|
| 200 | my $N = $_[0];
|
---|
| 201 | if(not $N) {
|
---|
| 202 | $N = 1;
|
---|
| 203 | }
|
---|
| 204 | else {
|
---|
| 205 | $N = int($N)+1;
|
---|
| 206 | }
|
---|
| 207 | if($N>3) {
|
---|
| 208 | return $N."th";
|
---|
| 209 | }
|
---|
| 210 | elsif($N==1) {
|
---|
| 211 | return "1st";
|
---|
| 212 | }
|
---|
| 213 | elsif($N==2) {
|
---|
| 214 | return "2nd";
|
---|
| 215 | }
|
---|
| 216 | elsif($N==3) {
|
---|
| 217 | return "3rd";
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | return $N;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | sub parseTag($$)
|
---|
| 224 | {
|
---|
| 225 | my ($CodeRef, $Tag) = @_;
|
---|
| 226 |
|
---|
| 227 | if(${$CodeRef}=~s/\<\Q$Tag\E\>((.|\n)+?)\<\/\Q$Tag\E\>//)
|
---|
| 228 | {
|
---|
| 229 | my $Content = $1;
|
---|
| 230 | $Content=~s/(\A\s+|\s+\Z)//g;
|
---|
| 231 | return $Content;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | return "";
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | sub isDump($)
|
---|
| 238 | {
|
---|
| 239 | if($_[0]=~/\A(.+)\.(api|dump|apidump)(\Q.tar.gz\E|\Q.zip\E|)\Z/) {
|
---|
| 240 | return $1;
|
---|
| 241 | }
|
---|
| 242 | return 0;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | sub isDump_U($)
|
---|
| 246 | {
|
---|
| 247 | if($_[0]=~/\.(api|dump|apidump)\Z/) {
|
---|
| 248 | return 1;
|
---|
| 249 | }
|
---|
| 250 | return 0;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | sub cmpVersions($$)
|
---|
| 254 | { # compare two version strings in dotted-numeric format
|
---|
| 255 | my ($V1, $V2) = @_;
|
---|
| 256 | return 0 if($V1 eq $V2);
|
---|
| 257 | my @V1Parts = split(/\./, $V1);
|
---|
| 258 | my @V2Parts = split(/\./, $V2);
|
---|
| 259 | for (my $i = 0; $i <= $#V1Parts && $i <= $#V2Parts; $i++)
|
---|
| 260 | {
|
---|
| 261 | return -1 if(int($V1Parts[$i]) < int($V2Parts[$i]));
|
---|
| 262 | return 1 if(int($V1Parts[$i]) > int($V2Parts[$i]));
|
---|
| 263 | }
|
---|
| 264 | return -1 if($#V1Parts < $#V2Parts);
|
---|
| 265 | return 1 if($#V1Parts > $#V2Parts);
|
---|
| 266 | return 0;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | sub getRelPath($$)
|
---|
| 270 | {
|
---|
| 271 | my ($A, $B) = @_;
|
---|
| 272 | return abs2rel($A, getDirname($B));
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | sub getPFormat($)
|
---|
| 276 | {
|
---|
| 277 | my $Name = $_[0];
|
---|
| 278 | $Name=~s/\//./g;
|
---|
| 279 | return $Name;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | return 1;
|
---|