0

I'm trying to get the hang of the Ada.Text_IO package, and it has many differences with IO of C.

One of them differences is how Get treats control characters and Horizontal or Character Tabulation specifically. I've read 83 and 2022 Reference Manual and I couldn't find anything about how Tab effects the column counter of a file (which is funny, because it specifically tells you it does at Ada2022 RM A.10 (8/5) :)).

I've written a small test:

procedure Ada_Parser.Parse (File : Ada.Text_IO.File_Type) is package TIO renames Ada.Text_IO; C : Character; Last_Column : TIO.Positive_Count; begin TIO.Get (File, C); Last_Column := TIO.Col (File); while not Ada.Text_IO.End_Of_File (File) loop case C is when ASCII.HT => TIO.Put_Line ("Tab! " & TIO.Col (File)'Image & " " & Last_Column'Image); when others => -- TIO.Put_Line ("Unhandled """ & C & """"); null; end case; Last_Column := TIO.Col (File); TIO.Get (File, C); end loop; end Ada_Parser.Parse; 

And in output I can see that Tab is only 1 column long:

 Tab! 2 18 Tab! 3 2 Tab! 4 3 Tab! 2 91 Tab! 3 2 

My goal is to write text file reading program with an option to set Tab Width, so the column number interpreted accordingly.

Unfortunately, I didn't find procedures to set Tab width in Ada.Text_IO package. How can I do it?

2 Answers 2

1

The page you linked in your question talks only about characters:

The characters of a line are numbered, starting from one; the number of a character is called its column number. For a line terminator, a column number is also defined: it is one more than the number of characters in the line.

This implies the tab character has to be 1 column wide. Imagine a line like this:

abc<TAB>d<NEWLINE> 

abc are in columns 1, 2, 3 respectively. <TAB> is in column 4. If tab stops are every 8 columns, then the d is in column 9 and the <NEWLINE> in column 10. But the second sentence from the RM says that the <NEWLINE> should be one more than the number of characters in the line. The number of characters is 5, and therefore the <NEWLINE> should have column number 6. This disagrees with having the <TAB> shift the column numbers; therefore <TAB> can only take up 1 column.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! Gotta to pay more attention to what I read...
1

Use the Set_Col procedure as described in https://www.adaic.org/resources/add_content/standards/22rm/html/RM-A-10-5.html 27 - 34

27

The following subprograms provide for the control of the current position of reading or writing in a file. In all cases, the default file is the current output file.

28

procedure Set_Col(File : in File_Type; To : in Positive_Count);
procedure Set_Col(To   : in Positive_Count);

29

If the file mode is Out_File or Append_File:

30

If the value specified by To is greater than the current column number, outputs spaces, adding one to the current column number after each space, until the current column number equals the specified value. If the value specified by To is equal to the current column number, there is no effect. If the value specified by To is less than the current column number, has the effect of calling New_Line (with a spacing of one), then outputs (To – 1) spaces, and sets the current column number to the specified value.

31

The exception Layout_Error is propagated if the value specified by To exceeds Line_Length when the line length is bounded (that is, when it does not have the conventional value zero).

32

If the file mode is In_File:

33

Reads (and discards) individual characters, line terminators, and page terminators, until the next character to be read has a column number that equals the value specified by To; there is no effect if the current column number already equals this value. Each transfer of a character or terminator maintains the current column, line, and page numbers in the same way as a Get procedure (see A.10.6). (Short lines will be skipped until a line is reached that has a character at the specified column position.)

34

The exception End_Error is propagated if an attempt is made to read a file terminator.

1 Comment

Sorry for the misunderstanding, but I've asked how to set Tab Width Text_IO procedures to interpret it not as 1 column, but as specific value. I've changed the question to be more clear. But, I assume, there are no built-in ways to do that, and I have to implement this myself... Thanks anyway!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.