[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters ]
![]() |
FOIL Library |
Written by Sami Hangaslammi.
foil-0-1.zip (22 kbytes) source code
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
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.
It is very easy to create customised tags, but here is a short overview of the standard tags provided with the formatter class.
&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)
&
Examples:
printf("His name is &s and he is &i years old.", << name, age >>)
printf("&s is &", << "pi", pi >>)
Some tags modify the formatting of another tag. Most modifier tags are mainly used when formatting COLLECTION objects.
&
&
&
&<(...> = 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")
&
= adds surrounding whitespaces if used in formatted output
&
Examples:
printf("His name is &.", << "foo" >>) (output: "His name is 'Foo'.")
printf("Foo: &.", << "bar" >>) (output: "Foo: '.......bar'.")
&<@...> = 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" >> >> )
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.
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 >> )
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
[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters ]