wordParse.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #define _GNU_SOURCE /* for getline() */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "wordParse.h"
  12. #include <ctype.h>
  13. #define MAX_WORD_SIZE 12
  14. char **wordArr;
  15. int numWords;
  16. int wordsToChoose;
  17. void readWordsFromFile(FILE* fp){
  18. if(fp == NULL) {
  19. setVeryEasy();
  20. return;
  21. }
  22. // Check if FalloutTerminal.cfg is invalid
  23. char * buf;
  24. size_t n = 0;
  25. numWords = 0;
  26. while(getline(&buf, &n, fp)){
  27. if(buf[0] == '#' || !strcmp(buf, "\n"))
  28. continue;
  29. // Remove the \n at the end of buff
  30. buf[strlen(buf)-1] = '\0';
  31. // Stop on :LAUNCH_ON_*
  32. if(buf[0] == ':' ) {
  33. if(!strncmp(buf, ":WORDS_TO_CHOOSE=",17)) {
  34. sscanf(buf+17, "%d", &wordsToChoose);
  35. continue;
  36. }
  37. else
  38. break;
  39. }
  40. // Check all chars in buf are A-Z or a-z
  41. int invalidChars = 0;
  42. for(int i=0; (unsigned long)i<strlen(buf); i++){
  43. if(buf[i] > 64 && buf[i] < 91)
  44. continue;
  45. else if(buf[i] > 96 && buf[i] < 123)
  46. buf[i] = toupper(buf[i]);
  47. else
  48. invalidChars = 1;
  49. }
  50. if(invalidChars)
  51. continue;
  52. // Check all words are the same size as the first word
  53. if(numWords != 0) {
  54. if(strlen(buf) != strlen(*wordArr))
  55. continue;
  56. // Check the word doesn't already exist in the list
  57. int dupes = 0;
  58. for(int i=0; i < numWords; i++)
  59. if(!strcmp(*(wordArr+i), buf))
  60. dupes = 1;
  61. if(dupes)
  62. continue;
  63. }
  64. // Read the word into wordArr
  65. wordArr = realloc(wordArr, sizeof(char*) * numWords+1);
  66. *(wordArr+numWords) = malloc(sizeof(char) * sizeof(buf));
  67. strncpy(*(wordArr+numWords),buf, MAX_WORD_SIZE);
  68. numWords++;
  69. }
  70. if(wordsToChoose == 0)
  71. wordsToChoose = 7;
  72. if(wordsToChoose > numWords){
  73. // Too few valid words recieved: default to very easy
  74. free(wordArr);
  75. setVeryEasy();
  76. }
  77. }
  78. void setWordArr(char *words[]){
  79. wordArr = malloc(numWords * sizeof(char*));
  80. for(int i=0; i<numWords; i++) {
  81. // TODO replace numWords with wordsize
  82. *(wordArr+i) = malloc(sizeof(char) * numWords);
  83. strcpy(*(wordArr+i), *(words+i));
  84. }
  85. }
  86. void setVeryEasy() {
  87. char * words[] = {
  88. "FRIED",
  89. "TREES",
  90. "RIGID",
  91. "HIRED",
  92. "TRIES",
  93. "WRITE",
  94. "TRIED",
  95. "GREED",
  96. "DRIED",
  97. "BRAIN",
  98. "SKIES",
  99. "LAWNS",
  100. "GHOST",
  101. "CAUSE",
  102. "PAINT",
  103. "SHINY",
  104. "MAKES",
  105. "GAINS",
  106. "THIEF",
  107. "BASES",
  108. "RAISE",
  109. "REFER",
  110. "CARES",
  111. "TAKEN",
  112. "WAKES",
  113. "WAVES",
  114. "WARNS",
  115. "SAVES"
  116. };
  117. numWords = 28;
  118. wordsToChoose = 10;
  119. setWordArr(words);
  120. }
  121. void setEasy() {
  122. char * words[] = {
  123. "STATING",
  124. "HEALING",
  125. "COSTING",
  126. "REASONS",
  127. "SEASIDE",
  128. "SPARING",
  129. "CAUSING",
  130. "CRAFTED",
  131. "PRISONS",
  132. "PRESENT",
  133. "DEALING",
  134. "SETTING",
  135. "LEAVING",
  136. "VERSION",
  137. "DEATHLY",
  138. "BLAZING",
  139. "GRANITE",
  140. "TESTING",
  141. "TRAITOR",
  142. "STAMINA",
  143. "TRINITY",
  144. "CALLING",
  145. "TALKING",
  146. "ACQUIRE",
  147. "WELCOME",
  148. "DECRIES",
  149. "FALLING",
  150. "PACKING",
  151. "ALLOWED",
  152. "SELLING",
  153. "AFFRONT",
  154. "WALKING"
  155. };
  156. numWords = 32;
  157. wordsToChoose = 11;
  158. setWordArr(words);
  159. }void setAverage() {
  160. char * words[] = {
  161. "CONQUORER",
  162. "CONSISTED",
  163. "WONDERFUL",
  164. "COMMITTEE",
  165. "SURRENDER",
  166. "SUBJECTED",
  167. "CONVICTED",
  168. "FORBIDDEN",
  169. "FORTIFIED",
  170. "COLLECTED",
  171. "CONTINUED",
  172. "PERIMETER",
  173. "SOUTHEAST",
  174. "RELEASING",
  175. "SOMETHING",
  176. "ACCEPTING",
  177. "MUTATIONS",
  178. "GATHERING",
  179. "LITERALLY",
  180. "REPAIRING",
  181. "INCESSANT",
  182. "INTERIORS",
  183. "REGARDING",
  184. "TELEPHONE",
  185. "OBTAINING",
  186. "EXTENSIVE",
  187. "DEFEATING",
  188. "REQUIRING",
  189. "UNLOCKING",
  190. "RECYCLING",
  191. "INSTINCTS",
  192. "BARTERING",
  193. "LEUTENANT",
  194. "COMMUNITY",
  195. "BATTERIES",
  196. "RECIEVING",
  197. "INCLUDING",
  198. "INITIALLY",
  199. "INVOLVING",
  200. "MOUNTAINS"
  201. };
  202. numWords = 40;
  203. wordsToChoose = 14;
  204. setWordArr(words);
  205. }void setHard() {
  206. char * words[] = {
  207. "DISCOVERING",
  208. "ELIMINATING",
  209. "UNIMPORTANT",
  210. "MISTRUSTING",
  211. "MANUFACTURE",
  212. "RADIOACTIVE",
  213. "EXCLUSIVELY",
  214. "BOMBARDMENT",
  215. "DECEPTIVELY",
  216. "INDEPENDENT",
  217. "UNBELIEVERS",
  218. "EFFECTIVELY",
  219. "IMMEDIATELY",
  220. "INFESTATION",
  221. "DESCRIPTION",
  222. "INFORMATION",
  223. "REMEMBERING",
  224. "NIGHTVISION",
  225. "DESTRUCTION",
  226. "OVERLOOKING"
  227. };
  228. numWords = 20;
  229. wordsToChoose = 7;
  230. setWordArr(words);
  231. }void setVeryHard() {
  232. char * words[] = {
  233. "INFILTRATION",
  234. "ORGANIZATION",
  235. "AUTHENTICITY",
  236. "APPRECIATION",
  237. "SPOKESPERSON",
  238. "LABORATORIES",
  239. "INITIATEHOOD",
  240. "SUBTERRANEAN",
  241. "PURIFICATION",
  242. "TRANSMISSION",
  243. "CIVILIZATION",
  244. "CONSTRUCTION",
  245. "RESURRECTION",
  246. "REPRIMANDING",
  247. "ACCOMPANYING",
  248. "OVERWHELMING",
  249. "CONVERSATION",
  250. "NORTHERNMOST",
  251. "TRANSCRIBING",
  252. "ANNOUNCEMENT",
  253. "SECLUTIONIST"
  254. };
  255. numWords = 21;
  256. wordsToChoose = 13;
  257. setWordArr(words);
  258. }
  259. char ** getWordArr(){
  260. return wordArr;
  261. }
  262. int getNumWords() {
  263. return numWords;
  264. }
  265. int getWordsToChoose() {
  266. return wordsToChoose;
  267. }
  268. int getWordLength() {
  269. return strlen(*wordArr);
  270. }
  271. void freeWordArr() {
  272. free(wordArr);
  273. }
  274. // end