time to turn to ansi C

#include "stdio.h"

//typedef  struct {void *_, __;} interface;
typedef  void * interface[2];


#define DATA(interface) *(void **)((char *)(interface) + sizeof (void *))

#define type typedef



struct Reader{
    int (*read)(void *, char *p, int n);
};


struct Writer{
    int (*write)(void *, const void* s, int n);
    int (*flush)(void *);
};



type  struct B{
    int k;
}B;

int readB(B *t, char *p, int d){
    return t->k + d;
}

struct Reader Reader_B = {
    readB
};


type struct C{
    int k;
}C;



int writeC(C* t, const void* s, int n){
    return t->k - n;
}

int flushC(C *t){
    return t->k;
}

struct Writer Writer_C = {
    writeC,
    flushC
};






int  fr(struct Reader** a){
   int d = (*a)->read(DATA(a), 0, 8);
   return  d;
}


int  fw(struct Writer** a){
   int d = (*a)->write(DATA(a), 0, 8);
   int r = (*a)->flush(DATA(a));
   return  d;
}




struct Closer{
    int (*close)(void *);
};

type struct D{
    int a;
    struct B B;
    struct C C;
}D;

int closeD(D *t){
    return  t->a;
}

struct Closer Closer_D={
    closeD
};


int fc(struct Closer** closer){
    int d = (*closer)->close(DATA(closer));
    return d;
}



int main(int argc, char *argv[]){
    B b = {3};
    interface rd = { &Reader_B,  &b };
    int e= fr(rd);
     C c = {9};
    interface writer = {&Writer_C, &c};
    int k = fw(writer);

    D d = {
       5, {7},{8}
    };
    {
        interface rd = {&Reader_B, &d.B},
                writer = {&Writer_C, &d.C},
                closer = {&Closer_D, &d};
        e= fr(rd);
        k = fw(writer);
        e = fc(closer);
    }

    e = writeC(&d.C, 0, 18);
    printf("%d", e);
    return 0;
}

posted @ 2020-06-24 15:55  zJanly  阅读(92)  评论(0编辑  收藏  举报