KMR
wc.mapper.c
Go to the documentation of this file.
1 /** \file wc.mapper.c
2  \brief Example for KMR shell command pipeline. It is a mapper
3  for word count. */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9 
10 /** Maximum line length. */
11 #define LINELEN 32767
12 
13 /** \brief Ignore characters.
14  These characters are replaced to a space character. */
15 
16 //static char ignoreChar[] = { ',', '.', '\t', '?', '"', '!', '$', '/', '<', '>', ':', ';', '[', ']', '(', ')', '-' };
17 static char ignoreChar[] = { '\t' };
18 
19 /** \breif Replace Character.
20  Replace ignore characters to a space character. */
21 
22 static inline void ReplaceChar(char *wp)
23 {
24  while (*wp != 0) {
25  /* uncomment if ignore case. */
26  // if (isalpha(*wp) && isupper(*wp))
27  // *wp = tolower(*wp);
28  for (int i = 0; i < (int)sizeof(ignoreChar); i++) {
29  if (*wp == ignoreChar[i]) {
30  *wp = ' ';
31  break;
32  }
33  }
34  wp++;
35  }
36 }
37 
38 /** \brief Main function.
39  Read line from stdin, replace character, separate word by space,
40  and output "WORD 1" to stdout. */
41 
42 int
43 main(int argc, char *argv[])
44 {
45  char line[LINELEN];
46 
47  while (fgets(line, sizeof(line), stdin) != NULL) {
48  char *wtop = NULL, *cp = line;
49  int len = (int)strlen(line);
50  ReplaceChar(line);
51  while ((cp - line) < len) {
52  while (*cp == ' ') {
53  cp++; // skip space
54  }
55  if (*cp == '\n' || *cp == '\0') {
56  break; // end of line or buffer end.
57  }
58  wtop = cp;
59  while (*cp != ' ' && *cp != '\n' && *cp != '\0') {
60  cp++;
61  }
62  *cp = '\0';
63  printf("%s 1\n", wtop);
64  cp++;
65  }
66  }
67  return 0;
68 }
int main(int argc, char *argv[])
Main function.
Definition: wc.mapper.c:43
static void ReplaceChar(char *wp)
Replace Character.
Definition: wc.mapper.c:22
#define LINELEN
Maximum line length.
Definition: wc.mapper.c:11
static char ignoreChar[]
Ignore characters.
Definition: wc.mapper.c:17