/usr/share/ada/adainclude/gnatcoll/gnatcoll-path.adb is in libgnatcoll1.6-dev 1.6gpl2014-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | ------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2009-2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Characters.Handling; use Ada.Characters.Handling;
with GNAT.OS_Lib; use GNAT.OS_Lib;
package body GNATCOLL.Path is
function Dir_Separator (FS : FS_Type) return Character;
-- return '/' or '\' depending of FS
-------------------
-- Dir_Separator --
-------------------
function Dir_Separator (FS : FS_Type) return Character is
begin
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return '/';
when FS_Windows =>
return '\';
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end Dir_Separator;
function Internal_Local_FS return FS_Type;
-- We cache this value as it is very often referenced... and is
-- a constant !
-----------------------
-- Internal_Local_FS --
-----------------------
function Internal_Local_FS return FS_Type is
function Get_File_Names_Case_Sensitive return Integer;
pragma Import
(C, Get_File_Names_Case_Sensitive,
"__gnat_get_file_names_case_sensitive");
begin
if GNAT.OS_Lib.Directory_Separator = '\' then
return FS_Windows;
else
if Get_File_Names_Case_Sensitive = 0 then
return FS_Unix_Case_Insensitive;
else
return FS_Unix;
end if;
end if;
end Internal_Local_FS;
Loc_FS : constant FS_Type := Internal_Local_FS;
--------------
-- Local_FS --
--------------
function Local_FS return FS_Type is
begin
return Loc_FS;
end Local_FS;
-----------------------
-- Is_Case_Sensitive --
-----------------------
function Is_Case_Sensitive (FS : FS_Type) return Boolean is
begin
case FS is
when FS_Unix =>
return True;
when FS_Windows | FS_Unix_Case_Insensitive =>
return False;
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end Is_Case_Sensitive;
-----------------
-- Has_Devices --
-----------------
function Has_Devices (FS : FS_Type) return Boolean is
begin
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return False;
when FS_Windows =>
return True;
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end Has_Devices;
---------------------------
-- Multi_Unit_Index_Char --
---------------------------
function Multi_Unit_Index_Char (FS : FS_Type) return Character is
pragma Unreferenced (FS);
begin
return '~';
end Multi_Unit_Index_Char;
-------------------
-- Exe_Extension --
-------------------
function Exe_Extension (FS : FS_Type) return FS_String is
begin
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return "";
when FS_Windows =>
return ".exe";
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end Exe_Extension;
--------------
-- Get_Root --
--------------
function Get_Root
(FS : FS_Type;
Path : FS_String) return FS_String
is
Found_One : Boolean;
begin
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return "/";
when FS_Windows =>
if Path'Length >= 2
and then Path (Path'First + 1) = ':'
then
return Path (Path'First) & ":\";
elsif Path'Length > 3
and then Path (Path'First .. Path'First + 1) = "\\"
then
-- We need to return "\\machine\svc\" in this case
Found_One := False;
-- Used to determine that we found at least one
-- '\' after the initial "\\"
for J in Path'First + 2 .. Path'Last loop
if Path (J) = '\' then
if not Found_One then
Found_One := True;
else
return Path (Path'First .. J);
end if;
end if;
end loop;
if Found_One then
-- Case where we had "\\machine\svc" to analyse. The root
-- is then "\\machine\src\"
return Path & '\';
else
-- Incomplete ... return the default
return "\";
end if;
else
return "\";
end if;
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end Get_Root;
----------------------
-- Is_Absolute_Path --
----------------------
function Is_Absolute_Path
(FS : FS_Type;
Path : FS_String) return Boolean is
begin
if Path'Length = 0 then
return False;
end if;
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return Path (Path'First) = '/';
when FS_Windows =>
if Path'Length >= 3
and then Path (Path'First + 1 .. Path'First + 2) = ":\"
then
return True;
elsif Path'Length > 1
and then (Path (Path'First) = '\'
or else Path (Path'First) = '/')
then
return True;
end if;
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
return False;
end Is_Absolute_Path;
----------
-- Path --
----------
function Path
(FS : FS_Type;
Device : FS_String;
Dir : FS_String;
File : FS_String) return FS_String
is
Has_Dirsep : constant Boolean :=
Dir'Length >= 1
and then Dir (Dir'Last) = Dir_Separator (FS);
begin
if FS = FS_Unknown then
raise Invalid_Filesystem;
end if;
if FS in FS_Unix .. FS_Unix_Case_Insensitive or else Device = "" then
if Has_Dirsep then
return Dir & From_Unix (FS, File);
else
return Dir & Dir_Separator (FS) & From_Unix (FS, File);
end if;
else
if Has_Dirsep then
return Device & ":" & Dir & From_Unix (FS, File);
else
return Device & ":" & Dir & Dir_Separator (FS) &
From_Unix (FS, File);
end if;
end if;
end Path;
-----------
-- Equal --
-----------
function Equal
(FS : FS_Type;
Path1, Path2 : FS_String) return Boolean
is
function Internal (S1, S2 : FS_String) return Boolean;
-- Compare, taking care of trailing dir separators
--------------
-- Internal --
--------------
function Internal (S1, S2 : FS_String) return Boolean is
begin
return S1 = S2
or else (S2'Length > 0
and then S2 (S2'Last) = Dir_Separator (FS)
and then S1 = S2 (S2'First .. S2'Last - 1))
or else (S1'Length > 0
and then S1 (S1'Last) = Dir_Separator (FS)
and then S2 = S1 (S1'First .. S1'Last - 1));
end Internal;
begin
if Is_Case_Sensitive (FS) then
return Internal (Path1, Path2);
else
return Internal (FS_String (To_Lower (String (Path1))),
FS_String (To_Lower (String (Path2))));
end if;
end Equal;
-------------
-- To_Unix --
-------------
function To_Unix
(FS : FS_Type;
Path : FS_String;
Cygwin_Path : Boolean := False) return FS_String
is
begin
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return Path;
when FS_Windows =>
declare
Ret : FS_String := Path;
begin
for J in Ret'Range loop
if Ret (J) = '\' then
Ret (J) := '/';
end if;
end loop;
if Cygwin_Path
and then Ret'Length > 2
and then Ret (Ret'First + 1) = ':'
then
return "/cygdrive/" &
Ret (Ret'First) & Ret (Ret'First + 2 .. Ret'Last);
else
return Ret;
end if;
end;
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end To_Unix;
---------------
-- From_Unix --
---------------
function From_Unix
(FS : FS_Type;
Path : FS_String) return FS_String is
begin
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return Path;
when FS_Windows =>
declare
Ret : FS_String := Path;
begin
for J in Ret'Range loop
if Ret (J) = '/' then
Ret (J) := '\';
end if;
end loop;
if Ret'Length >= 11 -- "/cygdrive/X"'Length
and then Ret (Ret'First .. Ret'First + 9) = "\cygdrive\"
then
return Ret (Ret'First + 10) & ":" &
Ret (Ret'First + 11 .. Ret'Last);
else
return Ret;
end if;
end;
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end From_Unix;
--------------------
-- File_Extension --
--------------------
function File_Extension
(FS : FS_Type;
Path : FS_String) return FS_String is
begin
for J in reverse Path'Range loop
if Path (J) = '.' then
case FS is
when FS_Unix | FS_Unix_Case_Insensitive =>
return Path (J .. Path'Last);
when FS_Windows =>
return FS_String
(To_Lower (String (Path (J .. Path'Last))));
when FS_Unknown =>
raise Invalid_Filesystem;
end case;
end if;
end loop;
return "";
end File_Extension;
---------------
-- Base_Name --
---------------
function Base_Name
(FS : FS_Type;
Path : FS_String;
Suffix : FS_String := "") return FS_String is
begin
for J in reverse Path'Range loop
if Path (J) = Dir_Separator (FS) then
if Path'Last - J < Suffix'Length
or else Path (Path'Last - Suffix'Length + 1 .. Path'Last)
/= Suffix
then
return Path (J + 1 .. Path'Last);
else
return Path (J + 1 .. Path'Last - Suffix'Length);
end if;
end if;
end loop;
return Path;
end Base_Name;
-------------------
-- Base_Dir_Name --
-------------------
function Base_Dir_Name
(FS : FS_Type;
Path : FS_String) return FS_String
is
Root : constant FS_String := Get_Root (FS, Path);
begin
if Path = Root then
return Path;
elsif Path (Path'Last) = Dir_Separator (FS) then
return Base_Name (FS, Path (Path'First .. Path'Last - 1));
else
return Base_Name (FS, Path);
end if;
end Base_Dir_Name;
--------------
-- Dir_Name --
--------------
function Dir_Name
(FS : FS_Type;
Path : FS_String) return FS_String
is
Root : constant FS_String := Get_Root (FS, Path);
begin
if Root'Length > Path'Length then
return "";
end if;
for J in reverse Path'Range loop
if Path (J) = Dir_Separator (FS) then
return Path (Path'First .. J);
end if;
end loop;
return Path;
end Dir_Name;
----------------
-- Get_Parent --
----------------
function Get_Parent
(FS : FS_Type;
Path : FS_String) return FS_String
is
begin
if not Is_Dir_Name (FS, Path) then
return Dir_Name (FS, Path);
else
return Dir_Name (FS, Path (Path'First .. Path'Last - 1));
end if;
end Get_Parent;
-----------------
-- Is_Dir_Name --
-----------------
function Is_Dir_Name
(FS : FS_Type;
Path : FS_String) return Boolean is
begin
return Path'Length > 0
and then Path (Path'Last) = Dir_Separator (FS);
end Is_Dir_Name;
----------------------
-- Ensure_Directory --
----------------------
function Ensure_Directory
(FS : FS_Type;
Path : FS_String) return FS_String is
begin
if not Is_Dir_Name (FS, Path) then
return Path & Dir_Separator (FS);
else
return Path;
end if;
end Ensure_Directory;
-----------------
-- Device_Name --
-----------------
function Device_Name
(FS : FS_Type;
Path : FS_String) return FS_String is
begin
if FS = FS_Windows
and then Path'Length > 2
and then Path (Path'First + 1) = ':'
then
return "" & Path (Path'First);
else
return "";
end if;
end Device_Name;
---------------
-- Normalize --
---------------
function Normalize
(FS : FS_Type;
Path : FS_String) return FS_String
is
Dest : FS_String := Path;
Src : Natural := Path'First;
Idx : Natural := Dest'First;
DS : Character renames Dir_Separator (FS);
begin
while Src <= Path'Last loop
if Path (Src) = DS then
if Src + 3 <= Path'Last
and then Path (Src + 1 .. Src + 3) = ".." & DS
then
for K in reverse Dest'First .. Idx - 1 loop
if Dest (K) = Dir_Separator (FS) then
Idx := K + 1;
exit;
end if;
end loop;
Src := Src + 4;
elsif Src + 2 <= Path'Last
and then Path (Src + 1 .. Src + 2) = "." & DS
then
Src := Src + 2;
Dest (Idx) := DS;
else
Dest (Idx) := Path (Src);
Idx := Idx + 1;
Src := Src + 1;
end if;
else
Dest (Idx) := Path (Src);
Idx := Idx + 1;
Src := Src + 1;
end if;
end loop;
return Dest (Dest'First .. Idx - 1);
end Normalize;
-------------------
-- Relative_Path --
-------------------
function Relative_Path
(FS : FS_Type;
Ref : FS_String;
Path : FS_String) return FS_String
is
Depth : Natural := 0;
Last : Natural := Ref'Last;
Old : Natural;
function Depth_Image return FS_String;
-- Return "../" * Depth
-----------------
-- Depth_Image --
-----------------
function Depth_Image return FS_String is
Ret : FS_String (1 .. 3 * Depth);
Sep : constant FS_String := ".." & Dir_Separator (FS);
begin
for J in 1 .. Depth loop
Ret (J * 3 - 2 .. J * 3) := Sep;
end loop;
return Ret;
end Depth_Image;
begin
-- If roots are not identical, then this means that we need to return
-- the file's full name.
if not Equal (FS, Get_Root (FS, Ref), Get_Root (FS, Path)) then
return Path;
end if;
if Equal (FS, Ref, Path) then
return ".";
end if;
loop
exit when Last - Ref'First + 1 <= Path'Length
and then Equal
(FS,
Path (Path'First .. Path'First + Last - Ref'First),
Ref (Ref'First .. Last));
Old := Last;
for J in reverse Ref'First .. Last - 1 loop
if Ref (J) = Dir_Separator (FS) then
Depth := Depth + 1;
Last := J;
exit;
end if;
end loop;
if Old = Last then
-- No Dir_Separator in Ref... Let's return Path
return Path;
end if;
end loop;
return Depth_Image &
Path (Path'First + Last - Ref'First + 1 .. Path'Last);
end Relative_Path;
end GNATCOLL.Path;
|