wordParse.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // File: wordParse.c
  3. // wordParse.c builds a table of words for pass() to use. It either uses
  4. // predefined words, or words read in from the config file.
  5. // @author Joshua Bicking <josh1147582>
  6. // // // // // // // // // // // // // // // // // // // // // // //
  7. #include "getline.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "wordParse.h"
  12. #include <ctype.h>
  13. char **wordArr;
  14. int numWords;
  15. int wordsToChoose;
  16. char * victoryProg;
  17. char * completeProg;
  18. keyConfig_type keyConfig = ARROWS;
  19. void readWordsFromFile(){
  20. // Open the config file
  21. FILE * fp = fopen("FalloutTerminal.cfg", "r");
  22. // If there's no config file, default to very easy
  23. if(fp == NULL) {
  24. setVeryEasy();
  25. return;
  26. }
  27. // Check each line for valid words. Stop once launching is reached.
  28. char * buf;
  29. size_t n = 0;
  30. numWords = 0;
  31. while(getline(&buf, &n, fp) != -1){
  32. if(buf[0] == '#' || !strcmp(buf, "\n"))
  33. continue;
  34. // Remove the \n at the end of buff
  35. buf[strlen(buf)-1] = '\0';
  36. // Check for :WORDS_TO_CHOOSE
  37. if(!strncmp(buf, ":WORDS_TO_CHOOSE=",17)) {
  38. sscanf(buf+17, "%d", &wordsToChoose);
  39. continue;
  40. }
  41. // Check all chars in buf are A-Z or a-z
  42. int invalidChars = 0;
  43. for(int i=0; (unsigned long)i<strlen(buf); i++){
  44. if(buf[i] > 64 && buf[i] < 91)
  45. continue;
  46. else if(buf[i] > 96 && buf[i] < 123)
  47. buf[i] = toupper(buf[i]);
  48. else
  49. invalidChars = 1;
  50. }
  51. if(invalidChars)
  52. continue;
  53. // Check all words are the same size as the first word
  54. if(numWords != 0) {
  55. if(strlen(buf) != strlen(*wordArr))
  56. continue;
  57. // Check the word doesn't already exist in the list
  58. int dupes = 0;
  59. for(int i=0; i < numWords; i++)
  60. if(!strcmp(*(wordArr+i), buf))
  61. dupes = 1;
  62. if(dupes)
  63. continue;
  64. }
  65. // Read the word into wordArr
  66. wordArr = realloc(wordArr, sizeof(char*) * (numWords+1));
  67. *(wordArr+numWords) = malloc(sizeof(char) * (strlen(buf)+1));
  68. strcpy(*(wordArr+numWords),buf);
  69. numWords++;
  70. }
  71. // If words to choose wasn't specified or found, default to 7
  72. if(wordsToChoose == 0)
  73. wordsToChoose = 7;
  74. if(wordsToChoose > numWords){
  75. // Too few valid words recieved: default to very easy
  76. free(wordArr);
  77. setVeryEasy();
  78. }
  79. free(buf);
  80. fclose(fp);
  81. }
  82. void readLaunches(){
  83. // Reopen the file
  84. FILE * fp = fopen("FalloutTerminal.cfg", "r");
  85. // If the file doesn't exist, stop
  86. if(fp == NULL){
  87. return;
  88. }
  89. char * buf;
  90. size_t n = 0;
  91. // Look for the parameters.
  92. while(getline(&buf, &n, fp) != -1){
  93. // Remove the \n at the end of buff
  94. buf[strlen(buf)-1] = '\0';
  95. // Search for :LAUNCH_ON_*
  96. if(!strncmp(buf, ":LAUNCH_ON_VICTORY=", 19) && victoryProg == NULL) {
  97. victoryProg = malloc(sizeof(char) * strlen(buf)-19+1);
  98. strcpy(victoryProg, buf+19);
  99. }
  100. if(!strncmp(buf, ":LAUNCH_ON_COMPLETE=", 20) && completeProg == NULL) {
  101. completeProg = malloc(sizeof(char) * strlen(buf)-20+1);
  102. strcpy(completeProg, buf+20);
  103. }
  104. }
  105. free(buf);
  106. fclose(fp);
  107. }
  108. void readKeys(){
  109. // Reopen the file
  110. FILE * fp = fopen("FalloutTerminal.cfg", "r");
  111. // If the file doesn't exist, stop
  112. if(fp == NULL){
  113. return;
  114. }
  115. char * buf;
  116. size_t n = 0;
  117. while(getline(&buf, &n, fp) != -1){
  118. // Remove the \n at the end of buff
  119. buf[strlen(buf)-1] = '\0';
  120. // Search for :KEYS=
  121. if(!strncmp(buf, ":KEYS=", 6)) {
  122. if(!strcmp(buf+6,"ARROWS")) {
  123. keyConfig = ARROWS;
  124. }else if(!strcmp(buf+6,"WASD")) {
  125. keyConfig = WASD;
  126. }else if(!strcmp(buf+6,"HJKL")) {
  127. keyConfig = HJKL;
  128. }
  129. }
  130. }
  131. free(buf);
  132. fclose(fp);
  133. }
  134. void setWordArr(char *words[]){
  135. wordArr = malloc(numWords * sizeof(char*));
  136. for(int i=0; i<numWords; i++) {
  137. *(wordArr+i) = malloc(sizeof(char) * (strlen(words[0])+1));
  138. strcpy(*(wordArr+i), *(words+i));
  139. }
  140. }
  141. void setVeryEasy() {
  142. char * words[] = {
  143. "FRIED",
  144. "TREES",
  145. "RIGID",
  146. "HIRED",
  147. "TRIES",
  148. "WRITE",
  149. "TRIED",
  150. "GREED",
  151. "DRIED",
  152. "BRAIN",
  153. "SKIES",
  154. "LAWNS",
  155. "GHOST",
  156. "CAUSE",
  157. "PAINT",
  158. "SHINY",
  159. "MAKES",
  160. "GAINS",
  161. "THIEF",
  162. "BASES",
  163. "RAISE",
  164. "REFER",
  165. "CARES",
  166. "TAKEN",
  167. "WAKES",
  168. "WAVES",
  169. "WARNS",
  170. "SAVES"
  171. };
  172. numWords = 28;
  173. wordsToChoose = 10;
  174. setWordArr(words);
  175. }
  176. void setEasy() {
  177. char * words[] = {
  178. "STATING",
  179. "HEALING",
  180. "COSTING",
  181. "REASONS",
  182. "SEASIDE",
  183. "SPARING",
  184. "CAUSING",
  185. "CRAFTED",
  186. "PRISONS",
  187. "PRESENT",
  188. "DEALING",
  189. "SETTING",
  190. "LEAVING",
  191. "VERSION",
  192. "DEATHLY",
  193. "BLAZING",
  194. "GRANITE",
  195. "TESTING",
  196. "TRAITOR",
  197. "STAMINA",
  198. "TRINITY",
  199. "CALLING",
  200. "TALKING",
  201. "ACQUIRE",
  202. "WELCOME",
  203. "DECRIES",
  204. "FALLING",
  205. "PACKING",
  206. "ALLOWED",
  207. "SELLING",
  208. "AFFRONT",
  209. "WALKING"
  210. };
  211. numWords = 32;
  212. wordsToChoose = 11;
  213. setWordArr(words);
  214. }void setAverage() {
  215. char * words[] = {
  216. "CONQUORER",
  217. "CONSISTED",
  218. "WONDERFUL",
  219. "COMMITTEE",
  220. "SURRENDER",
  221. "SUBJECTED",
  222. "CONVICTED",
  223. "FORBIDDEN",
  224. "FORTIFIED",
  225. "COLLECTED",
  226. "CONTINUED",
  227. "PERIMETER",
  228. "SOUTHEAST",
  229. "RELEASING",
  230. "SOMETHING",
  231. "ACCEPTING",
  232. "MUTATIONS",
  233. "GATHERING",
  234. "LITERALLY",
  235. "REPAIRING",
  236. "INCESSANT",
  237. "INTERIORS",
  238. "REGARDING",
  239. "TELEPHONE",
  240. "OBTAINING",
  241. "EXTENSIVE",
  242. "DEFEATING",
  243. "REQUIRING",
  244. "UNLOCKING",
  245. "RECYCLING",
  246. "INSTINCTS",
  247. "BARTERING",
  248. "LEUTENANT",
  249. "COMMUNITY",
  250. "BATTERIES",
  251. "RECIEVING",
  252. "INCLUDING",
  253. "INITIALLY",
  254. "INVOLVING",
  255. "MOUNTAINS"
  256. };
  257. numWords = 40;
  258. wordsToChoose = 14;
  259. setWordArr(words);
  260. }void setHard() {
  261. char * words[] = {
  262. "DISCOVERING",
  263. "ELIMINATING",
  264. "UNIMPORTANT",
  265. "MISTRUSTING",
  266. "MANUFACTURE",
  267. "RADIOACTIVE",
  268. "EXCLUSIVELY",
  269. "BOMBARDMENT",
  270. "DECEPTIVELY",
  271. "INDEPENDENT",
  272. "UNBELIEVERS",
  273. "EFFECTIVELY",
  274. "IMMEDIATELY",
  275. "INFESTATION",
  276. "DESCRIPTION",
  277. "INFORMATION",
  278. "REMEMBERING",
  279. "NIGHTVISION",
  280. "DESTRUCTION",
  281. "OVERLOOKING"
  282. };
  283. numWords = 20;
  284. wordsToChoose = 7;
  285. setWordArr(words);
  286. }void setVeryHard() {
  287. char * words[] = {
  288. "INFILTRATION",
  289. "ORGANIZATION",
  290. "AUTHENTICITY",
  291. "APPRECIATION",
  292. "SPOKESPERSON",
  293. "LABORATORIES",
  294. "INITIATEHOOD",
  295. "SUBTERRANEAN",
  296. "PURIFICATION",
  297. "TRANSMISSION",
  298. "CIVILIZATION",
  299. "CONSTRUCTION",
  300. "RESURRECTION",
  301. "REPRIMANDING",
  302. "ACCOMPANYING",
  303. "OVERWHELMING",
  304. "CONVERSATION",
  305. "NORTHERNMOST",
  306. "TRANSCRIBING",
  307. "ANNOUNCEMENT",
  308. "SECLUTIONIST"
  309. };
  310. numWords = 21;
  311. wordsToChoose = 13;
  312. setWordArr(words);
  313. }
  314. char ** getWordArr(){
  315. return wordArr;
  316. }
  317. int getNumWords() {
  318. return numWords;
  319. }
  320. int getWordsToChoose() {
  321. return wordsToChoose;
  322. }
  323. int getWordLength() {
  324. return strlen(*wordArr);
  325. }
  326. char * getVictoryProg() {
  327. if(victoryProg == NULL)
  328. return "";
  329. return victoryProg;
  330. }
  331. char * getCompleteProg() {
  332. if(completeProg == NULL)
  333. return "";
  334. return completeProg;
  335. }
  336. keyConfig_type getKeyConfig(){
  337. return keyConfig;
  338. }
  339. void freeAll() {
  340. for(int i=0; i<numWords; i++)
  341. free(*(wordArr+i));
  342. free(wordArr);
  343. free(victoryProg);
  344. free(completeProg);
  345. }
  346. // end