-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest6.c
More file actions
executable file
·79 lines (59 loc) · 1.62 KB
/
test6.c
File metadata and controls
executable file
·79 lines (59 loc) · 1.62 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
#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "libmapreduce.h"
#define CHARS_PER_INPUT 30000
#define INPUTS_NEEDED 10
static const char *long_key = "long_key";
/* Takes input string and maps the longest
* word to the key, long_key.
*/
void map(int fd, const char *data)
{
}
/* Takes two words and reduces to the longer of the two
*/
const char *reduce(const char *value1, const char *value2)
{
return NULL;
}
int main()
{
FILE *file = fopen("alice.txt", "r");
char s[1024];
int i;
char **values = malloc(INPUTS_NEEDED * sizeof(char *));
int values_cur = 0;
values[0] = malloc(CHARS_PER_INPUT + 1);
values[0][0] = '\0';
while (fgets(s, 1024, file) != NULL)
{
if (strlen(values[values_cur]) + strlen(s) < CHARS_PER_INPUT)
strcat(values[values_cur], s);
else
{
values_cur++;
values[values_cur] = malloc(CHARS_PER_INPUT + 1);
values[values_cur][0] = '\0';
strcat(values[values_cur], s);
}
}
values_cur++;
values[values_cur] = NULL;
fclose(file);
mapreduce_t mr;
mapreduce_init(&mr, map, reduce);
mapreduce_map_all(&mr, (const char **)values);
mapreduce_reduce_all(&mr);
const char *result_longest = mapreduce_get_value(&mr, long_key);
if (result_longest == NULL) { printf("MapReduce returned (null). The longest word was not found.\n"); }
else { printf("Longest Word: %s\n", result_longest); free((void *)result_longest); }
mapreduce_destroy(&mr);
for (i = 0; i < values_cur; i++)
free(values[i]);
free(values);
return 0;
}