示例#1
1
文件: workspace.cpp 项目: Quetuo/boef
int workspace_choose (char * ws) {
    char * buf = (char *) malloc (25 + strlen (ws));
    sprintf (buf, "Opening workspace %s...\n", ws);
    interface_log (buf);
    free (buf);
#ifdef WITH_SQLITE3
    sqlite3_stmt * stmt;
    if (sqlite3_prepare (db, "SELECT id FROM workspace WHERE name=?;", -1, &stmt, 0) != SQLITE_OK) {
        interface_error ("Failed to SELECT\n");
        return -1;
    }
    if (sqlite3_bind_text (stmt, 1, (const char *) ws, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
        interface_error ("Failed to bind\n");
        return -1;
    }
    if (sqlite3_step (stmt) == SQLITE_DONE) {
        sqlite3_finalize (stmt);
        if (sqlite3_prepare (db, "INSERT INTO workspace (name) VALUES (?);", -1, &stmt, 0) != SQLITE_OK) {
            interface_error ("Failed to INSERT\n");
            return -1;
        }
        if (sqlite3_bind_text (stmt, 1, (const char *) ws, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
            interface_error ("Failed to bind\n");
            return -1;
        }
        sqlite3_step (stmt);
        sqlite3_finalize (stmt);
        workspace_choose (ws);
        workspace_log ("Workspace created");
        return 0;
    }
    workspace_id = sqlite3_column_int (stmt, 0);
    sqlite3_finalize (stmt);
    workspace = ws;
    return 0;
#else
    if (file) {
        fclose (file);
        file = NULL;
    }
    workspace_file = (char *) malloc (strlen (workspace_dir) + strlen (ws) + 6);
    sprintf (workspace_file, "%s/%s.wsp", workspace_dir, ws);
    file = fopen (workspace_file, "a");
    if (file == NULL) {
        char * buf = (char *) malloc (27 + strlen (workspace_file) + strlen (strerror (errno)));
        sprintf (buf, "Could not fopen(2) %s: %s\n", workspace_file, strerror (errno));
        interface_error (buf);
        free (buf);
        return -1;
    }
    workspace = ws;
    return 0;
#endif
}
示例#2
0
/**
 * Log listener.
 *
 */
void
listener_log(listener_type* listener)
{
    uint16_t i = 0;
    if (!listener || listener->count <= 0) {
        return;
    }
    for (i=0; i < listener->count; i++) {
        interface_log(&listener->interfaces[i]);
    }
    return;
}
示例#3
0
文件: workspace.cpp 项目: Quetuo/boef
void workspace_init () {
    // Get path to workspace (~/.boef)
    workspace_home = getpwuid (getuid ()) -> pw_dir;
    workspace_dir = (char *) malloc (strlen (workspace_home) + 7);
    sprintf (workspace_dir, "%s/.boef", workspace_home);
    char * buf1 = (char *) malloc (28 + strlen (workspace_dir));
    sprintf (buf1, "Opening workspace in %s...\n", workspace_dir);
    interface_log (buf1);
    free (buf1);

    // Check whether it exists and create it if it doesn't
    struct stat buf;
    if (stat ((const char *) workspace_dir, &buf) == -1) {
        if (errno == ENOENT) {
            if (mkdir (workspace_dir, S_IRWXU | S_IRGRP | S_IROTH) == -1) {
                char * buf = (char *) malloc (27 + strlen (workspace_dir) + strlen (strerror (errno)));
                sprintf (buf, "Could not mkdir(2) %s: %s\n", workspace_dir, strerror (errno));
                interface_error (buf);
                free (buf);
                exit (1);
            }
        }
        else {
            char * buf = (char *) malloc (27 + strlen (workspace_dir) + strlen (strerror (errno)));
            sprintf (buf, "Could not stat(2) %s: %s\n", workspace_dir, strerror (errno));
            interface_error (buf);
            free (buf);
            exit (1);
        }
    }

#ifdef WITH_SQLITE3
    // Open database
    workspace_db = (char *) malloc (strlen (workspace_dir) + 7);
    sprintf (workspace_db, "%s/db.sl3", workspace_dir);
    if (sqlite3_open (workspace_db, &db)) {
        interface_error ("Failed to open SQLite database\n");
        exit (1);
    }

    int error = sqlite3_exec (db, "CREATE TABLE IF NOT EXISTS workspace(id INTEGER PRIMARY KEY ASC, name TEXT);", 0, 0, 0)
                | sqlite3_exec (db, "CREATE TABLE IF NOT EXISTS log(workspace_id INTEGER, value TEXT);", 0, 0, 0)
                | sqlite3_exec (db, "CREATE TABLE IF NOT EXISTS option(workspace_id INTEGER, name TEXT, value TEXT);", 0, 0, 0)
                | sqlite3_exec (db, "DELETE FROM option WHERE workspace_id=1;", 0, 0, 0);
    if (error != SQLITE_OK) {
        interface_error ("Failed to update database\n");
        exit (1);
    }
#endif
}
示例#4
0
文件: workspace.cpp 项目: Quetuo/boef
void workspace_cleanup () {
    interface_log ("Cleaning up workspace...");
#ifdef WITH_SQLITE3
    if (db != NULL) {
        sqlite3_close (db);
        db = NULL;
    }
#else
    if (file != NULL) {
        fclose (file);
        file = NULL;
    }
#endif
}