// // Search function uses Zip code index for fast data retrieval. // Actual data seach is two phase process. // First we seach the index values and collect the parent table primary key. // Then we go and get the primary data table records. // void SearchPhoneBook(SPhoneBookDB& dbf, SPhoneBookZipIDX& idx, int zip_code) { list<int> person_id; // Index search. Here we store seach values in the list. {{ CBDB_FileCursor cur(idx); cur.SetCondition(CBDB_FileCursor::eEQ); cur.From << zip_code; while (cur.Fetch() == eBDB_Ok) { person_id.push_back(idx.person_id.Get()); } }} if (person_id.empty()) { cout << "No records found." << endl; return; } // Data retrieval using index seach values. for (list<int>::const_iterator it = person_id.begin(); it != person_id.end(); ++it) { dbf.person_id = *it; EBDB_ErrCode ret = dbf.Fetch(); if (ret != eBDB_Ok) { assert(0); } cout << "====================================" << endl; PrintRecord(dbf); } }
/* Prompts the user for a name to search for. Performs a case-sensitive string search for a record by that name on disk, and prints out related data when found, or a failure message if not found. Pass: name of file to load from */ void SearchByNameMenu(char *filename) { FILE *f; struct employeeRecord *record; char buffer[1024]; bool found = false; f = fopen(filename, "r"); if (!f) { printf("File not found. Add some records first!\n"); return; } printf("Enter the name: "); fgets(buffer, 1024, stdin); /* Remove the \n from the string */ buffer[strlen(buffer) - 1] = '\0'; while(!feof(f) && !found) { record = ReadRecord(f); if (record == NULL) { break; } record->name = ReadString(f, record->nameLength); if (record->name != NULL) { if(!strcmp(buffer,record->name)) { found = 1; break; } free(record->name); } fseek(f, sizeof(char) * record->addressLength, SEEK_CUR); free(record); } if (found) { printf("Record found!\n"); record->address = ReadString(f, record->addressLength); PrintRecord(record); fclose(f); free(record->name); free(record); return; } fclose(f); printf("Record not found in file.\n"); }
/* Prompts the user for an index to search for. Iterates through records stored on-disk and prints out related data when index is found, or a failure message if not found. Pass: name of file to load from */ void SearchByIndexMenu(char *filename) { FILE *f; struct employeeRecord *record; char buffer[1024]; int index, count = 0; bool found = false; f = fopen(filename, "r"); if (!f) { printf("File not found. Add some records first!\n"); return; } printf("Enter a record number: "); fgets(buffer, 1024, stdin); sscanf(buffer, "%d", &index); while(!feof(f) && !found) { record = ReadRecord(f); if (record == NULL) { break; } if(count == index) { found = 1; break; } fseek(f, sizeof(char) * (record->nameLength + record->addressLength), SEEK_CUR); free(record); count++; } if (found) { printf("Record found!\n"); record->name = ReadString(f, record->nameLength); record->address = ReadString(f, record->addressLength); PrintRecord(record); fclose(f); free(record->name); free(record->address); free(record); return; } fclose(f); printf("Couldn't find the record index %d. There are only %d records.\n", index, count); }
// // Function scans all records in the phone book table. // One of the features of the BDB library is that cursor is attached // to the main open datafile instance and when we call fetch data become // immediately available in the datafile structure fields. // Cursor only defines search criteria. // void PrintPhoneBook() { SPhoneBookDB dbf; dbf.Open(s_PhoneBookDBFileName, CBDB_File::eReadOnly); CBDB_FileCursor cur(dbf); cur.SetCondition(CBDB_FileCursor::eFirst); unsigned int recs_fetched = 0; while (cur.Fetch() == eBDB_Ok) { cout << "====================================" << endl; PrintRecord(dbf); ++recs_fetched; } cout << "====================================" << endl; cout << "Records found:" << recs_fetched << endl; }
int main(int argc, char *argv[]) { int nNumInputs = 0; IRLOG_IOStruct *pInput = NULL; int rec_type; int bAllRecords = 1; int bExcludeType = 0; if (argc < 2) { printf("%s irlogfile [REC_TYPE]\n", argv[0]); return -1; } pInput = IRLOG_CreateInputStruct(argv[1]); if (pInput == NULL) { printf("Error setting up file '%s'\n", argv[1]); return -1; } if (argc > 2) { if (strcmp(argv[2], "RLOG_EVENT") == 0) { bAllRecords = 0; rec_type = RLOG_EVENT_TYPE; } if (strcmp(argv[2], "RLOG_ARROW") == 0) { bAllRecords = 0; rec_type = RLOG_ARROW_TYPE; } if (strcmp(argv[2], "RLOG_STATE") == 0) { bAllRecords = 0; rec_type = RLOG_STATE_TYPE; } if (strcmp(argv[2], "RLOG_COMM") == 0) { bAllRecords = 0; rec_type = RLOG_COMM_TYPE; } if (strcmp(argv[2], "RLOG_ENDLOG") == 0) { bAllRecords = 0; rec_type = RLOG_ENDLOG_TYPE; } } if (argc > 3) { if (strcmp(&argv[3][1], "exclude") == 0) bExcludeType = 1; } while (1) { if (bExcludeType) { if (pInput->header.type != rec_type) PrintRecord(pInput); } else { if ((bAllRecords) || (pInput->header.type == rec_type)) PrintRecord(pInput); } if (IRLOG_GetNextRecord(pInput)) { IRLOG_CloseInputStruct(&pInput); break; } } return 0; }