4. Resource

4.1. Struct

A wmp_resource is a record from the resource table. Any thing that is a thing has a record here. The only things that a resource stores is the UUID and the type flag.

<<typedefs>>=
typedef struct wmp_resource wmp_resource;
<<structs>>=
struct wmp_resource {
    unsigned int id;
    int type;
    int prog;
};
<<function_declarations>>=
void wmp_resource_init(wmp_resource *r);
<<functions>>=
void wmp_resource_init(wmp_resource *r)
{
    r->id = 0;
    r->type = -1;
    r->prog = -1;
}

4.2. Finding a Resource

The function wmp_find_resource will find a resource with a particular UUID and place store the values in the wmp_resource struct. If the return value is false (0), then there was a problem finding the resource.

<<function_declarations>>=
int wmp_find_resource(wmp_core *c,
                      unsigned int uuid,
                      wmp_resource *r,
                      int prog);

No free function is needed here, since this struct does not need to store any strings.

<<functions>>=
int wmp_find_resource(wmp_core *c,
                      unsigned int uuid,
                      wmp_resource *r,
                      int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int type;
    int rc;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT type FROM resources "
                       "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) {
        type = sqlite3_column_int(stmt, 0);

        r->id = uuid;
        r->type = type;
        r->prog = prog;
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return 0;
    }
    sqlite3_finalize(stmt);
    return 1;
}

4.3. Resource Type to String

Returns a human readable string from the type.

<<function_declarations>>=
const char *wmp_resource_typestring(wmp_resource *r);
<<functions>>=
const char *wmp_resource_typestring(wmp_resource *r)
{
    switch(r->type) {
        case 0:
            return "File";
        case 1:
            return "Block";
        case 2:
            return "Segment";
    }
    return "Unknown";
}

4.4. Find last significant ID of program

The last ID of a program can be reasonably assumed to be the greatest ID of a program, this can be found using the MAX argument. The last significant id of a program can be the largest resource ID whose type is one of the org structure types: header, content, or block reference. By the time it reaches this ID, it knows it is done rendering documents. Any of the resources with a larger ID will be rendered implicitely (such as a segment inside of a code block). The MAX SQLite function is still used, but on a special view created for this very purpose called orglist.

<<function_declarations>>=
unsigned int wmp_resource_last(wmp_core *c, wmp_resource *r, int prog);
<<functions>>=
unsigned int wmp_resource_last(wmp_core *c, wmp_resource *r, int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    unsigned int last;

    db = wmp_core_db(c);

    last = 0;

    sqlite3_prepare_v2(db,
                       "SELECT MAX(id) from orglist "
                       "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) {
        last = sqlite3_column_int(stmt, 0);
    }

    if (r != NULL) {
        rc = wmp_find_resource(c, last, r, prog);
        if (!rc) last = 0;
    }

    sqlite3_finalize(stmt);

    return last;
}

4.5. Find top ID of program

Needed for headerless single-page renders in weewiki. Similar to wmp_header_top.

<<function_declarations>>=
unsigned int wmp_resource_top(wmp_core *c, wmp_resource *r, int prog);
<<functions>>=
unsigned int wmp_resource_top(wmp_core *c, wmp_resource *r, 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 resources "
                       "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 (r != NULL) {
        rc = wmp_find_resource(c, top, r, prog);
        if (!rc) top = 0;
    }

    return top;
}



prev | home | next