I am planning to convert some programs written in C/C++ to Ada. These make heavy use of constant char literals often as ragged arrays like:
const char * stringsA = { "Up", "Down", "Shutdown" };
or string references in records like:
typedef struct
{
int something;
const char * regexp;
const char * errormsg
} ERRORDESCR;
ERRORDESCR edscrs [ ] =
{
{ 1, "regexpression1", "Invalid char in person name" },
{ 2, "regexp2", "bad bad" }
};
The presets are calculated by the C/C++ compiler and I want the Ada compiler to be able to do that too.
I used Google and searched for ragged arrays but could only find two ways of presetting the strings. One in Rationale for Ada 95 by John Barnes and another at http://computer-programming-forum.com/44-ada/d4767ad6125feac7.htm. These are shown as stringsA and stringsB below. StringsA is defined in two stages, which is a bit tedious if there are hundreds of strings to set up. StringsB uses one step only, but is compiler dependent.
Question 1: are there other ways? Question 2: would the second stringsB work with GNAT Ada?
I have not started converting. The packages below are just for experimenting and teaching myself...
package ragged is
type String_ptr is access constant String;
procedure mydummy;
end ragged;
package body ragged is
s1: aliased constant String := "Up";
s2: aliased constant String := "Down";
s3: aliased constant String := "Shutdown";
stringsA: array (1 .. 3) of String_ptr :=
(s1'Access, s2'Access, s3'Access); -- works
stringsB: array (1 .. 3) of String_ptr :=
(new String'("Up"), new String'("Down"),
new String'("Shutdown")); -- may work, compiler-dependent
-- this would be convenient and clear...
--stringsC: array (1 .. 3) of String_ptr :=
-- ("Up", "Down", "Shutdown"); -- BUT Error, expected String_ptr values
--stringsD: array (1 .. 3) of String_ptr :=
--("Up"'Access, "Down"'Access, "Shutdown"'Access); --Error - bad Access use
--stringsE: array (1 .. 3) of String_ptr :=
--(String_ptr("Up"), String_ptr("Down"),
-- String_ptr("Shutdown")); -- Error, invalid conversion
procedure mydummy is
begin
null;
end;
end ragged;