7. File

7.1. Struct

A wmp_file is a record containing the name of a file to tangle to.

<<typedefs>>=
typedef struct wmp_file wmp_file;
<<structs>>=
struct wmp_file {
    unsigned int id;
    char *filename;
    int top;
    int next_file;
    int prog;
};

7.2. init

It is initialized using wmp_file_init.

<<function_declarations>>=
void wmp_file_init(wmp_file *f);
<<functions>>=
void wmp_file_init(wmp_file *f)
{
    f->id = 0;
    f->filename = NULL;
    f->next_file = -1;
    f->prog = -1;
}

7.3. Free

It is freed using wmp_file_free.

<<function_declarations>>=
void wmp_file_free(wmp_file *f);
<<functions>>=
void wmp_file_free(wmp_file *f)
{
    free(f->filename);
}

7.4. Find

It is queried using wmp_find_file.

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

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "filename,"
                         "next_file, "
                         "top "
                       "FROM files "
                       "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) {
        f->id = uuid;
        nbytes = sqlite3_column_bytes(stmt, 0);
        f->filename = calloc(1, nbytes + 1);
        f->next_file = sqlite3_column_int(stmt, 1);
        str = (const char *)sqlite3_column_text(stmt, 0);
        strncpy(f->filename, str, nbytes);
        f->top = sqlite3_column_int(stmt, 2);
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return 0;
    }
    sqlite3_finalize(stmt);
    return 1;
}

7.5. Lookup

A file can be queried by name directly via the function wmp_lookup_file.

<<function_declarations>>=
int wmp_lookup_file(wmp_core *c,
                     const char *filename,
                     wmp_file *f);
<<functions>>=
int wmp_lookup_file(wmp_core *c,
                     const char *filename,
                     wmp_file *f)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    int nbytes;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "id,"
                         "next_file, "
                         "top, "
                         "program "
                       "FROM files "
                       "WHERE(filename==?1);",
                       -1,
                       &stmt,
                       NULL);
    sqlite3_bind_text(stmt, 1,
                      filename, strlen(filename),
                      NULL);

    rc = sqlite3_step(stmt);

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

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

7.6. Top File

Gets top file from the files column. Useful if you want to iterate throught the file list.

<<function_declarations>>=
int wmp_file_top(wmp_core *c, wmp_file *f, int prog);
<<functions>>=
int wmp_file_top(wmp_core *c, wmp_file *f, int prog)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    int nbytes;
    const char *filename;

    db = wmp_core_db(c);

    sqlite3_prepare_v2(db,
                       "SELECT "
                         "id,"
                         "filename,"
                         "next_file, "
                         "top "
                       "FROM files "
                       "WHERE (program == ?1) "
                       "LIMIT 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) {
        f->id = sqlite3_column_int(stmt, 0);
        f->next_file = sqlite3_column_int(stmt, 2);
        nbytes = sqlite3_column_bytes(stmt, 1);
        f->filename = calloc(1, nbytes + 1);
        filename = (const char *)sqlite3_column_text(stmt, 1);
        strncpy(f->filename, filename, nbytes);
        f->top = sqlite3_column_int(stmt, 2);
    } else {
        fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return 0;
    }
    sqlite3_finalize(stmt);
    return 1;
}



prev | home | next