source: josm/trunk/tools/japicc/modules/Internals/Basic.pm@ 13595

Last change on this file since 13595 was 13595, checked in by Don-vip, 6 years ago

tools update: Groovy 2.4.15, PMD 6.2.0, JAPICC 2.4

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