user1358
user1358

Reputation: 643

Is this Ada textbook example rubbish?

Here are the sources for the example programs of "Ada for software engineers":

http://www.springer.com/cda/content/document/cda_downloaddocument/978-1-84882-313-6_programs.zip

after extracting go to c04-arrays/justify

$ gnatmake justify.adb

Run justify, and compare output with "example.txt" which is the input. The output I get is has intertwined lines and has nothing to do with justification. If I save the output with:

$ justify > result.txt

then looking at that I can conclude that some lines where written ontop of others in the terminal. But even if I ignore that issue, the result can hardly be called justification.

My question: Do you get the same unexpected output? Does the output you get resemble justification?

Note that the input is also supplied with the source! So I am not even trying to break the program.

the output I get on the terminal:

  The  quicke quick brown fox jumped over the  lazy  dog.
          brown     fox     jumped     over     the     lazy     dog.
          ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedov
 The quickThe  quick  brown  fox  jumped over the lazy dog.
  The  quick  brown  foxed over the lazy dog.
 The quick brown fox jumped over dog.
 The quick brown  fox  jumped  over  the  lazy
 Thee  slow gray wolf skipped over the frisky cat.
 The  slow  grayray wolf skipped over the frisky cat.
  The  slow gray wolfed  over  the  frisky  cat.
 The  slow  gray  wolf  skippedky cat.
 The slow gray wolf skipped over the
 The slow gray  wolf  skipped  over  the  frisky
          cat.

the output I have after redirection:

          The quick brown fox jumped over the  lazy  dog.
  The  quick
          brown     fox     jumped     over     the     lazy     dog.
          ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedov
          The  quick  brown  fox  jumped over the lazy dog.
 The quick
          brown fox jumped over the lazy dog.
  The  quick  brown  fox
          jumped  over  the lazy dog.
 The quick brown fox jumped over
          the lazy dog.
 The quick brown  fox  jumped  over  the  lazy
          dog.
  The  slow gray wolf skipped over the frisky cat.
 The
          slow gray wolf skipped over the frisky cat.
 The  slow  gray
          wolf  skipped  over  the  frisky  cat.
  The  slow gray wolf
          skipped over the frisky cat.
 The  slow  gray  wolf  skipped
          over  the  frisky  cat.
 The slow gray wolf skipped over the
          frisky cat.
 The slow gray  wolf  skipped  over  the  frisky
          cat.


Is this supposed to be justification? Is this problem related to my machine/system/terminal/shell in particular, or is the problem somewhere else?

Upvotes: 1

Views: 219

Answers (2)

Simon Wright
Simon Wright

Reputation: 25501

I get the same output on Mac OS X.

The problem is that example.txt is a Windows file, with CR/LF line endings, and you're running on a Unix system, which expects just LF; and the Ada RTS is leaving the CR at the end of each input line. (I suspect the C RTS might do the same; Python handles it better).

Lots of info in Wikipedia, including that you can use cat -v to see the control characters:

      The quick brown fox jumped over the  lazy  dog.^M  The  quick
      brown     fox     jumped     over     the     lazy     dog.^M
      ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedov
      The  quick  brown  fox  jumped over the lazy dog.^M The quick
      brown fox jumped over the lazy dog.^M  The  quick  brown  fox
      jumped  over  the lazy dog.^M The quick brown fox jumped over
      the lazy dog.^M The quick brown  fox  jumped  over  the  lazy
      dog.^M  The  slow gray wolf skipped over the frisky cat.^M The
      slow gray wolf skipped over the frisky cat.^M The  slow  gray
      wolf  skipped  over  the  frisky  cat.^M  The  slow gray wolf
      skipped over the frisky cat.^M The  slow  gray  wolf  skipped
      over  the  frisky  cat.^M The slow gray wolf skipped over the
      frisky cat.^M The slow gray  wolf  skipped  over  the  frisky
      cat.^M ^M 

I don't know whether your system has a dos2unix utility, but in any case you can use tr:

$ tr -d '\r' <example.txt >example.lf
$ mv example.lf example.txt

Normally, you could have extracted the archive using unzip -a to convert line endings on all files that the original zip identified as text; in this case it identified all the files as binary, so you could use unzip -aa to force the conversion (given these are textbook examples, it's a fairly safe bet there won't be any actual binary files in there!)

Upvotes: 2

Shark8
Shark8

Reputation: 4198

The answer may be that your console is making tabs look like spaces, and thus giving different looking results.


You can test this with:

Ada.Text_IO.Put( ASCII.HT & '.' );
Ada.Text_IO.Put( ' ' & '.');

and see if the two periods are in the same column.

Upvotes: 0

Related Questions