I'm a long time C++ programmer learning Ada for fun. If any of the following is bad form, please feel free to point it out. I'm trying to learn the Ada way to do things, but old habits are hard to break (and I miss Boost!)
I'm trying to load a file that contains an integer, a space, and then a string of characters. There may be a better way to do this, but I thought that I ought to load the line into a string buffer that I know won't be more than 80 characters. I declare a buffer variable like the following in the appropriate place:
Line_Buffer : String(1..80);
After opening the file, I loop through each line and split the buffer at the space character:
while not Ada.Text_IO.End_Of_File(File_Handle) loop
Ada.Text_IO.Get_Line(File_Handle, Item=>Line_Buffer, Last=>Last);
-- Break line at space to get match id and entry
for String_Index in Line_Buffer'Range loop
if Line_Buffer(String_Index) = ' ' then
Add_Entry(Root_Link=>Root_Node,
ID_String=> Line_Buffer(1..String_Index-1),
Entry_String=> Line_Buffer(String_Index+1..Last-1)
);
end if;
end loop;
end loop;
What happens in Add_Entry is not that important, but its specification looks like this:
procedure Add_Entry(
Root_Link : in out Link;
ID_String : in String;
Entry_String : in String);
I wanted to use unbounded strings rather than bounded strings because I don't want to worry about having to specify size here and there. This compiles and works fine, but inside Add_Entry, when I try to loop over each character in Entry_String, rather than having indexes starting from 1, they start from the offset in the original string. For example, if Line_Buffer was "14 silicon", if I loop as follows, the index goes from 4 to 10.
for Index in Entry_String'Range loop
Ada.Text_IO.Put("Index: " & Integer'Image(Index));
Ada.Text_IO.New_Line;
end loop;
Is there a better way to do this parsing so that the strings I pass to Add_Entry have boundaries that begin with 1? Also, when I pass a sliced string as an "in" parameter to a procedure, is a copy created on the stack, or is a reference to the original string used?