5. Segment

5.1. Struct

A wmp_segment stores a segment of text from the segment table.

<<typedefs>>=
typedef struct wmp_segment wmp_segment;
<<structs>>=
struct wmp_segment {
    unsigned int id;
    int type;
    char *str;
    int linum;
    char *filename;
    int nxtseg;
    int prog;
};

A wmp_segment can be queried by UUID using the function wmp_find_segment. If the return value is false (0), then there was a problem finding the entry.

The function wmp_find_segment allocates memory to store the segment text as a C-string. Because of this, a segment must be freed using the function wmp_segment_free.

5.2. init

A segment is initialized with wmp_segment_init.

<<function_declarations>>=
void wmp_segment_init(wmp_segment *s);
<<functions>>=
void wmp_segment_init(wmp_segment *s)
{
    s->id = 0;
    s->type = -1;
    s->str = NULL;
    s->linum = 0;
    s->filename = NULL;
    s->nxtseg = -1;
    s->prog = -1;
}

5.3. Free

<<function_declarations>>=
void wmp_segment_free(wmp_segment *s);
<<functions>>=
void wmp_segment_free(wmp_segment *s)
{
    free(s->str);
    free(s->filename);
}

5.4. Find

<<function_declarations>>=
int wmp_find_segment(wmp_core *c,
                     unsigned int uuid,
                     wmp_segment *s,
                     int prog);
<<functions>>=
int wmp_find_segment(wmp_core *c,
                     unsigned int uuid,
                     wmp_segment *s,
                     int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    int nbytes;
    const char *str;
    const char *fname;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "type,"
                         "str,"
                         "linum,"
                         "filename,"
                         "next_segment, "
                         "program "
                       "FROM segments "
                       "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) {
        s->id = uuid;
        s->type = sqlite3_column_int(stmt, 0);

        nbytes = sqlite3_column_bytes(stmt, 1);
        s->str = calloc(1, nbytes + 1);
        str = (const char *)sqlite3_column_text(stmt, 1);
        strncpy(s->str, str, nbytes);

        s->linum = sqlite3_column_int(stmt, 2);

        nbytes = sqlite3_column_bytes(stmt, 3);
        s->filename = calloc(1, nbytes + 1);
        fname = (const char *)sqlite3_column_text(stmt, 3);
        strncpy(s->filename, fname, nbytes);

        s->nxtseg = sqlite3_column_int(stmt, 4);
        s->prog = sqlite3_column_int(stmt, 5);
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return WMP_NOT_OK;
    }
    sqlite3_finalize(stmt);
    return WMP_OK;
}



prev | home | next