//See if semaphor will work //Include files #include #include #include #include //Constant definitions const char LOW_MENU = '1'; const char HIGH_MENU = '5'; const char* const CHECKED_OUT_FILENAME = "check.out"; //Check this const char* const SEMAPHOR = "LOCK"; const int STRING_LENGTH = 20; const int MAX_FILES = 100; //Typedefs typedef char str[STRING_LENGTH]; //Global variables char id[80]; //The current user //Prototypes void Clear_Screen(); void Pause(); void Menu(); char Get_Choice(); bool Process_Choice(char); void Check_Out(); void Check_In(); void View_Checked_Out(); void Dir(); bool End(); int Read_File(ifstream&, str[], str[]); bool Get_Semaphor(); void Release_Semaphor(); int main () { char choice; bool DONE = false; Clear_Screen(); cout << "Enter your user id : "; cin >> id; while (!DONE) { Menu(); choice = Get_Choice(); DONE = Process_Choice(choice); if (DONE) DONE = End(); if (!DONE) Pause(); } //End of while } //--------------------------Function Definitions------------------------- void Clear_Screen() { cout << "\033[;H\033[2J" << flush; } void Pause() { char junk[80]; cout << "Press to continue."; cin.ignore(80,'\n'); cin.getline(junk, 80, '\n'); } void Menu() { Clear_Screen(); cout << " *************\n" << " * Main Menu *\n" << " *************\n" << " 1. Check out a file\n" << " 2. Check in a file\n" << " 3. View checked out files\n" << " 4. Directory\n" << " 5. End session/Check in files\n\n" << " ID : " << id << "\n\n"; } char Get_Choice() { char choice; bool VALID = false; while (!VALID) { cout << "Choice (V to view menu) :"; cin >> choice; if (choice == 'V' || choice == 'v') Menu(); else if (choice >= LOW_MENU && choice <= HIGH_MENU) VALID = true; else cout << "Invalid choice...Re-enter.\n"; } //End of while return choice; } bool Process_Choice(char choice) { bool DONE = false; switch (choice) { case HIGH_MENU : DONE = true; break; case '4': Dir(); break; case '3': View_Checked_Out(); break; case '2': Check_In(); break; case '1': Check_Out(); break; default : cerr << "Error in code in function Process_Choice\n"; DONE = true; break; } return DONE; } void View_Checked_Out() { ifstream fin(CHECKED_OUT_FILENAME); char file[STRING_LENGTH]; char name[STRING_LENGTH]; if (! (fin >> file)) { cout << "No files checked out.\n\n\n"; fin.close(); return; } fin.close(); fin.open(CHECKED_OUT_FILENAME); cout << "File Owner\n" << "----------------------------------------------\n"; while (fin >> file) { fin >> name; cout << setiosflags(ios::left) << setw(STRING_LENGTH) << file << resetiosflags(ios::left) << setiosflags(ios::right) << setw(STRING_LENGTH) << name << resetiosflags(ios::right) << endl; } //End of while cout << "\n\n"; } void Check_Out() { if (!Get_Semaphor()) return; str file; str file2; str name; cout << "Enter file to check out :"; cin >> file; ifstream fin(CHECKED_OUT_FILENAME); if (fin) { bool FOUND = false; while (fin >> file2 && !FOUND) { fin >> name; if (strcmp(file2,file) == 0) FOUND = true; } fin.close(); if (FOUND) cout << file << " is already checked out by " << name << "\n\n\n"; else { ofstream fout(CHECKED_OUT_FILENAME,ios::app); fout << file << endl << id << endl; fout.close(); cout << "You have checked out " << file << ".\n\n\n"; } } else { fin.close(); ofstream fout(CHECKED_OUT_FILENAME); fout << file << endl << id << endl; fout.close(); cout << "You have checked out " << file << ".\n\n\n"; } Release_Semaphor(); } //End of Check_Out int Read_File(ifstream& fin, str names[], str files[]) { int count = 0; while (fin >> files[count]) { fin >> names[count]; count++; } //End of while return count; } void Release_Semaphor() { str junk = ""; ofstream fout(SEMAPHOR); fout << junk; fout.close(); } bool Get_Semaphor() { str junk = ""; ifstream fin(SEMAPHOR); fin >> junk; fin.close(); if (strcmp(junk,SEMAPHOR) == 0) { cout << "File being updated...try later.\n\n\n"; return false; } ofstream fout(SEMAPHOR); fout << SEMAPHOR; fout.close(); return true; } void Check_In() { if (!Get_Semaphor()) return; str names[MAX_FILES]; str files[MAX_FILES]; str filename; int count = 0; ifstream fin(CHECKED_OUT_FILENAME); count = Read_File(fin, names, files); fin.close(); if (count == 0) { cout << "No files to check in.\n\n\n"; return; } cout << "Name of file to check in : "; cin >> filename; //Search for filename int i; for (i=0; i < count; i++) if (strcmp(filename, files[i]) == 0) break; if (strcmp(names[i],id) != 0) cout << "You don't own that file!\n\n\n"; else { int x; ofstream fout(CHECKED_OUT_FILENAME); for (x=0; x