source: josm/trunk/tools/japicc/modules/Internals/Utils.pm@ 11682

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

update to japi-compliance-checker 2.1

File size: 5.4 KB
Line 
1###########################################################################
2# A module with basic 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###########################################################################
21use strict;
22use POSIX;
23
24sub initAPI($)
25{
26 my $V = $_[0];
27 foreach my $K ("MethodInfo", "TypeInfo", "TName_Tid")
28 {
29 if(not defined $In::API{$V}{$K}) {
30 $In::API{$V}{$K} = {};
31 }
32 }
33}
34
35sub setTarget($)
36{
37 my $Target = $_[0];
38
39 if($Target eq "default")
40 {
41 $Target = getOSgroup();
42
43 $In::Opt{"OS"} = $Target;
44 $In::Opt{"Ar"} = getArExt($Target);
45 }
46
47 $In::Opt{"Target"} = $Target;
48}
49
50sub getMaxLen()
51{
52 if($In::Opt{"OS"} eq "windows") {
53 return 8191;
54 }
55
56 return undef;
57}
58
59sub getMaxArg()
60{
61 if($In::Opt{"OS"} eq "windows") {
62 return undef;
63 }
64
65 # Linux
66 return POSIX::sysconf(POSIX::_SC_ARG_MAX);
67}
68
69sub divideArray($)
70{
71 my $ArrRef = $_[0];
72
73 return () if($#{$ArrRef}==-1);
74
75 my $LEN_MAX = getMaxLen();
76 my $ARG_MAX = getMaxArg();
77
78 if(defined $ARG_MAX)
79 { # Linux
80 if($#{$ArrRef} < $ARG_MAX - 500) {
81 return $ArrRef;
82 }
83 }
84
85 my @Res = ();
86 my $Sub = [];
87 my $Len = 0;
88
89 foreach my $Pos (0 .. $#{$ArrRef})
90 {
91 my $Arg = $ArrRef->[$Pos];
92 my $Arg_L = length($Arg) + 1; # space
93
94 my ($LenLimit, $ArgLimit) = (1, 1);
95
96 if(defined $LEN_MAX) {
97 $LenLimit = ($Len < $LEN_MAX - 500);
98 }
99
100 if(defined $ARG_MAX) {
101 $ArgLimit = ($#{$Sub} < $ARG_MAX - 500);
102 }
103
104 if($LenLimit and $ArgLimit)
105 {
106 push(@{$Sub}, $Arg);
107 $Len += $Arg_L;
108 }
109 else
110 {
111 push(@Res, $Sub);
112
113 $Sub = [$Arg];
114 $Len = $Arg_L;
115 }
116 }
117
118 if($#{$Sub}!=-1) {
119 push(@Res, $Sub);
120 }
121
122 return @Res;
123}
124
125sub cmdFind(@)
126{ # native "find" is much faster than File::Find (~6x)
127 # also the File::Find doesn't support --maxdepth N option
128 # so using the cross-platform wrapper for the native one
129 my ($Path, $Type, $Name, $MaxDepth, $UseRegex) = ();
130
131 $Path = shift(@_);
132 if(@_) {
133 $Type = shift(@_);
134 }
135 if(@_) {
136 $Name = shift(@_);
137 }
138 if(@_) {
139 $MaxDepth = shift(@_);
140 }
141 if(@_) {
142 $UseRegex = shift(@_);
143 }
144
145 my $TmpDir = $In::Opt{"Tmp"};
146 my $TmpFile = $TmpDir."/null";
147
148 if($In::Opt{"OS"} eq "windows")
149 {
150 $Path = getAbsPath($Path);
151 my $Cmd = "cmd /C dir \"$Path\" /B /O";
152 if($MaxDepth!=1) {
153 $Cmd .= " /S";
154 }
155 if($Type eq "d") {
156 $Cmd .= " /AD";
157 }
158 elsif($Type eq "f") {
159 $Cmd .= " /A-D";
160 }
161
162 my @Files = split(/\n/, qx/$Cmd/);
163
164 if($Name)
165 {
166 if(not $UseRegex)
167 { # FIXME: how to search file names in MS shell?
168 # wildcard to regexp
169 $Name=~s/\*/.*/g;
170 $Name='\A'.$Name.'\Z';
171 }
172 @Files = grep { /$Name/i } @Files;
173 }
174 my @AbsPaths = ();
175 foreach my $File (@Files)
176 {
177 if(not isAbsPath($File)) {
178 $File = join_P($Path, $File);
179 }
180 if($Type eq "f" and not -f $File)
181 { # skip dirs
182 next;
183 }
184 push(@AbsPaths, $File);
185 }
186 if($Type eq "d") {
187 push(@AbsPaths, $Path);
188 }
189 return @AbsPaths;
190 }
191 else
192 {
193 my $FindCmd = "find";
194 if(not checkCmd($FindCmd)) {
195 exitStatus("Not_Found", "can't find a \"find\" command");
196 }
197 $Path = getAbsPath($Path);
198 if(-d $Path and -l $Path
199 and $Path!~/\/\Z/)
200 { # for directories that are symlinks
201 $Path.="/";
202 }
203 my $Cmd = $FindCmd." \"$Path\"";
204 if($MaxDepth) {
205 $Cmd .= " -maxdepth $MaxDepth";
206 }
207 if($Type) {
208 $Cmd .= " -type $Type";
209 }
210 if($Name and not $UseRegex)
211 { # wildcards
212 $Cmd .= " -name \"$Name\"";
213 }
214 my $Res = qx/$Cmd 2>"$TmpFile"/;
215 if($? and $!) {
216 printMsg("ERROR", "problem with \'find\' utility ($?): $!");
217 }
218 my @Files = split(/\n/, $Res);
219 if($Name and $UseRegex)
220 { # regex
221 @Files = grep { /$Name/ } @Files;
222 }
223 return @Files;
224 }
225}
226
227sub getVersion($)
228{
229 my $Cmd = $_[0];
230 my $TmpDir = $In::Opt{"Tmp"};
231 my $Ver = `$Cmd --version 2>\"$TmpDir/null\"`;
232 return $Ver;
233}
234
235return 1;
Note: See TracBrowser for help on using the repository browser.