rendered paste body/* ====================================================== */
/* libraries.h */
/* ====================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ====================================================== */
/* Statheres, katholikes metablhtes kai structures */
/* ====================================================== */
#ifdef WIN32
#define clear_screen "cls"
#else
#define clear_screen "clear"
#endif
#define D 3 /* Megistos arithmos upo diagrafh eggrafwn prin thn anasugkrothsh */
#define N 5 /* Megisto plithos eisagwghs eggrafwn prin thn anadiorganwsh */
#define INDEX ".ndx" /* Epektash tou arxeioy eyrethriou */
#define MASTER ".dat" /* Epektash tou arxeioy dedomenwn */
#define STRINGS 30 /* Megethos twn alfarithmitikwn */
FILE *dataf;
FILE *indexf;
char oad[STRINGS+1]="tenies.dat"; /* proepilegmeno onoma */
char oai[STRINGS+1]="tenies.ndx";
struct tenies{
char code[10]; /* Kodikos tainias */
char title[STRINGS+1]; /* Titlos */
char genre[STRINGS+1]; /* Eidos */
char director[STRINGS+1]; /* Skinotheths */
char actor1[STRINGS+1]; /* Protagonisths 1 */
char actor2[STRINGS+1]; /* Protagonisths 2 */
int year; /* Etos */
int avail; /* diathesimothta */
};
struct teniesIndex{
char code[10]; /* Pedio kleidi */
int dief; /* Dieuthunsh eggrafhs sto arxeio dedomenwn */
};
struct tenies film;
struct teniesIndex *ifilm;
/* ====================================================== */
/* Protupa sunartisewn */
/* ====================================================== */
void mainmenu(); /* Emfanish tou menou epilogwn */
void eksodos(); /* Emfanish munhmatos telous */
void epektash(); /* Dhmiourgia/epektash arxeiou */
int anazhthsh(); /* Anazhthsh mesw tou index file, epistrefei th dieuthinsh ths eggrafhs sto index arxeio 'h -1 se apotuxia */
void probolh(); /* Menou tupwn probolhs */
void probolh_index(); /* Probolh tou index arxeiou */
void probolh_all(); /* Probolh olou tou arxeio master */
void probolh_one(); /* Probolh mias eggrafhs */
void prob_genre(char *); /* Probolh ana eidos */
void prob_gen_av(char *); /* Probolh ana eidos k diathesimothta */
void prob_titl_taksin(); /* Probolh ana titlo, taksinomhmena */
char* select_genre(); /* Epilogh tou eidous tenias */
void diorthosh(); /* Diorthwsh eggrafhs */
void diagrafh(); /* Diagrafh eggrafhs */
int check_files(); /* Elegxos uparkshs arxeiwn kai epistrofh tou megethous */
void anasugkrothsh(); /* Anasugkrothsh tou index arxeiou */
void selection_sort(struct teniesIndex *, int);
/* ====================================================== */
/* Sunartish emfanishs tou menou epilogwn */
/* ====================================================== */
void mainmenu(){
// system(clear_screen);
printf(".------------| Main menu |------------.\n");
printf("| |\n");
printf("| 1. Dhmiourgia / Epektash |\n");
printf("| 2. Anazhthsh |\n");
printf("| 3. Probolh |\n");
printf("| 4. Diorthosh |\n");
printf("| 5. Diagrafh |\n");
printf("| 6. Eksodos |\n");
printf("| |\n");
printf("|-------------------------------------'\n");
printf("| Epilogh : |\n");
printf("'-------------------------------------'\n");
return;
}
/* ====================================================== */
/* Sunartish Emfanish munhmatos telous */
/* ====================================================== */
void eksodos(){
// system(clear_screen);
printf(".------------| Eksodos |------------.\n");
printf("| |\n");
printf("| Organwsh kai diaxeirhsh arxeiwn |\n");
printf("| Project |\n");
printf("| |\n");
printf("| |\n");
printf("| XXXXXXXXXXXXXXXXXXXX T-1786 |\n");
printf("| XXXXXXXXXXXXXXXXXXXX T-1800 |\n");
printf("| |\n");
printf("'-------------------------------------'\n");
// system(clear_screen);
exit;
}
/* ====================================================== */
/* Sunartish taksinomhshs tou index arxeiou */
/* ====================================================== */
void selection_sort(struct teniesIndex X[], int k){
int i,j,t;
struct teniesIndex temp;
for (i=0; i<k-1; i++) {
t = i;
for (j=i+1; j<k; j++)
if ( strcmp(X[t].code, X[j].code)>0 )
t=j;
temp = X[i];
X[i] = X[t];
X[t] = temp;
}
}
/* ====================================================== */
/* Sunartish Dhmiourgias/Epektashs */
/* ====================================================== */
void epektash(){
int pl, m=0, i, j, tm;
int synexeia;
pl = check_files();
m = pl;
dataf = fopen(oad, "rb+");
fseek(dataf, 0, SEEK_END);
ifilm = (struct teniesIndex *) malloc((pl+N) * sizeof(struct teniesIndex));
fputs("\n\nEISAGOGI EGGRAFON\n\n", stdout);
do {
printf("\nEisagogi kwdikou tenias : ");
scanf("%s", &film.code);
printf("\nEisagogi titlou tenias : ");
scanf("%s", &film.title);
printf("\nEisagogi eidous tenias : \n");
strcpy(film.genre,select_genre());
printf("\nEisagogi skhnotheth : ");
scanf("%s", &film.director);
printf("\nEisagogi prwtou protagwnisth : ");
scanf("%s", &film.actor1);
printf("\nEisagogi deuterou protagwnisth : ");
scanf("%s", &film.actor2);
printf("\nEisagogi etous tenias : ");
scanf("%d", &film.year);
printf("\nDiatheshmothta (1=NAI, 0=OXI) : ");
scanf("%d", &film.avail);
/* Kataxwrisi eggrafhs sto arxeio dedomenwn */
fwrite(&film, sizeof(struct tenies), 1, dataf);
/* Enhmerwsh pinaka euretiriou */
strcpy(ifilm[m].code, film.code);
ifilm[m].dief = m;
m++;
printf("\n\n Thelete nea eisagogi (1=NAI, 0=OXI) : ");
scanf("%d", &synexeia);
if ( synexeia==1 )
if ( m == pl+N ) {
/* Enhmerwsh arxeiou deiktwn kai anadiorganwsh pinaka deiktwn */
indexf = fopen(oai, "rb+");
fseek(indexf, 0, SEEK_END);
for (i=pl; i<pl+N; i++)
fwrite(&ifilm[i], sizeof(struct teniesIndex), 1, indexf);
fclose(indexf);
free(ifilm);
pl += N;
ifilm = malloc( (pl+N) * sizeof(struct teniesIndex));
}
} while ( synexeia == '1' );
fclose(dataf);
if ( m < pl+N) {
/* Enhmerwsh arxeiou deiktwn (eurethriou) */
indexf = fopen(oai, "rb+");
fseek(indexf, 0, SEEK_END);
for (i=pl; i<m; i++)
fwrite(&ifilm[i], sizeof(struct teniesIndex), 1, indexf);
fclose(indexf);
}
anasugkrothsh();
}
/* ====================================================== */
/* Sunartish anasugkrothshs tou arxeiou deiktwn */
/* ====================================================== */
void anasugkrothsh(){
int pl;
pl = check_files();
if (pl == 0) {
printf("\n\nTo arxeio einai adeio...\n\n");
//getchar();
return;
}
indexf = fopen(oai, "rb");
fseek(indexf, 0, SEEK_SET);
fread(&ifilm[0], sizeof(struct teniesIndex), pl, indexf);
fclose(indexf);
selection_sort(ifilm, pl);
indexf = fopen(oai, "wb");
fwrite(&ifilm[0], sizeof(struct teniesIndex), pl, indexf);
fclose(indexf);
free(ifilm);
}
/* ======================================================== */
/* Elegxos uparkshs/dhmiourgia arxeiwn - epistrofh plhthous */
/* ======================================================== */
int check_files(){
int pl=0;
int choice;
dataf = fopen(oad, "rb");
if (!dataf){
do{
printf("\nTa arxeia den uparxoun. Na dhmiourgithoun twra?");
printf("(1=NAI, 0=OXI) : ");
scanf("%d", &choice);
}while(choice != 0 && choice != 1);
if (choice == '1'){
printf("\n\nEisagwgi onomatos arxeiou (xoris epektasi): ");
scanf("%s", oad);
strcpy(oai, oad);
strcat(oad, MASTER);
strcat(oai, INDEX);
dataf = fopen(oad, "wb");
indexf = fopen(oai, "wb");
fclose(indexf);
}
else{
printf("\nSFALMA! Ta arxeia den uparxoun, oute tha dhmiourgithoun\n");
return 0;
}
}
else{
indexf = fopen(oai, "rb");
if (!indexf) {
fclose(dataf);
printf("\nSFALMA! Yparxei mono to master arxeio, oxi to index.");
eksodos();
}
fseek(indexf, 0, SEEK_END);
pl = ftell(indexf) / sizeof(struct teniesIndex);
fclose(indexf);
fclose(dataf);
}
return pl;
}
/* ====================================================== */
/* Sunartish anazhthshs */
/* ====================================================== */
int anazhthsh(){
char anaz[10];
int choice;
int arxi=0, telos, meso, flag=0, deiktis, pl; /* Deiktes duadikhs anazhthshs */
pl = check_files();
if (pl == 0) {
printf("\n\nTo arxeio einai adeio...\n\n");
//getchar();
return -1;
}
indexf = fopen(oai, "rb");
ifilm = malloc( pl * sizeof(struct teniesIndex));
fread(&ifilm[0], sizeof(struct teniesIndex), pl, indexf);
fclose(indexf);
printf("\n\n\nEisagogi kleidiou anazitisis : ");
scanf("%10s", anaz);
telos = pl - 1;
while (arxi<=telos && flag ==0) {
meso = (arxi + telos) / 2;
if (strcmp(anaz, ifilm[meso].code)>0 )
arxi = meso + 1;
else
if (strcmp(anaz, ifilm[meso].code)<0 )
telos = meso - 1;
else
flag = 1;
}
if ( flag == 0 ){
printf("\n\nH eggrafi den vrethike...\n\n");
return -1;
}
else {
do{
printf("\nH eggrafh brethike! Na problithei twra?");
printf("(1=NAI, 0=OXI) : ");
scanf("%d", &choice);
}while(choice != 0 && choice != 1);
if (choice == 1){
deiktis = ifilm[meso].dief;
probolh_one(deiktis);
}
}
free(ifilm);
return meso;
}
/* ====================================================== */
/* Sunartish diagrafhs */
/* ====================================================== */
void diagrafh(){
struct tenies temp;
struct teniesIndex *itemp;
int pl, i, m=0, meso; /* Πλήθος εγγραφών - δείκτης */
int diag;
char dg[10] = "*****", oadg[10] = "diag.dat", choice; /* Χαρακτήρες διαγραφής και όνομα αρχείου διαγραφής */
int eld; /* Πλήθος διαγραφέντων */
int deiktis; /* Dieuthinsh eggrafhs sto master arxeio */
FILE *diagf; /* Αρχείο ελέγχου διαγραφής */
FILE *tempf; /* Προσωρινό αρχείο */
pl = check_files();
/* perasma tou arxeiou index se pinaka */
indexf = fopen(oai, "rb");
ifilm = malloc( pl * sizeof(struct teniesIndex));
fread(&ifilm[0], sizeof(struct teniesIndex), pl, indexf);
fclose(indexf);
meso = anazhthsh();
if (meso == -1) /* An h eggrafh de brethike sthn anazhthsh */
return;
else{
do{
printf("\nNa diagrafei oristika h eggrafh?");
printf("(1=NAI, 0=OXI) : ");
choice = getchar();
}while(choice != '0' && choice != '1');
if (choice == 1){
printf("\n\nH eggrafi diagrafetai...\nPerimenete...\n\n");
deiktis = ifilm[meso].dief;
fseek(dataf, deiktis * sizeof(struct tenies), SEEK_SET);
temp = film;
strcpy(temp.code, dg);
fwrite(&temp, sizeof(struct tenies), 1, dataf);
/* Ενημέρωση πίνακα ευρετηρίου */
for (i=meso; i<pl-1; i++)
ifilm[i] = ifilm[i+1];
/* Plithos eggrafwn meiwthike kata 1 */
pl--;
/* Enhmerwsh arxeiou euretiriou */
indexf = fopen(oai, "wb");
fwrite(&ifilm[0], sizeof(struct teniesIndex), pl, indexf);
fclose(indexf);
/* Ενημέρωση αρχείου ελέγχου διαγραφών */
diagf = fopen(oadg, "rb+");
if ( !diagf ) {
diagf = fopen(oadg, "wb");
eld = 1;
fwrite(&eld, sizeof(int), 1, diagf);
fclose(diagf);
}
else {
fread(&eld, sizeof(int), 1, diagf);
eld++;
if ( eld <= D ) {
fseek(diagf, 0, SEEK_SET);
fwrite(&eld, sizeof(int), 1, diagf);
fclose(diagf);
}
else{
/* Antigrafh tou master arxeiou sto temp, ektos apo tis eggrafes pou diagrafikan */
dataf = fopen(oad, "rb");
fseek(dataf, 0, SEEK_SET);
tempf = fopen("temp.dat", "wb");
while (! feof(dataf) ) {
fread(&film, sizeof(struct tenies), 1, dataf);
if (! feof(dataf) && strcmp(film.code, dg)!=0) {
fwrite(&film, sizeof(struct tenies), 1, tempf);
}
}
fclose(dataf);
fclose(tempf);
/* telos antigrafhs */
dataf = fopen(oad, "wb");
tempf = fopen("temp.dat", "rb");
indexf = fopen(oai, "wb");
fseek(tempf, 0, SEEK_END);
m = ftell(tempf) / sizeof(struct tenies);
itemp = malloc(m * sizeof(struct teniesIndex));
fseek(tempf, 0, SEEK_SET);
m = 0;
while (! feof(tempf) ) {
fread(&film, sizeof(struct tenies), 1,tempf);
if (! feof(tempf) ) {
fwrite(&film, sizeof(struct tenies), 1, dataf);
strcpy(itemp[m].code, film.code);
itemp[m].dief = m;
m++;
}
}
/* Επαναταξινόμηση και επανοφαρά του αρχείου ευρετήριου */
selection_sort(itemp, m);
fwrite(&itemp[0], sizeof(struct teniesIndex), m, indexf);
fclose(indexf);
free(itemp);
fclose(tempf);
/* Ενημέρωση αρχείου διαγραφής */
diagf = fopen(oadg, "wb");
eld = 0;
fwrite(&eld, sizeof(int), 1, diagf);
fclose(diagf);
}
}
}
}
fclose(dataf);
}