OSDN Git Service

2006-10-31 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-fil.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                            M L I B . F I L                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                     Copyright (C) 2001-2006, AdaCore                     --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 --  This package provides a set of routines to deal with file extensions
28
29 with Ada.Strings.Fixed;
30 with MLib.Tgt;
31
32 package body MLib.Fil is
33
34    use Ada;
35
36    package Target renames MLib.Tgt;
37
38    ---------------
39    -- Append_To --
40    ---------------
41
42    function Append_To
43      (Filename : String;
44       Ext      : String) return String
45    is
46    begin
47       if Ext'Length = 0 then
48          return Filename;
49
50       elsif Filename (Filename'Last) = '.' then
51          if Ext (Ext'First) = '.' then
52             return Filename & Ext (Ext'First + 1 .. Ext'Last);
53
54          else
55             return Filename & Ext;
56          end if;
57
58       else
59          if Ext (Ext'First) = '.' then
60             return Filename & Ext;
61
62          else
63             return Filename & '.' & Ext;
64          end if;
65       end if;
66    end Append_To;
67
68    ------------
69    -- Ext_To --
70    ------------
71
72    function Ext_To
73      (Filename : String;
74       New_Ext  : String := "") return String
75    is
76       use Strings.Fixed;
77
78       J : constant Natural :=
79             Index (Source  =>  Filename,
80                    Pattern => ".",
81                    Going   => Strings.Backward);
82
83    begin
84       if J = 0 then
85          if New_Ext = "" then
86             return Filename;
87          else
88             return Filename & "." & New_Ext;
89          end if;
90
91       else
92          if New_Ext = "" then
93             return Head (Filename, J - 1);
94          else
95             return Head (Filename, J - 1) & '.' & New_Ext;
96          end if;
97       end if;
98    end Ext_To;
99
100    -------------
101    -- Get_Ext --
102    -------------
103
104    function Get_Ext (Filename : String) return String is
105       use Strings.Fixed;
106
107       J : constant Natural :=
108             Index (Source  =>  Filename,
109                    Pattern => ".",
110                    Going   => Strings.Backward);
111
112    begin
113       if J = 0 then
114          return "";
115       else
116          return Filename (J .. Filename'Last);
117       end if;
118    end Get_Ext;
119
120    ----------------
121    -- Is_Archive --
122    ----------------
123
124    function Is_Archive (Filename : String) return Boolean is
125       Ext : constant String := Get_Ext (Filename);
126    begin
127       return Target.Is_Archive_Ext (Ext);
128    end Is_Archive;
129
130    ----------
131    -- Is_C --
132    ----------
133
134    function Is_C (Filename : String) return Boolean is
135       Ext : constant String := Get_Ext (Filename);
136    begin
137       return Target.Is_C_Ext (Ext);
138    end Is_C;
139
140    ------------
141    -- Is_Obj --
142    ------------
143
144    function Is_Obj (Filename : String) return Boolean is
145       Ext : constant String := Get_Ext (Filename);
146    begin
147       return Target.Is_Object_Ext (Ext);
148    end Is_Obj;
149
150 end MLib.Fil;