❌ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Yesterday β€” 27 September 2023Newest questions tagged ada - Stack Overflow

Initialise type from address

I want to initialise a type from a System.address. For example :

generic
    type T_Generic is private;
package MyPackage is
    G_Addr : System.Address;
    procedure Register (myAddr : System.address);
    procedure MyProcedure (myAddr : System.address);
end MyPackage;

package body MyPackage is
    procedure Register(myAddr : System.address)
    is
        G_Addr := myAddr;
    end MyProcedure;

    procedure MyProcedure (myAddr : System.address)
        myVar : T_Generic with address => myAddr;
    is
        if (myAddr = G_Addr)
        then
            -- work with myVar 
        end if;
    end MyProcedure;
end MyPackage;
    

But I cannot compile this piece of code myVar : T_Generic with address => myAddr; :

aspect specification is an Ada 2012 feature

unit must be compiled with -gnat2012 switch

I cannot change compilation's options, how can I do ?

Before yesterdayNewest questions tagged ada - Stack Overflow

Declare function with undefined type

I want to create a library taking a kind of generic type (or something like this, I don't know how it is called).

What I have :

package A is
    type A_Type is record
        Field : Field_Type;
        A_Access : access A;
    end record;
end A;

package B is
    type B_Type is record
        OtherField : OtherField_Type;
        B_Access : access B;
    end record;
end B;

I want to create a common library taking A_Type or B_Type as parameters. My library cannot include A or B. It will be something like :

package CommonLib is
    procedure DoStuff (Param : Generic_Type);
end CommonLib;

-- Can be used by A :
A : A_Type;
CommonLib.DoStuff(A);

-- Can be used by B :
B : B_Type;
CommonLib.DoStuff(B);

How can I do ?

Import C function with void* parameter

I want to import a C function in Ada.

There is the C part :

void Register(const void *ctxt)
{
   saved_ctxt = ctxt; // This is a global variable 
}

void Send_Trace(const void *ctxt,
                const char *msg)
{
    if (saved_ctxt == ctxt)
    {
        // Do stuff with msg
    }
}

Now I want to use this function in a Ada program. There is my code :

type T_void is tagged null record;
package C renames Interfaces.C;

procedure Send_Trace_From_C(ctxt : in T_void;
                            msg  : in String)
is
    pragma Convention(C, T_void);

    procedure send_trace (A: out T_void; B : C.Strings.char_ptr);
    pragma import (C, Send_Trace, "Send_Trace");

    Char_ptr : C.Strings.char_ptr := C.Strings.New_String(msg);
begin
    send_trace (ctxt, Char_ptr);
end Send_Trace_From_C;

But I have errors :

pragma "convention" argument must be in same declarative part

warning "send_trace" involves a tagged type which does not correspond to any C type

How can I use a *void in Ada ?

String from integer8

I'm using a library in ada which contains many types :

type Int8 is range -8 ** 7 .. 2 ** 7 - 1;
subtype T_Name_String is Int8;
type T_Name_String_Fixed20 is array (range 1..20) of T_Name_String ;

And there is a record with :

type The_Record is record
   name : T_Name_String_Fixed20;
   -- and others
end record;

I can't change that, I don't know why there are using Int8 for ada strings but I'm not able to initiate the field name. I've try :

-- First try:
MyRecord.name = "hello               ";
-- Error : expected type T_Name_String_Fixed20 found a string type

-- Second try
Ada.Strings.Fixed.Move(Target => MyRecord.name;
                       Source => "hello               "

AUnit - Usefullness of the `Test_Caller` package

There seem to be two different ways to do the same thing when using the AUnit library:

Using the AUnit.Test_Cases.Test_Case type, creating some function tests, then register each tests with the AUnit.Test_Cases.Register_Tests function. We can then add this Test_Case to a Suite using the function AUnit.Test_Suites.Add_Test.

There some example of this:

(Surprisingly, there no examples using this way in the AUnit repository.)

The other way is to use the AUnit.Test_Fixtures.Test_Fixture type, creating some function tests. These tests can then be added to a Suite using the generic package AUnit.Test_Caller with the functions AUnit.Test_Suites.Add_Test and AUnit.Test_Caller.Create.

I can see way of using Test_Caller in:

The only difference I can see is that, when using the AUnit.Test_Cases.Test_Case, you can override the Set_Up_Case, Set_Up, Tear_Down and Tear_Down_Case functions. While with the AUnit.Test_Fixtures.Test_Fixture you can only override the Set_Up and Tear_Down functions (because the tests are not group under a Test_Case).

Appart from that, I don't really see much difference.

So, what is the use of the AUnit.Test_Fixtures.Test_Fixture type with the generic package AUnit.Test_Caller ? Why would use this over the (simpler ?) AUnit.Test_Cases.Test_Case type ?

Every example I have seen in one format can be transformed into the other (the Set_Up_Case and Tear_Down_Case set appart). Could give an example which use one but cannot be done by the other ?

How do I ensure a record's discriminant value is equal to another record component's value?

I have a File_Reader composed of two records, File and Buffer. I would like to ensure both Records always have a valid buffer size when initialized, i.e. Data_File.IO_Buffer_Size is equal to Data_In.Size.

I couldn't find a way of initializing a record component's value with another record's component value or discriminant, so I figured I would at least apply a static predicate which was unsuccessful. Using dynamic predicate as,an alternative poses issues in a LightRuntime environment.

I could easily add a Buffer_Size discriminant to File_Reader, but I would like to explore alternative solutions. Lastly, the record layouts must be preserved since they are memory mapped using representation clauses which are not shown:

type File is 
   record
      Name           : String;
      IO_Buffer_Size : Buffer_Size;
   end record; 

type Buffer(Size : Buffer_Size := 300)  is         
   record
      Pos   : Buffer_Size := 0;
      Value : String(1 .. Size);
   end record;

type File_Reader(Name : String) is 
   record        
       Data_In      : Buffer;    
       Data_File    : File := (Name, Data_In.Size); -- Won't work
   end record;

object "Antal_rader" cannot be used before end of its declaration

OBS! My code is written with Swedish terms.

I have to code a VAT table but i have run in to some problems concerning Floats and the way that it rounds values. At first, I was using a while loop but that didn't work since it (after a while) got too affected by the float-issue.

I created a For-loop instead which in theory should work better. However. I'm having an issue with one of my variables.

The variable "Antal_Rader" is being declared before the For-loop but i still get the error code: "object "Antal_rader" cannot be used before end of its declaration"

Code:

with Ada.Text_IO;              use Ada.Text_IO;
with Ada.Float_Text_IO;        use Ada.Float_Text_IO;

procedure Momstabellen is
   Forsta_Pris : Float;
   Sista_Pris : Float;
   Steglangd : Float;
   Momsprocent : Float;
   Moms : Float;
   Antal_Rader : Float;
   Nuvarande_Rad : Float;
   
   
begin
   -- 1. Ta emot alla variabler fΓΆr momstabellen:
   Put("FΓΆrsta pris: ");
   Get(Forsta_Pris);
   while Forsta_Pris < 0.0 loop
      Put("Felaktigt vΓ€rde!");
      New_Line;
      Put("FΓΆrsta pris: ");
      Get(Forsta_Pris);
    end loop;
   
   Put("Sista pris: ");
   Get(Sista_Pris);
   while Sista_Pris < Forsta_Pris loop
      Put("Felaktigt vΓ€rde!");
      New_Line;
      Put("Sista pris: ");
      Get(Sista_Pris);
   end loop;
   
   Put("Steg: ");
   Get(Steglangd);
   while Steglangd < 0.0 loop
      Put("Felaktigt vΓ€rde!");
      New_Line;
      Put("Steg: ");
      Get(Steglangd);
   end loop;
   
   Put("Momsprocent: ");
   Get(Momsprocent);
   while Momsprocent < 0.0 or Momsprocent > 100.0 loop
      Put("Felaktigt vΓ€rde!");
      New_Line;
      Put("Momsprocent: ");
      Get(Momsprocent);
   end loop;
   New_Line;

-- 2. Skapa en layout fΓΆr momstabellen:
   Put("============ Momstabell ============");
   New_Line;
   Put("Pris utan moms  Moms   Pris med moms");
   New_Line;

Issue lies here:

   

   Antal_Rader := (Sista_Pris - Forsta_Pris)/Steglangd;
   
   for I in 0 .. Antal_Rader loop
      
      Nuvarande_Rad := Antal_Rader * Steglangd + Forsta_Pris;   
      Moms := Nuvarande_Rad/100.0 * Momsprocent;
      
      Put(Nuvarande_Rad, Fore=> 6, Aft => 2, Exp => 0);
      Put(Moms, Fore => 8, Aft => 2, Exp => 0);
      Put(Nuvarande_Rad + Moms, Fore => 9, Aft => 2, Exp => 0);
     
   end loop;
    
     
end Momstabellen;

I have tried changing the first line of the For-loop and also tried changing the position of "Antal_Rader" but nothing has worked.

I am quite new to this so i can't think of many more things to do.

Ada Alire GNATColl_Postgres Installation failed

I am trying to get gnatcoll_postgres into C:\GNColl_Postgresql using Alire on Windows 10. But the process failed while installing mingw-w64-x86_64-postgresql.

The errors message are as follows:

error: mingw-w64-x86_64-mpc: signature from "David Macek <[email protected]>" is unknown trust
:: File /var/cache/pacman/pkg/mingw-w64-x86_64-mpc-1.2.1-1-any.pkg.tar.zst is corrupted (invalid or corrupted package (PGP signature)).
Do you want to delete it? [Y/n]
error: mingw-w64-x86_64-bzip2: signature from "David Macek <[email protected]>" is unknown trust
:: File /var/cache/pacman/pkg/mingw-w64-x86_64-bzip2-1.0.8-2-any.pkg.tar.zst is corrupted (invalid or corrupted package (PGP signature)).
Do you want to delete it? [Y/n]
error: mingw-w64-x86_64-termcap: signature from "David Macek <[email protected]>" is unknown trust
:: File /var/cache/pacman/pkg/mingw-w64-x86_64-termcap-1.3.1-6-any.pkg.tar.zst is corrupted (invalid or corrupted package (PGP signature)).
Do you want to delete it? [Y/n]
error: failed to commit transaction (invalid or corrupted package)
Errors occurred, no packages were upgraded.
ERROR: Deployment of system package from platform software manager: mingw-w64-x86_64-postgresql to C:\GNColl_Postgresql\gnatcoll_postgres_23.0.0_adf8e40f\alire\cache\dependencies\postgresql_15.1.0_system failed

Any idea on how to resolve this issue. Thanks in advance. Regards. Angelo.

find image tags with alt attribute without double quotes and replace with empty double quotes

<img src="first.jpg" alt="first" width="500" height="600" />
<img src="second.jpg" alt width="500" height="600" />
<img src="third.jpg" alt="third" width="500" height="600" />
<img src="fourth.jpg" alt width="500" height="600" />
<img src="fifth.jpg" alt="" width="500" height="600" />

find image tags with alt attribute without double quotes and replace with empty double quotes for ADA accessibility, I need to jquery to find second and fourth img tags replace with alt="" just like fifth. This is sample html markup, a page can have this instances 10 or 20 instances, all these should be replaced with alt=""

I tried this below didn't work

$(function() {
  $("img").each(function() {
    if (this.alt) {
      console.log(this.alt);
    } else {
      $(this).attr('alt', "");
    }
  });
});

How to add Headers in AWS (ada web server)?

I have been trying to add Headers (specifically for CORS) to my server response for hours now, but I cant find a way in ada with AWS. Can someone help me? ( So far I have found only ways to add the Headers to the Client, but not as a Server Response)

Please ignore that the code is a bit messy, this is my first real project in Ada and I am just trying things out.

with AWS.Client;
with AWS.Response;
with AWS.Server;
with AWS.Server.Status;
with AWS.Status;
with Ada.Text_IO;
with Ada.Strings.Unbounded;
with AWS.Headers;

procedure aws_test is:

   WS      : AWS.Server.HTTP;
   Headers : AWS.Headers.List;

   function HW_CB (Request : in AWS.Status.Data) return AWS.Response.Data is
      URI : constant String := AWS.Status.URI (Request);

      Test_Data : AWS.Response.Data :=
        AWS.Client.Get (URL => "http://192.168.190.129");

      Test_String   : constant String := AWS.Response.Message_Body (Test_Data);
      para          : Ada.Strings.Unbounded.Unbounded_String;
      test          : Integer         := 1;
      response_data : AWS.Response.Data;

    begin

      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Allow-Origin",
         Value => "*");
      -- Allow common methods
      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Allow-Methods",
         Value => "POST, GET, OPTIONS, PUT, DELETE");

      -- Allow all headers requested in the actual request
      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Allow-Headers",
         Value => "*");

      -- Set max age to 86400 seconds (24 hours)
      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Max-Age", Value => "86400");

      if URI = "/api/login" then
         -- ADD HERE
         test := test + 1;
         para := AWS.Status.Binary_Data (Request);

         Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.To_String (para));
         -- Ada.Text_IO.Put_Line (Test_String);

         response_data := AWS.Response.Build ("text/html", "<p>Hello world !");
         AWS.Response.Set.Add_Header
           (response_data, Name => "Access-Control-Max-Age", Value => "86400");
         return response_data;
      else
         return AWS.Response.Build ("text/html", "<p>Hum...");
      end if;
    end HW_CB;

    begin
     AWS.Server.Start
      (WS, "Hotel AI", Callback => HW_CB'Unrestricted_Access, Port => 8_080);
     delay 20.0;
     AWS.Server.Shutdown (WS);
     while AWS.Server.Status.Is_Shutdown (WS) = False loop
      delay 5.0;
     end loop;
    end aws_test;

I just would like to add Headers so I dont get CORS flagged in Javascript. Thank you very much for your Help ( I am a bit struggling to find good resources on these topics in the web )

I have already tried several possible ways (as well with the help of AI etc.), bu tI couldn't even really find good examples on Github or in the documentation

How do I read a binary file in BigEndian order to a record?

I have a binary file format that is written in BigEndian order. The files are of varying size so I can't use Sequential_IO for this, as I need to read different types.

The problem is, when using Stream_IO, I can't find a way to use BigEndian, and Scalar_Storage_Order also doesn't affect anything. Also, I'm a very fresh beginner at Ada and overall tips and suggestions to the code are very welcome.

share.adb:
with Ada.Text_IO;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Ada.Streams;           use Ada.Streams;

package body Share is

   procedure Read_Share (Segment_Size : Positive; Required_Shares : Positive)
   is
      --  Ceiling function
      Block_Size : constant Positive :=
        (Segment_Size + (Required_Shares - 1)) / Required_Shares;
      type Block is array (Integer range 0 .. Block_Size) of Byte;

      S               : Stream_Access;
      Share_File      : File_Type;
      My_Share_Header : Share_Header;
   begin
      Open (Share_File, In_File, "../go-tahoe/3");
      S := Stream (Share_File);
      Share_Header'Read (S, My_Share_Header);
      My_Share_Header.Block_Size := Unsigned_32 (Block_Size);
      Display_Share_Content (My_Share_Header);

      Close (Share_File);
      --  Read_Blocks (My_Share_Header, Share_File);

      --  Now My_Share contains the values read from the binary file
   end Read_Share;

   procedure Display_Share_Content (My_Share_Header : Share_Header) is
   begin
      Ada.Text_IO.Put_Line
        ("Share version: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Version));
      Ada.Text_IO.Put_Line
        ("Share Data Length: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Data_Length));
      Ada.Text_IO.Put_Line
        ("Lease Number: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Lease_number));
      Ada.Text_IO.Put_Line
        ("Share version: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Version));
      Ada.Text_IO.Put_Line
        ("Block Size: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Block_Size));
      Ada.Text_IO.Put_Line
        ("Data Size: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Data_Size));
      Ada.Text_IO.Put_Line
        ("Data offset: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Data_Offset));
      Ada.Text_IO.Put_Line
        ("Plaintext hash tree offset: " &
         Interfaces.Unsigned_32'Image
           (My_Share_Header.Plaintext_Hash_Tree_Offset));
      Ada.Text_IO.Put_Line
        ("Crypttext hash tree offset: " &
         Interfaces.Unsigned_32'Image
           (My_Share_Header.Crypttext_Hash_Tree_Offset));
      Ada.Text_IO.Put_Line
        ("Block hashes offset: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Block_Hashes_Offset));
      Ada.Text_IO.Put_Line
        ("Share hashes offset: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.Share_Hashes_Offset));
      Ada.Text_IO.Put_Line
        ("URI Extension Length and URI Extension block offset: " &
         Interfaces.Unsigned_32'Image (My_Share_Header.URI_Extension_Offset));
   end Display_Share_Content;

   procedure Read_Blocks
     (My_Share_Header : Share_Header; Share_File : File_Type)
   is
      Total_Blocks : Interfaces.Unsigned_32 := My_Share_Header.Data_Size;
   begin
      Ada.Text_IO.Put ("");

   end Read_Blocks;
end Share;

share.ads:
with Interfaces; use Interfaces;
with System;     use System;
with Ada.Streams.Stream_IO;

package Share is

   type Byte is new Interfaces.Unsigned_8;
   type Kilobyte is array (Integer range 0 .. 1_023) of Byte;
   type Kilobyte_array is array (Integer range <>) of Kilobyte;

   type Share_Header is record
      Version                    : Unsigned_32;
      Data_Length                : Unsigned_32;
      Lease_number               : Unsigned_32;
      Version_Junk               : Unsigned_32;
      --  unused as it can be calculated from the URI
      Block_Size                 : Unsigned_32;
      Data_Size                  : Unsigned_32;
      Data_Offset                : Unsigned_32;
      Plaintext_Hash_Tree_Offset : Unsigned_32;
      Crypttext_Hash_Tree_Offset : Unsigned_32;
      Block_Hashes_Offset        : Unsigned_32;
      Share_Hashes_Offset        : Unsigned_32;
      URI_Extension_Offset       : Unsigned_32;
   end record;

   for Share_Header use record
      Version at 0 range 0 .. 32;
      --  Data_Length                : Unsigned_32;
      --  Lease_number               : Unsigned_32;
      --  Version_Junk               : Unsigned_32;
      --  --  unused as it can be calculated from the URI
      --  Block_Size                 : Unsigned_32;
      --  Data_Size                  : Unsigned_32;
      --  Data_Offset                : Unsigned_32;
      --  Plaintext_Hash_Tree_Offset : Unsigned_32;
      --  Crypttext_Hash_Tree_Offset : Unsigned_32;
      --  Block_Hashes_Offset        : Unsigned_32;
      --  Share_Hashes_Offset        : Unsigned_32;
      --  URI_Extension_Offset       : Unsigned_32;
   end record;

   for Share_Header'Bit_Order use High_Order_First;
   for Share_Header'Scalar_Storage_Order use High_Order_First;

   procedure Read_Share (Segment_Size : Positive; Required_Shares : Positive);
   procedure Display_Share_Content (My_Share_Header : Share_Header);
   procedure Read_Blocks
     (My_Share_Header : Share_Header;
      Share_File      : Ada.Streams.Stream_IO.File_Type);
end Share;

Tried defining the component clauses to not much success, different bit orders, modifying the Stream_IO storage arrays.

Ada scale exceeds maximum value of 18

Dear Ada Enthusiasts,

type Long_Money_Type is delta 10.0**(-22) digits 38;

On The First Machine Ubuntu Linux 64 bit GNAT 8.3.0 this works.
On The Second Machine Alpine Linux 64 Bit GNAT 10.3.1 20211027 i get this error:

adx-lib-money.ads:14:29: scale exceeds maximum value of 18
adx-lib-money.ads:14:54: digits value out of range, maximum is 18

Is There an way to change the maximum value ?

Unexpected Ada Type Compatibility

Why are Volts, Amps, and Ohms compatible?


with ada.text_io; use ada.text_io;
                                                                    
procedure main is                                                   
    type Volts is delta 1.0 / 2.0 ** 12 range -45_000.0 .. 45_000.0;
    type Amps is delta 1.0 / 2.0 ** 16 range -1_000.0 .. 1_000.0;   
    type Ohms is delta 0.125 range 0.0 .. 1.0E8;                    
                                                                    
    V : Volts := 1.0;                                               
    A : Amps := 1.0;                                                
    R1 : Ohms := 1.0;                                               
    R2 : Ohms := 1.0;                                               
                                                                   
begin                                             
                     
    v := A * (R1 + R2);
                       
    put_line(V'Img);   
                       
end main;  

If the types the types are defined as new Float I get the following exception during compilation:


main.adb:22:12: error: invalid operand types for operator "*"
main.adb:22:12: error: left operand has type "Amps" defined at line 5
main.adb:22:12: error: right operand has type "Ohms" defined at line 6

I expected the use of type with Volts to define a new type that was incompatible with the other types as it wasn't an explicit subtype of the fixed point type.

What is the difference between `long_integer` and `integer` in ada language?

with ada.text_io;

procedure numbers is 

   int1 : integer := 2147483647; --almost 4 bytes
   int2 : integer := -2147483647;

   lng1 : long_integer := 2147483647;
   lng2 : long_integer := -2147483647;

begin 

   ada.text_io.put_line(int1'image);
   ada.text_io.put_line(int2'image);

   ada.text_io.put_line(lng1'image);
   ada.text_io.put_line(lng2'image);

end numbers;

result :

 2147483647
-2147483647
 2147483647
-2147483647

I am new to this programming language and suddenly found this long_integer and integer datatypes in the ada language are the same in size and the type of data they can represent is there any key difference between these two datatypes ?

Gtkada widget from scratch

I don’t have a lot of hope for this but anyway.

I need to create a widget to select a range (like a scale widget but with two cursors, one for the lower bound and another for the upper bound). This needs to be built "from scratch". According to the Gtkada documentation, I just have to take a look at the gtkdial example (gtkdial seems to be the classic example for gtk to build widget from scratch).

The example is clearly out of date, a lot of functions, packages and events don’t exist anymore.

For example Gtk.Widget.Realize_Handling is gone (instead we have on_Realize I think), the function Get_Count doesn’t exist as the event "size_request"… I tried to do it on my own by reading and understanding the source code, but I’ m still a beginner with GTK.

Do you know if an up-to-date version of this example exists?

What are the semantics of Ada task priorities on Linux?

Background: I'm working on porting an Ada project from Vxworks to a Linux platform. The project heavily relies on usage of Ada's dynamic task priorities.

I've done a couple experiments which have left me confused. Ada task priorities seem to have no effect on the underlying Linux priority/niceness. Two identical tasks with different priorities take equally long to complete, even when pinned to one CPU core.

Question: What are the semantics of Ada tasks on Linux? Why am I seeing no effect from setting task priorities?

❌
❌