1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#define NOMINMAX
#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstring>
/* User info output file name */
const char* FILE_NAME = "user.uls";
/* used as a passowrd override */
const char* OVERRIDE = "admin1234";
/* global constants for various flags */
const int FORCE_EXIT = 1,
NO_FORCE_EXIT = 0,
ENTER_INFO = 1,
NEW_USER = 1,
BUFFER_CLEAR = 0,
ERROR_MESSAGE = 1,
PAUSE = 2,
EXIT_MESSAGE = 3,
MAX_CHAR = 4;
//----------------------------------------------//
/* utility(int):
clears buffer, displays a message, and clears
cin error flag if necessary.
Can be used to pause the program. */
//----------------------------------------------//
void utility(int message)
{
if (std::cin.fail())
std::cin.clear();
if (message == PAUSE)
std::cout << "\nPress Enter to continue....\n";
if (message == EXIT_MESSAGE)
std::cout << "\nPress Enter to exit....\n";
/* clear cin buffer */
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
if (message == ERROR_MESSAGE) {
std::cout << "INVALID INPUT\n";
std::cout << "TRY AGAIN\n\n";
}
if (message == MAX_CHAR) {
std::cout << "TOO MANY CHARACTERS\n";
std::cout << "TRY AGAIN\n\n";
}
}
//------------------------------------------//
/* clear_screen():
Uses Windows API to clear console screen */
//------------------------------------------//
void clear_screen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE)
return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi ))
return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter( hStdOut, (TCHAR) ' ', cellCount, homeCoords, &count ))
return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute( hStdOut, csbi.wAttributes, cellCount, homeCoords, &count ))
return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
//------------------------------------//
/* FILE_HANDLE:
template structure to handle files */
//------------------------------------//
template<class TYPE>
struct FILE_HANDLE
{
TYPE file; // ifstream, ofstream, or fstream data type
FILE_HANDLE(const char* f_name, int mode, int force_exit)
{
file.open( f_name, mode );
if (file.fail()) {
std::cout << "\nCould not open " << f_name << '\n';
if (force_exit) {
utility( EXIT_MESSAGE );
exit(1);
}
else
utility( PAUSE );
}
}
/* close file upon destruction of handle */
~FILE_HANDLE() { file.close(); }
};
//------------------------------------------------------------------------------------------------------//
/* UserList:
This class uses a linked list structure to store users and their
info.
The design is (kinda like) a privately contained user interface. The program just needs to
call mainMenu() method and the Object does the rest.
Private Methods:
void mainMenuPrompt() clear screen and prompt for the main menu
void userMenuPrompt() clear screen and prompt fot the user menu
void newUser(int = 0) creates new user node in linked list
void getUser(int) selects user node
bool correctPassword(char[]) checks string input
void userMenu() menu for current user
void showInfo() clear screen and display current user's info
void enterInfo(int = 0) enters or edits current user's info
void deleteUser() deletes current user node
void encryption() encrypts or decrypts user's data
void readFile() reads list data from file and creates nodes from data
void writeFile() writes list data to file and deletes all nodes
Public Methods:
UserList() calls readFile()
~UserList() calls writeFile()
void mainMenu() main 'run' method for User List
void printUsers() prints a list of saved user's
-----------------------------------------------------------------------------------------------------
[User List user interface Flow:]
mainMenu() [loop]
/ \
newUser(int = 0) getUser(int)
| |
enterInfo(int = 0) userMenu() [loop]
| |
userMenu() [loop] -------------------------
| / | \
------------------------- showInfo() enterInfo(int = 0) deleteUser()
/ | \
showInfo() enterInfo(int = 0) deleteUser()
*/
//------------------------------------------------------------------------------------------------------//
class UserList
{
//-----------------//
/* private membors */
//-----------------//
static const int USERNAME_SIZE = 31,
PASSWORD_SIZE = 11;
int user_node_count, // node counter
current_node_id; // assigns a different ID to each node
/* Node/Node Pointer Structure */
typedef struct user_node {
char user_name[USERNAME_SIZE]; // array to hold username
char user_password[PASSWORD_SIZE]; // array to hold password
int user_id, // stores unique user id
secret_number; // user's secret data
user_node* next; // points to next node in list
}* user_node_ptr;
/* pointers to manipulate linked list */
user_node_ptr head; // points to first node in the list at all times
user_node_ptr curr; // points to current node that is being manipulated
user_node_ptr temp; // temporary pointer: used for writing nodes to a file and/or deleting nodes
|