home *** CD-ROM | disk | FTP | other *** search
- This package is a set of routines for formatting data in a more flexible
- way than UNIX's printf routines. In the simplest case:
-
- format("This is a test.\n");
-
- works the way
-
- printf("This is a test.\n");
-
- does. However, printf uses percents (%) to denote substitutions. Format
- encloses substitutions with braces ({ and }). I.E. {s} denotes "substitute
- the next argument as a string." After the letter denoting the substitution
- type, modifiers may be supplied. {sl5} means print a string left justified
- to 5 characters. If the value is not supplied, it is taken from the
- argument list. The EBNF for this is:
-
- '{' <type-letter> { <modifier-character> [ <modifier-value> ] } '}'
-
- Available type letters so far are:
-
- s string (Char *)
- i int
- l long
- c character
- f float (double)
- S Another format string (I smell recursion)
-
- Available modifers (and their defaults) are:
-
- l(0) Number of columns to take up, value will be left justified
- r(0) Number of columns to take up, value will be right justified
- c(0) Number of columns to take up, value will be centered
- m(0) Maximum number of columns display of data
- .(6) Number of places to display to the right of the decimal point
- b(10) Base of number
- p(32) Ascii value of pad character for l, r or c.
- u(0) Unsigned status (signed=0, unsigned!=0)
- n(1) Count of times to repeat string
-
- If a modifer value is followed by a string of digits then that string is
- converted to an integer and used as the modifier's value, else the value will
- be taken from the next argument to format.
-
- Some ridiculous variable type/modifer combinations exist:
- What is an unsigned string (or character) of base 8 with 4 trailing
- decimal places? (All three modifiers are ignored)
- Do you really want to see floating point variables in base 3? (Yes!)
- What does centering a left or right justifed variable look like?
- (The last justification character is the one used)
-
- Now, an example program:
-
- main()
- {
- int i;
-
- format("Left justified integer: '{il10}'\n",1234);
- format("###{f.1c}###\n",123.4,20);
- format("Octal: {lb8r11pu1}\n",01234L,'0');
- format("Truncate this: {sm10}\n", "This is a test" );
- format("Print 5 arfs: {sn5}\n","arf");
- i = 1;
- format("Do it {i} time{sn}.\n",i,"s",i!=1);
- i = 2;
- format("Do it {i} time{sn}.\n",i,"s",i!=1);
- format("Center this: ###{Sc20p}###\n","Answer={i}",'$',1234);
- exit(0);
- }
-
- and its standard output:
-
- Left justified integer: '1234 '
- ### 123.4 ###
- Octal: 00000001234
- Truncate this: This is a
- Print 5 arfs: arfarfarfarfarf
- Do it 1 time.
- Do it 2 times.
- Center this: ###$$$$$Answer=1234$$$$###
-
- The following flavors of format are available:
-
- format( formatstring, args... ) Send to standard out
- fformat( stream, formatstring, args... )Send to specified stream
- i = cformat( formatstring, args... ) Calculate number of chars
- sformat( buf, formatstring, args... ) Send to the character array buf
- ptr = mformat( formatstring, args... ) Malloc enough space for result
- (and trailing 0), put the
- result in malloced space, and
- return pointer to it
- Also, as a kluge:
- ptr = sformat( NULL, formatstring, args... )
-
- mformat (and sformat with a NULL array), perform the format twice,
- once to find out how many characters needed to malloc, and the next
- time to fill up the array.
-
- Advantages:
- Easier to understand justification
- Centering available
- More flexible padding (instead of just spaces or zeros)
- Arbitrary base output (2 to 36)
- Complex formats available ("S")
- Arbitrarily lengthed everything (up do what you can malloc)
- And from my point of view, I have the source!
-