如何在数组和记录中使用refs来表示常量字符串?

时间:2017-01-24 19:47:31

标签: ada

我打算将一些用C / C ++编写的程序转换为Ada。 这些大量使用常量字符文字通常作为不规则的数组,如:

const char * stringsA = { "Up", "Down", "Shutdown" };

或记录中的字符串引用,如:

typedef struct
   {
   int something;
   const char * regexp;
   const char * errormsg
   } ERRORDESCR;
ERRORDESCR edscrs [ ] =
   {
    { 1, "regexpression1", "Invalid char in person name" },
    { 2, "regexp2", "bad bad" }
   };

预设由C / C ++编译器计算,我想要Ada编译器 能够做到这一点。

我使用谷歌并搜索了不规则的数组,但只能找到两种方法 预设字符串。约翰巴恩斯和另一个人在Ada 95的基本原理中 在http://computer-programming-forum.com/44-ada/d4767ad6125feac7.htm。 这些在下面显示为stringsA和stringsB。 StringsA分为两个阶段,如果有数百个阶段,这有点单调乏味 要设置的字符串。 StringsB仅使用一步,但依赖于编译器。

问题1:还有其他方法吗? 问题2:第二个字符串B是否可以与GNAT Ada一起使用?

我还没有开始转换。下面的包只是用于试验 并自学......

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;

2 个答案:

答案 0 :(得分:1)

稍微明智的运算符重载可以以更简洁的方式执行此操作:

(在包体内)

   function New_String(S : String) return String_Ptr is
   begin
      return new String'(S);
   end New_String;

   function "+" (S : String) return String_Ptr renames New_String;

现在你可以做到:

   stringsC: array (1 .. 3) of String_ptr := (+"Up", +"Down", +"Shutdown");

答案 1 :(得分:0)

对此评论的空间不足 测试程序

package raggedtest is
   type String_ptr is access constant String;
   procedure mytest;
end raggedtest;

with ada.text_IO; use Ada.Text_IO;
package body raggedtest 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);

   stringsB: array (1 .. 3) of String_ptr :=
      (new String'("UpB"), new String'("DownB"),
       new String'("ShutdownB"));

   function New_String(S : String) return String_Ptr is
   begin
      return new String'(S);
   end New_String;
   function "+" (S : String) return String_Ptr renames New_String;
   stringsC: array (1 .. 3) of String_ptr := (+"UpC", +"DownC", +"ShutdownC");


   procedure mytest is
   begin
      put ( "s1A: " ); put( stringsA(1).all ); New_line;
      put ( "s2A " ); put( stringsA(2).all ); New_line;
      put ( "s3A: " ); put( stringsA(3).all ); New_line;

      put ( "s1B: " ); put( stringsB(1).all ); New_line;
      put ( "s2B " ); put( stringsB(2).all ); New_line;
      put ( "s3B: " ); put( stringsB(3).all ); New_line;

      put ( "s1C: " ); put( stringsC(1).all ); New_line;
      put ( "s2C " ); put( stringsC(2).all ); New_line;
     put ( "s3C: " ); put( stringsC(3).all ); New_line;
   end;
end raggedtest;

with raggedtest; use raggedtest;
procedure main is
begin
   mytest;
end main;