Ejemplo n.º 1
0
/* libdatabase_database_delete */
static void _libdatabase_database_delete(PyObject * self)
{
    Database * database;

    if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
            == NULL)
        return;
    database_delete(database);
}
Ejemplo n.º 2
0
void database_delete(struct Connection *conn, int id) {
	struct Address addr = {.id=id, .set=0};
	conn->db->rows[id] = addr;
}

void database_list(struct Connection *conn) {
	for(int i=0; i<MAX_ROWS; ++i) {
		struct Address *addr = &conn->db->rows[i];
		if(addr->set)
			address_print(addr);
	}
}

int main(int argc, char *argv[]) {
	if(argc < 3)
		die("usage: a.out file action additional_params");

	char *filename = argv[1];

	char action = argv[2][0];
	struct Connection *conn = database_open(filename, action);
	int id=0;

	if(argc > 3) id = atoi(argv[3]);
	if(id >= MAX_ROWS) die("record id too high");

	switch (action) {
		case 'c':
			database_create(conn);
			database_write(conn);
			break;
		case 'g':
			if(argc!=4) die("need an id for database_get()");
			database_get(conn,id);
			break;
		case 's':
			if(argc!=6) die("need id, name, email for database_set()");
			database_set(conn,id,argv[4],argv[5]);
			database_write(conn);
			break;
		case 'd':
			if(argc!=4) die("need id to delete");
			database_delete(conn,id);
			database_write(conn);
			break;
		case 'l':
			database_list(conn);
			break;
		default:
			die("invalid action: c=create, g=get, s=set, d=delete, l=list");
	}

	database_close(conn);
	return 0;
}
Ejemplo n.º 3
0
void database_create(struct Connection *conn)
{
	int i = 0;

	for(i = 0; i < MAX_ROWS; i++) {
		// make a prototype to initialize the database
		struct Address addr = {.id = i, .set = 0};
		// then assign it to conn
		conn->db->rows[i] = addr;
	}
	printf("got to bottom of database create");
}

void database_set(struct Connection *conn, int id, const char *name,
                  const char *email)
{
	struct Address *addr = &conn->db->rows[id];
	if(addr->set) die("Already set, delete it first");

	addr->set = 1;
	// WARNING FIX THIS
	char *res = strncpy(addr->name, name, MAX_DATA);
	// demonstrate that strncpy bug
	if(!res) die("Name copy failed");

	res = strncpy(addr->email, email, MAX_DATA);
	if(!res) die("Email copy failed");
}

void database_get(struct Connection *conn, int id)
{
	struct Address *addr = &conn->db->rows[id];
	
	if(addr->set) {
		address_print(addr);
	} else {
		die("ID is not set");
	}
}

void database_delete(struct Connection *conn, int id)
{
	struct Address addr = {.id = id, .set = 0};
	conn->db->rows[id] = addr;
}

void database_list(struct Connection *conn)
{
	int i = 0;
	struct Database *db = conn->db;

	for(i = 0; i < MAX_ROWS; i++) {
		struct Address *cur = &db->rows[i];

		if(cur->set) {
			address_print(cur);
		}
	}
}

int main(int argc, char *argv[])
{
    if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");

    char *filename = argv[1];
    char action = argv[2][0];
    struct Connection *conn = database_open(filename, action);
    int id = 0;

    if(argc > 3) id = atoi(argv[3]);
    if(id >= MAX_ROWS) die("There's not that many records.");

    switch(action) {
        case 'c':
            database_create(conn);
            database_write(conn);
            break;

        case 'g':
            if(argc != 4) die("Need an id to get");

            database_get(conn, id);
            break;

        case 's':
            if(argc != 6) die("Need id, name, email to set");

            database_set(conn, id, argv[4], argv[5]);
            database_write(conn);
            break;

        case 'd':
            if(argc != 4) die("Need id to delete");

            database_delete(conn, id);
            database_write(conn);
            break;

        case 'l':
            database_list(conn);
            break;
        default:
            die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
    }

    database_close(conn);

    return 0;
}