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