view src/parallel_execution/examples/wc/WcImpl.cbc @ 969:5573463fe469

add buffer with Array
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Tue, 13 Apr 2021 17:50:24 +0900
parents d082c861d5c2
children 89ea952f0f2b
line wrap: on
line source

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

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\n");
  }
  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 buf_size = 10000;
  int LINE_SIZE = 1024;
  char line[LINE_SIZE];
  /*
  if( setvbuf( wc->file, buffer, BUFFERING_MODE, sizeof(buffer)) != 0 ){
    fputs("stdinのバッファリングを変更できませんでした。", stderr);
    exit(EXIT_FAILURE);
  } else {
  printf("setvbuffer\n");  
  }
  */
  char buffer[buf_size];
  int m = fread(buffer, sizeof( char ), buf_size ,wc->file);  
  int i = 0;
  printf("文字数=%d\n", m);
  printf("%s\n" ,buffer);

  //goto countUp(wc, next);
}