-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdadsay.c
More file actions
145 lines (129 loc) · 4.37 KB
/
dadsay.c
File metadata and controls
145 lines (129 loc) · 4.37 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Name: Thao Nguyen
Case Network ID: ttn60
The filename: dadsay.c
Date created: Dec 2, 2025
Description: Print ASCII picture of a smiley face with a given message
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include "constants.h"
#include "random_joke.h"
#define MARGIN_SMALL 2
#define MARGIN_LARGE 5
/* fill lines array with each line in a random joke,
calculate max line length,
and return the number of lines read
or -1 if error
*/
int dadsay_random_joke(char** lines, int* max_len, FILE* csp) {
*max_len = 0;
int line_count = 0;
struct dirent* dir_entry;
DIR* dir = opendir(JOKES_DIR);
if (dir == NULL) {
fputs("Error: Empty jokes database. Add some jokes!\n", csp);
return ERROR;
}
int random = random_joke_index();
if (random < 0) {
fputs("Error: Empty jokes database. Add some jokes!\n", csp);
return ERROR;
}
int index = 1;
// Find the file at that index
while ((dir_entry = readdir(dir)) != NULL) {
// Skip current and parent dir
if (strcmp(dir_entry->d_name,".") == 0 ||
strcmp(dir_entry->d_name,"..") == 0
) {
continue;
}
if (index == random) {
// when found, open the file
char* filename = dir_entry->d_name;
char pathname[strlen(JOKES_DIR)+strlen(filename)+1];
strcpy(pathname, JOKES_DIR);
strcat(pathname, filename);
FILE* joke = fopen(pathname, "r");
if (joke == NULL) {
fputs("Unable to open file\n", csp);
closedir(dir);
return ERROR;
}
// Get each line and add to the lines array
char buffer[JOKE_LINE_LENGTH];
while (fgets(buffer, JOKE_LINE_LENGTH, joke) != NULL && line_count < MAX_LINES) {
// Remove newline character if present
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n') {
buffer[len-1] = '\0';
len--;
}
// Allocate memory and copy the line to array of lines
lines[line_count] = malloc(len + 1);
if (lines[line_count] == NULL) {
fputs("Memory allocation failed\n", csp);
fclose(joke);
closedir(dir);
return ERROR;
}
strcpy(lines[line_count], buffer);
// Compute max line length
if ((int)len > *max_len) *max_len = (int)len;
line_count++;
}
fclose(joke);
break;
}
index++;
}
closedir(dir);
return line_count;
}
/*
have winking smiley face say a random joke
*/
int dadsay(FILE* csp) {
char* lines[MAX_LINES];
int max_len = 0;
int line_count = dadsay_random_joke(lines, &max_len, csp);
if (line_count == -1) {
return ERROR;
}
// top bar
fputs(" ", csp);
for (int i = 0; i < max_len + MARGIN_SMALL; i++) fputs("_", csp);
fputs("\n", csp);
// lines with side borders
for (int i = 0; i < line_count; i++) {
char border = (line_count == 1) ? '<' : (i == 0) ? '/' :
(i == line_count - 1) ? '\\' :
'|';
char line[max_len];
snprintf(line, max_len + MARGIN_LARGE, "%c %-*s %c\n", border, max_len, lines[i], border);
fputs(line, csp);
fputs("\n", csp);
}
// bottom bar
fputs(" ", csp);
for (int i = 0; i < max_len + 2; i++) fputs("-", csp);
fputs("\n", csp);
// line to speech bubble
fputs(" \\\n \\\n", csp);
// dad
fputs(" .-\"\"\"\"\"\"-. \n", csp);
fputs(" .' '. \n", csp);
fputs(" / O -=- \\ \n", csp);
fputs(" : : \n", csp);
fputs(" | | \n", csp);
fputs(" : ', ,' : \n", csp);
fputs(" \\ '-......-' / \n", csp);
fputs(" '. .' \n", csp);
fputs(" '-......-' \n", csp);
for (int i = 0; i < line_count; i++) {
free(lines[i]);
}
return 0;
}