From 18c88c85c04fe32fc3afea86053a29376530457f Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Mon, 15 Jan 2007 01:10:46 +0000 Subject: [PATCH] M agpl-strings-fields.adb M agpl-strings-fields.ads M psql/agpl-db-psql.adb M psql/agpl-db-psql.ads M agpl-strings.adb M agpl-url.adb M agpl-url.ads --- agpl-strings-fields.adb | 11 ++++-- agpl-strings-fields.ads | 10 +++-- agpl-strings.adb | 3 +- agpl-url.adb | 82 ++++++++++++++++++++++++++--------------- agpl-url.ads | 32 ++-------------- psql/agpl-db-psql.adb | 11 ++++++ psql/agpl-db-psql.ads | 4 ++ 7 files changed, 86 insertions(+), 67 deletions(-) diff --git a/agpl-strings-fields.adb b/agpl-strings-fields.adb index 18ac51f..8c966cd 100644 --- a/agpl-strings-fields.adb +++ b/agpl-strings-fields.adb @@ -91,16 +91,21 @@ package body Agpl.Strings.Fields is -- Returns the head or "" if no tokenizer found. function String_tail (s : String; - Separator : Character := '/') + Separator : Character := '/'; + Start : Positive := 1) return String is + Skip : Natural := 0; begin if S = "" then return S; end if; for n in S'Range loop - if S (n) = Separator and n < S'Last then - return S (n + 1 .. S'Last); + if S (n) = Separator then + Skip := Skip + 1; + if Skip = Start and then n < S'Last then + return S (N + 1 .. S'Last); + end if; end if; end loop; return ""; diff --git a/agpl-strings-fields.ads b/agpl-strings-fields.ads index 4c065d9..6368c2a 100644 --- a/agpl-strings-fields.ads +++ b/agpl-strings-fields.ads @@ -70,19 +70,23 @@ package Agpl.Strings.Fields is -- String_Tail -- ------------------------------------------------------------------------ -- Returns "" if no @Separator@ found. + -- Starts counts the amount of separators to skip over function String_Tail (S : String; - Separator : Character := '/') + Separator : Character := '/'; + Start : Positive := 1) return String; function Tail (S : String; - Separator : Character := '/') + Separator : Character := '/'; + Start : Positive := 1) return String renames String_Tail; function T (S : String; - Separator : Character := '/') + Separator : Character := '/'; + Start : Positive := 1) return String renames String_Tail; ------------------------------------------------------------------------ diff --git a/agpl-strings.adb b/agpl-strings.adb index 47b3010..5b760fa 100644 --- a/agpl-strings.adb +++ b/agpl-strings.adb @@ -141,7 +141,7 @@ package body Agpl.Strings is Wnew : constant Natural := New_Pattern'Length - 1; I : Positive := S'First; begin - loop + while I + Wold <= S'Last loop if S (I .. I + Wold) = Pattern then Result (Pos .. Pos + Wnew) := New_Pattern; I := I + Pattern'Length; @@ -151,7 +151,6 @@ package body Agpl.Strings is I := I + 1; Pos := Pos + 1; end if; - exit when I + Wold > S'Last; end loop; Result (Pos .. Pos + S'Last - I) := S (I .. S'Last); diff --git a/agpl-url.adb b/agpl-url.adb index 6d15313..f44977b 100644 --- a/agpl-url.adb +++ b/agpl-url.adb @@ -1,36 +1,8 @@ ------------------------------------------------------------------------------- --- Ada Web Server -- --- -- --- Copyright (C) 2000-2004 -- --- ACT-Europe -- --- -- --- This library is free software; you can redistribute it and/or modify -- --- it under the terms of the GNU General Public License as published by -- --- the Free Software Foundation; either version 2 of the License, 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 -- --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- --- General Public License for more details. -- --- -- --- You should have received a copy of the GNU General Public License -- --- along with this library; if not, write to the Free Software Foundation, -- --- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- --- -- --- As a special exception, if other files instantiate generics from this -- --- unit, or you link this unit with other files to produce an executable, -- --- this unit does not by itself cause the resulting executable to be -- --- covered by the GNU General Public License. This exception does not -- --- however invalidate any other reasons why the executable file might be -- --- covered by the GNU Public License. -- ------------------------------------------------------------------------------- - --- $Id: aws-url.adb,v 1.37 2004/11/10 13:53:24 obry Exp $ - with Agpl.Conversions; +with Agpl.Ustrings; use Agpl.Ustrings; with Ada.Characters.Handling; +with Ada.Strings.Fixed; package body Agpl.URL is @@ -156,4 +128,54 @@ package body Agpl.URL is return Res (1 .. K); end Encode; + --------------- + -- Normalize -- + --------------- + + function Normalize (Url : in String) return String is + Url_Path : Ustring := +Url; + + K : Natural; + P : Natural; + + use Asu; + begin + -- Checks for current directory and removes all occurences + + -- Look for starting ./ + + if Length (URL_Path) >= 2 and then Slice (URL_Path, 1, 2) = "./" then + Delete (URL_Path, 1, 1); + end if; + + -- Look for all /./ references + + loop + K := Index (URL_Path, "/./"); + + exit when K = 0; + + Delete (URL_Path, K, K + 1); + end loop; + + -- Checks for parent directory + + loop + K := Index (URL_Path, "/../"); + + exit when K = 0; + + -- Look for previous directory, which should be removed + + P := Strings.Fixed.Index + (Slice (URL_Path, 1, K - 1), "/", Strings.Backward); + + exit when P = 0; + + Delete (URL_Path, P, K + 2); + end loop; + + return +URL_Path; + end Normalize; + end Agpl.URL; diff --git a/agpl-url.ads b/agpl-url.ads index 4617c29..6b86a67 100644 --- a/agpl-url.ads +++ b/agpl-url.ads @@ -1,32 +1,3 @@ ------------------------------------------------------------------------------- --- Ada Web Server -- --- -- --- Copyright (C) 2000-2004 -- --- ACT-Europe -- --- -- --- This library is free software; you can redistribute it and/or modify -- --- it under the terms of the GNU General Public License as published by -- --- the Free Software Foundation; either version 2 of the License, 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 -- --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- --- General Public License for more details. -- --- -- --- You should have received a copy of the GNU General Public License -- --- along with this library; if not, write to the Free Software Foundation, -- --- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- --- -- --- As a special exception, if other files instantiate generics from this -- --- unit, or you link this unit with other files to produce an executable, -- --- this unit does not by itself cause the resulting executable to be -- --- covered by the GNU General Public License. This exception does not -- --- however invalidate any other reasons why the executable file might be -- --- covered by the GNU Public License. -- ------------------------------------------------------------------------------- - --- $Id: aws-url.ads,v 1.18 2004/11/10 13:53:24 obry Exp $ with Ada.Strings.Maps; @@ -58,6 +29,9 @@ package Agpl.URL is Default_Encoding_Set : constant Strings.Maps.Character_Set; + function Normalize (URL : in String) return String; + -- Remove . and .. instances + function Encode (Str : in String; Encoding_Set : in Strings.Maps.Character_Set := Default_Encoding_Set) diff --git a/psql/agpl-db-psql.adb b/psql/agpl-db-psql.adb index 38bd093..7fa79bb 100644 --- a/psql/agpl-db-psql.adb +++ b/psql/agpl-db-psql.adb @@ -60,6 +60,17 @@ package body Agpl.Db.Psql is Log ("Agpl.Db.Psql.Query [Exception for query]: " & Q, Error); end Query; + ----------- + -- Query -- + ----------- + + function Query (Db : Database; Sql : String) return String is + R : Result; + begin + Query (R, Db, Sql); + return Value (R, 0, 0); + end Query; + ----------------------- -- Begin_Transaction -- ----------------------- diff --git a/psql/agpl-db-psql.ads b/psql/agpl-db-psql.ads index 81be153..e2a509d 100644 --- a/psql/agpl-db-psql.ads +++ b/psql/agpl-db-psql.ads @@ -51,6 +51,10 @@ package Agpl.Db.Psql is procedure Query (R : out Result; Db : Database; Q : String); -- Performs the query and checks for failure + function Query (Db : Database; Sql : String) return String; + -- Get the first column of the first row of a query. + -- Useful for count and the like + procedure Begin_Transaction (Db : Database); procedure Commit_Transaction (Db : Database);