[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters ]

Arc de Triomphe clipart (2486 bytes)

FOIL Library


Written by Sami Hangaslammi.

foil-0-1.zip (22 kbytes) source code


FOIL v0.1

Using

Create an instance of FORMATTED_IO and use its 'printf', 'format_string' and 'read_formatted' methods. See the short form for more details. There is also an example program 'format_test.e' which demonstrates the usage of formatting tags. You can compile the test program in SmallEiffel using the following command line:

compile format_test -o format_test

Formatting Tag Syntax

The formatting string contains standard text except for tags that represent variables (to be printed or created). Tags are written after '&'-character (use "&&" to insert a literal '&' in the text) and must be enclosed in a '< >' pair when several characters long.

Example tags "&s", "&"

Most tags work for both input and output.

Standard Tags

It is very easy to create customised tags, but here is a short overview of the standard tags provided with the formatter class.

Basic Type Tags

&i = INTEGER variable
&r = REAL variable
&d = DOUBLE variable
&n = number variable (any of the above, the most appropriate is used)

&c = CHARACTER variable
&b = BOOLEAN variable (printed as "true" or "false")
&s = STRING variable

&v = value (number, CHARACTER, BOOLEAN or STRING)

& = fixed point formatted numbers, f is followed by number of decimals (0-9) reads any number if used in formatted input

Examples:

printf("His name is &s and he is &i years old.", << name, age >>)
printf("&s is &", << "pi", pi >>)

Modifier Tags

Some tags modify the formatting of another tag. Most modifier tags are mainly used when formatting COLLECTION objects.

& = capitalise the first letter of parameter tag
& = enclose in single quotes ' '
& = enclose in double quotes " "
&<(...> = enclose in brackets ( )
&<[...> = enclose in square brackets [ ]
&<{...> = enclose in curly brackets { }
&<<...> = enclose in < >

&<+...> = append some text to tag (e.g. "&<+'suffix's>" adds "suffix" in front of a string)

&<+/...> = prepend with some text (e.g. "&<+/'prefix's>" prefixes a string with "prefix")

& = removes surrounding whitespaces in input
= adds surrounding whitespaces if used in formatted output

& = width formatting, adds/removes padding characters. The first character after 'W' is used as the fill character. The next character must be either 'l' or 'r' and chooses wether the data is justified to left or right. Finally, the width itself which must be enclosed in single quotes if it is more than one digit.

Examples:

printf("His name is &.", << "foo" >>) (output: "His name is 'Foo'.")
printf("Foo: &.", << "bar" >>) (output: "Foo: '.......bar'.")

Formatted Input/Output for COLLECTION Objects

&<@...> = variable of type COLLECTION[ANY]

The main tag id (@) can be followed by several parameters, but there has to be at least one paramter, either 'n' (no separator) or the list separator enclosed in single quotes (to use single quote as a separator, enter ''). 'f' followed by a separator in single quotes sets the first separator and 'l' followed by a separator in single quotes sets the last separator. After these parameters, you must a tag to read an individual item in the collection (for example &s for a collection of strings).

Examples:

printf("The values are &<@l' and '', 'i>.", << << 1, 2, 3, 4, 5 >> >> )
printf("The strings are &<@','qs>.", << << "foo", "bar" >> >> )

Grouping/Degrouping COLLECTION Objects

COLLECTIONs can be grouped into subgroups. For example, to print a list of ten values as five value-pairs, would use a tag &<@@2', '(@','v> which would output << 1,2,3,4,5,6,7,8,9,0 >> as (1,2), (3,4), (5,6), (7,8), (9,0). Note that using the same tag for formatted input would reverse the operation.

Tags within Tags

Sometimes you might want to group variables into entities that are formatted separately. To make this look nice, there is the 'recursive' tag that allows you to place a piece of formatted i/o inside a formatting string.

&<&"..."> = the section within the double quotes is formatted as if it was a separate formatting string and the variable represented by it is an array of variables

Examples:

printf("Some data: &<(&%"&s = &%">.", << << key, value >> >>)
printf("Array of key/value pairs:%N&<@'%N'+/'  '[&%"&s = &v%">%N", << items >> )

Reporting Bugs

This is a very early version of FOIL, so there probably are a few bugs. Send any bug-reports or suggestions for improving the library to shang@st.jyu.fi

Sami
shang@st.jyu.fi
Nov 7, 1999

[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters ]