Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. /*
  2. Author: Alexander Parent
  3. Date: June 22th
  4. Purpose: Assignment 2
  5.  
  6. University of Guelph, 2017.
  7. CIS*2520 (DE) S17
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14.  
  15. #include <signal.h>
  16.  
  17. #include <unistd.h> /* for sleep() */
  18. #include <curses.h>
  19.  
  20. #include "HashTableAPI.h"
  21.  
  22. typedef struct passwordInfo {
  23. char *username;
  24. char *password;
  25. char *websiteURL;
  26. char *description;
  27. int dateAdded;
  28. int dateModified;
  29. } passwordInfo;
  30.  
  31. int hashPassword(size_t tableSize, char *key)
  32. {
  33. int sum = 0;
  34. int i;
  35.  
  36. for (i = 0; i < strlen(key); ++i)
  37. sum += key[i];
  38.  
  39. return sum%tableSize;
  40. }
  41.  
  42. void destroyPassword(void *data)
  43. {
  44. return;
  45. }
  46.  
  47. void printPassword(void *toBePrinted)
  48. {
  49. passwordInfo *printInfo = (passwordInfo*)toBePrinted;
  50. printf("%s\n", printInfo->username);
  51. }
  52.  
  53. typedef struct Coordinate {
  54. int x, y;
  55. } Coordinate;
  56.  
  57. FILE *createFile(char *username, char *password)
  58. {
  59. char *filename = malloc(strlen(username)+6);
  60. strcpy(filename, username);
  61. strcat(filename, ".psswd");
  62.  
  63. FILE *newFile = fopen(filename, "wb+");
  64.  
  65. fwrite(username, 1, strlen(username)+1, newFile);
  66. fwrite(password, 1, strlen(password)+1, newFile);
  67.  
  68. free(filename);
  69.  
  70. return newFile;
  71. }
  72.  
  73. FILE* checkLogin(char *username, char *password)
  74. {
  75. endwin();
  76.  
  77. char *filename = malloc(strlen(username)+6);
  78. strcpy(filename, username);
  79. strcat(filename, ".psswd");
  80.  
  81. //printf("%s\n", username);
  82.  
  83. FILE *foundFile = fopen(filename, "rb");
  84.  
  85. if (foundFile == NULL)
  86. return NULL;
  87.  
  88. char currentChar = ' ';
  89.  
  90. while (currentChar != '\0') {
  91. currentChar = fgetc(foundFile);
  92. printf("%c", currentChar);
  93. }
  94.  
  95. printf("\n");
  96.  
  97. currentChar = " ";
  98.  
  99. while (currentChar != '\0') {
  100. currentChar = fgetc(foundFile);
  101. printf("%c", currentChar);
  102. }
  103.  
  104. printf("\n");
  105. return foundFile;
  106. }
  107.  
  108. FILE *loginScreen(int overMessage)
  109. {
  110. clear();
  111.  
  112. int i;
  113. int currentChar = 0;
  114.  
  115. /* 0 - username; 1 - password; 2 - enter; 3 - cancel*/
  116. int selected = 0;
  117. int row, col;
  118. char *overMessages[3] = {"Enter new user information", "Enter login information", "Invalid password or username"};
  119. char *messages[4] = {"Username: ", "Password: ", "Enter", "Cancel"};
  120.  
  121. char *usernameString = malloc(1);
  122. char *passwordString = malloc(1);
  123.  
  124. usernameString[0] = '\0';
  125. passwordString[0] = '\0';
  126. getmaxyx(stdscr, row, col);
  127.  
  128. Coordinate usernamePlace = {(col-strlen(messages[0])) / 2 + strlen(messages[0]), row/2-1};
  129. Coordinate passwordPlace = {(col-strlen(messages[1])) / 2 + strlen(messages[1]), row/2};
  130. Coordinate enterPlace = {(col-strlen(messages[2])) / 2 + strlen(messages[2]), row/2+1};
  131. Coordinate cancelPlace = {(col-strlen(messages[3])) / 2 + strlen(messages[3]), row/2+2};
  132. Coordinate places[4] = {usernamePlace, passwordPlace, enterPlace, cancelPlace};
  133.  
  134. mvprintw(row/2-5, (col-strlen(overMessages[overMessage])) / 2, "%s",overMessages[overMessage]);
  135.  
  136. for (i = 0; i < 4; ++i)
  137. mvprintw(places[i].y, places[i].x - strlen(messages[i]),"%s",messages[i]);
  138.  
  139. move(places[0].y, places[0].x);
  140.  
  141. while (!(currentChar == 10 && (selected == 0 || selected == 3))) {
  142. currentChar = getch();
  143.  
  144. if (currentChar == KEY_DOWN || currentChar == 10 || currentChar == KEY_UP)
  145. {
  146.  
  147. if (currentChar == KEY_UP)
  148. {
  149. selected--;
  150. if (selected == -1)
  151. selected = 3;
  152. } else {
  153. selected++;
  154. if (selected == 4)
  155. selected = 0;
  156. }
  157.  
  158. move(places[selected].y, places[selected].x);
  159.  
  160. if (selected == 0)
  161. {
  162. printw("%*c", strlen(usernameString), ' ');
  163. usernameString = realloc(usernameString, 1);
  164. usernameString[0] = '\0';
  165. } else if (selected == 1) {
  166. printw("%*c", strlen(passwordString), ' ');
  167. passwordString = realloc(passwordString, 1);
  168. passwordString[0] = '\0';
  169. }
  170.  
  171. move(places[selected].y, places[selected].x);
  172.  
  173. } else if (currentChar == KEY_BACKSPACE) {
  174.  
  175. if (selected == 0 && strlen(usernameString) > 0)
  176. {
  177. usernameString[strlen(usernameString)-1] = '\0';
  178. usernameString = realloc(usernameString, strlen(usernameString)+1);
  179.  
  180. getyx(stdscr, row, col);
  181. mvprintw(row, col-1, " ");
  182. move(row, col-1);
  183.  
  184. } else if (selected == 1 && strlen(passwordString) > 0) {
  185. passwordString[strlen(passwordString)-1] = '\0';
  186. passwordString = realloc(passwordString, strlen(passwordString)+1);
  187.  
  188. getyx(stdscr, row, col);
  189. mvprintw(row, col-1, " ");
  190. move(row, col-1);
  191. }
  192.  
  193. } else if (currentChar >= ' ' && currentChar <= '~') {
  194. if (selected == 0)
  195. {
  196. int temp = strlen(usernameString);
  197. usernameString = realloc(usernameString, temp+1);
  198.  
  199. usernameString[temp] = currentChar;
  200. usernameString[temp+1] = '\0';
  201.  
  202. printw("%c", currentChar);
  203.  
  204. } else if (selected == 1) {
  205. int temp = strlen(passwordString);
  206. passwordString = realloc(passwordString, temp+1);
  207.  
  208. passwordString[temp] = currentChar;
  209. passwordString[temp+1] = '\0';
  210.  
  211. printw("%c", currentChar);
  212. }
  213. }
  214.  
  215. }
  216.  
  217. FILE *userFile = NULL;
  218.  
  219. if (overMessage == 0)
  220. userFile = createFile(usernameString, passwordString);
  221. else
  222. userFile = checkLogin(usernameString, passwordString);
  223.  
  224. fclose(userFile);
  225.  
  226. checkLogin(usernameString, passwordString);
  227.  
  228. free(usernameString);
  229. free(passwordString);
  230.  
  231. return 1;
  232. }
  233.  
  234. int firstMenu()
  235. {
  236. int i;
  237. int row, col;
  238. char *messages[4] = {"1. Create new user", "2. Edit existing user password", "3. Delete user", "4. Open user file"};
  239. Coordinate *coordinates[4];
  240.  
  241. getmaxyx(stdscr, row, col);
  242.  
  243. for (i = 0; i < 4; ++i)
  244. {
  245. coordinates[i] = malloc(sizeof(Coordinate));
  246. coordinates[i]->x = col/3 + strlen(messages[i]);
  247. coordinates[i]->y = row/2+i-3;
  248. }
  249.  
  250. for (i = 0; i < 4; ++i)
  251. mvprintw(coordinates[i]->y, coordinates[i]->x - strlen(messages[i]),"%s",messages[i]);
  252.  
  253. refresh();
  254.  
  255. getch();
  256.  
  257. for (i = 0; i < 4; ++i)
  258. free(coordinates[i]);
  259.  
  260. return 1;
  261. }
  262.  
  263. int main()
  264. {
  265. initscr();
  266. noecho();
  267. keypad(stdscr, TRUE);
  268.  
  269. HTable *passwordTable = createTable(10, hashPassword, destroyPassword, printPassword);
  270.  
  271. /*passwordInfo asdf;
  272.  
  273. asdf.username = "asdfg";
  274.  
  275. char *p = "ASDFASDF";
  276.  
  277. insertData(passwordTable, p, &asdf);
  278. passwordTable->printData(lookupData(passwordTable, p));*/
  279.  
  280. /*if (!loginScreen(0))
  281. return 0;*/
  282.  
  283. firstMenu();
  284.  
  285. endwin();
  286.  
  287.  
  288.  
  289.  
  290. destroyTable(passwordTable);
  291.  
  292. return 0;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement