3. wmp_core

3.1. Struct

SQLite data is indirectly handled inside of a struct called wmp_core. That's all it does for now, but I fully expect things to change in the future.

<<typedefs>>=
typedef struct wmp_core wmp_core;
<<structs>>=
struct wmp_core {
    sqlite3 *db;
};

3.2. DONE Opening the database

CLOSED: [2019-08-31 Sat 06:09] The database handled is opened with the function wmp_core_open.

<<function_declarations>>=
int wmp_core_open(wmp_core *core, const char *filename);
<<functions>>=
int wmp_core_open(wmp_core *core, const char *filename)
{
    sqlite3 *db;
    int rc;
    FILE *fp;

    fp = fopen(filename, "r");
    if (fp == NULL) {
        fprintf(stderr,
                "Database %s not found.\n",
                filename);
        return 0;
    }
    fclose(fp);

    core->db = NULL;
    rc = sqlite3_open(filename, &db);
    if (rc) {
        fprintf(stderr,
                "Could not open database: %s",
                sqlite3_errmsg(db));
        sqlite3_close(db);
        return 0;
    }

    core->db = db;
    return 1;
}

3.3. DONE Closing the database

CLOSED: [2019-08-31 Sat 06:10] The database handle is closed with the function wmp_core_close.

<<function_declarations>>=
void wmp_core_close(wmp_core *core);
<<functions>>=
void wmp_core_close(wmp_core *core)
{
    sqlite3_close(core->db);
}

3.4. DONE Getting SQLite data type

CLOSED: [2019-08-31 Sat 06:13] Retrieved using the function wmp_core_db.

This will only be defined if the sqlite3 header file is included before this header. Otherwise, including sqlite3 would be a prereq for anything wanting to include core.h.

<<function_declarations>>=
#ifdef SQLITE3_H
sqlite3 * wmp_core_db(wmp_core *core);
#endif
<<functions>>=
sqlite3 * wmp_core_db(wmp_core *core)
{
    return core->db;
}

3.5. DONE Default Filename

CLOSED: [2019-08-31 Sat 06:21] To make the CLI more terse, a default filename is used, which can be configured through command line flags.

3.6. Global Definition

The default filename is a.db.

<<constants>>=
const char *db_filename = "a.db";

3.7. Setters/Getters

<<function_declarations>>=
const char * wmp_filename_get(void);
void wmp_filename_set(const char *filename);
<<functions>>=
const char * wmp_filename_get(void)
{
    return db_filename;
}

void wmp_filename_set(const char *filename)
{
    db_filename = filename;
}



prev | home | next