int RMM_GetFirstRec(int fileDesc, char *record, int recordSize) { int max_records, blockNum, i; char *blockBuf; if ((blockBuf = malloc(BF_BLOCK_SIZE)) == NULL) { BF_Errno = BFE_NOMEM; return (HF_errno = HFE_BFERROR); } max_records = BF_BLOCK_SIZE*8 / (recordSize*8 + 1); //-> Ypologizei to megisto plithos eggrafwn // pou xwraei to kathe block. blockNum = 0; //-> Tha psaksei na vrei to 1o eguro block // meta to 'block 0' dld meta to HF_HEADER. if(BF_GetNextBlock(fileDesc, &blockNum, &blockBuf) != BFE_OK ) //-> Fernei to prwto block pou vrei me estw mia return (HF_errno = HFE_BFERROR); // eggrafi. for(i=0 ; i < max_records ; i++) //-> Gia kathe thesi eggrafis sto block, koitaei if(isBitActive(blockBuf, BF_BLOCK_SIZE*8-1 -i)) { // ean uparxei eggrafi. memcpy(record, blockBuf+i*recordSize, recordSize); //-> Ean nai, adigrafei tin eggrafi stin 'record'. break; // } if (BF_UnpinBlock(fileDesc, blockNum, 0 ) != BFE_OK ) //-> Kanei unpin to block pou efere sti mnimi. return (HF_errno = HFE_BFERROR); return ((blockNum-1)*max_records + i); }
int RMM_GetNextRec(int fileDesc, int recId, char *record, int recordSize) { int ret, i, max_records, blockNum, startingRec; char *blockBuf; if (( blockBuf = malloc(BF_BLOCK_SIZE)) == NULL) { BF_Errno = BFE_NOMEM; return (HF_errno = HFE_BFERROR); } max_records = BF_BLOCK_SIZE*8 / (recordSize*8 + 1); //-> Ypologizei to megisto plithos eggrafwn // pou xwraei to kathe block. blockNum = recId / max_records + 1; //-> Ypologizei poio einai to block sto opoio // vrisketai i 'recId'-iosti eggrafi. startingRec = recId % max_records + 1; //-> Vriskei ti thesi tis epomenis eggrafis mesa // sto block. Ean ksepernaei to 'max_records' // den einai provlima. if (BF_GetThisBlock(fileDesc, blockNum, &blockBuf) != BFE_OK ) //-> Fernei stin mnimi to block sto opoio vrisketai return (HF_errno = HFE_BFERROR); // i eggrafi me anagnwristiko 'recId'. do { //-> Gia to block pou exei erthei sti mnimi, for(i = startingRec ; i < max_records; i++) //-> Gia kathe thesi eggrafis tou block if (isBitActive(blockBuf, BF_BLOCK_SIZE*8-1 -i)) { // ksekinontas apo tin 'startingRec', // elegxei ean uparxei eggrafi. memcpy(record, blockBuf+i*recordSize, recordSize); //-> Ean nai, adigrafei tin eggrafi sto // 'record'. if (BF_UnpinBlock(fileDesc, blockNum, 0) != BFE_OK) //-> Kanei unpin to block pou exei ferei return (HF_errno = HFE_BFERROR); // sti mnimi. return ((blockNum-1)*max_records + i); } if (BF_UnpinBlock(fileDesc, blockNum, 0) != BFE_OK) //-> Kanei unpin to block pou exei ferei return (HF_errno = HFE_BFERROR); // sti mnimi. startingRec = 0; //-> Thetei tin arxiki thesi anazitisis // eggrafis enos block se '0' (1i thesi). } while ((ret = BF_GetNextBlock(fileDesc, &blockNum , &blockBuf)) == BFE_OK ); //-> Den exei vrethei akomi i zitoumeni "epomeni // eggrafi", ki etsi psaxnei sto epomeno block. if (ret == BFE_EOF) return (HF_errno = HFE_EOF); else return (HF_errno = HFE_BFERROR); }
int main() { int athletesFd; int athletesBlockNum; char *athletesBlockBuf; int athleteId; char athleteName[BF_MAIN_NAME_SIZE]; char athleteSurName[BF_MAIN_NAME_SIZE]; /* Αρχικοποίηση του επιπέδου αρχείου μπλοκ */ BF_Init(); /* ’νοιγμα του αρχείου ATHLETES */ athletesFd = BF_OpenFile("ATHLETES"); if (athletesFd < 0) { BF_PrintError("Error in BF_OpenFile called on ATHLETES."); return; } /* Εκτύπωση όλων των αθλητών */ if (BF_GetFirstBlock(athletesFd, &athletesBlockNum, &athletesBlockBuf) != BFE_OK) BF_PrintError("Error in BF_GetFirstBlock called on ATHLETES."); printf("File ATHLETES contains the following athletes:\nID NAME\n--- ----------------------------\n"); do { int i; for (i = 0; i < BF_MAIN_MAX_NUM_OF_RECS; i++) { athleteId = * (int *) athletesBlockBuf; athletesBlockBuf += sizeof(int); strcpy(athleteName, athletesBlockBuf); athletesBlockBuf += BF_MAIN_NAME_SIZE; strcpy(athleteSurName, athletesBlockBuf); athletesBlockBuf += BF_MAIN_NAME_SIZE; if (strlen(athleteName) == 0) break; printf("%03d %s %s\n", athleteId, athleteName, athleteSurName); } BF_UnpinBlock(athletesFd, athletesBlockNum, FALSE); } while(BF_GetNextBlock(athletesFd, &athletesBlockNum, &athletesBlockBuf) == BFE_OK); printf("\n**********************\n\n"); /* Κλείσιμο του αρχείου ATHLETES. */ if (BF_CloseFile(athletesFd) < 0) BF_PrintError("Error in BF_CloseFile called on ATHLETES."); /* Σβήσιμο των τριών αρχείων */ if (BF_DestroyFile("EVENTS") != BFE_OK) BF_PrintError("Error in BF_DestroyFile called on EVENTS."); else printf("File EVENTS successfully destroyed.\n"); if (BF_DestroyFile("ATHLETES") != BFE_OK) BF_PrintError("Error in BF_DestroyFile called on ATHLETES."); else printf("File ATHLETES successfully destroyed.\n"); if (BF_DestroyFile("PARTICIPATIONS") != BFE_OK) BF_PrintError("Error in BF_DestroyFile called on PARTICIPATIONS."); else printf("File PARTICIPATIONS successfully destroyed.\n"); return 0; }