view Paper/codes/wc/WcImpl.cbc @ 17:0d703e0d9781

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 05 May 2021 15:31:39 +0900
parents 92975c2d26b3
children
line wrap: on
line source

#include "../../../context.h"
#include <stdio.h>
#impl "Wc.h" as "WcImpl.h"
#interface "WcResult.h"

Wc* createWcImpl(struct Context* context) {
    Wc *wc = new Wc();
    wc->wc = (union Data*)new WcImpl();
    wc->bytes = 0;
    wc->words = 0;
    wc->lines = 0;
    return wc;
}

__code take(Impl* wc, Block *block,__code next(Ack *ack, ...),__code finish(StdData *result,...) {
    if (isEof(block->eof )) {
        result.buffer = new Buffer(1);
        result.buffer->data = new Byte(BUSIZE);
        result.size = 1;
        result.buffer->size = 
            snprintf(result.buffer[0]->data, "%d %d %d\n",wc->bytes,wc->words,wc->lines);
        goto finish(resut);
    }
    for(size_t i = 0 ; i<block->size; i++) {
        if (block->data[i] == '\n') wc->lines++;
        if (block->data[i] == ' ') {
            wc->words++;
            while(block->data[i] == ' ') {
               if(i>=block->size) 
                    goto next(ack,take);
               i++;
               wc->bytes++;
            }
        }
        wc->bytes++;
    }
    goto next(ack,take);
}