Ada字符串文字常量数组 [英] Ada constant array of string literals
问题描述
我有大量的C语言,希望进入我的ada项目。该数组用于存储资产的文件名,这些文件将在以后加载。看起来像这样:
I have a large array in C that I wish to move into my ada project. That array is used to store filenames for assets which will later be loaded. It looks something like:
const char *filenames[NUMBER_OF_FILES] = {
/* file[0] */ "res/0.file",
/* ... */
/* file[n] */ "res/some_more/complicated/file.name"
};
我想将其移动到ada程序包中,但找不到合适的方法它。显然,我的第一个尝试是:
I want to move this into an ada package body, but can't find a decent way to do it. Obviously my first attempt was:
filenames : constant array (File_Index) of String := (
index_0 => "res/0.file",
-- ...
index_n => "res/some_more/complicated/file.name"
);
但是String当然是不受限制的类型,所以Ada不允许这样做。我将其切换为使用Unbounded_Strings,该方法有效,但非常难看(必须使用 To_Unbounded_String
包装每个字符串。
But of course String is an unconstrained type, so Ada won't allow that. I switched it to use Unbounded_Strings, which worked, but was very ugly (having to wrap each string with To_Unbounded_String
.
是否有任何方法可以生成无约束类型的数组,其大小将在编译时就这样知道,还是我必须使用无界字符串?
Is there any way to make an array of unconstrained types whose size will be known at compile time like this, or do I have to use unbounded strings?
推荐答案
有点低级且重复,但是也许您可以创建一个小程序(甚至在Ada中也可以)以生成类似
It’s a bit low-level and repetitive, but perhaps you can create a little program (maybe even in Ada!) to generate something like
with Ada.Text_IO; use Ada.Text_IO;
procedure Lambdabeta is
F1 : aliased constant String := "res/0.file";
F2 : aliased constant String := "res/some_more/complicated/file.name";
type Strings is array (Positive range <>) of access constant String;
Filenames : constant Strings := (F1'Access,
F2'Access);
begin
for J in Filenames'Range loop
Put_Line (Filenames (J).all);
end loop;
end Lambdabeta;
另请参见此答案关于最大程度地减少使用 To_Unbounded_String
的痛苦。
See also this answer on minimising the pain of using To_Unbounded_String
.
这篇关于Ada字符串文字常量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!