Add "street" and make searchable

This commit is contained in:
Dan Buch 2016-04-13 10:20:23 -04:00
parent 0cc65b0f76
commit 1970801db8

View File

@ -13,6 +13,7 @@ struct Address {
int set; int set;
char name[MAX_DATA]; char name[MAX_DATA];
char email[MAX_DATA]; char email[MAX_DATA];
char street[MAX_DATA];
}; };
struct Database { struct Database {
@ -41,8 +42,8 @@ void die(const char *message, struct Connection *conn)
void Address_print(struct Address *addr) void Address_print(struct Address *addr)
{ {
printf("%d %s %s\n", printf("%d %s %s \"%s\"\n",
addr->id, addr->name, addr->email); addr->id, addr->name, addr->email, addr->street);
} }
void Database_load(struct Connection *conn) void Database_load(struct Connection *conn)
@ -106,7 +107,8 @@ 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, const char *street)
{ {
struct Address *addr = &conn->db->rows[id]; struct Address *addr = &conn->db->rows[id];
if(addr->set) die("Already set, delete it first", conn); if(addr->set) die("Already set, delete it first", conn);
@ -119,6 +121,9 @@ void Database_set(struct Connection *conn, int id, const char *name, const char
res = strncpy(addr->email, email, MAX_DATA); res = strncpy(addr->email, email, MAX_DATA);
if(!res) die("Email copy failed", conn); if(!res) die("Email copy failed", conn);
res = strncpy(addr->street, street, MAX_DATA);
if(!res) die("Street copy failed", conn);
} }
void Database_get(struct Connection *conn, int id) void Database_get(struct Connection *conn, int id)
@ -162,7 +167,8 @@ void Database_find(struct Connection *conn, char *search)
if(cur->set) { if(cur->set) {
if(strstr(cur->email, search) != NULL || if(strstr(cur->email, search) != NULL ||
strstr(cur->name, search) != NULL) { strstr(cur->name, search) != NULL ||
strstr(cur->street, search) != NULL) {
Address_print(cur); Address_print(cur);
} }
} }
@ -197,9 +203,9 @@ int main(int argc, char *argv[])
break; break;
case 's': case 's':
if(argc != 6) die("Need id, name, email to set", conn); if(argc != 7) die("Need id, name, email, and street to set", conn);
Database_set(conn, id, argv[4], argv[5]); Database_set(conn, id, argv[4], argv[5], argv[6]);
Database_write(conn); Database_write(conn);
break; break;