Skip to content

Instantly share code, notes, and snippets.

@stantona
Last active December 17, 2015 19:09
Show Gist options
  • Select an option

  • Save stantona/5658226 to your computer and use it in GitHub Desktop.

Select an option

Save stantona/5658226 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MAX_DATA 512
#define MAX_ROWS 100
struct Address {
int id;
int set;
char *name;
char *email;
};
struct Database {
int maxrows;
int maxdata;
struct Address *rows;
};
struct Connection {
FILE *file;
struct Database *db;
};
void Database_close(struct Connection *);
char *String_copy(char *dest, const char *source, size_t length)
{
char *res = strncpy(dest, source, length);
dest[length - 1] = '\0';
return res;
}
void print_error(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
}
void die_m(const char *message)
{
print_error(message);
exit(1);
}
void die(const char *message, struct Connection *conn)
{
print_error(message);
Database_close(conn);
exit(1);
}
void Address_write(struct Connection *conn, struct Address *address, int maxdata)
{
int rc = fwrite(&address->id, sizeof(int), 1, conn->file);
if (rc != 1) die("Can not write Address to the database", conn);
rc = fwrite(&address->set, sizeof(int), 1, conn->file);
if (rc != 1) die("Can not write Address to the database", conn);
rc = fwrite(address->name, sizeof(char), maxdata, conn->file);
if (rc != maxdata) die("Can not write Address to the database", conn);
rc = fwrite(address->email, sizeof(char), maxdata, conn->file);
if (rc != maxdata) die("Can not write Address to the database", conn);
}
void Address_load(struct Connection *conn, struct Address *address, int maxdata)
{
int rc = fread(&address->id, sizeof(int), 1, conn->file);
if (rc != 1) die("Can not read Address", conn);
rc = fread(&address->set, sizeof(int), 1, conn->file);
if (rc != 1) die("Can not read Address", conn);
address->name = calloc(maxdata, sizeof(char));
rc = fread(address->name, sizeof(char), maxdata, conn->file);
if (rc != maxdata) die("Can not read Address", conn);
address->email = calloc(maxdata, sizeof(char));
rc = fread(address->email, sizeof(char), maxdata, conn->file);
if (rc != maxdata) die("Can not read Address", conn);
}
void Address_print(struct Address *address)
{
printf("%d %s %s\n", address->id, address->name, address->email);
}
void Database_load(struct Connection *conn)
{
int i = 0;
int rc = fread(&conn->db->maxrows, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to load database", conn);
rc = fread(&conn->db->maxdata, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to load database", conn);
conn->db->rows = calloc(conn->db->maxrows, sizeof(struct Address));
for(i = 0; i < conn->db->maxrows; i++) {
Address_load(conn, &conn->db->rows[i], conn->db->maxdata);
}
}
struct Connection *Database_open(const char *filename, char mode)
{
struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error", conn);
conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error", conn);
if(mode == 'c') {
conn->file = fopen(filename, "w");
} else {
conn->file = fopen(filename, "r+");
if (conn->file) {
Database_load(conn);
}
}
if (!conn->file) die("Failed to open file", conn);
return conn;
}
void Address_free(struct Address *addr)
{
if(addr->name) free(addr->name);
if(addr->email) free(addr->email);
}
void Database_close(struct Connection *conn)
{
if(conn) {
if(conn->file) fclose(conn->file);
if(conn->db) {
int i = 0;
for(i = 0; i < conn->db->maxrows; i++) {
Address_free(&conn->db->rows[i]);
}
free(conn->db->rows);
free(conn->db);
}
free(conn);
}
}
void Database_write(struct Connection *conn)
{
int i = 0;
rewind(conn->file);
int rc = fwrite(&conn->db->maxrows, sizeof(int), 1, conn->file);
if (rc != 1) die("Can not write to the database", conn);
rc = fwrite(&conn->db->maxdata, sizeof(int), 1, conn->file);
if (rc != 1) die("Can not write to the database", conn);
for(i = 0; i < conn->db->maxrows; i++) {
Address_write(conn, &conn->db->rows[i], conn->db->maxdata);
}
rc = fflush(conn->file);
if (rc == -1) die("Can not flush database", conn);
}
void Database_initialize(struct Connection *conn, int maxrows, int maxdata)
{
conn->db->maxrows = maxrows;
conn->db->maxdata = maxdata;
conn->db->rows = calloc(conn->db->maxrows, sizeof(struct Address));
}
void Database_create(struct Connection *conn, int maxrows, int maxdata)
{
Database_initialize(conn, maxrows, maxdata);
int i = 0;
for(i = 0; i < maxrows; i++) {
struct Address *row = &conn->db->rows[i];
row->id = i;
row->set = 0;
row->email = calloc(maxdata, sizeof(char));
row->name = calloc(maxdata, sizeof(char));
}
}
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", conn);
addr->set = 1;
char *res = String_copy(addr->name, name, MAX_DATA);
if(!res) die("Name copy failed", conn);
res = String_copy(addr->email, email, MAX_DATA);
if(!res) die("Email copy failed", conn);
}
void Database_get(struct Connection *conn, int id)
{
struct Address *addr = &conn->db->rows[id];
if(addr->set) {
Address_print(addr);
} else {
die("Address is not set", conn);
}
}
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 < db->maxrows; i++) {
struct Address *addr = &db->rows[i];
if (addr->set) {
Address_print(addr);
}
}
}
int main(int argc, char *argv[])
{
if(argc < 3) die_m("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;
switch(action) {
case 'c':
if (argc != 5) die("Need max rows, and max data", conn);
int maxrows = atoi(argv[3]);
int maxdata = atoi(argv[4]);
Database_create(conn, maxrows, maxdata);
Database_write(conn);
break;
case 'g':
if (argc != 4) die("Need an id to get", conn);
id = atoi(argv[3]);
Database_get(conn, id);
break;
case 's':
if (argc != 6) die("Need id, name, email to set", conn);
id = atoi(argv[3]);
Database_set(conn, id, argv[4], argv[5]);
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", conn);
}
Database_close(conn);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment