xdg_base_dir_1.0.0_e0e3de18/src/xdg_base_dir.adb

 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
--  SPDX-License-Identifier: Apache-2.0
--
--  Copyright (c) 2022 onox <denkpadje@gmail.com>
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.

with Ada.Environment_Variables;
with Ada.Strings.Fixed;

package body XDG_Base_Dir is

   function Is_Absolute (Path : String) return Boolean is
     (Path'Length > 0 and then Path (Path'First) = '/');

   function Split (Value, Separator : String) return String_List is
      package SF renames Ada.Strings.Fixed;

      Index : Positive := Value'First;
      Count : constant Positive := SF.Count (Value, Separator) + 1;

      Result : String_List (1 .. Count);
      Result_Index : Positive := Result'First;
   begin
      for I in Result'First .. Result'Last - 1 loop
         declare
            Next_Index : constant Positive := SF.Index (Value, Separator, Index);
            Path : constant String := Value (Index .. Next_Index - 1);
         begin
            if Is_Absolute (Path) then
               Result (Result_Index) := SU.To_Unbounded_String (Path);
               Result_Index := Result_Index + 1;
            end if;
            Index := Next_Index + Separator'Length;
         end;
      end loop;

      declare
         Path : constant String := Value (Index .. Value'Last);
      begin
         if Is_Absolute (Path) then
            Result (Result_Index) := SU.To_Unbounded_String (Path);
            Result_Index := Result_Index + 1;
         end if;
      end;

      return Result (Result'First .. Result_Index - 1);
   end Split;

   function From_Variable (Variable, Default : String) return String_List is
      Result : constant String_List :=
        Split (Ada.Environment_Variables.Value (Variable, Default), ":");
   begin
      return (if Result'Length > 0 then Result else Split (Default, ":"));
   end From_Variable;

   function From_Variable (Variable, Default : String) return String is
      Result : constant String := Ada.Environment_Variables.Value (Variable, Default);
   begin
      return (if Is_Absolute (Result) then Result else Default);
   end From_Variable;

end XDG_Base_Dir;