a-textio.adb (Set_Col): Fix two errors noticed recently...
authorRobert Dewar <dewar@adacore.com>
Thu, 16 Jun 2005 08:33:07 +0000 (10:33 +0200)
committerArnaud Charlet <charlet@gcc.gnu.org>
Thu, 16 Jun 2005 08:33:07 +0000 (10:33 +0200)
2005-06-14  Robert Dewar  <dewar@adacore.com>

* a-textio.adb (Set_Col): Fix two errors noticed recently, having to
do with treatment of Set_Col when positioned at end of line character.

From-SVN: r101025

gcc/ada/a-textio.adb

index 3fc95f02bd83e8d7fae8885553c1eba68fd83312..306ab592e8c635d9229b176f23e512a4dec57021 100644 (file)
@@ -1,6 +1,6 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---                         GNAT RUNTIME COMPONENTS                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
 --                          A D A . T E X T _ I O                           --
 --                                                                          --
@@ -1340,43 +1340,82 @@ package body Ada.Text_IO is
 
       FIO.Check_File_Open (AP (File));
 
-      if To = File.Col then
-         return;
-      end if;
+      --  Output case
 
       if Mode (File) >= Out_File then
+
+         --  Error if we attempt to set Col to a value greater than the
+         --  maximum permissible line length.
+
          if File.Line_Length /= 0 and then To > File.Line_Length then
             raise Layout_Error;
          end if;
 
+         --  If we are behind current position, then go to start of new line
+
          if To < File.Col then
             New_Line (File);
          end if;
 
+         --  Loop to output blanks till we are at the required column
+
          while File.Col < To loop
             Put (File, ' ');
          end loop;
 
+      --  Input case
+
       else
+         --  If we are logically before a LM, but physically after it, the
+         --  file position still reflects the position before the LM, so eat
+         --  it now and adjust the file position appropriately.
+
+         if File.Before_LM then
+            File.Before_LM := False;
+            File.Before_LM_PM := False;
+            File.Line := File.Line + 1;
+            File.Col := 1;
+         end if;
+
+         --  Loop reading characters till we get one at the required Col value
+
          loop
+            --  Read next character. The reason we have to read ahead is to
+            --  skip formatting characters, the effect of Set_Col is to set
+            --  us to a real character with the right Col value, and format
+            --  characters don't count.
+
             ch := Getc (File);
 
+            --  Error if we hit an end of file
+
             if ch = EOF then
                raise End_Error;
 
+            --  If line mark, eat it and adjust file position
+
             elsif ch = LM then
                File.Line := File.Line + 1;
                File.Col := 1;
 
+            --  If recognized page mark, eat it, and adjust file position
+
             elsif ch = PM and then File.Is_Regular_File then
                File.Page := File.Page + 1;
                File.Line := 1;
                File.Col := 1;
 
+            --  Otherwise this is the character we are looking for, so put it
+            --  back in the input stream (we have not adjusted the file
+            --  position yet, so everything is set right after this ungetc).
+
             elsif To = File.Col then
                Ungetc (ch, File);
                return;
 
+            --  Keep skipping characters if we are not there yet, updating the
+            --  file position past the skipped character.
+
             else
                File.Col := File.Col + 1;
             end if;