#include // All this to mmap a file #include #include #include #include #include #include #include "recform.h" #define DEFAULTFILENAME "/usr/local/csci/343/testdata/home01.dat" int rangeSearch(const simpRecT *, const char *, int, int, simpRecT &) ; void main(int argc, char *argv[]) { char key[KEYSIZE+80] ; simpRecT buff ; simpRecT *dbFile ; int dbFD ; struct stat fStat ; int maxRec ; dbFD = open(DEFAULTFILENAME, O_RDONLY) ; if (!dbFD) { cerr << "Unable to open " << DEFAULTFILENAME << endl ; return ; } (void) fstat(dbFD, &fStat) ; maxRec = fStat.st_size / sizeof(simpRecT) - 1; if (maxRec < 0) { cerr << DEFAULTFILENAME << " has no records!" << endl ; return ; } if ((dbFile = (simpRecT *)mmap((void *)NULL, fStat.st_size, PROT_READ, MAP_SHARED, dbFD, (off_t)0)) == (simpRecT *)NULL) { cerr << "Unable to map " << DEFAULTFILENAME << endl ; return ; } cout << "Enter search key: " ; while (cin.getline(key, sizeof(key)) && key[0]) { int pbeg = -1 ; int pend = -1 ; key[KEYSIZE] = '\0' ; cout << "Enter indices of starting and ending records" << endl ; while (pbeg < 0 || pbeg > maxRec) { cout << "Start (0 to " << maxRec << "): " ; cin >> pbeg ; } while (pend < pbeg || pend > maxRec) { cout << "End (" << pbeg << " to " << maxRec << "): " ; cin >> pend ; } cout << endl << "Starting search" << endl ; cout << " Key: " << key << endl ; cout << " Start: " << pbeg << endl ; cout << " End: " << pend << endl ; if (rangeSearch(dbFile, key, pbeg, pend, buff)) cout << "Record found with key " << buff.name << " and value " << buff.value << endl ; else cout << "No record found with key " << key << endl ; cin.ignore(1999, (int) '\n') ; // pretty silly cout << endl << "New key: " ; } close(dbFD) ; return ; }