Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cow that recite quotes #72

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/sl
/.vscode
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ CFLAGS=-O -Wall

all: sl

sl: sl.c sl.h
$(CC) $(CFLAGS) -o sl sl.c -lncurses
sl: sl.c sl.h quotes.c quotes.h
$(CC) $(CFLAGS) -o sl sl.c quotes.c quotes.h -lncurses

clean:
rm -f sl
Expand Down
55 changes: 55 additions & 0 deletions quotes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define FILENAME "quotes/quotes.txt"
#define QUOTEBUFFER 1024
#define NUMQUOTES 5422

char *get_quotes()
{
char *buffer;
long length;
FILE *f = fopen(FILENAME, "rb");

if (f)
{
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(length);
if (buffer)
{
size_t d = fread(buffer, 1, length, f);
if (d < 0)
exit(EXIT_FAILURE);
return buffer;
}
perror("Unable to allocate buffer");
exit(EXIT_FAILURE);
}
perror("unable to open the file");
exit(EXIT_FAILURE);
}

char *select_random_quote(char *buffer)
{
srand(time(NULL));
// there are 5421 quotes until now in the file.
int selected_num_quote = (rand() % NUMQUOTES) + 1;
char *token_quotes = strtok(buffer, "\n");
int num_quote = 1;

while (token_quotes != NULL)
{
token_quotes = strtok(NULL, "\n");
if (num_quote == selected_num_quote)
{
return token_quotes;
}

num_quote++;
}
return ("MUUUUUUUUUUUU");
}
12 changes: 12 additions & 0 deletions quotes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define FILENAME "quotes/quotes.txt"
#define QUOTEBUFFER 1024
#define NUMQUOTES 5422

char *get_quotes();
char *select_random_quote(char *buffer);

Loading