summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ouija.c21
-rw-r--r--session.c30
-rw-r--r--session.h2
-rw-r--r--utils.c47
-rw-r--r--utils.h10
5 files changed, 59 insertions, 51 deletions
diff --git a/ouija.c b/ouija.c
index c55737f..5b90cdf 100644
--- a/ouija.c
+++ b/ouija.c
@@ -1,9 +1,10 @@
-#include <stdio.h>
-#include <stdlib.h>
#include "session.h"
#include "utils.h"
+#include <stdio.h>
+#include <stdlib.h>
-int main(int argc, char **argv)
+int
+main (int argc, char **argv)
{
FILE *dictionary = NULL;
@@ -11,26 +12,26 @@ int main(int argc, char **argv)
switch (argc)
{
case 1:
- if (checkIfFileExists("dictionary.dict") == 0)
+ if (checkIfFileExists ("dictionary.dict") == 0)
{
- dictionary = fopen("dictionary.dict", "r");
+ dictionary = fopen ("dictionary.dict", "r");
}
break;
case 2:
- if (checkIfFileExists(argv[1]) == 0)
+ if (checkIfFileExists (argv[1]) == 0)
{
- dictionary = fopen(argv[1], "r");
+ dictionary = fopen (argv[1], "r");
}
}
if (dictionary != NULL)
{
- startSession(dictionary);
- fclose(dictionary);
+ startSession (dictionary);
+ fclose (dictionary);
}
else
{
- printf("No Dictionary Found\n");
+ printf ("No Dictionary Found\n");
}
return 0;
diff --git a/session.c b/session.c
index e3b8702..2991469 100644
--- a/session.c
+++ b/session.c
@@ -1,32 +1,34 @@
+#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "utils.h"
-void startSession(FILE *dictionary)
+void
+startSession (FILE *dictionary)
{
- if ( dictionary != NULL )
+ if (dictionary != NULL)
{
- int totalWords = lineCounter(dictionary);
- char *word, *question = (char *) calloc(MAX_LINE, sizeof(char));;
+ int totalWords = lineCounter (dictionary);
+ char *word, *question = (char *)calloc (MAX_LINE, sizeof (char));
char reload = '\0';
- printf("WELCOME\n"
+ printf ("WELCOME\n"
"Type GOODBYE to exit\n\n");
- while( strcmp(question, "GOODBYE\n") != 0 )
+ while (strcmp (question, "GOODBYE\n") != 0)
{
- printf("Enter a question: ");
- fgets(question, MAX_LINE, stdin);
+ printf ("Enter a question: ");
+ fgets (question, MAX_LINE, stdin);
- if ( strcmp(question, "GOODBYE\n") != 0 )
+ if (strcmp (question, "GOODBYE\n") != 0)
{
- word = getLine(dictionary, getRandomNumber(1, totalWords, question));
- printf("-> %s", word);
- free(word);
+ word = getLine (dictionary,
+ getRandomNumber (1, totalWords, question));
+ printf ("-> %s", word);
+ free (word);
}
}
- free(question);
+ free (question);
}
}
diff --git a/session.h b/session.h
index 989fd65..283e429 100644
--- a/session.h
+++ b/session.h
@@ -1,4 +1,4 @@
#include <stdio.h>
/* Starts a oujia session using a inputted dictionary file */
-void startSession(FILE *dictionary);
+void startSession (FILE *dictionary);
diff --git a/utils.c b/utils.c
index 83bfdd2..057f6ef 100644
--- a/utils.c
+++ b/utils.c
@@ -1,62 +1,67 @@
+#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/time.h>
#include <time.h>
#include <unistd.h>
-#include <sys/time.h>
-#include "utils.h"
-int checkIfFileExists(char *path)
+int
+checkIfFileExists (char *path)
{
- return access(path, F_OK);
+ return access (path, F_OK);
}
-int numerize(char *string)
+int
+numerize (char *string)
{
int val = 0;
- for( int i = 0; i < strlen(string); i++ )
+ for (int i = 0; i < strlen (string); i++)
{
val += string[i];
}
return val;
}
-int lineCounter(FILE *dictionary)
+int
+lineCounter (FILE *dictionary)
{
int words = 0;
- if ( dictionary != NULL )
+ if (dictionary != NULL)
{
char line[MAX_LINE];
- for( int i = words; (!feof(dictionary)); i++ )
+ for (int i = words; (!feof (dictionary)); i++)
{
- fgets(line, MAX_LINE, dictionary);
+ fgets (line, MAX_LINE, dictionary);
words = i;
}
- rewind(dictionary);
+ rewind (dictionary);
words--;
}
return words;
}
-char* getLine(FILE *dictionary, int lineNumber)
+char *
+getLine (FILE *dictionary, int lineNumber)
{
- char *line = (char *) calloc(MAX_LINE, sizeof(char));
- if ( dictionary != NULL )
+ char *line = (char *)calloc (MAX_LINE, sizeof (char));
+ if (dictionary != NULL)
{
- for ( int i = 0; (!feof(dictionary) && i != lineNumber ); i++)
+ for (int i = 0; (!feof (dictionary) && i != lineNumber); i++)
{
- fgets(line, MAX_LINE, dictionary);
+ fgets (line, MAX_LINE, dictionary);
}
- rewind(dictionary);
+ rewind (dictionary);
}
return line;
}
-int getRandomNumber(int min, int max, char *question)
+int
+getRandomNumber (int min, int max, char *question)
{
struct timespec s;
- clock_gettime(CLOCK_REALTIME, &s);
- srand(s.tv_nsec * numerize(question));
+ clock_gettime (CLOCK_REALTIME, &s);
+ srand (s.tv_nsec * numerize (question));
- return ((rand() % (max - min + 1)) + min);
+ return ((rand () % (max - min + 1)) + min);
}
diff --git a/utils.h b/utils.h
index 0903c86..5ccabe3 100644
--- a/utils.h
+++ b/utils.h
@@ -3,17 +3,17 @@
#define MAX_LINE 256
/* Checks if a file exists */
-int checkIfFileExists(char *path);
+int checkIfFileExists (char *path);
/* Turns a string into a number by
* using the sum of its ascii values.
*/
-int numerize(char *string);
+int numerize (char *string);
/* Counts the lines from a dictionary file */
-int lineCounter(FILE *dictionary);
+int lineCounter (FILE *dictionary);
/* Returns a specified line from a dictionary file */
-char* getLine(FILE *dictionary, int line);
+char *getLine (FILE *dictionary, int line);
/* Returns a random number within the inclusive number range
* and based on the inputted question.
* Uses a fast seeded number generator.
*/
-int getRandomNumber(int min, int max, char *question);
+int getRandomNumber (int min, int max, char *question);