int RMM_GetThisRec(int fileDesc, int recId, char *record, int recordSize) { int max_records, blockNum, recordNum; 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; //-> Vriskei poio einai to block sto opio vrisketai // i eggrafi pou zitithike na diavasei. recordNum = recId % max_records; //-> Vriskei ti thesi tis eggrafis mesa sto block. if (BF_GetThisBlock(fileDesc, blockNum, &blockBuf) != BFE_OK ) //-> Fernei sti mnimi to block sto opoio return (HF_errno = HFE_BFERROR); // vrisketai i eggrafi pou zitithike. if (!isBitActive(blockBuf, BF_BLOCK_SIZE*8-1 -recordNum)) { //-> Elegxei ean uparxei i eggrafi pou zitithike. if (BF_UnpinBlock(fileDesc, blockNum, 0) != BFE_OK) //-> Kanei unpin to block pou efere sti mnimi. return (HF_errno = HFE_BFERROR); // return (HF_errno = HFE_INVALIDREC); } memcpy(record, blockBuf+recordNum*recordSize, recordSize); //-> Adigrafei tin eggrafi sto 'record'. if(BF_UnpinBlock(fileDesc, blockNum, 0) != BFE_OK ) //-> Kanei unpin to block pou efere proigoumenws return (HF_errno = HFE_INVALIDREC); // sti mnimi. return HFE_OK; }
int RMM_InsertRec(int fileDesc, char *record, int recordSize) { int blockNum, ret, max_records, recordNum; char *blockBuf, *HF_HeadBuf; if ((HF_HeadBuf = malloc(BF_BLOCK_SIZE)) == NULL ) { BF_Errno = BFE_NOMEM; return (HF_errno = HFE_BFERROR); } if ((blockBuf = malloc(BF_BLOCK_SIZE)) == NULL ) { BF_Errno = BFE_NOMEM; return (HF_errno = HFE_BFERROR); } if(BF_GetThisBlock(fileDesc, 0, &HF_HeadBuf) != BFE_OK) //-> Fernei to HF_HEADER stin mnimi. return (HF_errno = HFE_BFERROR); // if ((blockNum = getFirstZeroBitPos(HF_HeadBuf,BF_BLOCK_SIZE)) == -1) //-> Psaxnei to HF_HEADER gia to 1o mi mideniko. Ean ola return (HF_errno = HFE_EOF); // einai 1 epistrefei 'EOF'. I epistrefomeni timi '0' blockNum ++; // anaferetai sto 1o block. if ((ret = BF_GetThisBlock(fileDesc, blockNum, &blockBuf)) == BFE_INVALIDBLOCK) { //-> Epixeirei na ferei sti mnimi to block sto if (BF_AllocBlock(fileDesc, &blockNum, &blockBuf) != BFE_OK) // opoio tha eisagagei tin eggrafi. Ean itan return (HF_errno = HFE_BFERROR); // mi eguro block, to dimiourgei kanontas alloc. } // else if (ret != BFE_OK ) return (HF_errno = HFE_BFERROR); max_records = BF_BLOCK_SIZE*8 / (recordSize*8 + 1); //-> Ypologizei to megisto plithos eggrafwn pou xwraei // to kathe block. for(recordNum=0 ; recordNum < max_records ; recordNum++) //-> Psaxnei sto anestrammeno bitmap tou block gia na if (!isBitActive(blockBuf, BF_BLOCK_SIZE*8-1 -recordNum)) // vrei tin prwti adeia thesi eggrafis sto block, break; // i opoia kai einai i 'recordNum'. Sigoura tha vrei // mia adeia thesi eggrafis, mias kai to block epilexthike // ef'oson den itan plirws gemato apo eggrafes. // memcpy(blockBuf + recordSize*recordNum, record, recordSize); //-> Adigrafei tin eggrafi sto block, stin thesi // 'recordNum'. setBit(blockBuf, BF_BLOCK_SIZE*8-1 -recordNum); //-> Enimerwnei to bitmap tou block gia to gemisma tis // eggrafis. if ((ret = FullOrEmpty(blockBuf, max_records, BF_BLOCK_SIZE, 1))) //-> Ean to block gemise me eggrafes, tote enimerwnei setBit(HF_HeadBuf, blockNum-1); // to HF_HEADER gia to pleon plirws gemato block. // Ean to to block einai gemato i 'ret' exei timi '1' // alliws '0'. if (BF_UnpinBlock(fileDesc, blockNum, 1) != BFE_OK) //-> Kanei unpin to block pou eisigage tin eggrafi me return (HF_errno = HFE_BFERROR); // dirty=1. if (BF_UnpinBlock(fileDesc, 0, ret) != BFE_OK) //-> Kanei unpin to HF_HEADER me dirty iso me tin timi return (HF_errno = HFE_BFERROR); // tis 'ret' pou epistrafike parapanw. return ((blockNum-1)*max_records + recordNum); }
int RMM_DeleteRec(int fileDesc, int recId, int recordSize) { int full, max_records, blockNum, recordNum; char *HF_HeadBuf, *blockBuf; if (( HF_HeadBuf = malloc(BF_BLOCK_SIZE)) == NULL ) { BF_Errno = BFE_NOMEM; return (HF_errno = HFE_BFERROR); } if (( blockBuf = malloc ( BF_BLOCK_SIZE )) == NULL ) { BF_Errno = BFE_NOMEM; return (HF_errno = HFE_BFERROR); } if(BF_GetThisBlock(fileDesc, 0, &HF_HeadBuf) != BFE_OK) //-> Fernei to HF_HEADER sti mnimi. 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; //-> Vriskei poio einai to block sto opoio vrisketai // i eggrafi pou zitithike na diagrapsei. if(BF_GetThisBlock(fileDesc, blockNum, &blockBuf) != BFE_OK) //-> Fernei stin mnimi to block sto opoio vrisketai return (HF_errno = HFE_BFERROR); // i eggrafi pou zitithike na diagrapsei. recordNum = recId % max_records; //-> Vriskei ti thesi tis eggrafis mesa sto block. if(!isBitActive(blockBuf, BF_BLOCK_SIZE*8-1 -recordNum)) //-> Elegxei ean i eggrafi pou zitithike na diagrafei { if (BF_UnpinBlock(fileDesc, blockNum, 0) != BFE_OK) // ontws uparxei mesa sto block. return (HF_errno = HFE_BFERROR); return (HF_errno = HFE_INVALIDREC); } resetBit(blockBuf, BF_BLOCK_SIZE*8-1 -recordNum); //-> "Diagrafei" ti zitoumeni eggrafi sto block, // midenizontas tin thesi tis sto bitmap tou block. if(( full = isBitActive(HF_HeadBuf, blockNum-1 ))) //-> Elegxei to HF_HEADER gia na dei ean to block prin resetBit(HF_HeadBuf, blockNum-1); // tin diagrafi tis eggrafis, itan plirws gemato. // Ean nai, enimerwnei to HF_HEADER gia tin nea // katastasi tou block. if(BF_UnpinBlock(fileDesc, blockNum, 1) != BFE_OK) //-> Kanei unpin to block. return (HF_errno = HFE_BFERROR); // if(FullOrEmpty(blockBuf, max_records, BF_BLOCK_SIZE, 0)) //-> Elegxei, ean meta apo tin diagrafi tis eggrafis if(BF_DisposeBlock(fileDesc, blockNum) != BFE_OK) // pou molis egine, to block den periexei kammia return (HF_errno = HFE_BFERROR); // eggrafi. Ean nai, kanei dispose to adeio block. if(BF_UnpinBlock(fileDesc, 0, full) != BFE_OK) //-> Kanei unpin to HF_HEADER me dirty iso me 'full', return (HF_errno = HFE_BFERROR); // i opoia exei timi '0' i '1' analoga me to ean // to block itan prin diagrafei oxi plirws gemato // i plirws gemato, antistoixa. return HFE_OK; }
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; }