Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Alexander Parent
- Date: June 22th
- Purpose: Assignment 2
- University of Guelph, 2017.
- CIS*2520 (DE) S17
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <signal.h>
- #include <unistd.h> /* for sleep() */
- #include <curses.h>
- #include "HashTableAPI.h"
- typedef struct passwordInfo {
- char *username;
- char *password;
- char *websiteURL;
- char *description;
- int dateAdded;
- int dateModified;
- } passwordInfo;
- int hashPassword(size_t tableSize, char *key)
- {
- int sum = 0;
- int i;
- for (i = 0; i < strlen(key); ++i)
- sum += key[i];
- return sum%tableSize;
- }
- void destroyPassword(void *data)
- {
- return;
- }
- void printPassword(void *toBePrinted)
- {
- passwordInfo *printInfo = (passwordInfo*)toBePrinted;
- printf("%s\n", printInfo->username);
- }
- typedef struct Coordinate {
- int x, y;
- } Coordinate;
- FILE *createFile(char *username, char *password)
- {
- char *filename = malloc(strlen(username)+6);
- strcpy(filename, username);
- strcat(filename, ".psswd");
- FILE *newFile = fopen(filename, "wb+");
- fwrite(username, 1, strlen(username)+1, newFile);
- fwrite(password, 1, strlen(password)+1, newFile);
- free(filename);
- return newFile;
- }
- FILE* checkLogin(char *username, char *password)
- {
- endwin();
- char *filename = malloc(strlen(username)+6);
- strcpy(filename, username);
- strcat(filename, ".psswd");
- //printf("%s\n", username);
- FILE *foundFile = fopen(filename, "rb");
- if (foundFile == NULL)
- return NULL;
- char currentChar = ' ';
- while (currentChar != '\0') {
- currentChar = fgetc(foundFile);
- printf("%c", currentChar);
- }
- printf("\n");
- currentChar = " ";
- while (currentChar != '\0') {
- currentChar = fgetc(foundFile);
- printf("%c", currentChar);
- }
- printf("\n");
- return foundFile;
- }
- FILE *loginScreen(int overMessage)
- {
- clear();
- int i;
- int currentChar = 0;
- /* 0 - username; 1 - password; 2 - enter; 3 - cancel*/
- int selected = 0;
- int row, col;
- char *overMessages[3] = {"Enter new user information", "Enter login information", "Invalid password or username"};
- char *messages[4] = {"Username: ", "Password: ", "Enter", "Cancel"};
- char *usernameString = malloc(1);
- char *passwordString = malloc(1);
- usernameString[0] = '\0';
- passwordString[0] = '\0';
- getmaxyx(stdscr, row, col);
- Coordinate usernamePlace = {(col-strlen(messages[0])) / 2 + strlen(messages[0]), row/2-1};
- Coordinate passwordPlace = {(col-strlen(messages[1])) / 2 + strlen(messages[1]), row/2};
- Coordinate enterPlace = {(col-strlen(messages[2])) / 2 + strlen(messages[2]), row/2+1};
- Coordinate cancelPlace = {(col-strlen(messages[3])) / 2 + strlen(messages[3]), row/2+2};
- Coordinate places[4] = {usernamePlace, passwordPlace, enterPlace, cancelPlace};
- mvprintw(row/2-5, (col-strlen(overMessages[overMessage])) / 2, "%s",overMessages[overMessage]);
- for (i = 0; i < 4; ++i)
- mvprintw(places[i].y, places[i].x - strlen(messages[i]),"%s",messages[i]);
- move(places[0].y, places[0].x);
- while (!(currentChar == 10 && (selected == 0 || selected == 3))) {
- currentChar = getch();
- if (currentChar == KEY_DOWN || currentChar == 10 || currentChar == KEY_UP)
- {
- if (currentChar == KEY_UP)
- {
- selected--;
- if (selected == -1)
- selected = 3;
- } else {
- selected++;
- if (selected == 4)
- selected = 0;
- }
- move(places[selected].y, places[selected].x);
- if (selected == 0)
- {
- printw("%*c", strlen(usernameString), ' ');
- usernameString = realloc(usernameString, 1);
- usernameString[0] = '\0';
- } else if (selected == 1) {
- printw("%*c", strlen(passwordString), ' ');
- passwordString = realloc(passwordString, 1);
- passwordString[0] = '\0';
- }
- move(places[selected].y, places[selected].x);
- } else if (currentChar == KEY_BACKSPACE) {
- if (selected == 0 && strlen(usernameString) > 0)
- {
- usernameString[strlen(usernameString)-1] = '\0';
- usernameString = realloc(usernameString, strlen(usernameString)+1);
- getyx(stdscr, row, col);
- mvprintw(row, col-1, " ");
- move(row, col-1);
- } else if (selected == 1 && strlen(passwordString) > 0) {
- passwordString[strlen(passwordString)-1] = '\0';
- passwordString = realloc(passwordString, strlen(passwordString)+1);
- getyx(stdscr, row, col);
- mvprintw(row, col-1, " ");
- move(row, col-1);
- }
- } else if (currentChar >= ' ' && currentChar <= '~') {
- if (selected == 0)
- {
- int temp = strlen(usernameString);
- usernameString = realloc(usernameString, temp+1);
- usernameString[temp] = currentChar;
- usernameString[temp+1] = '\0';
- printw("%c", currentChar);
- } else if (selected == 1) {
- int temp = strlen(passwordString);
- passwordString = realloc(passwordString, temp+1);
- passwordString[temp] = currentChar;
- passwordString[temp+1] = '\0';
- printw("%c", currentChar);
- }
- }
- }
- FILE *userFile = NULL;
- if (overMessage == 0)
- userFile = createFile(usernameString, passwordString);
- else
- userFile = checkLogin(usernameString, passwordString);
- fclose(userFile);
- checkLogin(usernameString, passwordString);
- free(usernameString);
- free(passwordString);
- return 1;
- }
- int firstMenu()
- {
- int i;
- int row, col;
- char *messages[4] = {"1. Create new user", "2. Edit existing user password", "3. Delete user", "4. Open user file"};
- Coordinate *coordinates[4];
- getmaxyx(stdscr, row, col);
- for (i = 0; i < 4; ++i)
- {
- coordinates[i] = malloc(sizeof(Coordinate));
- coordinates[i]->x = col/3 + strlen(messages[i]);
- coordinates[i]->y = row/2+i-3;
- }
- for (i = 0; i < 4; ++i)
- mvprintw(coordinates[i]->y, coordinates[i]->x - strlen(messages[i]),"%s",messages[i]);
- refresh();
- getch();
- for (i = 0; i < 4; ++i)
- free(coordinates[i]);
- return 1;
- }
- int main()
- {
- initscr();
- noecho();
- keypad(stdscr, TRUE);
- HTable *passwordTable = createTable(10, hashPassword, destroyPassword, printPassword);
- /*passwordInfo asdf;
- asdf.username = "asdfg";
- char *p = "ASDFASDF";
- insertData(passwordTable, p, &asdf);
- passwordTable->printData(lookupData(passwordTable, p));*/
- /*if (!loginScreen(0))
- return 0;*/
- firstMenu();
- endwin();
- destroyTable(passwordTable);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement