wordParse.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. void readWordsFromFile(FILE* fp){
  19. if(fp == NULL) {
  20. setVeryEasy();
  21. return;
  22. }
  23. // Check if FalloutTerminal.cfg is invalid
  24. char * buf;
  25. size_t n = 0;
  26. numWords = 0;
  27. while(getline(&buf, &n, fp)){
  28. if(buf[0] == '#' || !strcmp(buf, "\n"))
  29. continue;
  30. // Remove the \n at the end of buff
  31. buf[strlen(buf)-1] = '\0';
  32. // Stop on :LAUNCH_ON_*
  33. if(buf[0] == ':' ) {
  34. if(!strncmp(buf, ":WORDS_TO_CHOOSE=",17)) {
  35. sscanf(buf+17, "%d", &wordsToChoose);
  36. continue;
  37. }
  38. else
  39. break;
  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(wordsToChoose == 0)
  72. wordsToChoose = 7;
  73. if(wordsToChoose > numWords){
  74. // Too few valid words recieved: default to very easy
  75. free(wordArr);
  76. setVeryEasy();
  77. }
  78. free(buf);
  79. }
  80. void readLaunches(FILE* fp){
  81. rewind(fp);
  82. if(fp == NULL){
  83. return;
  84. }
  85. char * buf;
  86. size_t n = 0;
  87. while(getline(&buf, &n, fp)){
  88. // Remove the \n at the end of buff
  89. buf[strlen(buf)-1] = '\0';
  90. // Search for :LAUNCH_ON_*
  91. if(!strncmp(buf, ":LAUNCH_ON_VICTORY=", 19) && victoryProg == NULL) {
  92. victoryProg = malloc(sizeof(char) * strlen(buf)-19+1);
  93. strcpy(victoryProg, buf+19);
  94. }
  95. if(!strncmp(buf, ":LAUNCH_ON_COMPLETE=", 20) && completeProg == NULL) {
  96. completeProg = malloc(sizeof(char) * strlen(buf)-20+1);
  97. strcpy(completeProg, buf+20);
  98. break;
  99. }
  100. }
  101. free(buf);
  102. }
  103. void setWordArr(char *words[]){
  104. wordArr = malloc(numWords * sizeof(char*));
  105. for(int i=0; i<numWords; i++) {
  106. // TODO replace numWords with wordsize
  107. *(wordArr+i) = malloc(sizeof(char) * numWords);
  108. strcpy(*(wordArr+i), *(words+i));
  109. }
  110. }
  111. void setVeryEasy() {
  112. char * words[] = {
  113. "FRIED",
  114. "TREES",
  115. "RIGID",
  116. "HIRED",
  117. "TRIES",
  118. "WRITE",
  119. "TRIED",
  120. "GREED",
  121. "DRIED",
  122. "BRAIN",
  123. "SKIES",
  124. "LAWNS",
  125. "GHOST",
  126. "CAUSE",
  127. "PAINT",
  128. "SHINY",
  129. "MAKES",
  130. "GAINS",
  131. "THIEF",
  132. "BASES",
  133. "RAISE",
  134. "REFER",
  135. "CARES",
  136. "TAKEN",
  137. "WAKES",
  138. "WAVES",
  139. "WARNS",
  140. "SAVES"
  141. };
  142. numWords = 28;
  143. wordsToChoose = 10;
  144. setWordArr(words);
  145. }
  146. void setEasy() {
  147. char * words[] = {
  148. "STATING",
  149. "HEALING",
  150. "COSTING",
  151. "REASONS",
  152. "SEASIDE",
  153. "SPARING",
  154. "CAUSING",
  155. "CRAFTED",
  156. "PRISONS",
  157. "PRESENT",
  158. "DEALING",
  159. "SETTING",
  160. "LEAVING",
  161. "VERSION",
  162. "DEATHLY",
  163. "BLAZING",
  164. "GRANITE",
  165. "TESTING",
  166. "TRAITOR",
  167. "STAMINA",
  168. "TRINITY",
  169. "CALLING",
  170. "TALKING",
  171. "ACQUIRE",
  172. "WELCOME",
  173. "DECRIES",
  174. "FALLING",
  175. "PACKING",
  176. "ALLOWED",
  177. "SELLING",
  178. "AFFRONT",
  179. "WALKING"
  180. };
  181. numWords = 32;
  182. wordsToChoose = 11;
  183. setWordArr(words);
  184. }void setAverage() {
  185. char * words[] = {
  186. "CONQUORER",
  187. "CONSISTED",
  188. "WONDERFUL",
  189. "COMMITTEE",
  190. "SURRENDER",
  191. "SUBJECTED",
  192. "CONVICTED",
  193. "FORBIDDEN",
  194. "FORTIFIED",
  195. "COLLECTED",
  196. "CONTINUED",
  197. "PERIMETER",
  198. "SOUTHEAST",
  199. "RELEASING",
  200. "SOMETHING",
  201. "ACCEPTING",
  202. "MUTATIONS",
  203. "GATHERING",
  204. "LITERALLY",
  205. "REPAIRING",
  206. "INCESSANT",
  207. "INTERIORS",
  208. "REGARDING",
  209. "TELEPHONE",
  210. "OBTAINING",
  211. "EXTENSIVE",
  212. "DEFEATING",
  213. "REQUIRING",
  214. "UNLOCKING",
  215. "RECYCLING",
  216. "INSTINCTS",
  217. "BARTERING",
  218. "LEUTENANT",
  219. "COMMUNITY",
  220. "BATTERIES",
  221. "RECIEVING",
  222. "INCLUDING",
  223. "INITIALLY",
  224. "INVOLVING",
  225. "MOUNTAINS"
  226. };
  227. numWords = 40;
  228. wordsToChoose = 14;
  229. setWordArr(words);
  230. }void setHard() {
  231. char * words[] = {
  232. "DISCOVERING",
  233. "ELIMINATING",
  234. "UNIMPORTANT",
  235. "MISTRUSTING",
  236. "MANUFACTURE",
  237. "RADIOACTIVE",
  238. "EXCLUSIVELY",
  239. "BOMBARDMENT",
  240. "DECEPTIVELY",
  241. "INDEPENDENT",
  242. "UNBELIEVERS",
  243. "EFFECTIVELY",
  244. "IMMEDIATELY",
  245. "INFESTATION",
  246. "DESCRIPTION",
  247. "INFORMATION",
  248. "REMEMBERING",
  249. "NIGHTVISION",
  250. "DESTRUCTION",
  251. "OVERLOOKING"
  252. };
  253. numWords = 20;
  254. wordsToChoose = 7;
  255. setWordArr(words);
  256. }void setVeryHard() {
  257. char * words[] = {
  258. "INFILTRATION",
  259. "ORGANIZATION",
  260. "AUTHENTICITY",
  261. "APPRECIATION",
  262. "SPOKESPERSON",
  263. "LABORATORIES",
  264. "INITIATEHOOD",
  265. "SUBTERRANEAN",
  266. "PURIFICATION",
  267. "TRANSMISSION",
  268. "CIVILIZATION",
  269. "CONSTRUCTION",
  270. "RESURRECTION",
  271. "REPRIMANDING",
  272. "ACCOMPANYING",
  273. "OVERWHELMING",
  274. "CONVERSATION",
  275. "NORTHERNMOST",
  276. "TRANSCRIBING",
  277. "ANNOUNCEMENT",
  278. "SECLUTIONIST"
  279. };
  280. numWords = 21;
  281. wordsToChoose = 13;
  282. setWordArr(words);
  283. }
  284. char ** getWordArr(){
  285. return wordArr;
  286. }
  287. int getNumWords() {
  288. return numWords;
  289. }
  290. int getWordsToChoose() {
  291. return wordsToChoose;
  292. }
  293. int getWordLength() {
  294. return strlen(*wordArr);
  295. }
  296. char * getVictoryProg() {
  297. if(victoryProg == NULL)
  298. return "";
  299. return victoryProg;
  300. }
  301. char * getCompleteProg() {
  302. if(completeProg == NULL)
  303. return "";
  304. return completeProg;
  305. }
  306. void freeAll() {
  307. for(int i=0; i<numWords; i++)
  308. free(*(wordArr+i));
  309. free(wordArr);
  310. free(victoryProg);
  311. free(completeProg);
  312. }
  313. // end