2011-09-20 19:15:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-09-24 02:54:52 +00:00
|
|
|
#define DEFAULT_MAX_DATA 512
|
|
|
|
#define DEFAULT_MAX_ROWS 100
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
struct Address {
|
|
|
|
int id;
|
|
|
|
int set;
|
2011-09-24 02:54:52 +00:00
|
|
|
char *name;
|
|
|
|
char *email;
|
2011-09-20 19:15:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Database {
|
2011-10-12 12:04:23 +00:00
|
|
|
size_t size;
|
2011-09-24 02:54:52 +00:00
|
|
|
int max_data;
|
|
|
|
int max_rows;
|
2011-10-12 12:04:23 +00:00
|
|
|
struct Address *rows;
|
2011-09-20 19:15:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Connection {
|
|
|
|
FILE *file;
|
|
|
|
struct Database *db;
|
|
|
|
};
|
|
|
|
|
2011-09-22 02:30:46 +00:00
|
|
|
void Database_close(struct Connection *conn);
|
|
|
|
|
|
|
|
void die(const char *message, struct Connection *conn)
|
2011-09-20 19:15:43 +00:00
|
|
|
{
|
|
|
|
if(errno) {
|
|
|
|
perror(message);
|
|
|
|
} else {
|
|
|
|
printf("ERROR: %s\n", message);
|
|
|
|
}
|
|
|
|
|
2011-09-22 02:30:46 +00:00
|
|
|
Database_close(conn);
|
|
|
|
|
2011-09-20 19:15:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Address_print(struct Address *addr)
|
|
|
|
{
|
|
|
|
printf("%d %s %s\n",
|
|
|
|
addr->id, addr->name, addr->email);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database_load(struct Connection *conn)
|
|
|
|
{
|
2011-10-12 12:04:23 +00:00
|
|
|
rewind(conn->file);
|
|
|
|
|
|
|
|
int rc = fread(&conn->db->max_rows, sizeof(int), 1, conn->file);
|
|
|
|
if(rc != 1) die("Failed to load database max_rows.", conn);
|
|
|
|
|
|
|
|
rc = fread(&conn->db->max_data, sizeof(int), 1, conn->file);
|
|
|
|
if(rc != 1) die("Failed to load database max_data.", conn);
|
|
|
|
|
|
|
|
rc = fread(conn->db->rows, sizeof(struct Address), conn->db->max_rows, conn->file);
|
|
|
|
if(rc != 1) die("Failed to load database rows.", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
}
|
|
|
|
|
2011-10-12 12:04:23 +00:00
|
|
|
struct Connection* Database_open(const char *filename, char mode, int max_rows)
|
2011-09-20 19:15:43 +00:00
|
|
|
{
|
|
|
|
struct Connection *conn = malloc(sizeof(struct Connection));
|
2011-09-22 02:30:46 +00:00
|
|
|
if(!conn) die("Memory error", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
conn->db = malloc(sizeof(struct Database));
|
2011-09-22 02:30:46 +00:00
|
|
|
if(!conn->db) die("Memory error", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
2011-10-12 12:04:23 +00:00
|
|
|
size_t rowsize = max_rows * sizeof(struct Address);
|
|
|
|
conn->db->size = rowsize;
|
|
|
|
conn->db->rows = malloc(rowsize);
|
|
|
|
if(!conn->db->rows) die("Memory error", conn);
|
|
|
|
|
2011-09-20 19:15:43 +00:00
|
|
|
if(mode == 'c') {
|
|
|
|
conn->file = fopen(filename, "w");
|
|
|
|
} else {
|
|
|
|
conn->file = fopen(filename, "r+");
|
|
|
|
|
|
|
|
if(conn->file) {
|
|
|
|
Database_load(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-22 02:30:46 +00:00
|
|
|
if(!conn->file) die("Failed to open the file", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database_close(struct Connection *conn)
|
|
|
|
{
|
2011-09-22 02:30:46 +00:00
|
|
|
if(conn != NULL) {
|
2011-09-20 19:15:43 +00:00
|
|
|
if(conn->file) fclose(conn->file);
|
|
|
|
if(conn->db) free(conn->db);
|
|
|
|
free(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database_write(struct Connection *conn)
|
|
|
|
{
|
|
|
|
rewind(conn->file);
|
|
|
|
|
2011-10-12 12:04:23 +00:00
|
|
|
int rc = fwrite(&conn->db->size, sizeof(size_t), 1, conn->file);
|
|
|
|
if(rc != 1) die("Failed to write database size.", conn);
|
|
|
|
|
|
|
|
rc = fwrite(&conn->db->max_rows, sizeof(int), 1, conn->file);
|
|
|
|
if(rc != 1) die("Failed to write database max_rows.", conn);
|
|
|
|
|
|
|
|
rc = fwrite(&conn->db->max_data, sizeof(int), 1, conn->file);
|
|
|
|
if(rc != 1) die("Failed to write database max_data.", conn);
|
|
|
|
|
|
|
|
rc = fwrite(conn->db->rows, conn->db->size, 1, conn->file);
|
|
|
|
if(rc != 1) die("Failed to write database rows.", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
rc = fflush(conn->file);
|
2011-09-22 02:30:46 +00:00
|
|
|
if(rc == -1) die("Cannot flush database.", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
}
|
|
|
|
|
2011-09-24 02:54:52 +00:00
|
|
|
void Database_create(struct Connection *conn, int max_rows, int max_data)
|
2011-09-20 19:15:43 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
2011-09-24 02:54:52 +00:00
|
|
|
conn->db->max_rows = max_rows;
|
|
|
|
conn->db->max_data = max_data;
|
2011-09-20 19:15:43 +00:00
|
|
|
|
2011-09-24 02:54:52 +00:00
|
|
|
for(i = 0; i < conn->db->max_rows; i++) {
|
2011-09-20 19:15:43 +00:00
|
|
|
// make a prototype to initialize it
|
|
|
|
struct Address addr = {.id = i, .set = 0};
|
|
|
|
// then just assign it
|
|
|
|
conn->db->rows[i] = addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database_set(struct Connection *conn, int id, const char *name, const char *email)
|
|
|
|
{
|
2011-09-24 02:54:52 +00:00
|
|
|
int max_data = conn->db->max_data;
|
2011-09-20 19:15:43 +00:00
|
|
|
struct Address *addr = &conn->db->rows[id];
|
2011-09-22 02:30:46 +00:00
|
|
|
if(addr->set) die("Already set, delete it first", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
addr->set = 1;
|
2011-09-24 02:54:52 +00:00
|
|
|
char *res = strncpy(addr->name, name, max_data);
|
2011-09-22 02:30:46 +00:00
|
|
|
if(!res) die("Name copy failed", conn);
|
2011-09-24 02:54:52 +00:00
|
|
|
addr->name[max_data - 1] = '\0';
|
2011-09-20 19:15:43 +00:00
|
|
|
|
2011-09-24 02:54:52 +00:00
|
|
|
res = strncpy(addr->email, email, max_data);
|
2011-09-22 02:30:46 +00:00
|
|
|
if(!res) die("Email copy failed", conn);
|
2011-09-24 02:54:52 +00:00
|
|
|
addr->email[max_data - 1] = '\0';
|
2011-09-20 19:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Database_get(struct Connection *conn, int id)
|
|
|
|
{
|
|
|
|
struct Address *addr = &conn->db->rows[id];
|
|
|
|
|
|
|
|
if(addr->set) {
|
|
|
|
Address_print(addr);
|
|
|
|
} else {
|
2011-09-22 02:30:46 +00:00
|
|
|
die("ID is not set", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-09-24 02:54:52 +00:00
|
|
|
for(i = 0; i < conn->db->max_rows; i++) {
|
2011-09-20 19:15:43 +00:00
|
|
|
struct Address *cur = &db->rows[i];
|
|
|
|
|
|
|
|
if(cur->set) {
|
|
|
|
Address_print(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2011-09-24 02:54:52 +00:00
|
|
|
if(argc < 3) die("USAGE: ex17 <dbfile> <action>[:max_data,max_rows] [action params]", NULL);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
char *filename = argv[1];
|
2011-09-24 02:54:52 +00:00
|
|
|
|
|
|
|
int max_data = DEFAULT_MAX_DATA;
|
|
|
|
int max_rows = DEFAULT_MAX_ROWS;
|
|
|
|
|
2011-09-20 19:15:43 +00:00
|
|
|
char action = argv[2][0];
|
2011-09-24 02:54:52 +00:00
|
|
|
if(':' == argv[2][1]) {
|
|
|
|
sscanf(argv[2], "%c:%d,%d", &action, &max_data, &max_rows);
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:04:23 +00:00
|
|
|
struct Connection *conn = Database_open(filename, action, max_rows);
|
2011-09-20 19:15:43 +00:00
|
|
|
int id = 0;
|
|
|
|
|
|
|
|
if(argc > 3) id = atoi(argv[3]);
|
2011-09-24 02:54:52 +00:00
|
|
|
if(id >= max_rows) die("There's not that many records.", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
switch(action) {
|
|
|
|
case 'c':
|
2011-09-24 02:54:52 +00:00
|
|
|
Database_create(conn, max_rows, max_data);
|
2011-09-20 19:15:43 +00:00
|
|
|
Database_write(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g':
|
2011-09-22 02:30:46 +00:00
|
|
|
if(argc != 4) die("Need an id to get", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
Database_get(conn, id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2011-09-22 02:30:46 +00:00
|
|
|
if(argc != 6) die("Need id, name, email to set", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
Database_set(conn, id, argv[4], argv[5]);
|
|
|
|
Database_write(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
2011-09-22 02:30:46 +00:00
|
|
|
if(argc != 4) die("Need id to delete", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
|
|
|
|
Database_delete(conn, id);
|
|
|
|
Database_write(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
Database_list(conn);
|
|
|
|
break;
|
|
|
|
default:
|
2011-09-22 02:30:46 +00:00
|
|
|
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn);
|
2011-09-20 19:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Database_close(conn);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-21 17:24:43 +00:00
|
|
|
|