augmenting "die" to close db connection

This commit is contained in:
Dan Buch 2011-09-21 22:30:46 -04:00
parent 198b54d316
commit c5a1f90200

40
ex17.c
View File

@ -23,7 +23,9 @@ struct Connection {
struct Database *db; struct Database *db;
}; };
void die(const char *message) void Database_close(struct Connection *conn);
void die(const char *message, struct Connection *conn)
{ {
if(errno) { if(errno) {
perror(message); perror(message);
@ -31,6 +33,8 @@ void die(const char *message)
printf("ERROR: %s\n", message); printf("ERROR: %s\n", message);
} }
Database_close(conn);
exit(1); exit(1);
} }
@ -43,16 +47,16 @@ void Address_print(struct Address *addr)
void Database_load(struct Connection *conn) void Database_load(struct Connection *conn)
{ {
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file); int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to load database."); if(rc != 1) die("Failed to load database.", conn);
} }
struct Connection* Database_open(const char *filename, char mode) struct Connection* Database_open(const char *filename, char mode)
{ {
struct Connection *conn = malloc(sizeof(struct Connection)); struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error"); if(!conn) die("Memory error", conn);
conn->db = malloc(sizeof(struct Database)); conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error"); if(!conn->db) die("Memory error", conn);
if(mode == 'c') { if(mode == 'c') {
conn->file = fopen(filename, "w"); conn->file = fopen(filename, "w");
@ -64,14 +68,14 @@ struct Connection* Database_open(const char *filename, char mode)
} }
} }
if(!conn->file) die("Failed to open the file"); if(!conn->file) die("Failed to open the file", conn);
return conn; return conn;
} }
void Database_close(struct Connection *conn) void Database_close(struct Connection *conn)
{ {
if(conn) { if(conn != NULL) {
if(conn->file) fclose(conn->file); if(conn->file) fclose(conn->file);
if(conn->db) free(conn->db); if(conn->db) free(conn->db);
free(conn); free(conn);
@ -83,10 +87,10 @@ void Database_write(struct Connection *conn)
rewind(conn->file); rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file); int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to write database."); if(rc != 1) die("Failed to write database.", conn);
rc = fflush(conn->file); rc = fflush(conn->file);
if(rc == -1) die("Cannot flush database."); if(rc == -1) die("Cannot flush database.", conn);
} }
void Database_create(struct Connection *conn) void Database_create(struct Connection *conn)
@ -104,15 +108,15 @@ void Database_create(struct Connection *conn)
void Database_set(struct Connection *conn, int id, const char *name, const char *email) void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{ {
struct Address *addr = &conn->db->rows[id]; struct Address *addr = &conn->db->rows[id];
if(addr->set) die("Already set, delete it first"); if(addr->set) die("Already set, delete it first", conn);
addr->set = 1; addr->set = 1;
char *res = strncpy(addr->name, name, MAX_DATA); char *res = strncpy(addr->name, name, MAX_DATA);
if(!res) die("Name copy failed"); if(!res) die("Name copy failed", conn);
addr->name[MAX_DATA - 1] = '\0'; addr->name[MAX_DATA - 1] = '\0';
res = strncpy(addr->email, email, MAX_DATA); res = strncpy(addr->email, email, MAX_DATA);
if(!res) die("Email copy failed"); if(!res) die("Email copy failed", conn);
addr->email[MAX_DATA - 1] = '\0'; addr->email[MAX_DATA - 1] = '\0';
} }
@ -123,7 +127,7 @@ void Database_get(struct Connection *conn, int id)
if(addr->set) { if(addr->set) {
Address_print(addr); Address_print(addr);
} else { } else {
die("ID is not set"); die("ID is not set", conn);
} }
} }
@ -149,7 +153,7 @@ void Database_list(struct Connection *conn)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]"); if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]", NULL);
char *filename = argv[1]; char *filename = argv[1];
char action = argv[2][0]; char action = argv[2][0];
@ -157,7 +161,7 @@ int main(int argc, char *argv[])
int id = 0; int id = 0;
if(argc > 3) id = atoi(argv[3]); if(argc > 3) id = atoi(argv[3]);
if(id >= MAX_ROWS) die("There's not that many records."); if(id >= MAX_ROWS) die("There's not that many records.", conn);
switch(action) { switch(action) {
case 'c': case 'c':
@ -166,20 +170,20 @@ int main(int argc, char *argv[])
break; break;
case 'g': case 'g':
if(argc != 4) die("Need an id to get"); if(argc != 4) die("Need an id to get", conn);
Database_get(conn, id); Database_get(conn, id);
break; break;
case 's': case 's':
if(argc != 6) die("Need id, name, email to set"); if(argc != 6) die("Need id, name, email to set", conn);
Database_set(conn, id, argv[4], argv[5]); Database_set(conn, id, argv[4], argv[5]);
Database_write(conn); Database_write(conn);
break; break;
case 'd': case 'd':
if(argc != 4) die("Need id to delete"); if(argc != 4) die("Need id to delete", conn);
Database_delete(conn, id); Database_delete(conn, id);
Database_write(conn); Database_write(conn);
@ -189,7 +193,7 @@ int main(int argc, char *argv[])
Database_list(conn); Database_list(conn);
break; break;
default: default:
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list"); die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn);
} }
Database_close(conn); Database_close(conn);