10. Header

Gets header information.

10.1. struct

<<typedefs>>=
typedef struct wmp_header wmp_header;
<<structs>>=
struct wmp_header {
    int prog;
    int id;
    char *section;
    int level;
    char *name;
    char *filename;
    int linum;
    int next;
};

10.2. init

<<function_declarations>>=
void wmp_header_init(wmp_header *h);
<<functions>>=
void wmp_header_init(wmp_header *h)
{
    h->prog = -1;
    h->id = -1;
    h->section = NULL;
    h->level = -1;
    h->name = NULL;
    h->filename = NULL;
    h->linum = -1;
    h->next = -1;
}

10.3. free

<<function_declarations>>=
void wmp_header_free(wmp_header *h);
<<functions>>=
void wmp_header_free(wmp_header *h)
{
    if (h->section != NULL) {
        free(h->section);
        h->section = NULL;
    }

    if (h->name != NULL) {
        free(h->name);
        h->name = NULL;
    }

    if (h->filename != NULL) {
        free(h->filename);
        h->filename = NULL;
    }
}

10.4. find

<<function_declarations>>=
int wmp_header_find(wmp_core *c,
                    unsigned int uuid,
                    wmp_header *h,
                    int prog);
<<functions>>=
int wmp_header_find(wmp_core *c,
                    unsigned int uuid,
                    wmp_header *h,
                    int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "program, "
                         "id, "
                         "section, "
                         "level, "
                         "name , "
                         "filename , "
                         "linum , "
                         "next "
                       "FROM headers "
                       "WHERE(id==?1) AND "
                       "(program==?2);",
                       -1,
                       &stmt,
                       NULL);
    sqlite3_bind_int(stmt, 1, uuid);
    sqlite3_bind_int(stmt, 2, prog);

    rc = sqlite3_step(stmt);

    if (rc == SQLITE_DONE) {
        sqlite3_finalize(stmt);
        return 0;
    }

    if (rc == SQLITE_ROW) {
        h->prog = sqlite3_column_int(stmt, 0);
        h->id = sqlite3_column_int(stmt, 1);

        {
            int nbytes;
            const char *str;
            nbytes = sqlite3_column_bytes(stmt, 2);
            h->section = calloc(1, nbytes + 1);
            str = (const char *)sqlite3_column_text(stmt, 2);
            strncpy(h->section, str, nbytes);
        }

        h->level = sqlite3_column_int(stmt, 3);

        {
            int nbytes;
            const char *str;
            nbytes = sqlite3_column_bytes(stmt, 4);
            h->name = calloc(1, nbytes + 1);
            str = (const char *)sqlite3_column_text(stmt, 4);
            strncpy(h->name, str, nbytes);
        }

        {
            int nbytes;
            const char *str;
            nbytes = sqlite3_column_bytes(stmt, 5);
            h->filename = calloc(1, nbytes + 1);
            str = (const char *)sqlite3_column_text(stmt, 5);
            strncpy(h->filename, str, nbytes);
        }

        h->linum = sqlite3_column_int(stmt, 6);
        h->next = sqlite3_column_int(stmt, 7);
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return 0;
    }
    sqlite3_finalize(stmt);
    return 1;
}

10.5. get top header

Finds the topmost header in a program. This is assumed to be header chunk smallest ID.

This can be done with the following SQL query:

SELECT MIN(id) from headers where program == prog;

Where prog is the program id.

From there wmp_header_find can be called as usual.

<<function_declarations>>=
unsigned int wmp_header_top(wmp_core *c, wmp_header *h, int prog);
<<functions>>=
unsigned int wmp_header_top(wmp_core *c, wmp_header *h, int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    int top;

    db = wmp_core_db(c);

    top = 0;

    sqlite3_prepare_v2(db,
                       "SELECT MIN(id) from headers "
                       "where program == ?1;",
                       -1,
                       &stmt,
                       NULL);

    sqlite3_bind_int(stmt, 1, prog);

    rc = sqlite3_step(stmt);

    if (rc == SQLITE_DONE) {
        sqlite3_finalize(stmt);
        return 0;
    }

    if (rc == SQLITE_ROW) {
        top = sqlite3_column_int(stmt, 0);
    }

    sqlite3_finalize(stmt);

    if (h != NULL) {
        rc = wmp_header_find(c, top, h, prog);
        if (!rc) top = 0;
    }

    return top;
}



prev | home | next