6. Block

6.1. struct

wmp_block stores a reference to a codeblock.

<<typedefs>>=
typedef struct wmp_block wmp_block;
<<structs>>=
struct wmp_block {
    int id;
    int head_segment;
    char *name;
    int prog;
    int nblocks;
    int nsegs;
};

6.2. init

It is initialized using wmp_block_init.

<<function_declarations>>=
void wmp_block_init(wmp_block *b);
<<functions>>=
void wmp_block_init(wmp_block *b)
{
    b->id = -1;
    b->head_segment = -1;
    b->name = NULL;
    b->prog = -1;
    b->nblocks = -1;
    b->nsegs = -1;
}

6.3. free

It is freed using wmp_block_free.

<<function_declarations>>=
void wmp_block_free(wmp_block *b);
<<functions>>=
void wmp_block_free(wmp_block *b)
{
    free(b->name);
}

6.4. find

It is queried using wmp_find_block.

<<function_declarations>>=
int wmp_find_block(wmp_core *c,
                   unsigned int uuid,
                   wmp_block *b,
                   int prog);
<<functions>>=
int wmp_find_block(wmp_core *c,
                   unsigned int uuid,
                   wmp_block *b,
                   int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    int nbytes;
    const char *str;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "head_segment,"
                         "name, "
                         "program, nblocks, nsegs "
                       "FROM blocks "
                       "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 WMP_NOT_OK;
    }

    if (rc == SQLITE_ROW) {
        b->id = uuid;
        b->head_segment = sqlite3_column_int(stmt, 0);
        nbytes = sqlite3_column_bytes(stmt, 1);
        b->name = calloc(1, nbytes + 1);
        str = (const char *)sqlite3_column_text(stmt, 1);
        strncpy(b->name, str, nbytes);
        b->prog = sqlite3_column_int(stmt, 2);
        b->nblocks = sqlite3_column_int(stmt, 3);
        b->nsegs = sqlite3_column_int(stmt, 4);
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return WMP_NOT_OK;
    }
    sqlite3_finalize(stmt);
    return WMP_OK;
}

6.5. lookup

A block can also be found by querying the name via the function wmp_lookup_block.

<<function_declarations>>=
int wmp_lookup_block(wmp_core *c,
                     const char *name,
                     wmp_block *b,
                     int prog);
<<functions>>=
int wmp_lookup_block(wmp_core *c,
                     const char *name,
                     wmp_block *b,
                     int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    int nbytes;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "id,"
                         "head_segment, nblocks, nsegs "
                       "FROM blocks "
                       "WHERE(name==?1) AND "
                       "(program == ?2);",
                       -1,
                       &stmt,
                       NULL);
    sqlite3_bind_text(stmt, 1, name, strlen(name), NULL);
    sqlite3_bind_int(stmt, 2, prog);

    rc = sqlite3_step(stmt);

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

    if(rc == SQLITE_ROW) {
        b->id = sqlite3_column_int(stmt, 0);
        b->head_segment = sqlite3_column_int(stmt, 1);
        nbytes = strlen(name);
        b->name = calloc(1, nbytes + 1);
        strncpy(b->name, name, nbytes);
        b->prog = prog;
        b->nblocks = sqlite3_column_int(stmt, 2);
        b->nsegs = sqlite3_column_int(stmt, 3);
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return 0;
    }
    sqlite3_finalize(stmt);
    return 1;
}



prev | home | next