upt-pt-labs/lab8/2.c

271 lines
7.2 KiB
C
Raw Normal View History

2016-04-14 15:27:49 +00:00
#include <stdio.h>
2016-04-14 19:40:40 +00:00
#include <string.h>
2016-04-14 18:28:43 +00:00
#include <stdlib.h>
2016-04-14 15:27:49 +00:00
#define ROW_LEN 50
typedef struct Db_tag {
2016-04-15 07:54:44 +00:00
int blacklisted : 1; // 1 yes, 0 no
2016-04-14 15:27:49 +00:00
char repair_type[ROW_LEN];
2016-04-15 07:54:44 +00:00
long imei;
2016-04-14 15:27:49 +00:00
int price;
int investment;
int profit;
} Database;
2016-04-14 20:45:22 +00:00
// function signatures
2016-04-14 18:28:43 +00:00
void display_entry(Database * entry, unsigned index);
void insert_entry(FILE * file, Database * entry);
void read_entry(FILE * file, Database * entry);
2016-04-14 19:40:40 +00:00
void validate_input(int i);
2016-04-15 09:59:26 +00:00
void display_header();
void display_menu();
2016-04-14 17:29:01 +00:00
2016-04-14 20:45:22 +00:00
// Deletes entry by making a temporary file and copying everything in it.
// Except for the deleted entry, after that it deletes the original file
// and renames the temporary one to match the original file's name.
2016-04-14 18:28:43 +00:00
void delete_entry(FILE * file, unsigned entry_no) {
fseek(file, 0, SEEK_SET);
Database entry;
short found = 0;
2016-04-18 09:10:45 +00:00
FILE * out;
2016-04-18 13:22:48 +00:00
if ( !(out = fopen(".temp", "wb")) ) {
2016-04-18 09:10:45 +00:00
perror("Error opening file!");
exit(0);
}
2016-04-14 18:28:43 +00:00
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
if ( i == entry_no ) {
printf("Entry found and deleted!\n");
found = 1;
} else {
insert_entry(out, &entry);
}
}
2016-04-15 08:21:54 +00:00
2016-04-14 18:28:43 +00:00
if ( !found ) {
printf("Entry not found!\n");
2016-04-15 08:21:54 +00:00
system("rm -f .temp");
return;
2016-04-14 18:28:43 +00:00
}
2016-04-15 08:24:54 +00:00
if ( fclose(file) || fclose(out) ) {
2016-04-14 18:28:43 +00:00
perror("Can't close file!");
}
2016-04-15 08:21:54 +00:00
2016-04-15 09:59:26 +00:00
system("rm -f data.bdb; mv .temp data.bdb"); // copy and remove, ty unix <3
2016-04-14 18:28:43 +00:00
if ( !(file = fopen("data.bdb", "r+b")) ) {
perror("Can't open file");
2016-04-18 09:10:45 +00:00
exit(0);
2016-04-14 18:28:43 +00:00
}
}
2016-04-14 20:45:22 +00:00
// Reads new entry from stdin.
// if exists is 1, it prints the entry's values.
// if the entry is blacklisted it quits;
2016-04-14 19:40:40 +00:00
void modify_entry(Database * entry, int exists) {
if (entry->blacklisted && exists) {
printf("Entry is blacklisted!\n Access Denied\n");
return;
}
2016-04-14 18:51:45 +00:00
printf("Enter new repair type:\n");
2016-04-14 19:40:40 +00:00
if (exists) printf("Current: %s\n> ", entry->repair_type);
2016-04-14 19:57:03 +00:00
validate_input(0); scanf("%50[a-zA-Z -_'\"!~?]", entry->repair_type);
2016-04-14 19:40:40 +00:00
printf("Enter new IMEI:\n");
2016-04-15 07:54:44 +00:00
if (exists) printf("Current: %ld\n> ", entry->imei);
validate_input(0); scanf("%10ld", &entry->imei);
2016-04-14 18:51:45 +00:00
printf("Enter new price:\n");
2016-04-15 07:54:44 +00:00
if (exists) printf("Current: %10d\n> ", entry->price);
validate_input(0); scanf("%10d", &entry->price);
2016-04-14 18:51:45 +00:00
printf("Enter new investment:\n");
2016-04-14 19:40:40 +00:00
if (exists) printf("Current: %d\n> ", entry->investment);
2016-04-15 07:54:44 +00:00
validate_input(0); scanf("%10d", &entry->investment);
entry->profit = entry->price - entry->investment;
entry->blacklisted = 0; // in case there is an 1 that's not touched.
validate_input(0); // clear remaining trash, if any.
2016-04-14 18:34:42 +00:00
}
void update_entry(FILE * file, unsigned entry_no) {
fseek(file, 0, SEEK_SET);
Database entry;
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
if ( i == entry_no ) {
printf("Entry found!\n");
2016-04-14 18:51:45 +00:00
fseek(file, -sizeof(Database), SEEK_CUR);
2016-04-14 19:40:40 +00:00
modify_entry(&entry, 1);
2016-04-14 18:34:42 +00:00
fwrite(&entry, sizeof(Database), 1, file);
2016-04-15 07:54:44 +00:00
return;
2016-04-14 19:40:40 +00:00
}
}
2016-04-15 07:54:44 +00:00
printf("Entry not found!\n");
2016-04-14 19:40:40 +00:00
}
2016-04-15 07:54:44 +00:00
// Blacklists or Unblacklists an entry.
2016-04-14 19:40:40 +00:00
void blacklist_entry(FILE * file, unsigned entry_no) {
fseek(file, 0, SEEK_SET);
Database entry;
2016-04-18 13:22:48 +00:00
// long imei = 0;
2016-04-14 19:40:40 +00:00
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
if ( i == entry_no ) {
printf("Entry found!\n");
fseek(file, -sizeof(Database), SEEK_CUR);
2016-04-18 13:22:48 +00:00
// imei = entry.imei;
entry.blacklisted ^= 1;
2016-04-14 19:40:40 +00:00
fwrite(&entry, sizeof(Database), 1, file);
}
}
2016-04-18 13:22:48 +00:00
// fseek(file, 0, SEEK_SET);
// for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
// if ( entry.imei == imei) {
// fseek(file, -sizeof(Database), SEEK_CUR);
// entry.blacklisted ^= 1;
// fwrite(&entry, sizeof(Database), 1, file);
// }
// }
2016-04-14 19:40:40 +00:00
}
2016-04-15 07:54:44 +00:00
void search_entry(FILE * file, long imei) {
2016-04-14 19:40:40 +00:00
fseek(file, 0, SEEK_SET);
Database entry;
2016-04-15 09:59:26 +00:00
short found = 0;
2016-04-14 19:40:40 +00:00
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
2016-04-15 07:54:44 +00:00
if ( entry.imei == imei ) {
2016-04-15 09:59:58 +00:00
if (!found) display_header(); // Display header only once.
2016-04-15 08:21:54 +00:00
display_entry(&entry, i);
2016-04-15 09:59:26 +00:00
found = 1;
2016-04-14 18:34:42 +00:00
}
}
2016-04-15 09:59:26 +00:00
if ( !found ) {
printf("Entry not found!\n");
}
2016-04-14 18:34:42 +00:00
}
2016-04-14 18:28:43 +00:00
2016-04-14 17:29:01 +00:00
void insert_entry(FILE * file, Database * entry) {
fseek(file, 0, SEEK_END);
2016-04-15 07:54:44 +00:00
entry->profit = entry->price - entry->investment;
2016-04-14 17:29:01 +00:00
fwrite(entry, sizeof(Database), 1, file);
}
void read_entry(FILE * file, Database * entry) {
fread(entry, sizeof(Database), 1, file);
}
2016-04-14 18:28:43 +00:00
void display_entry(Database * entry, unsigned index) {
2016-04-15 07:54:44 +00:00
printf("|%3d|%50s|%10ld|%10d| %10d| %10d| %c\n",
2016-04-14 18:28:43 +00:00
index,
2016-04-14 17:29:01 +00:00
entry->repair_type,
entry->imei,
entry->price,
entry->investment,
2016-04-14 19:40:40 +00:00
entry->profit,
2016-04-14 20:45:22 +00:00
(entry->blacklisted ? 'B' : '_') // ternary operator, if blaclist is 1 it prints B
2016-04-14 17:29:01 +00:00
);
}
void display_header() {
2016-04-15 07:54:44 +00:00
printf("|%3s|%50s|%10s|%10s| %10s| %10s|\n",
2016-04-14 17:29:01 +00:00
"i", "Repair Type", "IMEI", "Price", "Investment",
2016-04-14 18:28:43 +00:00
"Profit");
2016-04-14 17:29:01 +00:00
}
2016-04-14 20:45:22 +00:00
// shows all entries and calculates profit + influx
2016-04-14 17:29:01 +00:00
void display_database_entries(FILE * file) {
fseek(file, 0, SEEK_SET);
Database entry;
2016-04-14 18:28:43 +00:00
display_header();
long int total_profit = 0;
long int total_influx = 0;
for ( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
display_entry(&entry, i);
total_profit += entry.profit;
total_influx += entry.price;
2016-04-14 17:29:01 +00:00
}
2016-04-14 18:28:43 +00:00
printf("TOTAL PROFIT: %ld TOTAL INFLUX: %ld \n", total_profit, total_influx);
2016-04-14 17:29:01 +00:00
}
void display_menu() {
printf("\n");
printf("0. Exit program\n");
printf("1. Display the database.\n");
printf("2. Add an entry.\n");
printf("3. Update an entry.\n");
printf("4. Delete an entry.\n");
printf("5. Search by IMEI\n");
2016-04-14 19:40:40 +00:00
printf("6. Blacklist/UnBlacklist\n");
2016-04-14 17:29:01 +00:00
printf("\n");
}
2016-04-14 20:45:22 +00:00
// removes all characters from stdin until newline.
// basically it clears all the trash from stdin.
2016-04-14 19:40:40 +00:00
void validate_input(int i) {
if (!i) {
int c;
while( (c = getchar()) != '\n' );
}
}
2016-04-14 20:45:22 +00:00
// takes user choices.
2016-04-14 19:40:40 +00:00
void process_choice(FILE * file) {
display_menu();
2016-04-14 18:28:43 +00:00
Database e;
2016-04-14 19:40:40 +00:00
enum choices { exit_program_c, display_the_database_c, add_entry_c, update_entry_c,
delete_entry_c, search_by_imei_c, blacklist_c };
int choice = -1;
int entry_no = 0;
printf("Enter your choice\n>> ");
validate_input(scanf("%d", &choice));
switch (choice) {
case exit_program_c:
printf("Exiting...\n");
exit(0);
break;
case display_the_database_c:
display_database_entries(file);
break;
case add_entry_c:
modify_entry(&e, 0);
insert_entry(file, &e);
break;
case update_entry_c:
printf("Enter the entry number: ");
validate_input(scanf("%d", &entry_no));
update_entry(file, entry_no);
break;
case delete_entry_c:
2016-04-15 07:54:44 +00:00
printf("Enter the entry number: ");
validate_input(scanf("%d", &entry_no));
2016-04-14 19:40:40 +00:00
delete_entry(file, entry_no);
break;
case search_by_imei_c:
printf("Enter the IMEI: ");
2016-04-15 07:54:44 +00:00
validate_input(scanf("%ld", &e.imei));
search_entry(file, e.imei);
2016-04-14 19:40:40 +00:00
break;
case blacklist_c:
2016-04-15 07:54:44 +00:00
printf("Enter the entry number: ");
validate_input(scanf("%d", &entry_no));
2016-04-14 19:40:40 +00:00
blacklist_entry(file, entry_no);
break;
default:
printf("Invalid choice!\n");
}
process_choice(file); // redo the choice, since the user hasn't exited;
}
int main(void) {
FILE * fp;
if ( !(fp = fopen("data.bdb", "r+b")) ) {
perror("Error opening file");
2016-04-18 09:10:45 +00:00
return -1;
2016-04-14 19:40:40 +00:00
}
process_choice(fp);
2016-04-14 15:27:49 +00:00
return 0;
}