summaryrefslogtreecommitdiff
path: root/utils.c
blob: e349368997ac789f6aabec2e2c336fad0c3aaf5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include "utils.h"

int checkIfFileExists(char *path)
{
	return access(path, F_OK);
}

int numerize(char *string)
{
	int val = 0;
	for( int i = 0; i < strlen(string); i++ )
	{
		val += string[i];
	}
	return val;
}

int lineCounter(FILE *dictionary)
{
	int words = 0;
	if ( dictionary != NULL )
	{
		char line[MAX_LINE];
		for( int i = words; (!feof(dictionary)); i++ )
		{
			fgets(line, MAX_LINE, dictionary);
			words = i;
		}
		rewind(dictionary);
		words--;
	}
	return words;
}

char* getLine(FILE *dictionary, int lineNumber)
{
	char *line = (char *) calloc(MAX_LINE, sizeof(char));
	if ( dictionary != NULL )
	{
		for ( int i = 0; (!feof(dictionary) && i != lineNumber ); i++)
		{
			fgets(line, MAX_LINE, dictionary);
		}
		rewind(dictionary);
	}
	return line;
}

int getRandomNumber(int min, int max, char *question)
{
	// Adding Randomize Time Values for Less Chances of Running into Seed Collisions
	struct timeval randtime;
	gettimeofday(&randtime,NULL);
	srand(((randtime.tv_sec * 100) + time(NULL) * numerize(question)) + ((randtime.tv_usec / 100) - time(NULL) * numerize(question)));

	return ((rand() % (max - min + 1)) + 1);
}