2. The Top Level Interface

The btprnt interface is one that handles all the low-level details.

<<typedefs>>=
typedef struct btprnt btprnt;

The btprnt struct contains a canvas, and a buffer.

<<structs>>=
struct btprnt {
    btprnt_buf *buf;
    btprnt_canvas *canvas;
};

Canvas can be retrieved using the function btprnt_canvas_get.

<<funcdefs>>=
btprnt_canvas *btprnt_canvas_get(btprnt *b);
<<funcs>>=
btprnt_canvas *btprnt_canvas_get(btprnt *b)
{
    return b->canvas;
}

The buffer can be retrieved using btprnt_buf_get.

<<funcdefs>>=
btprnt_buf * btprnt_buf_get(btprnt *b);
<<funcs>>=
btprnt_buf * btprnt_buf_get(btprnt *b)
{
    return b->buf;
}

The function btprnt_new will allocate and return a new btprnt instance. Internally, this will allocate and initialize the canvas and the buffer. The dimensions of the buffer are needed to be known at init time.

<<funcdefs>>=
btprnt * btprnt_new(int w, int h);
<<funcs>>=
btprnt * btprnt_new(int w, int h)
{
    btprnt *b;

    b = calloc(1, sizeof(btprnt));

    if (b == NULL) return NULL;

    b->buf = btprnt_buf_init(w, h);

    if (b->buf == NULL) {
        free(b);
        return NULL;
    }

    b->canvas = btprnt_canvas_new(b->buf);

    if (b->canvas == NULL) {
        btprnt_buf_free(&b->buf);
        free(b);
        return NULL;
    }
    return b;
}

The function btprnt_del will delete that which has been previously allocated.

<<funcdefs>>=
void btprnt_del(btprnt **b);

This pointer notation is a bit too clever for my peabrain, but it is very convenient. I will explain it while it is still fresh in my head:

b is a pointer to a pointer to btprnt (aka double star)

*b will return the pointer to btprnt. We need this to access things like canvas with (*b)->canvas.

The freeing functions take pointers to pointers (double stars), so we need to give it the address of the pointer &(*b)->canvas, NOT the pointer itself which would be (*b)->canvas.

<<funcs>>=
void btprnt_del(btprnt **b)
{
    if (*b == NULL) return;
    btprnt_canvas_del(&(*b)->canvas);
    btprnt_buf_free(&(*b)->buf);
    free(*b);
    *b = NULL;
}
<<funcdefs>>=
void btprnt_del(btprnt **b);



prev | home | next