changeset 5:7599728af8ba default tip

add wc
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Sat, 19 Jun 2021 07:24:43 +0900
parents d6911fe00127
children
files wc_sigos/Ack.h wc_sigos/Block.h wc_sigos/DMGImpl.h wc_sigos/File.cbc wc_sigos/File.h wc_sigos/FileChrs.h wc_sigos/RemoteDMGImpl.h wc_sigos/StdData.h wc_sigos/TopologyManager.h wc_sigos/UnixChrisFileImpl.cpp wc_sigos/UnixFileImpl.cpp wc_sigos/UnixFileImpl.h wc_sigos/Wc.h wc_sigos/WcChrsImpl.cbc wc_sigos/WcImpl.cbc wc_sigos/WcImpl.h wc_sigos/main.cbc
diffstat 17 files changed, 286 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/Ack.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,3 @@
+typedef struct Ack <> {
+  int ok;
+} Ack;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/Block.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,5 @@
+typedef struct Block <> {
+  int eof;   /* EOF etc */
+  size_t size;
+  unsigned char *buf;
+} Block;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/DMGImpl.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,7 @@
+typedef struct DataGearMangerImpl <Record> ipml File {
+  struct Queue<Record>* lists;
+  int mode; 
+  Data *fileImpl ;
+  __code dmgOpen(DataGearMangerImpl* file,Key *key, __code next(Record *record,...));
+  __code dmgAck(DataGearMangerImpl* file,Ack *ack, __code next(Record *record,...));
+} File;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/File.cbc	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,7 @@
+typedef struct File <> {
+  Path *path;
+  int mode; /* rw .. */
+  Data *fileImpl ;
+  __code open(FileImpl* file,Key *key, __code next(Block *block,...));
+  __code ack(FileImpl* file,Ack *ack, __code next(Block *block,...));
+} File;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/File.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,10 @@
+typedef struct File <Record> {
+  int mode; /* rw .. */
+  Key *key;
+  Ack *ack;
+  Record *record;
+  Data *fileImpl ;
+
+  __code open(FileImpl* file,Key *key, int mode, __code next(Record *record,...));
+  __code ack(FileImpl* file,Ack *ack, __code next(Record *record,...));
+} File;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/FileChrs.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,11 @@
+typedef struct FileChrs <Record> {
+  int mode; /* rw .. */
+  Key *key;
+  Ack *ack;
+  Record *record;
+  Output *reader;
+  Data *fileImpl ;
+  __code setOutput(Output *output,__code next(...)) ;
+  __code Open(Key *key, Output reader, __code next(...)) ;
+  __code Ack(Ack *ack, Output reader, __code next(...)) ;
+} FileChrs;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/RemoteDMGImpl.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,7 @@
+typedef struct RemoteDataGearMangerImpl <Record> ipml File {
+  struct Queue<Record>* lists;
+  int mode; 
+  Data *fileImpl ;
+  __code rdmgOpen(DataGearMangerImpl* file,Connection *conn, Key *key, __code next(Record *record,...));
+  __code rdmgAck(DataGearMangerImpl* file,Ack *ack, __code next(Record *record,...));
+} File;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/StdData.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,4 @@
+typedef struct StdData <> {
+  size_t size;
+  Buffer *buffer;
+} StdData;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/TopologyManager.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,13 @@
+// Topologymanger connect file impl and wc impl
+//    setput remote data Gear Manager
+
+run() {
+    foreach imple in (codeGearList) {
+       intf = create imple
+       imple->intf = im
+    }
+    foreach imple in (codeGearList) {
+       imple->intf->setOutput();
+    }
+    goto imple->intf->start();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/UnixChrisFileImpl.cpp	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#impl "UnixCharImpl.h" as "UnixCharImpl.h"
+
+typedef struct UnixFileImpl <> ipml File {
+  int fd;    // unix file descriptor
+  int mode; 
+  Data *fileImpl ;
+  __code setOutput(UnixFileImpl* file, Output *output,__code next(...)) ;
+  __code unixOpen(UnixFileImpl* file,Key *key, Output reader, __code next(...)) ;
+  __code uniAck(UnixFileImpl* file,Ack *ack, Output reader, __code next(...)) ;
+} File;
+
+File* createUnixChrisFileImpl(struct Context* context) {
+    File *file = new File();
+    file->ChrisFileImpl = (union Data*)new ChrisFileImpl();
+    return file;
+}
+
+// Topology manageres determins input / output connection
+// 
+// Topology manageres determins input / output connection
+//
+__code gearsConnect(Wc* wc, Input *input, Output *output,__code next(...)) {
+   file->reader = output ;
+   input->add(&file->ack,"ack");
+   goto next(...);
+}
+
+int
+putBlock(UnixFileImpl* file, Output *reader) {
+   Block *block = new Block();
+   int len = read(fd, BUFSIZE, block->data);
+   if (len <=0 ) {
+       block->eof |= BLOCK_FLAG_EOF;
+       close(file->fd);
+       return 0;
+   }
+   reader->put(block);
+   return 1 ;
+}
+
+__code unixOpen(UnixFileImpl* file,Key *key, Output reader, __code next(...)) {
+   file->fd = open(key->path,unix_mode(key->modde));
+   if (fd < 0) {
+       goto error("can't open");
+   }
+   if (putBlock(file,reader)) {
+      putBlock(file,reader);
+   }
+   goto next(...);
+}
+
+__code uniAck(UnixFileImpl* file,Ack *ack, Output reader, __code next(...)) {
+   if (!ack->isOk) {
+      close(file->fd);
+      goto next(...);
+   }
+   putBlock(file,reader);
+   goto next(...);   // file is automaticaly put into local dataGearManger/input
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/UnixFileImpl.cpp	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#impl UnixFileImp as "UnixFileImpl.h"
+
+
+File* createUnixFileImpl(struct Context* context) {
+    File *file = new File();
+    file->FileImpl = (union Data*)new FileImpl();
+    return file;
+}
+
+readBlock(UnixFileImpl* file) {
+   Block *block = new Block();
+   int len = read(fd, BUFSIZE, block->data);
+   block->eof &= ~BLOCK_FLAG_EOF;
+   if (len <=0 ) {
+       block->eof |= BLOCK_FLAG_EOF;
+       close(file->fd);
+   } 
+   return block ;
+}
+
+__code unixOpen(UnixFileImpl* file,Key *key, __code next(Block *block,...));
+   file->fd = open(key->path,unix_mode(key->modde));
+   if (fd < 0) {
+       goto error("can't open");
+   }
+   goto next(readBlock(file), ...);
+}
+
+__code uniAck(UnixFileImpl* file,Ack *ack, __code next(Block *block,...));
+   if (!ack->isOk) {
+      close(file->fd);
+      goto next(...);
+   }
+   goto next(readBlock(file), ...);   // file is automaticaly put into local dataGearManger/input
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/UnixFileImpl.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,7 @@
+typedef struct UnixFileImpl <> ipml File {
+  int fd;    // unix file descriptor
+  int mode; 
+  Data *fileImpl ;
+  __code unixOpen(UnixFileImpl* file,Key *key, __code next(Block *block,...));
+  __code uniAck(UnixFileImpl* file,Ack *ack, __code next(Block *block,...));
+} File;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/Wc.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,9 @@
+typedef struct Wc <> {
+  // all arguments names in this interfaces here
+  union Data* wc;       // an implementation
+  Block * block;    
+  StdData * reuslt;    
+  Ack * ack;    
+  // codeGear entries of this interface
+  __code take(Impl* wc, Block *block,__code next(Ack *ack, ...),__code finish(StdData *result,...);
+} Wc;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/WcChrsImpl.cbc	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#impl "Wc.h" as "WcChrsImpl.h"
+
+Wc* createChrsWcImpl(struct Context* context) {
+    Wc *wc = new Wc();
+    wc->wc = (union Data*)new WcImpl();
+    wc->bytes = 0;
+    wc->words = 0;
+    wc->lines = 0;
+    return wc;
+}
+
+// Topology manageres determins input / output connection
+// 
+__code gearsConnect(Wc* wc, Input *input,Output *output, Output *stdout,__code next(...)) {
+   input->add(&wc->block,"block");
+   output->add(&wc->ack,"ack");
+   stdout->add(&wc->result,"result");
+   goto next(...);
+}
+
+__code take(Impl* wc, Block *block /* input */, __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);
+        got 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,...);
+               i++;
+               wc->bytes++;
+            }
+        }
+        wc->bytes++;
+    }
+    goto next(ack,...);     // output ack, activate take
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/WcImpl.cbc	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,39 @@
+#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);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/WcImpl.h	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,5 @@
+typedef struct WcImpl <> impl Wc {
+  size_t bytes
+  size_t words
+  size_t lines
+} WcImpl;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wc_sigos/main.cbc	Sat Jun 19 07:24:43 2021 +0900
@@ -0,0 +1,15 @@
+#data "wcTarget.h"
+#interface "Wc.h"
+
+//
+// start file  and wc 
+//   connect continuation each other
+//
+
+__code startCode(struct GearsData* gearsData) {   
+  File* file = createUnixFileImpl(context, /* file name */ gearsData->args[1] );
+  Wc* wc = createWcImpl(context);
+  goto file->openFile(asReader(wc)); // readerInterface
+}
+
+