wordParse.c 7.6 KB

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