view src/parallel_execution/examples/wc/WcImpl.cbc @ 965:d00ec1274f23

add fgets
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Tue, 30 Mar 2021 17:54:46 +0900
parents 359913ba0175
children 9efaa40e56fa
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, char* filename) {
    struct Wc* wc  = new Wc();
    struct WcImpl* wc_impl = new WcImpl();
    wc->wc = (union Data*)wc_impl;
    wc_impl->filename = filename;
    wc->result = NULL;
    wc_impl->Keyword = NULL;
    wc_impl->wordNum = 0;
    wc->openFile = C_openFileWcImpl;
    wc->countUp = C_countUpWcImpl;
    return wc;
}
__code openFile(struct WcImpl* wc, __code next(...)) {
  FILE* file = fopen(wc->filename, "r");
  if (file == NULL){
    printf("ファイルが開ませんでした\n");
    exit(1);
  } else {
    printf("file open");
  }
  wc->file = (union Data*)file; 
  goto countUp(wc, next);
}

__code countUp(struct WcImpl* wc,__code next(WcResult* result, ...)) {
    /*
    Read data from file
    if eof
      setup result
      GOTO next(WcResult* result, ...);

    befor if eof
    Count new line
    Count word
    Count char
    TODO
    */
  int N = 256;
  char str[N];
  int line = 0;
  while(fgets(str, N, wc->file)!=NULL){
    printf("%s",str);
    line++;
  }
  printf("line = %d", line);
  goto countUp(wc, next);
}