This source file includes following definitions.
- main
1
2
3
4 #include <time.h>
5 #include <stdio.h>
6
7 #include "orte/constants.h"
8
9 #include "opal/class/opal_list.h"
10 #include "opal/util/argv.h"
11
12 #include "orte/runtime/runtime.h"
13
14 typedef struct {
15 opal_list_item_t super;
16 char *word;
17 int count;
18 } word_count_t;
19 OBJ_CLASS_INSTANCE(word_count_t,
20 opal_list_item_t,
21 NULL, NULL);
22
23 int main(int argc, char* argv[])
24 {
25 char text[100];
26 opal_list_t words;
27 word_count_t *cnt;
28 char **incnt;
29 bool found;
30 opal_list_item_t *item;
31
32 if (ORTE_SUCCESS != orte_init(&argc, &argv, ORTE_PROC_NON_MPI)) {
33 fprintf(stderr, "Failed orte_init\n");
34 exit(1);
35 }
36
37 OBJ_CONSTRUCT(&words, opal_list_t);
38 while (fgets(text, sizeof(text), stdin)) {
39
40 if ('\n' == text[strlen(text)-1]) {
41 text[strlen(text)-1] = '\0';
42 }
43 incnt = opal_argv_split(text, '\t');
44 found = false;
45 if (opal_argv_count(incnt) < 2) {
46 opal_output(0, "INCORRECT WORD SPLIT: %s", text);
47 opal_argv_free(incnt);
48 continue;
49 }
50 for (item = opal_list_get_first(&words);
51 item != opal_list_get_end(&words);
52 item = opal_list_get_next(item)) {
53 cnt = (word_count_t*)item;
54 if (0 == strcmp(cnt->word, incnt[0])) {
55 cnt->count += atoi(incnt[1]);
56 found = true;
57 break;
58 }
59 }
60 if (!found) {
61 cnt = OBJ_NEW(word_count_t);
62 cnt->word = strdup(incnt[0]);
63 cnt->count = atoi(incnt[1]);
64 opal_list_append(&words, &cnt->super);
65 }
66 opal_argv_free(incnt);
67 }
68
69 fprintf(stdout, "FINAL COUNT:\n");
70 while (NULL != (item = opal_list_remove_first(&words))) {
71 cnt = (word_count_t*)item;
72 fprintf(stdout, "%s: %d\n", cnt->word, cnt->count);
73 OBJ_RELEASE(item);
74 }
75 OBJ_DESTRUCT(&words);
76
77 if (ORTE_SUCCESS != orte_finalize()) {
78 fprintf(stderr, "Failed orte_finalize\n");
79 exit(1);
80 }
81 return 0;
82 }