annotate Paper/codes/wc/UnixChrisFileImpl.cpp @ 19:2d91b4d2569c

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 05 May 2021 18:43:47 +0900
parents
children 218abc95edeb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #impl "Wc.h" as "WcChrsImpl.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 typedef struct UnixFileImpl <> ipml File {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 int fd; // unix file descriptor
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 int mode;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Data *fileImpl ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 __code setOutput(UnixFileImpl* file, Output *output,__code next(...)) ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 __code unixOpen(UnixFileImpl* file,Key *key, Output reader, __code next(...)) ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 __code uniAck(UnixFileImpl* file,Ack *ack, Output reader, __code next(...)) ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 } File;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 File* createUnixChrisFileImpl(struct Context* context) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 File *file = new File();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 file->ChrisFileImpl = (union Data*)new ChrisFileImpl();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 return file;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 // Topology manageres determins input / output connection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 __code setOutput(Wc* wc, Output *output,__code next(...)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 file->reader = output ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 goto next(...);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 int
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 putBlock(UnixFileImpl* file, Output *reader) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 Block *block = new Block();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 int len = read(fd, BUFSIZE, block->data);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 if (len <=0 ) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 block->eof |= BLOCK_FLAG_EOF;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 return 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 reader->put(block);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 return 1 ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 __code unixOpen(UnixFileImpl* file,Key *key, Output reader, __code next(...)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 file->fd = open(key->path,unix_mode(key->modde));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 if (fd < 0) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 goto error("can't open");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 if (putBlock(file,reader)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 putBlock(file,reader);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 goto next(file,...);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 __code uniAck(UnixFileImpl* file,Ack *ack, Output reader, __code next(...)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 if (!ack->isOk) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 close(fd);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 goto next(...);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 putBlock(file,reader);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 goto next(...); // file is automaticaly put into local dataGearManger/input
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 }