Skip to content

Commit

Permalink
M agpl-xml.adb
Browse files Browse the repository at this point in the history
M    agpl-xml.ads
M    dom-core-nodes-output.adb
A    agpl-ustrings.ads
A    agpl-containers-string_vectors.ads
M    dom-core-nodes-output.ads
A    agpl-url.adb
A    agpl-url.ads
A    agpl-strings-utf8.adb
M    agpl-filesystem.adb
A    agpl-strings-utf8.ads
M    agpl-filesystem.ads
  • Loading branch information
mosteo committed Jan 12, 2007
1 parent 08e7d76 commit 0b93903
Show file tree
Hide file tree
Showing 12 changed files with 533 additions and 18 deletions.
33 changes: 33 additions & 0 deletions agpl-containers-string_vectors.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
------------------------------------------------------------------------------
-- AGPL --
-- --
-- Copyright (C) 2003 --
-- A. Mosteo. --
-- --
-- Authors: A. Mosteo. (public@mosteo.com) --
-- --
-- If you have any questions in regard to this software, please address --
-- them to the above email. --
-- --
-- This program 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 program 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. --
------------------------------------------------------------------------------

with Ada.Containers.Indefinite_Vectors;

package Agpl.Containers.String_Vectors is new
Ada.Containers.Indefinite_Vectors (Positive, String);

pragma Preelaborate (Agpl.Containers.String_Vectors);

35 changes: 35 additions & 0 deletions agpl-filesystem.adb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

with Agpl.Strings.Fields;

with Ada.Streams.Stream_Io;

package body Agpl.Filesystem is

------------------
Expand Down Expand Up @@ -61,4 +63,37 @@ package body Agpl.Filesystem is
return Strings.Fields.String_Tail_Reverse (This, '.') & '.' & New_Ext;
end Replace_Extension;

---------------
-- Read_File --
---------------

function Read_File (Name : String) return Ustring is
Result : Ustring;
use Ada.Streams.Stream_Io;
F : File_Type;
begin
Open (F, In_File, Name);
declare
Length : constant Natural := Natural (Size (F));
Remain : Natural := Length;
Stream : constant Stream_Access := Ada.Streams.Stream_Io.Stream (F);
begin
while Remain > 0 loop
declare
Chunk : String (1 .. Natural'Min (Remain, 1000));
begin
String'Read (Stream, Chunk);
Asu.Append (Result, Chunk);
Remain := Remain - Chunk'Length;
end;
end loop;
end;
Close (F);
return Result;
exception
when others =>
Close (F);
raise;
end Read_File;

end Agpl.Filesystem;
7 changes: 6 additions & 1 deletion agpl-filesystem.ads
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
------------------------------------------------------------------------------
-- $Id: agpl.ads,v 1.4 2004/01/21 21:05:25 Jano Exp $

with Agpl.Types.Ustrings; use Agpl.Types.Ustrings;

package Agpl.Filesystem is

pragma Preelaborate;
-- pragma Preelaborate;

-- Returns the same string with a Folder_Separator added if it is missing.
function Ensure_Slash (This : in String; Separator : in Character := '/')
Expand All @@ -45,4 +47,7 @@ package Agpl.Filesystem is
return String;
-- Replace the extension by a new one

function Read_File (Name : String) return Ustring;
-- Read a full file as a string!

end Agpl.Filesystem;
46 changes: 46 additions & 0 deletions agpl-strings-utf8.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
with Agpl.Types.Ustrings; use Agpl.Types.Ustrings;

package body Agpl.Strings.Utf8 is

-------------------
-- Extract_Words --
-------------------

function Extract_Words (Str : String) return Containers.String_Vectors.Vector
is
Words : Containers.String_Vectors.Vector;
Str_Idx : Natural := Str'First;
Word : Ustring;
use Asu;
begin
while Str_Idx <= Str'Last loop
declare
Char : Unicode.Unicode_Char;
begin
Unicode.Ces.Utf8.Read (Str, Str_Idx, Char);

if Unicode.Is_Letter (Char) or else Unicode.Is_Digit (Char) then
declare
Code : String (1 .. 20);
Last : Natural := Code'First;
begin
Unicode.Ces.Utf8.Encode (Char, Code, Last);
Append (Word, Code (Code'First + 1 .. Last));
end;
end if;

if Str_Idx > Str'Last or else
not (Unicode.Is_Letter (Char) or else Unicode.Is_Digit (Char))
then
if Word /= Null_Ustring then
Words.Append (+Word);
Word := Null_Ustring;
end if;
end if;
end;
end loop;

return Words;
end Extract_Words;

end Agpl.Strings.Utf8;
46 changes: 46 additions & 0 deletions agpl-strings-utf8.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
------------------------------------------------------------------------------
-- ADAGIO - ADALID - AENEA. --
-- --
-- Copyright (C) 2003 --
-- A. Mosteo. --
-- --
-- Authors: A. Mosteo. (adagio@mosteo.com) --
-- --
-- If you have any questions in regard to this software, please address --
-- them to the above email. --
-- --
-- This program 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 program 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. --
-- --
-- You are not allowed to use any part of this code to develop a program --
-- whose output would be used to harass or prosecute other users of the --
-- networks Adagio connects with. All data collected with Adagio or a tool --
-- containing Adagio code about other network users must remain --
-- confidential and cannot be made public by any mean, nor be used to --
-- harass or legally prosecute these users. --
------------------------------------------------------------------------------
-- $Id: agpl-strings.ads,v 1.3 2004/02/03 22:52:09 Jano Exp $

with Agpl.Containers.String_Vectors;

with Unicode.Ces.Utf8;

package Agpl.Strings.Utf8 is

subtype Utf8_String is Unicode.Ces.Utf8.Utf8_String;

function Extract_Words (Str : String) return Containers.String_Vectors.Vector;
-- Will split the string using non-letters and non-numbers as delimiters

end Agpl.Strings.Utf8;
159 changes: 159 additions & 0 deletions agpl-url.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
------------------------------------------------------------------------------
-- 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 Ada.Characters.Handling;

package body Agpl.URL is

subtype Escape_Code is String (1 .. 2);

Not_Escaped : constant Escape_Code := " ";

function Code (C : in Character) return Escape_Code;
pragma Inline (Code);
-- Returns hexadecimal code for character C

subtype ASCII_7 is Character range Character'First .. Character'Val (127);
type ASCII_7_Set is array (ASCII_7) of Escape_Code;

function Build_Hex_Escape return ASCII_7_Set;
-- Returns the table with pre-computed encoding for 7bits characters

----------------------
-- Build_Hex_Escape --
----------------------

function Build_Hex_Escape return ASCII_7_Set is
Result : ASCII_7_Set;
begin
for C in Character'Val (0) .. Character'Val (127) loop
if Strings.Maps.Is_In (C, Default_Encoding_Set) then
Result (C) := Code (C);
else
Result (C) := Not_Escaped;
end if;
end loop;
return Result;
end Build_Hex_Escape;

----------
-- Code --
----------

function Code (C : in Character) return Escape_Code is
begin
return Conversions.To_Hex (Character'Pos (C));
end Code;

Hex_Escape : constant ASCII_7_Set := Build_Hex_Escape;
-- Limit Hex_Escape to 7bits ASCII characters only. Other ISO-8859-1 are
-- handled separately in Encode function. Space character is not processed
-- specifically, contrary to what is done in AWS.URL.

------------
-- Decode --
------------

function Decode (Str : in String) return String is
Res : String (1 .. Str'Length);
K : Natural := 0;
I : Positive := Str'First;
begin
if Str = "" then
return "";
end if;

loop
K := K + 1;

if Str (I) = '%'
and then I + 2 <= Str'Last
and then Characters.Handling.Is_Hexadecimal_Digit (Str (I + 1))
and then Characters.Handling.Is_Hexadecimal_Digit (Str (I + 2))
then
Res (K) := Conversions.From_Hex (Str (I + 1 .. I + 2));
I := I + 2;

elsif Str (I) = '+' then
Res (K) := ' ';

else
Res (K) := Str (I);
end if;

I := I + 1;
exit when I > Str'Last;
end loop;

return Res (1 .. K);
end Decode;

------------
-- Encode --
------------

function Encode
(Str : in String;
Encoding_Set : in Strings.Maps.Character_Set := Default_Encoding_Set)
return String
is
C_128 : constant Character := Character'Val (128);
Res : String (1 .. Str'Length * 3);
K : Natural := 0;
begin
for I in Str'Range loop
if Strings.Maps.Is_In (Str (I), Encoding_Set) then
-- This character must be encoded

K := K + 1;
Res (K) := '%';
K := K + 1;

if Str (I) < C_128 then
-- We keep a table for characters lower than 128 for efficiency
Res (K .. K + 1) := Hex_Escape (Str (I));
else
Res (K .. K + 1) := Code (Str (I));
end if;

K := K + 1;

else
K := K + 1;
Res (K) := Str (I);
end if;
end loop;

return Res (1 .. K);
end Encode;

end Agpl.URL;

0 comments on commit 0b93903

Please sign in to comment.