Is there a way to format text output in Ada
Solution 1
It can be done, but the mechanisms are a bit cumbersome and quite a bit more verbose.
What I would generally do is write separate procedures to handle your more complicated output, e.g. dates and use that with the rest of the string handling for clarity.
package Integer_IO is new Ada.Text_IO.Integer_IO (Integer);
procedure Output_Date ( Day : in Integer; Month: in Integer; Year: in Integer) is
begin
Integer_IO.Put(Item => Day, Width => 2);
Text_IO.Put("-");
Integer_IO.Put(Item => Month, Width => 2);
Text_IO.Put("-");
Integer_IO.Put(Item => Year, Width => 4);
end Output_Date;
procedure Output_String ( Item : in String;
Width : in Integer;
Separator : in String := "|";
Truncate : Boolean := False) is
Field_Index : Integer := Text_IO.Col;
begin
if Item'length > Width and Truncate then
Text_IO.Put(Item(1..Width) & Separator);
else
Text_IO.Put(Item) & Separator;
end if;
Text_IO.Set_Col ( Field_Index + Width + 1 );
end Output_String;
This will enforce fixed length fields, which will optionally allow truncation of long strings, or else move subsequent entries onto the next line. Set_Col will set the line position for the next write, potentially placing it on the next line if the current write position has already exceeded the one requested.
I threw string truncation in there as a chance to use array slicing and Text_IO's output manipulation, but I'm not generally a fan of default truncation, as allowing the string to overrun the requested width or indenting on the next line tend make formatting errors more obvious.
So printing out something like your first line, given the code above, might look something like:
Name : String := "Ashley"
Phone : String := "01033438392"
Address: String := "Wellington, New Zealand"
Day : Integer := 14;
Month : Integer := 4;
Year : Integer := 1987;
Output_String(Item=> Name, Width => 10);
Output_String(Item=> Phone, Width => 11);
Output_String(Item=> Address, Width => 20);
Output_Date(Day,Month,Year);
Text IO in Ada is usually cumbersome, but generally has the virtue of making what you are doing relatively clear.
Solution 2
Note that in C++ these days printf()
is on the verge of being depreciated, in favor of using streams with stream formatters. It is convenient, but massively unsafe (in at least a couple senses of the word). These days developers are encouraged to use C++ streams (with their assorted manipulators) instead.
In Ada you can manipulate strings in a very similar style to C++ streams using the string catenation operator &
where C++ folks use the stream insertion operator (<<
). In some ways, Ada's method is better because you can nest catenated expressions, which you can't do with stream-inserted expressions.
The problem here is that there aren't any handy equivalents to the C++ formatters like setfill()
, hex
, and setw()
. There really ought to be, and (hex
excepted) they aren't tough to write yourself, but for now they don't exist.
For example, a setw()/setfill()
equivalent would be something like:
Fill_Char : Character := ' ';
function Set_Fill (New_Fill : Character) return String is
begin
Fill_Char := New_Fill;
return "";
end Set_Fill;
--// Dumb tail-recursive implementation.
function Set_Width(Source : in String; Width : in Positive) return String is
begin
if Width <= Source'length then --'
return Source;
else
return Fill_Char & Set_Width(Source, Width - 1);
end if;
end Set_Width;
Unfilled_String : constant String := "123456";
Filled_String : constant String := Set_Width(Unfilled_String & Set_Fill('0'), 8);
--// The above string should end up being "00123456"
If you really want a printf()
interface, printf()
is quite callable from Ada of course. You have to worry about transitioning between Ada's sized strings and C's nul-terminated strings, but that's what Ada.Interfaces.C.Strings
is there for.
Solution 3
There is auxiliary tools for this particular format setup.
Package Ada.Text_IO.Integer_IO
procedure Put(Item : in Num; Width : in Field := Default_Width; Base : in Number_Base := Default_Base);
Puts a field with Item
aligned to right and white-space character as filler. Where Width
is field width and Base
is 10 as defualt.
Package Ada.Strings.Fixed
function Head (Source : in String; Count : in Natural; Pad : in Character := Space) return String;
function Tail (Source : in String; Count : in Natural; Pad : in Character := Space) return String;
Returns a formatted string. Where Count
is the field width and Pad
is filler for the field. Head
aligns string to left. Tail
aligns string to right.
Let column width be 8 character long and use dash as filler.
Put_Line (Head ("Ashley", 8, '-'));
Put_Line (Head ("Aloha", 8, '-'));
Put_Line (Head ("Jack", 8, '-'));
Put_Line (Tail ("Ashley", 8, '-'));
Put_Line (Tail ("Aloha", 8, '-'));
Put_Line (Tail ("Jack", 8, '-'));
Output
Ashley--
Aloha---
Jack----
--Ashley
---Aloha
----Jack
Attribute discrete_type'Width
Returns a length which the discrete type requires to be represented as text.
Example
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Calendar; use Ada.Calendar;
procedure Test is
subtype Index is Positive range 95 .. 1223;
procedure Put_Line ( I : in out Index; Name : String; Phone : Natural; Address : String; T : in out Time ) is
begin
Put (I, Index'Width);
Put (": ");
Put (Head (Name, 10, ' '));
Put (" | ");
Put (Tail (Phone'Img (Phone'Img'First + 1 .. Phone'Img'Last), 13, '0'));
Put (" | ");
Put (Head (Address, 20, ' '));
Put (Year (T), Year_Number'Width);
Put ("-");
Put (Month (T), Month_Number'Width);
Put ("-");
Put (Day (T), Day_Number'Width);
I := Positive'Succ (I);
T := T + Duration (60 * 60 * 24 * 3);
New_Line;
end;
I : Index := Index'First;
Now : Time := Clock;
begin
Put_Line (I, "Ashley", 1033438392, "Wellington, New Zealand", Now);
Put_Line (I, "Aloha", 01087651234, "Hawaii, United States of America", Now);
Put_Line (I, "Jack", 01082840184, "Beijing, China", Now);
I := Index'Last - 3;
Put_Line (I,"Ashley", 1033438392, "Wellington, New Zealand", Now);
Put_Line (I,"Aloha", 01087651234, "Hawaii, United States of America", Now);
Put_Line (I,"Jack", 01082840184, "Beijing, China", Now);
end;
Output
95: Ashley | 0001033438392 | Wellington, New Zeal 2015- 5- 24
96: Aloha | 0001087651234 | Hawaii, United State 2015- 5- 27
97: Jack | 0001082840184 | Beijing, China 2015- 5- 30
1220: Ashley | 0001033438392 | Wellington, New Zeal 2015- 6- 2
1221: Aloha | 0001087651234 | Hawaii, United State 2015- 6- 5
1222: Jack | 0001082840184 | Beijing, China 2015- 6- 8
I would recommend to create a type for a phone number, I dunno if it should be string or number with the about of heading zeros, phone number can have different length I guess.
Solution 4
Yes there is. Although its not as easy as in c.
Have a look at §A.4.4 Bounded-Length String Handling for how to create strings of a predefined size, and use integer'image to convert your numbers. The & operator is useful to concatenate strings and output using ada.text_io.put_line().
Solution 5
You might like this simple card game simulation that uses Ada.Strings.Fixed
to format the range axis labels for an ASCII graph. See function Label
, which uses Tail
and Trim
to format the Integer'Image
of a Lower
and Upper
value.
Code:
function Label (J : Integer) return String is
use Ada.Strings; use Ada.Strings.Fixed;
Lower : String := Integer'Image(J * Bin_Size);
Upper : String := Integer'Image((J + 1) * Bin_Size);
begin
return Tail(Trim(Lower, Left), 4, '0') & "-" &
Tail(Trim(Upper, Left), 4, '0') & " |*";
end Label;
Console:
Distribution of lengths:
0000-0100 |**********
0100-0200 |*****************************
0200-0300 |**********************
0300-0400 |***************
0400-0500 |**********
0500-0600 |*******
0600-0700 |****
0700-0800 |****
0800-0900 |**
0900-1000 |**
1000-1100 |*
1100-1200 |*
Chang Hyun Park
Updated on July 16, 2022Comments
-
Chang Hyun Park almost 2 years
Is there a way to format the string being outputted? I'm trying get a pretty view of the following output
1: Ashley | 01033438392 | Wellington, New Zealand | 1987- 4-14 2: Aloha | 01087651234 | Hawaii, United States of America | 1988- 9-23 3: Jack | 01082840184 | Beijing, China | 1989- 6-19
If I was programming in C, I would do something like
printf("%10s | %11s | %20s | %4d-%2d-%2d\n",name,phone,address,year,month,day);
Would it be possible to do this kind of formatting in Ada 05?
PS Please just ignore the names, phone numbers, address, and birthdate. I made them up in like 30 seconds...
-
trashgod almost 13 yearsLink updated. The 'Image attribute prepends a space for positive numbers, while
Ada.Text_IO.Integer_IO
does not. See also §A.4.3 Fixed-Length String Handling. -
trashgod almost 13 years+1 for
Integer_IO
. For convenience,Integer_Text_IO
may already be defined. -
T.E.D. almost 13 yearsHmm.. I haven't used it before, but it looks like
Ada.Strings.Fixed."*"(Width - Source'length, Fill_Char)
in myelse
branch allow you to get rid of the recursion. -
trashgod almost 13 yearsAlso, a good example of Ada's judicious use of operator overloading.
-
T.E.D. almost 13 years@oenone - It is in fact operator overloading if you write it like this:
return ((Width - Source'length) * Fill_Char) & Source;
. -
Chang Hyun Park almost 13 yearsGreat! I'll try this! Thank you!
-
Chang Hyun Park almost 13 yearsHmm Just out of curiosity? What exaclty is dangerous about printf()? I never knew that printf could be dangerous lol
-
Greg almost 13 yearsI am trying to decide whether making Set_Fill a function is incredibly crafty or Evil™
-
T.E.D. almost 13 years@Greg - I'd probably vote for Evil. This is what C++ does with
std::setfill()
though, which is what I was trying to duplicate. I'm not a big fan of adding implicit state, but its their paradigm. -
T.E.D. almost 13 years@Heartinpiece -
printf()
doesn't type check the arguments at all, so it is very easy to crash your program by getting one wrong. Just try feeding an integer to a "%s" format and see what happens. I do it myself regularly and I've been using C since the mid 80's. Additionally, this family of routines (particularlysprintf()
) is probably the single greatest target of buffer overrun exploits in the world. There are entire hacker websites dedicated to helping find and exploit uses ofsprintf()
. -
LoneWanderer almost 6 yearsThere is a small pitfall while using this inside a loop : the
+"myformat: %s"
seems to be altered when resolving the formatting (with-(myformat & value)
in some cases. The trick is to re-perform affectation of format at each loop start. -
LoneWanderer almost 6 yearsHere is the explanation (found in the same source file):
-- Note that a Formatted_String object can't be reused as it serves as recipient for the final result. That is, each use of "&" will build incrementally the final result string which can be retrieved with the "-" routine below.
Better be warned. -
LoneWanderer almost 6 yearsWorking examples added with OP data.
-
Álex almost 6 yearsI had forgotten about the
'Width
attribute, nice one.