Skip to content

Commit 1d6c74c

Browse files
committed
Replace path representation as String by Virtual_File
1 parent 55d0151 commit 1d6c74c

File tree

2 files changed

+135
-74
lines changed

2 files changed

+135
-74
lines changed

src/metrics-actions.adb

Lines changed: 134 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ package body METRICS.Actions is
383383
function Fine_Kind_String_For_Header (M : Metrix) return String;
384384
-- Name of the node kind for printing in both XML and text
385385

386-
procedure Write_XML_Schema (Xsd_File_Name : String);
386+
procedure Write_Xsd (Xsd_Virtual_File : GNATCOLL.VFS.Virtual_File);
387387
-- Write the XSD file
388388

389389
function XML (X : String) return String;
@@ -1985,24 +1985,26 @@ package body METRICS.Actions is
19851985
Free (M);
19861986
end Destroy;
19871987

1988-
----------------------
1989-
-- Write_XML_Schema --
1990-
----------------------
1988+
---------------
1989+
-- Write_Xsd --
1990+
---------------
19911991

1992-
procedure Write_XML_Schema (Xsd_File_Name : String) is
1993-
XSD_Out_File : Text_IO.File_Type;
1992+
procedure Write_Xsd (Xsd_Virtual_File : GNATCOLL.VFS.Virtual_File) is
1993+
use GNATCOLL.VFS;
19941994

1995-
Has_Parent_Directory : constant Boolean :=
1996-
Ada.Directories.Simple_Name (Xsd_File_Name) /= Xsd_File_Name;
1995+
Xsd_File : Ada.Text_IO.File_Type;
19971996

19981997
begin
1998+
Xsd_Virtual_File.Normalize_Path;
1999+
19992000
if not Output_To_Standard_Output then
2000-
if Has_Parent_Directory then
2001-
Ada.Directories.Create_Path
2002-
(Ada.Directories.Containing_Directory (Xsd_File_Name));
2001+
if Xsd_Virtual_File.Get_Parent /= No_File then
2002+
Make_Dir (Xsd_Virtual_File.Get_Parent);
20032003
end if;
2004-
Text_IO.Create (XSD_Out_File, Name => Xsd_File_Name);
2005-
Text_IO.Set_Output (XSD_Out_File);
2004+
2005+
Ada.Text_IO.Create
2006+
(Xsd_File, Name => Xsd_Virtual_File.Display_Full_Name);
2007+
Ada.Text_IO.Set_Output (Xsd_File);
20062008
end if;
20072009

20082010
pragma Style_Checks ("M200"); -- Allow long lines
@@ -2073,10 +2075,10 @@ package body METRICS.Actions is
20732075
pragma Style_Checks ("M79");
20742076

20752077
if not Output_To_Standard_Output then
2076-
Text_IO.Set_Output (Text_IO.Standard_Output);
2077-
Text_IO.Close (XSD_Out_File);
2078+
Ada.Text_IO.Set_Output (Ada.Text_IO.Standard_Output);
2079+
Ada.Text_IO.Close (Xsd_File);
20782080
end if;
2079-
end Write_XML_Schema;
2081+
end Write_Xsd;
20802082

20812083
----------
20822084
-- Init --
@@ -2584,7 +2586,13 @@ package body METRICS.Actions is
25842586
Outdent;
25852587
end XML_Print_Coupling;
25862588

2589+
-----------
2590+
-- Final --
2591+
-----------
2592+
25872593
procedure Final (Tool : in out Metrics_Tool; Cmd : Command_Line) is
2594+
use type GNATCOLL.VFS.Virtual_File;
2595+
25882596
Metrics_To_Compute : Metrics_Set renames Tool.Metrics_To_Compute;
25892597
Without_Coupling : constant Metrics_Set :=
25902598
Metrics_To_Compute and not Coupling_Only;
@@ -2601,66 +2609,110 @@ package body METRICS.Actions is
26012609
-- switch was given. By default, this information is sent to standard
26022610
-- output.
26032611

2604-
function Get_Object_Dir return String;
2605-
-- Name of the Object_Dir specified in the project file, if any
2612+
function Get_Object_Directory return GNATCOLL.VFS.Virtual_File;
2613+
-- Object_Dir specified in the project file.
2614+
-- Returns GNATCOLL.VFS.No_File is not specified.
2615+
2616+
function Get_Xml_Virtual_File
2617+
(Object_Directory : GNATCOLL.VFS.Virtual_File)
2618+
return GNATCOLL.VFS.Virtual_File;
2619+
-- If the user supplies a full or relative file path, it is used
2620+
-- directly.
2621+
-- If the user provides only a filename (e.g., "data.xml"), it is
2622+
-- resolved relative to the object directory (if one is configured).
2623+
-- If no user-provided path or filename is given, the default
2624+
-- "metrix.xml" is used, also resolved relative to the object directory
2625+
-- (if configured).
2626+
2627+
function Get_Xsd_Virtual_File
2628+
(Xml_Virtual_File : GNATCOLL.VFS.Virtual_File)
2629+
return GNATCOLL.VFS.Virtual_File;
2630+
-- Return the GNATCOLL.VFS.Virtual_File of the XSD (schema) file name,
2631+
-- which is based on the XML file name. In particular if the XML file
2632+
-- name ends in ".xml", that is replaced by ".xsd"; otherwise,
2633+
-- ".xsd" is appended. So "foo.xml" --> "foo.xsd", but
2634+
-- "foo.bar" --> "foo.bar.xsd".
2635+
--
2636+
-- Note that this name is written to the XML file, in addition
2637+
-- to being used to open the XSD file.
2638+
2639+
--------------------------
2640+
-- Get_Object_Directory --
2641+
--------------------------
26062642

2607-
function Get_Object_Dir return String is
2643+
function Get_Object_Directory return GNATCOLL.VFS.Virtual_File is
26082644
use GNATCOLL.Projects, GNATCOLL.VFS;
2645+
26092646
begin
26102647
if Status (Tool.Project_Tree.all) = Empty then
2611-
return "";
2648+
return No_File;
2649+
26122650
else
2613-
declare
2614-
Prj : constant Project_Type := Tool.Project_Tree.Root_Project;
2615-
Name : constant Filesystem_String :=
2616-
Full_Name (Object_Dir (Prj));
2617-
begin
2618-
return String (Name);
2619-
end;
2651+
return Object_Dir (Tool.Project_Tree.Root_Project);
26202652
end if;
2621-
end Get_Object_Dir;
2653+
end Get_Object_Directory;
26222654

2623-
Object_Dir : constant String := Get_Object_Dir;
2655+
--------------------------
2656+
-- Get_Xml_Virtual_File --
2657+
--------------------------
26242658

2625-
Xml_F_Name : constant String :=
2626-
(if Arg (Cmd, Xml_File_Name) = null
2627-
then "metrix.xml"
2628-
else Arg (Cmd, Xml_File_Name).all);
2629-
-- ASIS-based gnatmetric ignores Output_Dir for the xml.
2630-
2631-
Has_Parent_Directory : constant Boolean :=
2632-
Directories.Simple_Name (Xml_F_Name) /= Xml_F_Name;
2633-
-- True if Xml_F_Name contains directory information
2659+
function Get_Xml_Virtual_File
2660+
(Object_Directory : GNATCOLL.VFS.Virtual_File)
2661+
return GNATCOLL.VFS.Virtual_File
2662+
is
2663+
use GNATCOLL.VFS;
26342664

2635-
Xml_FD_Name : constant String :=
2636-
(if Object_Dir = "" or else Has_Dir
2637-
then Xml_F_Name
2638-
else Directories.Compose (Object_Dir, Xml_F_Name));
2665+
Xml_Virtual_File_Aux : constant Virtual_File :=
2666+
(if Arg (Cmd, Xml_File_Name) = null
2667+
then Create_From_UTF8 ("metrix.xml")
2668+
else
2669+
Create_From_UTF8
2670+
(Arg (Cmd, Xml_File_Name).all, Normalize => True));
2671+
begin
2672+
return
2673+
(if Xml_Virtual_File_Aux.Is_Absolute_Path
2674+
then Xml_Virtual_File_Aux
2675+
elsif Object_Directory /= No_File
2676+
then Object_Directory / Xml_Virtual_File_Aux
2677+
else Get_Current_Dir / Xml_Virtual_File_Aux);
2678+
end Get_Xml_Virtual_File;
2679+
2680+
--------------------------
2681+
-- Get_Xsd_Virtual_File --
2682+
--------------------------
2683+
2684+
function Get_Xsd_Virtual_File
2685+
(Xml_Virtual_File : GNATCOLL.VFS.Virtual_File)
2686+
return GNATCOLL.VFS.Virtual_File
2687+
is
2688+
use GNATCOLL.VFS;
26392689

2640-
XML_File : Text_IO.File_Type;
2641-
-- All XML output for all source files goes to this file.
2690+
Normalized_Xml_Path : constant String :=
2691+
Xml_Virtual_File.Display_Full_Name;
26422692

2643-
function Xsd_File_Name return String;
2644-
-- Return the name of the XSD (schema) file name, which is based
2645-
-- on the XML file name. In particular if the XML file name
2646-
-- ends in ".xml", that is replaced by ".xsd"; otherwise,
2647-
-- ".xsd" is appended. So "foo.xml" --> "foo.xsd", but
2648-
-- "foo.bar" --> "foo.bar.xsd".
2649-
--
2650-
-- Note that this name is written to the XML file, in addition
2651-
-- to being used to open the XSD file.
2693+
Xml : constant String := ".xml";
2694+
Xsd : constant String := ".xsd";
26522695

2653-
function Xsd_File_Name return String is
2654-
Norm : constant String := Normalize_Pathname (Xml_FD_Name);
2655-
Xml : constant String := ".xml";
2656-
Xsd : constant String := ".xsd";
26572696
begin
2658-
if Has_Suffix (Norm, Suffix => Xml) then
2659-
return Replace_String (Norm, Xml, Xsd);
2697+
if Xml_Virtual_File.Has_Suffix (+Xml) then
2698+
return
2699+
Create_From_UTF8
2700+
(Replace_String (Normalized_Xml_Path, Xml, Xsd));
26602701
else
2661-
return Norm & Xsd;
2702+
return Create_From_UTF8 (Normalized_Xml_Path & Xsd);
26622703
end if;
2663-
end Xsd_File_Name;
2704+
end Get_Xsd_Virtual_File;
2705+
2706+
Object_Directory : constant GNATCOLL.VFS.Virtual_File :=
2707+
Get_Object_Directory;
2708+
2709+
Xml_Virtual_File : constant GNATCOLL.VFS.Virtual_File :=
2710+
Get_Xml_Virtual_File (Object_Directory);
2711+
XML_File : Text_IO.File_Type;
2712+
-- All XML output for all source files goes to this file.
2713+
2714+
Xsd_Virtual_File : constant GNATCOLL.VFS.Virtual_File :=
2715+
Get_Xsd_Virtual_File (Xml_Virtual_File);
26642716

26652717
procedure Print_Computed_Metric
26662718
(T : Formatted_Output.Template;
@@ -2710,14 +2762,18 @@ package body METRICS.Actions is
27102762
Pop (Metrix_Stack);
27112763
Clear (Metrix_Stack);
27122764

2713-
if Object_Dir /= "" and then not Directories.Exists (Object_Dir) then
2765+
if Object_Directory /= GNATCOLL.VFS.No_File then
27142766
begin
2715-
Directories.Create_Path (Object_Dir);
2767+
GNATCOLL.VFS.Make_Dir (Object_Directory);
2768+
27162769
exception
2717-
when Directories.Name_Error | Directories.Use_Error =>
2718-
Cmd_Error ("cannot create directory " & Object_Dir);
2770+
when GNATCOLL.VFS.VFS_Directory_Error =>
2771+
Cmd_Error
2772+
("cannot create directory "
2773+
& Object_Directory.Display_Full_Name (Normalize => True));
27192774
end;
27202775
end if;
2776+
Xml_Virtual_File.Normalize_Path;
27212777

27222778
Dump (Tool, Global_M.all, "Initial:");
27232779
Compute_Indirect_Dependencies (Global_M.all);
@@ -2729,18 +2785,19 @@ package body METRICS.Actions is
27292785
-- Generate schema (XSD file), if requested
27302786

27312787
if Arg (Cmd, Generate_XML_Schema) then
2732-
Write_XML_Schema (Xsd_File_Name);
2788+
Write_Xsd (Xsd_Virtual_File);
27332789
end if;
27342790

27352791
-- Put initial lines of XML
27362792

27372793
if not Output_To_Standard_Output then
2738-
if Has_Parent_Directory then
2739-
Ada.Directories.Create_Path
2740-
(Ada.Directories.Containing_Directory (Xml_FD_Name));
2794+
if Xml_Virtual_File.Get_Parent /= GNATCOLL.VFS.No_File then
2795+
GNATCOLL.VFS.Make_Dir (Xml_Virtual_File.Get_Parent);
27412796
end if;
2742-
Text_IO.Create (XML_File, Name => Xml_FD_Name);
2743-
Text_IO.Set_Output (XML_File);
2797+
2798+
Ada.Text_IO.Create
2799+
(XML_File, Name => Xml_Virtual_File.Display_Full_Name);
2800+
Ada.Text_IO.Set_Output (XML_File);
27442801
end if;
27452802
Put ("<?xml version=\1?>\n", Q ("1.0"));
27462803

@@ -2749,7 +2806,7 @@ package body METRICS.Actions is
27492806
("<global xmlns:xsi="
27502807
& """http://www.w3.org/2001/XMLSchema-instance"" "
27512808
& "xsi:noNamespaceSchemaLocation=""\1"">\n",
2752-
Xsd_File_Name);
2809+
Xsd_Virtual_File.Display_Full_Name);
27532810
else
27542811
Put ("<global>\n");
27552812
end if;
@@ -2764,7 +2821,11 @@ package body METRICS.Actions is
27642821
for File_M of Global_M.Submetrix loop
27652822
pragma Assert (Debug_Flag_V or else Indentation_Level = 0);
27662823
Print_File_Metrics
2767-
(Cmd, XML_File, File_M.all, Without_Coupling, Object_Dir);
2824+
(Cmd,
2825+
XML_File,
2826+
File_M.all,
2827+
Without_Coupling,
2828+
Object_Directory.Display_Full_Name);
27682829
pragma Assert (Debug_Flag_V or else Indentation_Level = 0);
27692830
-- Destroy (File_M);
27702831
end loop;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
procedure Simple is
22
begin
33
null;
4-
end Simple;
4+
end Simple;

0 commit comments

Comments
 (0)