with Io;
with String_Utilities;
package body Unit_Stats is

    package Sio renames Io;

    function Evaluate (File : String) return Statistics is

        St : Statistics := (0, (others => 0));

        This_File : Sio.File_Type;
        Line_Charac : Line_Utilities.Line_Type;
    begin

        Sio.Open (This_File, Sio.In_File, File);

        loop
            Line_Charac := Line_Utilities.Characterize
                              (Sio.Get_Line (This_File));
            St.Total_Lines := St.Total_Lines + 1;
            St.Counts (Line_Charac) := St.Counts (Line_Charac) + 1;
        end loop;
    exception
        when Sio.End_Error =>
            Sio.Close (This_File);
            return St;
    end Evaluate;

    function Total_Lines (Within : Statistics) return Integer is
    begin
        return Within.Total_Lines;
    end Total_Lines;

    function Count (Of_Kind : Line_Utilities.Line_Type; Within : Statistics)
                   return Integer is
    begin
        return Within.Counts (Of_Kind);
    end Count;

    function Percentage_Whitespace (S : Statistics) return Integer is
    begin
        if S.Total_Lines = 0 then
            return 100;
        else
            return (100 * (S.Counts (Line_Utilities.Blank_Line) +
                           S.Counts (Line_Utilities.Comment_Line)) /
                    S.Total_Lines);
        end if;
    end Percentage_Whitespace;
end Unit_Stats;