Rosetta Code/Count examples: Difference between revisions

Content added Content deleted
m (omits for Brainfuck, batch files and PostScript)
(Added Ada example /currently 17158 exemples.)
Line 10: Line 10:


Total: X examples.</lang>
Total: X examples.</lang>
=={{header|Ada}}==
<lang Ada>with AWS.Response; use AWS.Response;
with AWS.Resources; use AWS.Resources;
with AWS.Messages; use AWS.Messages;
with AWS.URL;
with DOM.Readers; use DOM.Readers;
with DOM.Core; use DOM.Core;
with DOM.Core.Documents; use DOM.Core.Documents;
with DOM.Core.Nodes; use DOM.Core.Nodes;
with DOM.Core.Attrs; use DOM.Core.Attrs;
with AWS.Client; use AWS.Client;
with Input_Sources.Strings; use Input_Sources.Strings;
with Unicode;
with Unicode.CES.Utf8;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO, Ada.Strings.Fixed;
with Ada.Containers.Doubly_Linked_Lists;
use Ada, AWS;

procedure Count_examples is

type Exemple_Data is record
Title : Unbounded_String := Null_Unbounded_String;
end record;

package Exemple_List is new Containers.Doubly_Linked_Lists (Exemple_Data);
use Exemple_List;

Exemples : Exemple_List.List;
Cursor : Exemple_List.Cursor;
Exemple : Exemple_Data;
Nbr_Lg, Total : Natural := 0;

procedure Get_List is
Reader : Tree_Reader;
Doc : Document;
List : Node_List;
N : Node;
A : Attr;
Page : AWS.Response.Data;
URI_XML : constant String :=
"http://rosettacode.org/mw/api.php?action=query&list=categorymembers&"
&
"cmtitle=Category:Programming_Tasks&cmlimit=600&format=xml";
begin
Page := AWS.Client.Get (URI_XML);
if Response.Status_Code (Page) not in Success then
raise Connection_Error;
end if;
declare
XML : constant String := Message_Body (Page);
Source : String_Input;
begin
Open
(XML'Unrestricted_Access,
Unicode.CES.Utf8.Utf8_Encoding,
Source);
Parse (Reader, Source);
Close (Source);
end;
Doc := Get_Tree (Reader);
List := Get_Elements_By_Tag_Name (Doc, "cm");
for Index in 1 .. Length (List) loop
N := Item (List, Index - 1);
A := Get_Named_Item (Attributes (N), "title");
Exemple_List.Append
(Exemples,
(Title => To_Unbounded_String (Value (A))));
end loop;
Free (List);
Free (Reader);
end Get_List;

function Scan_Page (Title : String) return Natural is
Page : AWS.Response.Data;
File : AWS.Resources.File_Type;
Buffer : String (1 .. 1024);
Languages, Position, Last : Natural := 0;
begin
Page :=
Client.Get
("http://rosettacode.org/mw/index.php?title=" &
AWS.URL.Encode (Title) &
"&action=raw");
Response.Message_Body (Page, File);
while not End_Of_File (File) loop
Resources.Get_Line (File, Buffer, Last);
Position :=
Strings.Fixed.Index
(Source => Buffer (Buffer'First .. Last),
Pattern => "=={{header|");
if Position > 0 then
Languages := Languages + 1;
end if;
end loop;
Close (File);
return Languages;
end Scan_Page;

begin
Get_List;

Cursor := First (Exemples);
while Has_Element (Cursor) loop
Exemple := Exemple_List.Element (Cursor);
Nbr_Lg := Scan_Page (To_String (Exemple.Title));
Total := Total + Nbr_Lg;
Text_IO.Put_Line
(To_String (Exemple.Title) &
" :" &
Integer'Image (Nbr_Lg) &
" exemples.");
Next (Cursor);
end loop;
Text_IO.Put_Line ("Total :" & Integer'Image (Total) & " exemples.");
end Count_examples;
</lang>
<lang>$>count_examples
100 doors : 107 exemples.
24 game : 29 exemples.
24 game/Solve : 20 exemples.
99 Bottles of Beer : 124 exemples.
.....
Yahoo! search interface : 10 exemples.
Zig-zag matrix : 49 exemples.
Total : 17158 exemples.
</lang>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>UrlDownloadToFile
<lang AutoHotkey>UrlDownloadToFile