Libfront reference manual

See also:

Book

A book is an advanced string type. What book offers over your regular strings is the ability to automatically indent, align, and to select parts of the string that can be written to later, so called sections. It also includes a very powerful printf-like function that supports all of these constructions.

Sections:

Construction and Destruction

fe_book_start

#include <front/book.h>

fe_book_t *fe_book_start (void);
Create a new, empty book.

See also:

fe_book_add_section, fe_book_free

fe_book_add_section

#include <front/book.h>

book_t *fe_book_add_section (fe_book_t *book);
Add a new, empty section at the end of book. This section is in itself another book that can be written to, sub-sectioned, even converted to a string. All settings, like indentation and alignment, applied to the section are local, they do not change the settings of the "master" book.

See also:

fe_book_start, fe_book_free, fe_book_flattened

fe_book_free

#include <front/book.h>

void fe_book_free (fe_book_t *book);
Free all memory occupied by book.

See also:

fe_book_start, fe_book_add_section

Writing in a book

fe_book_putc

#include <front/book.h>

void fe_book_putc (fe_book_t *book, char c);
Write character c at the end of book.

See also:

fe_book_putnc, fe_book_puts, fe_book_putns

fe_book_putnc

#include <front/book.h>

void fe_book_putnc (fe_book_t *book, int n, char c);
Write n times the character c at the end of book.

See also:

fe_book_putc, fe_book_puts, fe_book_putns

fe_book_puts

#include <front/book.h>

void fe_book_puts (fe_book_t *book, const char *s);
Write string s at the end of book.

See also:

fe_book_putc, fe_book_putnc, fe_book_putns, fe_book_printf

fe_book_putns

#include <front/book.h>

void fe_book_putns (fe_book_t *book, int n, const char *s);
Write at most n characters of string s at the end of book.

See also:

fe_book_putc, fe_book_putnc, fe_book_puts

fe_book_printf

#include <front/book.h>

void fe_book_printf (fe_book_t *book, char *fmt, ...);
va_list fe_book_vprintf (fe_book_t *book, char *fmt, va_list args);
A printf function that writes the resulting string at the end of book. fe_book_vprintf returns an argument list containing the remaining, unread arguments. Beside the normal printf conversions, this function also recognizes the following:
%&
%+&
Add a section at the place of the conversion. The argument has to have type fe_book_t ** in which the section pointer will be stored.

With the + flag the section will be indented using fe_book_indent.

%=
%#=
%+=
%-=
Change the indentation. Without a flag the indentation is set to the current column, using fe_book_indent_here. The old indentation is written to the argument, which must have type int *.

With the # flag, set the indentation to the column specified by the argument, of type int *, using fe_book_set_indent. The old indentation is written to the argument.

With the + flag, the indentation is increased, using fe_book_indent. No argument is consumed.

With the - flag, the indentation is decreased, using fe_book_undent. No argument is consumed.

%| Set an alignment specification, using fe_book_align. The width field can be used to specify an alignment specification number, the default is 0.
%n
%#n
The argument supplied, which must have type char *, contains another format string, arguments required by this format string are taken from the remaining arguments. By default a new-line will be added before the formatted string, to disable this behaviour add the # flag.

If a width is specified in the conversion, the new-line will only be added if the current column is greater than the specified width.

%B Add the string of the book specified by the argument, which must has type fe_book_t *. This conversion recognizes the same flags as %s

Example

#include <stdio.h>
#include <front/book.h>

void
main(void)
{
  fe_book_t *book = fe_book_start ();
  fe_book_t *body = NULL;
  int ind;
  fe_book_printf (book,
                  "void%n",
                  "main(%=int arg1, %70n", &ind,
                  "int arg2, %70n",
                  "int arg3, %70n",
                  "int arg4, %70n",
                  "int arg5, %70n",
                  "int arg6, %70n",
                  "int arg7, %70n",
                  "int arg8, %70n",
                  "int arg9, %70n",
                  "int arg10, %70n",
                  "int arg11, %70n",
                  "int arg12, %70n",
                  "int arg13, %70n",
                  "int arg14)%#=%n", &ind,
                  "{\n"
                  "%+&"
                  "}\n",&body);
  
  fe_book_printf (body, "printf (\"Hello, world\\n\");\n");
  printf ("%s", fe_book_to_string (book));
}
This program will print the following text:
void
main(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, 
     int arg8, int arg9, int arg10, int arg11, int arg12, int arg13, int arg14)
{
  printf ("Hello, world\n");
}

See also:

fe_book_puts

fe_book_register_conversion

#include <front/book.h>

void fe_book_register_conversion (char c, fe_conversion_function f);
Add the conversion c to the fe_book_printf function. When this conversion occurs in a format string the function f is called. The type fe_conversion_function is defined as follows: typedef struct {
int flag_alternate : 1;
int flag_zero : 1;
int flag_minus : 1;
int flag_blank : 1;
int flag_plus : 1;
int width;
int precision;
enum {
fe_mod_hh,
fe_mod_h,
fe_mod_none,
fe_mod_l,
fe_mod_ll,
fe_mod_L
} modifier;
char conversion;
} fe_conversion_spec_t;

typedef va_list (*fe_conversion_function)(fe_book_t *, fe_conversion_spec_t *, va_list);
A format conversion has the following format Known flags are
#flag_alternatePrint an alternate form
0flag_zero When the field is right justified, pas with the '0' character
-flag_minus Left justify the field
flag_blank Pad the field with spaces ' '
+flag_plus Always print the sign
All flags are optional.

The width specifies the minimum width of the field. If the conversion is smaller the field is justified and padded according to the flags, default is blank padded right justified. If the width has not been specified, the width will be 0.

When the width was specified using a '*' the width will have been read from the arguments. If this value was negative, the - flag will be enabled, and the width will contain the absolute value.

The precision is specified by a '.' followed by either a number or a '*' character. The meaning varies between conversions. If the precision has not been specified, it will be negative.

The modifier is optional and can be one of the following: hh, h, l, ll, and L. The corresponding values of modifier are obvious.

The conversion is the character that identifies the actual conversion.

Indentation

fe_book_set_indent_size

#include <front/book.h>

void fe_book_set_indent_size (fe_book_t *book, int size);
Use size spaces for indentation in book, the default is 2.

fe_book_indent

#include <front/book.h>

void fe_book_indent (fe_book_t *book);
Increase the indentation. Indentation occurs just before the first character of a line is written, so changing the indentation after writing a new-line character affects the same line. By default this function indents with 2 spaces. This can be changed using fe_book_set_indent_size.

See also:

fe_book_undent, fe_book_set_indent_size

fe_book_undent

#include <front/book.h>

void fe_book_undent (fe_book_t *book);
Decrease the indentation. Indentation occurs just before the first character of a line is written, so changing the indentation after writing a new-line character affects the same line. By default this function indents with 2 spaces. This can be changed using fe_book_set_indent_size.

See also:

fe_book_indent, fe_book_set_indent_size

fe_book_set_indent

#include <front/book.h>

int fe_book_set_indent (fe_book_t *book, int column);
Set the indentation to column. Indentation occurs just before the first character of a line is written, so changing the indentation after writing a new-line character affects the same line.

Result

This function returns the old indentation column.

See also:

fe_book_indent, fe_book_set_indent_size

fe_book_indent_here

#include <front/book.h>

int fe_book_indent_here (fe_book_t *book);
Set the indentation to the current column of book. Indentation occurs just before the first character of a line is written, so changing the indentation after writing a new-line character affects the same line.

Result

This function returns the old indentation column.

See also:

fe_book_indent, fe_book_column, fe_book_set_indent_size

Alignment

fe_book_set_indent

#include <front/book.h>

int fe_book_align (fe_book_t *book, int number);
Add an alignment specification identified by number. All alignment specification with this number will be aligned to the same column.

Retrieving the text

fe_book_to_string

#include <front/book.h>

char *fe_book_to_string (const fe_book_t *book);
Create a string containing the entire text of book. This function does not change book in any way. The caller must make sure that the string is freed afterwards.

See also:

fe_book_to_string, fe_book_flattened

fe_book_to_file

#include <front/book.h>

void fe_book_to_file (const fe_book_t *book, FILE *fs);
Write the entire text of book to the file stream fs. This function does not change book in any way.

See also:

fe_book_to_string, fe_book_flattened

fe_book_flattened

#include <front/book.h>

book_t *fe_book_flattened (const fe_book_t *book);
Create a new book containing the entire text of book in one section . This function does not change book in any way. The caller must make sure that the new book is freed afterwards.

See also:

fe_book_to_string

Miscellaneous

fe_book_column

#include <front/book.h>

int fe_book_column (const fe_book_t *book);
Get the current column of book. Lines start at column 0. The current column is the column where the next character will be written.

fe_book_position

#include <front/book.h>

char *fe_book_position (const fe_book_t *book);
Get a pointer to where the next character will be written in book. This pointer may be invalid after the next time something is written to the book.