[BACK]Return to ws_fileio.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/ws_fileio.c, Revision 1.1.1.1

1.1       noro        1: /* $OpenXM: OpenXM/src/asir99/io/ws_fileio.c,v 1.2 1999/11/19 10:41:08 noro Exp $ */
                      2: #if defined(VISUAL) || MPI
                      3: #include<stdio.h>
                      4: #include"wsio.h"
                      5:
                      6: STREAM* WSIO_open(int, char *);
                      7: int WSIO_read (char *, int, STREAM *);
                      8: int WSIO_write (char *, int, STREAM *);
                      9: int WSIO_flushbuf(STREAM *);
                     10: int WSIO_fillbuf(STREAM *);
                     11:
                     12: int cread (data, element_size, count, s)
                     13: char* data;
                     14: unsigned element_size;
                     15: int count;
                     16: STREAM* s;
                     17: {
                     18:        int length,total,r;
                     19:        char *p;
                     20:
                     21:        if (!s || !s->read_flag || !data
                     22:                || !element_size || !count)
                     23:                return 0;
                     24:
                     25:        length = element_size * count;
                     26:
                     27:        total = length;
                     28:        p = data;
                     29:        while ( length > (r = s->buf_size - s->p) ) {
                     30:                memcpy((void *)p,(void *)(s->buf+s->p),r);
                     31:                s->p += r;
                     32:                p += r;
                     33:                length -= r;
                     34:                if ( !WSIO_fillbuf(s) )
                     35:                        return (total-length)/ element_size;
                     36:        }
                     37:        memcpy((void *)p,(void *)(s->buf+s->p),length);
                     38:        s->p += length;
                     39:        return count;
                     40: }
                     41:
                     42: int cwrite (data, element_size, count, s)
                     43: char* data;
                     44: unsigned element_size;
                     45: int count;
                     46: STREAM* s;
                     47: {
                     48:        int length,total,r;
                     49:        char *p;
                     50:
                     51:        if (!s || s->read_flag || !data
                     52:                || !element_size || !count)
                     53:                return 0;
                     54:
                     55:        length = element_size * count;
                     56:
                     57:        total = length;
                     58:        p = data;
                     59:        while ( length > (r = s->buf_size - s->p) ) {
                     60:                memcpy((void *)(s->buf+s->p),(void *)p,r);
                     61:                s->p += r;
                     62:                p += r;
                     63:                length -= r;
                     64:                if ( !WSIO_flushbuf(s) )
                     65:                        return (total-length)/ element_size;
                     66:        }
                     67:        memcpy((void *)(s->buf+s->p),(void *)p,length);
                     68:        s->p += length;
                     69:        return count;
                     70: }
                     71:
                     72: int cflush(s)
                     73: STREAM* s;
                     74: {
                     75:        if (!s || s->read_flag)
                     76:                return EOF;
                     77:
                     78:        if (!WSIO_flushbuf(s))
                     79:                return EOF;
                     80:        else
                     81:                return 0;
                     82: }
                     83:
                     84: int WSIO_fillbuf(s)
                     85: STREAM* s;
                     86: {
                     87:        s->buf_size = WSIO_read(s->buf, s->max_buf_size, s);
                     88:        s->p = 0;
                     89:
                     90:        return s->buf_size;
                     91: }
                     92:
                     93: int WSIO_flushbuf(s)
                     94: STREAM* s;
                     95: {
                     96:        int rst;
                     97:
                     98:
                     99:        rst = WSIO_write(s->buf, s->p, s);
                    100:        s->p = 0;
                    101:
                    102:        return rst;
                    103: }
                    104:
                    105: STREAM* WSIO_open(fildes, mode)
                    106: int fildes;
                    107: char* mode;
                    108: {
                    109:        STREAM* rst = 0;
                    110:        int flag = 0;
                    111:
                    112:        rst = (STREAM*)malloc(sizeof(STREAM));
                    113:        if (rst) {
                    114: #if defined(VISUAL)
                    115:                _fileno(&rst->fp) = -1;
                    116: #elif MPI
                    117: #if defined(sparc)
                    118:                (&rst->fp)->_file = -1;
                    119: #else
                    120:                fileno(&rst->fp) = -1;
                    121: #endif
                    122: #endif
                    123:                rst->fildes = fildes;
                    124:
                    125:                if (mode[0] == 'r') {
                    126:                        rst->read_flag = 1;
                    127:                        rst->p = WSIO_STRING_LENGTH;
                    128:                } else {
                    129:                        rst->read_flag = 0;
                    130:                        rst->p = 0;
                    131:                }
                    132:                rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH;
                    133:                rst->buf = (char*)malloc(WSIO_STRING_LENGTH);
                    134:                if (!rst->buf) {
                    135:                        free (rst);
                    136:                        rst = 0;
                    137:                }
                    138:                rst->eof = 0;
                    139:                rst->error = 0;
                    140:        }
                    141:        return rst;
                    142: }
                    143:
                    144: int WSIO_read (data, count, s)
                    145: char* data;
                    146: int count;
                    147: STREAM* s;
                    148: {
                    149:        int size;
                    150:
                    151:        if (!s)
                    152:                return 0;
                    153:
                    154: #if defined(VISUAL)
                    155:        size = recv(s->fildes,data,count,0);
                    156: #elif MPI
                    157:        {
                    158:                MPI_Status status;
                    159:
                    160:                MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status);
                    161:                if ( size > count )
                    162:                        size = count;
                    163:                MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status);
                    164:        }
                    165: #endif
                    166:        return size;
                    167: }
                    168:
                    169: int WSIO_write (data, count, s)
                    170: char* data;
                    171: int count;
                    172: STREAM* s;
                    173: {
                    174:        int size;
                    175:
                    176:        if (!s || !count)
                    177:                return 0;
                    178:
                    179: #if defined(VISUAL)
                    180:        size = send(s->fildes,data,count,0);
                    181:        return size;
                    182: #elif MPI
                    183:        MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD);
                    184:        MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD);
                    185:        return count;
                    186: #endif
                    187: }
                    188:
                    189: #if MPI
                    190: int mpi_nprocs,mpi_myid;
                    191:
                    192: void mpi_init()
                    193: {
                    194:        int argc;
                    195:        char *argv[1];
                    196:
                    197:        argc = 0; argv[0] = 0;
                    198:        MPI_Init(&argc,&argv);
                    199:        MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs);
                    200:        MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid);
                    201: }
                    202:
                    203: void mpi_finalize()
                    204: {
                    205:        MPI_Finalize();
                    206: }
                    207: #endif
                    208: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>