/usr/share/ada/adainclude/gnatcoll/gnatcoll-config.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 | ------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2010-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.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNATCOLL.Mmap; use GNATCOLL.Mmap;
with GNATCOLL.Templates; use GNATCOLL.Templates;
with GNATCOLL.Utils; use GNATCOLL.Utils;
with GNATCOLL.VFS; use GNATCOLL.VFS;
package body GNATCOLL.Config is
use String_Maps;
function Internal_Get
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key) return Config_Value;
-- Internal version of Get
function At_Index
(Value : Config_Value; Index : Natural := Whole_Value) return String;
-- Extract an element from a comma-separated list
function Substitute
(Self : INI_Parser'Class; Value : String) return String;
-- Substitute various strings in the value read from the config file,
-- for instance $HOME.
-------------------
-- Set_System_Id --
-------------------
procedure Set_System_Id (Self : in out Config_Parser; System_ID : String) is
begin
Self.System_ID := To_Unbounded_String (Normalize_Pathname (System_ID));
end Set_System_Id;
----------------
-- As_Integer --
----------------
function As_Integer (Self : Config_Parser) return Integer is
begin
return Integer'Value (Value (Config_Parser'Class (Self)));
end As_Integer;
----------------
-- As_Boolean --
----------------
function As_Boolean (Self : Config_Parser) return Boolean is
begin
return Boolean'Value (Value (Config_Parser'Class (Self)));
end As_Boolean;
----------------------
-- As_Absolute_File --
----------------------
function As_Absolute_File (Self : Config_Parser) return String is
Val : constant String := Value (Config_Parser'Class (Self));
begin
if Val = "" then
return "";
elsif Val (Val'First) = '/' then
return Val;
else
return Normalize_Pathname (Val, To_String (Self.System_ID));
end if;
end As_Absolute_File;
---------------------
-- As_Absolute_Dir --
---------------------
function As_Absolute_Dir (Self : Config_Parser) return String is
V : constant String := As_Absolute_File (Config_Parser'Class (Self));
begin
if V = "" then
return "";
elsif V (V'Last) = Directory_Separator then
return V;
else
return V & Directory_Separator;
end if;
end As_Absolute_Dir;
----------
-- Open --
----------
procedure Open (Self : in out File_Config_Parser; Filename : String) is
F : Mapped_File;
Str : Str_Access;
begin
F := Open_Read (Filename);
Read (F);
Str := Data (F);
Self.Contents := To_Unbounded_String (String (Str (1 .. Last (F))));
Self.System_ID := To_Unbounded_String
(Normalize_Pathname (Dir_Name (Filename)));
Self.First := 1;
Close (F);
end Open;
------------
-- At_End --
------------
overriding function At_End (Self : File_Config_Parser) return Boolean is
begin
return Self.First > Length (Self.Contents);
end At_End;
----------
-- Open --
----------
overriding procedure Open (Self : in out INI_Parser; Filename : String) is
begin
Open (File_Config_Parser (Self), Filename);
Self.Eol := 0;
Next (Self);
end Open;
----------
-- Next --
----------
overriding procedure Next (Self : in out INI_Parser) is
Eol : Integer := Self.First;
Tmp : Integer;
Last : constant Integer := Length (Self.Contents);
Comment : constant Integer := Length (Self.Comment_Start);
begin
Self.First := Self.Eol + 1;
-- Search end of current line
while Self.First <= Last loop
Eol := Self.First;
Self.Equal := 0;
while Eol <= Last
and then Element (Self.Contents, Eol) /= ASCII.LF
loop
if Self.Equal = 0 and then Element (Self.Contents, Eol) = '=' then
Self.Equal := Eol;
end if;
Eol := Eol + 1;
end loop;
Self.Eol := Eol;
Tmp := Eol - 1;
if Element (Self.Contents, Tmp) = ASCII.CR then
Tmp := Tmp - 1;
end if;
-- Are we seeing a comment ?
if Self.First + Comment - 1 <= Eol
and then Slice
(Self.Contents, Self.First, Self.First + Comment - 1) =
Self.Comment_Start
then
null;
elsif Self.Use_Sections
and then Element (Self.Contents, Self.First) = '['
and then Element (Self.Contents, Tmp) = ']'
then
Self.Current_Section := To_Unbounded_String
(Strip_CR (Slice (Self.Contents, Self.First + 1, Tmp - 1)));
elsif Self.Equal /= 0 then
return;
end if;
Self.First := Eol + 1;
end loop;
end Next;
---------------
-- Configure --
---------------
procedure Configure
(Self : in out INI_Parser;
Comment_Start : String := "#";
Handles_Sections : Boolean := True;
Home : String := "")
is
begin
Self.Comment_Start := To_Unbounded_String (Comment_Start);
Self.Use_Sections := Handles_Sections;
if Home /= "" then
Self.Home := Create (+Home);
end if;
end Configure;
----------------
-- Substitute --
----------------
function Substitute
(Self : INI_Parser'Class; Value : String) return String
is
function Callback (Name : String; Quoted : Boolean) return String;
function Callback (Name : String; Quoted : Boolean) return String is
pragma Unreferenced (Quoted);
begin
if Name = "HOME" then
return +Self.Home.Full_Name;
else
raise Invalid_Substitution;
end if;
end Callback;
begin
return Substitute
(Str => Value,
Callback => Callback'Unrestricted_Access,
Delimiter => '$');
end Substitute;
-------------
-- Section --
-------------
overriding function Section (Self : INI_Parser) return String is
begin
return To_String (Self.Current_Section);
end Section;
---------
-- Key --
---------
overriding function Key (Self : INI_Parser) return String is
begin
return Trim
(Strip_CR (Slice (Self.Contents, Self.First, Self.Equal - 1)),
Side => Ada.Strings.Both);
end Key;
-----------
-- Value --
-----------
overriding function Value (Self : INI_Parser) return String is
begin
return Substitute
(Self,
Trim (Strip_CR (Slice (Self.Contents, Self.Equal + 1, Self.Eol - 1)),
Side => Ada.Strings.Left));
end Value;
----------
-- Fill --
----------
procedure Fill
(Self : in out Config_Pool;
Config : in out Config_Parser'Class)
is
begin
Set_System_Id (Self, To_String (Config.System_ID));
while not At_End (Config) loop
Set (Self, Section (Config), Key (Config), Value (Config));
Next (Config);
end loop;
end Fill;
-------------------
-- Set_System_Id --
-------------------
procedure Set_System_Id (Self : in out Config_Pool; System_ID : String) is
begin
Self.System_ID := To_Unbounded_String (Normalize_Pathname (System_ID));
end Set_System_Id;
------------------
-- Internal_Get --
------------------
function Internal_Get
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key) return Config_Value is
begin
if Section = Section_From_Key then
for D in Key'Range loop
if Key (D) = '.' then
return Element (Self.Keys,
Key (Key'First .. D - 1)
& '#' & Key (D + 1 .. Key'Last));
end if;
end loop;
return Element (Self.Keys, '#' & Key);
else
return Element (Self.Keys, Section & "#" & Key);
end if;
end Internal_Get;
--------------
-- At_Index --
--------------
function At_Index
(Value : Config_Value; Index : Natural := Whole_Value) return String
is
S : String_List_Access;
begin
if Index = Whole_Value then
return Value.Value;
else
S := Split (Value.Value, ',');
if Index > S'Last then
Free (S);
return "";
else
return R : constant String := S (Index).all do
Free (S);
end return;
end if;
end if;
end At_Index;
---------
-- Get --
---------
function Get
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key;
Index : Natural := Whole_Value) return String
is
begin
return At_Index (Internal_Get (Self, Key, Section), Index);
end Get;
-----------------
-- Get_Integer --
-----------------
function Get_Integer
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key;
Index : Natural := Whole_Value) return Integer is
begin
return Integer'Value (Get (Self, Key, Section, Index));
end Get_Integer;
-----------------
-- Get_Boolean --
-----------------
function Get_Boolean
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key;
Index : Natural := Whole_Value) return Boolean is
begin
return Boolean'Value (Get (Self, Key, Section, Index));
end Get_Boolean;
--------------
-- Get_File --
--------------
function Get_File
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key;
Index : Natural := Whole_Value) return String
is
Val : constant Config_Value := Internal_Get (Self, Key, Section);
V : constant String := At_Index (Val, Index);
begin
if V = "" then
return "";
elsif Is_Absolute_Path (V) then
return V;
else
return Normalize_Pathname (V, To_String (Val.System_ID));
end if;
end Get_File;
-------------
-- To_File --
-------------
function To_File
(Self : Config_Pool;
Key : String;
Section : String := Section_From_Key;
Value : String) return Virtual_File
is
Val : constant Config_Value := Internal_Get (Self, Key, Section);
begin
if Value = "" then
return GNATCOLL.VFS.No_File;
elsif Is_Absolute_Path (Value) then
return Create (+Value);
else
return Create
(+Normalize_Pathname (Value, To_String (Val.System_ID)));
end if;
end To_File;
---------
-- Set --
---------
procedure Set (Self : in out Config_Pool; Section, Key, Value : String) is
begin
Include (Self.Keys, Section & "#" & Key,
Config_Value'
(Len => Value'Length,
Value => Value,
System_ID => Self.System_ID));
end Set;
------------
-- Create --
------------
function Create (Key : String; Section : String := "") return Config_Key is
begin
return Config_Key'(Section => To_Unbounded_String (Section),
Key => To_Unbounded_String (Key));
end Create;
---------
-- Get --
---------
function Get
(Self : Config_Key;
Conf : Config_Pool'Class;
Index : Natural := Whole_Value) return String is
begin
return Get (Conf, To_String (Self.Key), To_String (Self.Section), Index);
end Get;
-----------------
-- Get_Integer --
-----------------
function Get_Integer
(Self : Config_Key;
Conf : Config_Pool'Class;
Index : Natural := Whole_Value) return Integer is
begin
return Get_Integer
(Conf, To_String (Self.Key), To_String (Self.Section), Index);
end Get_Integer;
-----------------
-- Get_Boolean --
-----------------
function Get_Boolean
(Self : Config_Key;
Conf : Config_Pool'Class;
Index : Natural := Whole_Value) return Boolean is
begin
return Get_Boolean
(Conf, To_String (Self.Key), To_String (Self.Section), Index);
end Get_Boolean;
--------------
-- Get_File --
--------------
function Get_File
(Self : Config_Key;
Conf : Config_Pool'Class;
Index : Natural := Whole_Value) return String is
begin
return Get_File
(Conf, To_String (Self.Key), To_String (Self.Section), Index);
end Get_File;
-------------
-- To_File --
-------------
function To_File
(Self : Config_Key;
Conf : Config_Pool'Class;
Value : String) return Virtual_File is
begin
return To_File
(Conf, To_String (Self.Key), To_String (Self.Section), Value);
end To_File;
end GNATCOLL.Config;
|