5
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include "../../../context.h"
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdio.h>
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #impl "Wc.h" as "WcImpl.h"
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #interface "WcResult.h"
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 Wc* createWcImpl(struct Context* context) {
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 Wc *wc = new Wc();
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 wc->wc = (union Data*)new WcImpl();
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 wc->bytes = 0;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 wc->words = 0;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 wc->lines = 0;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 return wc;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 }
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 __code take(Impl* wc, Block *block,__code next(Ack *ack, ...),__code finish(StdData *result,...) {
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 if (isEof(block->eof )) {
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 result.buffer = new Buffer(1);
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 result.buffer->data = new Byte(BUSIZE);
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 result.size = 1;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 result.buffer->size =
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 snprintf(result.buffer[0]->data, "%d %d %d\n",wc->bytes,wc->words,wc->lines);
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 goto finish(resut);
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 }
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 for(size_t i = 0 ; i<block->size; i++) {
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 if (block->data[i] == '\n') wc->lines++;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 if (block->data[i] == ' ') {
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 wc->words++;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 while(block->data[i] == ' ') {
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 if(i>=block->size)
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 goto next(ack,take);
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 i++;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 wc->bytes++;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 }
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 wc->bytes++;
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 }
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 goto next(ack,take);
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 }
|
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39
|