comp.lang.ada
 help / color / mirror / Atom feed
* Help with type definition
@ 2014-05-16  7:37 hanslad
  2014-05-16  7:54 ` Dmitry A. Kazakov
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: hanslad @ 2014-05-16  7:37 UTC (permalink / raw)


Hello,
I need an advise on how to define a type in my project.
I want to implement a network protocol with the following definition on the type "string":

"All String values are encoded as a sequence of UTF8 characters without a null terminator and preceded by the length in bytes.
The length in bytes is encoded as Int32. A value of -1 is used to indicate a 'null' string."

The string is used in different datastructures send on network eg. like the one implemented in "Node" type below.
Am I on the right track here? 

Here is my code:
Ads:

with Interfaces;
with Ada.Finalization; use Ada.Finalization;
package Types is

   type String_Type is new Ada.Finalization.Controlled with private;
   
   type IdentifierType is
     (
      IdentifierType_Numeric,
      IdentifierType_String
     );
   for IdentifierType use
     (IdentifierType_Numeric => 1,
      IdentifierType_String  => 2);
   for IdentifierType'Size use 8;


   type Node (IdType  : IdentifierType := IdentifierType_Numeric )is record
      NamespaceIndex : Interfaces.Unsigned_16  := 0;
      case IdType is
      when IdentifierType_Numeric =>
         Numeric    : Interfaces.Unsigned_32 ;
      when IdentifierType_String =>
         String     : String_Type;
      end case;
   end record;

private
   overriding procedure Initialize (This: in out String_Type);
   overriding procedure Adjust (This: in out String_Type);
   overriding procedure Finalize (This : in out String_Type);

   type String_Type_Implementation(Count : Natural) is record
      Data : String(1 .. Count) := "";
   end record;

   type String_Type_Implementation_Ptr is access String_Type_Implementation;

   type String_Type is new Ada.Finalization.Controlled with record
      Length         : Integer;
      Implementation : String_Type_Implementation_Ptr;
   end record;
end Types;

Adb:
with Ada.Unchecked_Deallocation;
package body Types is 
   
    procedure Free is
     new Ada.Unchecked_Deallocation (String_Type_Implementation, String_Type_Implementation_Ptr);
   
   procedure Finalize (This : in out String_Type) is
   begin
      if(This.Implementation /= null) then
         Free (This.Implementation);
      end if;
   end Finalize;

   overriding procedure Adjust (This : in out String_Type) is
   begin
      This.Implementation :=
        new String_Type_Implementation'(This.Implementation.all);
   end Adjust;

   overriding procedure Initialize (This: in out String_Type) is
   begin
      This.Length := -1;
      This.Implementation := new String_Type_Implementation(0);
   end Initialize;
end Types;


Thanks,

Hans 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:37 Help with type definition hanslad
@ 2014-05-16  7:54 ` Dmitry A. Kazakov
  2014-05-16 10:03   ` G.B.
                     ` (2 more replies)
  2014-05-16 11:29 ` Jacob Sparre Andersen
                   ` (4 subsequent siblings)
  5 siblings, 3 replies; 13+ messages in thread
From: Dmitry A. Kazakov @ 2014-05-16  7:54 UTC (permalink / raw)


On Fri, 16 May 2014 00:37:25 -0700 (PDT), hanslad@gmail.com wrote:

> I need an advise on how to define a type in my project.
> I want to implement a network protocol with the following definition on the type "string":
> 
> "All String values are encoded as a sequence of UTF8 characters without a
> null terminator and preceded by the length in bytes.
> The length in bytes is encoded as Int32. A value of -1 is used to indicate
> a 'null' string."

That is not definition of a type. It is definition of encoding. Though it
does not define the way the string length is encoded. Why -1 should
indicate null string and not 0 is a mystery.

[Usually, unless the network transport has fixed-size blocks (real-time
protocols do), for a stream-oriented transport, it is better to encode
counts using chained codes. That reduces space required for lesser numbers.
E.g. UTF-8 uses chained codes to encode character code points.]

> The string is used in different datastructures send on network eg. like
> the one implemented in "Node" type below.
> Am I on the right track here? 

No. Internal representation and types have nothing to do with the protocol,
except for encoding and decoding stages (presentation layer). You said, it
is a string, let it be String, Wide_Wide_String, Unbounded_String.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:54 ` Dmitry A. Kazakov
@ 2014-05-16 10:03   ` G.B.
  2014-05-16 11:26   ` Jacob Sparre Andersen
  2014-05-16 15:38   ` Shark8
  2 siblings, 0 replies; 13+ messages in thread
From: G.B. @ 2014-05-16 10:03 UTC (permalink / raw)


On 16.05.14 09:54, Dmitry A. Kazakov wrote:
> Why -1 should
> indicate null string and not 0 is a mystery.

Very likely because of s[0] being a char at offset 0 at
the C end of things, but -1 being suitably off there?



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:54 ` Dmitry A. Kazakov
  2014-05-16 10:03   ` G.B.
@ 2014-05-16 11:26   ` Jacob Sparre Andersen
  2014-05-16 15:38   ` Shark8
  2 siblings, 0 replies; 13+ messages in thread
From: Jacob Sparre Andersen @ 2014-05-16 11:26 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> Why -1 should indicate null string and not 0 is a mystery.

Possibly because it is considered relevant to distinguish between "no
string" and "an empty string".

Greetings,

Jacob
-- 
"The pictures on radio are always so much better than those on TV."
                                       -- Pratchet, Stewart & Cohen


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:37 Help with type definition hanslad
  2014-05-16  7:54 ` Dmitry A. Kazakov
@ 2014-05-16 11:29 ` Jacob Sparre Andersen
  2014-05-16 12:29 ` G.B.
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Jacob Sparre Andersen @ 2014-05-16 11:29 UTC (permalink / raw)


Hans <hanslad@gmail.com> wrote:

> "All String values are encoded as a sequence of UTF8 characters
> without a null terminator and preceded by the length in bytes.  The
> length in bytes is encoded as Int32. A value of -1 is used to indicate
> a 'null' string."

What is the byte order of the "Int32", which I assume is a 32 bit signed
integer?

Your packages seem to do lots of irrelevant things, and not much which
is relevant.

Greetings,

Jacob
-- 
Trusted third party: Someone whom you know can violate your
                     security policy without getting caught.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:37 Help with type definition hanslad
  2014-05-16  7:54 ` Dmitry A. Kazakov
  2014-05-16 11:29 ` Jacob Sparre Andersen
@ 2014-05-16 12:29 ` G.B.
  2014-05-16 17:57 ` Jeffrey Carter
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: G.B. @ 2014-05-16 12:29 UTC (permalink / raw)


On 16.05.14 09:37, hanslad@gmail.com wrote:
> Hello,
> I need an advise on how to define a type in my project.
> I want to implement a network protocol with the following definition on the type "string":
>
> "All String values are encoded as a sequence of UTF8 characters without a null terminator and preceded by the length in bytes.
> The length in bytes is encoded as Int32.

Does Int32 stand for a type that has exactly 32 bits
and is signed?  Then, Integer is not a suitable candidate
on the Ada side, because Integer is implementation defined
(and may well be 16 bits on a 32 bits platform).
Similarly, predefined type String may not be suitable
because String is indexed by Positive, which is a subtype
of Integer.

If all is well, though, I'd choose Interfaces.Integer_32.

Or just define some type if the benefits (and requirements)
stated for Interfaces.Integer_32 by the LRM are not that important.

   type Int32 is range -2^31 .. +2**31-1;
   for Int32'Size use 32;

Do you read octets? Some conversion might be needed
in any case, so you might as well try Storage_Array
instead of String, and then convert (or re-interpret)
as needed.

Your pointer-to-string type can be an "access constant" if
the strings so allocated do not need any overwrites.

Can you reuse a string buffer and pass it to processing
routines?



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:54 ` Dmitry A. Kazakov
  2014-05-16 10:03   ` G.B.
  2014-05-16 11:26   ` Jacob Sparre Andersen
@ 2014-05-16 15:38   ` Shark8
  2014-05-16 15:50     ` Simon Wright
  2 siblings, 1 reply; 13+ messages in thread
From: Shark8 @ 2014-05-16 15:38 UTC (permalink / raw)


On 16-May-14 01:54, Dmitry A. Kazakov wrote:
> Why -1 should
> indicate null string and not 0 is a mystery.

Because of the C-isms (a) that -1 indicates failure/non-presence, and 
(b) that all arrays are zero-based. -- IOW this is a format that has 
been prematurely optimized for C.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16 15:38   ` Shark8
@ 2014-05-16 15:50     ` Simon Wright
  2014-05-16 16:30       ` Shark8
  0 siblings, 1 reply; 13+ messages in thread
From: Simon Wright @ 2014-05-16 15:50 UTC (permalink / raw)


Shark8 <OneWingedShark@gmail.com> writes:

> On 16-May-14 01:54, Dmitry A. Kazakov wrote:
>> Why -1 should
>> indicate null string and not 0 is a mystery.
>
> Because of the C-isms (a) that -1 indicates failure/non-presence, and
> (b) that all arrays are zero-based. -- IOW this is a format that has
> been prematurely optimized for C.

But it's the _length_ of the string; so what meaning would OP attach to
a received message with a length of zero, vs a length -1?


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16 15:50     ` Simon Wright
@ 2014-05-16 16:30       ` Shark8
  0 siblings, 0 replies; 13+ messages in thread
From: Shark8 @ 2014-05-16 16:30 UTC (permalink / raw)


On 16-May-14 09:50, Simon Wright wrote:
> But it's the_length_  of the string; so what meaning would OP attach to
> a received message with a length of zero, vs a length -1?

While it's true that the empty-string and null are different, when it 
comes to transmitting the data it makes little sense to transmit what is 
non-existent -- though I suppose you could.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:37 Help with type definition hanslad
                   ` (2 preceding siblings ...)
  2014-05-16 12:29 ` G.B.
@ 2014-05-16 17:57 ` Jeffrey Carter
  2014-05-21 20:45 ` hanslad
  2014-06-27 22:05 ` hanslad
  5 siblings, 0 replies; 13+ messages in thread
From: Jeffrey Carter @ 2014-05-16 17:57 UTC (permalink / raw)


On 05/16/2014 12:37 AM, hanslad@gmail.com wrote:
>
>    type String_Type_Implementation(Count : Natural) is record
>       Data : String(1 .. Count) := "";
>    end record;
>
>    type String_Type_Implementation_Ptr is access String_Type_Implementation;
>
>    type String_Type is new Ada.Finalization.Controlled with record
>       Length         : Integer;
>       Implementation : String_Type_Implementation_Ptr;
>    end record;

String_Type is not suitable for sending over the network, so why not simply use 
Ada.Strings.Unbounded.Unbounded_String for the string value in type Node? You're 
going to have to construct the sequence of bytes to go over the network in 
either case.

A consistent coding and naming style, and a lot fewer occurrences of the 
sequence [t|T]ype would make your code easier to read.

-- 
Jeff Carter
"Hold your temper. Count ten.... Now let 'er go.
You got a good aim."
Never Give a Sucker an Even Break
105

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:37 Help with type definition hanslad
                   ` (3 preceding siblings ...)
  2014-05-16 17:57 ` Jeffrey Carter
@ 2014-05-21 20:45 ` hanslad
  2014-06-27 22:05 ` hanslad
  5 siblings, 0 replies; 13+ messages in thread
From: hanslad @ 2014-05-21 20:45 UTC (permalink / raw)


Thank you for all your replies. I was a bit impresise in my question. The question should be: How I should represent a string with the given protocol specification? I have got a number of good advices on this, so thanks.

Regards,
Hans




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-05-16  7:37 Help with type definition hanslad
                   ` (4 preceding siblings ...)
  2014-05-21 20:45 ` hanslad
@ 2014-06-27 22:05 ` hanslad
  2014-06-28  7:26   ` Shark8
  5 siblings, 1 reply; 13+ messages in thread
From: hanslad @ 2014-06-27 22:05 UTC (permalink / raw)


Hello again,
I still need some help with type definition in my program.
The specification of the protocol I try to implement the identifier type could be a byte string with similar specification as the string identifier type.
Is there a type in Ada for unbounded byte arrays similar to the unbounded string?

Belowis an excerpt from the protocol specification of how the bytestring is presented on the wire:

"A ByteString is encoded as sequence of bytes preceded by its length in bytes. The length is encoded as a 32-bit signed integer as described above.
If the length of the byte string is -1 then the byte string is 'null'."

 
I got a new version of my code:

package Types is

   package SU   renames Ada.Strings.Unbounded;

   type StringIdentifier is new SU.Unbounded_String;

   type ByteStringIdentifier is new ??;
   
   type IdentifierType is
     (
      Identifier_Numeric,
      Identifier_String,
      Identifier_Bytestring
     );
   for IdentifierType use
     (Identifier_Numeric => 1,
      Identifier_String  => 2,
      Identifier_ByteString  => 3,);
   for IdentifierType'Size use 8;


   type Node_Id (IdType  : IdentifierType := IdentifierType_Numeric )is record
      NamespaceIndex : Interfaces.Unsigned_16  := 0;
      case IdType is
      when Identifier_Numeric =>
         Numeric    : Interfaces.Unsigned_32 ;
      when Identifier_String =>
         String     : StringIdentifier;
      when Identifier_String =>
         ByteString     : ByteStringIdentifier;
      end case;
   end record;
end Types;



Thanks

HP


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Help with type definition
  2014-06-27 22:05 ` hanslad
@ 2014-06-28  7:26   ` Shark8
  0 siblings, 0 replies; 13+ messages in thread
From: Shark8 @ 2014-06-28  7:26 UTC (permalink / raw)


On 27-Jun-14 16:05, hanslad@gmail.com wrote:
> Is there a type in Ada for unbounded byte arrays similar to the unbounded string?

You could use a vector; but you really don't need it for this problem; 
in fact it seems you're better off using a regular array.

>   "A ByteString is encoded as sequence of bytes preceded by its
>    length in bytes. The length is encoded as a 32-bit signed
>    integer as described above. If the length of the byte string
>    is -1 then the byte string is 'null'."

So, your from-the-wire reading algorithm would be something like this:

Type Byte_String is Array(Positive range <>) of Interfaces.Unsigned_8;

-- Assuming use of Ada.Streams
Function Read(Stream : not null access Root_Stream_Type'Class) return 
Byte_String is
   Length : Interfaces.Integer_32:= Interfaces.Integer_32'Input(Stream);
begin
   Return Result : Byte_Array(1..Length) do
    For Index in Result'Range loop
     Result(Index):= Interfaces.Unsigned_8'Input(Stream);
    end loop;
   end return;
end Read;

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-06-28  7:26 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-16  7:37 Help with type definition hanslad
2014-05-16  7:54 ` Dmitry A. Kazakov
2014-05-16 10:03   ` G.B.
2014-05-16 11:26   ` Jacob Sparre Andersen
2014-05-16 15:38   ` Shark8
2014-05-16 15:50     ` Simon Wright
2014-05-16 16:30       ` Shark8
2014-05-16 11:29 ` Jacob Sparre Andersen
2014-05-16 12:29 ` G.B.
2014-05-16 17:57 ` Jeffrey Carter
2014-05-21 20:45 ` hanslad
2014-06-27 22:05 ` hanslad
2014-06-28  7:26   ` Shark8

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox