// Check if the page is on the free list. Requires traversing the link list // More efficient data structures are possible! const Status File::onFreeList(const int pageNo, bool& onFL) const { int pgNo; Page header; Status status; onFL = false; if ((status = intread(0, &header)) != OK) return status; // Check the free list pages if (DBP(header).nextFree != -1) // Free list exists { while ((pgNo = DBP(header).nextFree) != -1) { if (pageNo == pgNo) { onFL = true; return OK; } // Read the next page; if ((status = intread(pgNo, &header)) != OK) return status; } } return OK; }
const Status File::allocatePage(int& pageNo) { Page header; Status status; if ((status = intread(0, &header)) != OK) return status; // If free list has pages on it, take one from there // and adjust free list accordingly. if (DBP(header).nextFree != -1) { // free list exists? // Return first page on free list to the caller, // adjust free list accordingly. pageNo = DBP(header).nextFree; Page firstFree; if ((status = intread(pageNo, &firstFree)) != OK) return status; DBP(header).nextFree = DBP(firstFree).nextFree; } else { // no free list, have to extend file // Extend file -- the current number of pages will be // the page number of the page to be returned. pageNo = DBP(header).numPages; Page newPage; memset(&newPage, 0, sizeof newPage); if ((status = intwrite(pageNo, &newPage)) != OK) return status; DBP(header).numPages++; if (DBP(header).firstPage == -1) // first user page in file? DBP(header).firstPage = pageNo; } if ((status = intwrite(0, &header)) != OK) return status; #ifdef DEBUGFREE listFree(); #endif return OK; }
/* * "Window" change handler */ winch() { (void)signal(SIGWIND, winch); sigs |= S_WINCH; if (reading) intread(); }
/* * "Stop" (^Z) signal handler. */ static void stop() { (void)signal(SIGTSTP, stop); sigs |= S_STOP; if (reading) intread(); }
const Status File::readPage(const int pageNo, Page* pagePtr) const { if (!pagePtr) return BADPAGEPTR; if (pageNo < 1) return BADPAGENO; return intread(pageNo, pagePtr); }
const Status File::getFirstPage(int& pageNo) const { Page header; Status status; if ((status = intread(0, &header)) != OK) return status; pageNo = DBP(header).firstPage; return OK; }
// Deallocate a page from file. The page will be put on a free // list and returned back to the caller upon a subsequent // allocPage() call. // ALERT: No error returned if disposePage called on the same page twice! const Status File::disposePage(int pageNo) { if (pageNo < 1) return BADPAGENO; Page header; Status status; if ((status = intread(0, &header)) != OK) return status; // The first user-allocated page in the file cannot be // disposed of. The File layer has no knowledge of what // is the next page in the file and hence would not be // able to adjust the firstPage field in file header. if (DBP(header).firstPage == pageNo || pageNo >= DBP(header).numPages) return BADPAGENO; // Deallocate page by attaching it to the free list. Page away; if ((status = intread(pageNo, &away)) != OK) return status; memset(&away, 0, sizeof away); DBP(away).nextFree = DBP(header).nextFree; DBP(header).nextFree = pageNo; if ((status = intwrite(pageNo, &away)) != OK) return status; if ((status = intwrite(0, &header)) != OK) return status; #ifdef DEBUGFREE listFree(); #endif return OK; }
void File::listFree() { cerr << "%% File " << (int)this << " free pages:"; int pageNo = 0; for(int i = 0; i < 10; i++) { Page page; if (intread(pageNo, &page) != OK) break; pageNo = DBP(page).nextFree; cerr << " " << pageNo; if (pageNo == -1) break; } cerr << endl; }
const Status File::readPage(int pageNo, Page* pagePtr) const { if (!pagePtr) return BADPAGEPTR; if (pageNo < 1) return BADPAGENO; bool onFL; Status status; if ((status = onFreeList(pageNo, onFL)) != OK) return status; if (onFL) return BADPAGENO; // The page is on the free list. else return intread(pageNo, pagePtr); }
int readmap () { int x; file = fopen(temp,"r"); if(file == FALSE) return 1; for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } scripts = intread(ts); fread(&enemytf,1,1,file); if(enemytf == 'y') enemytf = TRUE; fread(&npctf,1,1,file); if(npctf == 'y') npctf = TRUE; if (npctf == TRUE) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } npcnum = intread(ts); } fread(&startmaptf,1,1,file); if(startmaptf == 'y') startmaptf = TRUE; if (startmaptf == TRUE) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } startx = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } starty = intread(ts); } for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } warpnum = intread(ts); for (maprunx = 0; maprunx < 20; maprunx++) { fread(&mapname [maprunx],1,1,file); } for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mapx = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mapy = intread(ts); for (mapruny = 0; mapruny < mapy; mapruny++) { for (maprunx = 0; maprunx < mapx; maprunx++) { fread(&map [maprunx][mapruny],1,1,file); } } for (mapruny = 0; mapruny < mapy; mapruny++) { for (maprunx = 0; maprunx < mapx; maprunx++) { fread(&maperm [maprunx][mapruny],1,1,file); } } for (mapruny = 0; mapruny < warpnum; mapruny++) { for (maprunx = 0; maprunx < 12; maprunx++) { fread(&mapwarps [mapruny].mapname [maprunx],1,1,file); } mapwarps[mapruny].mapname[12] = '\0'; for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mapwarps [mapruny].x [0] = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mapwarps [mapruny].y [0] = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mapwarps [mapruny].x [1] = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mapwarps [mapruny].y [1] = intread(ts); } for (mapruny = 0; mapruny < scripts; mapruny++) { for (maprunx = 0; maprunx < 12; maprunx++) { fread(&mscript [mapruny].scriptfilename [maprunx],1,1,file); } mscript [mapruny].scriptfilename [12] = '\0'; for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mscript [mapruny].fdependsnum = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mscript [mapruny].fnotifnum = intread(ts); for (maprunx = 0; maprunx < mscript [mapruny].fdependsnum; maprunx++) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mscript [mapruny].fdepends[maprunx] = intread(ts); } for (maprunx = 0; maprunx < mscript [mapruny].fnotifnum; maprunx++) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } mscript [mapruny].fnotif[maprunx] = intread(ts); } } fread(&enemytf,1,1,file); if(enemytf == 'y') enemytf = TRUE; if (enemytf == TRUE) { for (mapruny = 0; mapruny < 10; mapruny++) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } enemyent [0][mapruny] = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } enemyent [1][mapruny] = intread(ts); } } if (npctf == TRUE) { for (maprunz = 0;maprunz < npcnum; maprunz++) { fread(&people [maprunz].name [0],1,1,file); fread(&people [maprunz].name [1],1,1,file); fread(&people [maprunz].name [2],1,1,file); fread(&people [maprunz].name [3],1,1,file); fread(&people [maprunz].name [4],1,1,file); fread(&people [maprunz].name [5],1,1,file); fread(&people [maprunz].name [6],1,1,file); fread(&people [maprunz].name [7],1,1,file); fread(&people [maprunz].name [8],1,1,file); fread(&people [maprunz].name [9],1,1,file); fread(&people [maprunz].name [10],1,1,file); fread(&people [maprunz].name [11],1,1,file); for (maprunx = 0; maprunx < 13; maprunx++) { fread(&people [maprunz].scriptfile [maprunx],1,1,file); } for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].x = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].y = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].movetype = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].fdependsnum = intread(ts); for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].fnotifnum = intread(ts); for (maprunx = 0; maprunx < people [maprunz].fdependsnum; maprunx++) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].fdepends[maprunx] = intread(ts); } for (maprunx = 0; maprunx < people [maprunz].fnotifnum; maprunx++) { for(x = 0; x < 10; x++) { fread(&ts[x],1,1,file); } people [maprunz].fnotif[maprunx] = intread(ts); } } } fclose(file); return 0; }