Adding "find" to ex17 extra credit

This commit is contained in:
Dan Buch 2016-04-13 10:14:33 -04:00
parent 6c837610a1
commit a3f31cd6bb

View File

@ -152,6 +152,23 @@ void Database_list(struct Connection *conn)
}
}
void Database_find(struct Connection *conn, char *search)
{
int i = 0;
struct Database *db = conn->db;
for(i = 0; i < MAX_ROWS; i++) {
struct Address *cur = &db->rows[i];
if(cur->set) {
if(strstr(cur->email, search) != NULL ||
strstr(cur->name, search) != NULL) {
Address_print(cur);
}
}
}
}
int main(int argc, char *argv[])
{
char usage[64];
@ -193,11 +210,16 @@ int main(int argc, char *argv[])
Database_write(conn);
break;
case 'f':
if(argc != 4) die("Need something to find", conn);
Database_find(conn, argv[3]);
break;
case 'l':
Database_list(conn);
break;
default:
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn);
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list, f=find", conn);
}
Database_close(conn);