=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/io/ws_fileio.c,v retrieving revision 1.6 retrieving revision 1.13 diff -u -p -r1.6 -r1.13 --- OpenXM_contrib2/asir2000/io/ws_fileio.c 2003/03/07 06:39:57 1.6 +++ OpenXM_contrib2/asir2000/io/ws_fileio.c 2018/09/09 08:13:42 1.13 @@ -44,13 +44,14 @@ * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. - * $OpenXM: OpenXM_contrib2/asir2000/io/ws_fileio.c,v 1.5 2003/03/07 03:12:28 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/io/ws_fileio.c,v 1.12 2018/03/29 01:32:54 noro Exp $ */ -#if defined(VISUAL) || defined(MPI) +#if defined(VISUAL) || defined(__MINGW32__) || defined(MPI) #include #include"wsio.h" STREAM* WSIO_open(int, char *); +int WSIO_fileno(STREAM *); int WSIO_read (char *, int, STREAM *); int WSIO_write (char *, int, STREAM *); int WSIO_flushbuf(STREAM *); @@ -62,28 +63,28 @@ unsigned element_size; int count; STREAM* s; { - int length,total,r; - char *p; + int length,total,r; + char *p; - if (!s || !s->read_flag || !data - || !element_size || !count) - return 0; + if (!s || !s->read_flag || !data + || !element_size || !count) + return 0; - length = element_size * count; + length = element_size * count; - total = length; - p = data; - while ( length > (r = s->buf_size - s->p) ) { - memcpy((void *)p,(void *)(s->buf+s->p),r); - s->p += r; - p += r; - length -= r; - if ( !WSIO_fillbuf(s) ) - return (total-length)/ element_size; - } - memcpy((void *)p,(void *)(s->buf+s->p),length); - s->p += length; - return count; + total = length; + p = data; + while ( length > (r = s->buf_size - s->p) ) { + memcpy((void *)p,(void *)(s->buf+s->p),r); + s->p += r; + p += r; + length -= r; + if ( !WSIO_fillbuf(s) ) + return (total-length)/ element_size; + } + memcpy((void *)p,(void *)(s->buf+s->p),length); + s->p += length; + return count; } int cwrite (data, element_size, count, s) @@ -92,125 +93,139 @@ unsigned element_size; int count; STREAM* s; { - int length,total,r; - char *p; + int length,total,r; + char *p; - if (!s || s->read_flag || !data - || !element_size || !count) - return 0; + if (!s || s->read_flag || !data + || !element_size || !count) + return 0; - length = element_size * count; + length = element_size * count; - total = length; - p = data; - while ( length > (r = s->buf_size - s->p) ) { - memcpy((void *)(s->buf+s->p),(void *)p,r); - s->p += r; - p += r; - length -= r; - if ( !WSIO_flushbuf(s) ) - return (total-length)/ element_size; - } - memcpy((void *)(s->buf+s->p),(void *)p,length); - s->p += length; - return count; + total = length; + p = data; + while ( length > (r = s->buf_size - s->p) ) { + memcpy((void *)(s->buf+s->p),(void *)p,r); + s->p += r; + p += r; + length -= r; + if ( !WSIO_flushbuf(s) ) + return (total-length)/ element_size; + } + memcpy((void *)(s->buf+s->p),(void *)p,length); + s->p += length; + return count; } int cflush(s) STREAM* s; { - if (!s || s->read_flag) - return EOF; + if (!s || s->read_flag) + return EOF; - if (!WSIO_flushbuf(s)) - return EOF; - else - return 0; + if (!WSIO_flushbuf(s)) + return EOF; + else + return 0; } int WSIO_fillbuf(s) STREAM* s; { - s->buf_size = WSIO_read(s->buf, s->max_buf_size, s); - s->p = 0; + s->buf_size = WSIO_read(s->buf, s->max_buf_size, s); + s->p = 0; - return s->buf_size; + return s->buf_size; } int WSIO_flushbuf(s) STREAM* s; { - int rst; + int rst; - rst = WSIO_write(s->buf, s->p, s); - s->p = 0; + rst = WSIO_write(s->buf, s->p, s); + s->p = 0; - return rst; + return rst; } STREAM* WSIO_open(fildes, mode) int fildes; char* mode; { - STREAM* rst = 0; - int flag = 0; + STREAM* rst = 0; + int flag = 0; - rst = (STREAM*)malloc(sizeof(STREAM)); - if (rst) { + rst = (STREAM*)malloc(sizeof(STREAM)); + if (rst) { #if defined(VISUAL) - _fileno(&rst->fp) = -1; + rst->_p = NULL; +#elif defined(__MINGW32__) + (&rst->fp)->_file = -1; #elif defined(MPI) -#if defined(sparc) - (&rst->fp)->_file = -1; +#if defined(sparc) || defined(__FreeBSD__) + (&rst->fp)->_file = -1; +#elif defined(linux) + (&rst->fp)->_fileno = -1; #else - fileno(&rst->fp) = -1; + fileno(&rst->fp) = -1; #endif #endif - rst->fildes = fildes; + rst->fildes = fildes; - if (mode[0] == 'r') { - rst->read_flag = 1; - rst->p = WSIO_STRING_LENGTH; - } else { - rst->read_flag = 0; - rst->p = 0; - } - rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH; - rst->buf = (char*)malloc(WSIO_STRING_LENGTH); - if (!rst->buf) { - free (rst); - rst = 0; - } - rst->eof = 0; - rst->error = 0; - } - return rst; + if (mode[0] == 'r') { + rst->read_flag = 1; + rst->p = WSIO_STRING_LENGTH; + } else { + rst->read_flag = 0; + rst->p = 0; + } + rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH; + rst->buf = (char*)malloc(WSIO_STRING_LENGTH); + if (!rst->buf) { + free (rst); + rst = 0; + } + rst->eof = 0; + rst->error = 0; + } + return rst; } +int WSIO_fileno(STREAM *s) +{ +#if defined(VISUAL) + return s->_p==NULL? -1: _fileno((FILE *)s); +#elif defined(__MINGW32__) + return _fileno((FILE *)s); +#endif + return (int)fileno((FILE *)s); +} + int WSIO_read (data, count, s) char* data; int count; STREAM* s; { - int size; + int size; - if (!s) - return 0; + if (!s) + return 0; -#if defined(VISUAL) - size = recv(s->fildes,data,count,0); +#if defined(VISUAL) || defined(__MINGW32__) + size = recv(s->fildes,data,count,0); #elif defined(MPI) - { - MPI_Status status; + { + MPI_Status status; - MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status); - if ( size > count ) - size = count; - MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status); - } + MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status); + if ( size > count ) + size = count; + MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status); + } #endif - return size; + return size; } int WSIO_write (data, count, s) @@ -218,18 +233,18 @@ char* data; int count; STREAM* s; { - int size; + int size; - if (!s || !count) - return 0; + if (!s || !count) + return 0; -#if defined(VISUAL) - size = send(s->fildes,data,count,0); - return size; +#if defined(VISUAL) || defined(__MINGW32__) + size = send(s->fildes,data,count,0); + return size; #elif defined(MPI) - MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD); - MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD); - return count; + MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD); + MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD); + return count; #endif } @@ -238,18 +253,18 @@ int mpi_nprocs,mpi_myid; void mpi_init() { - int argc; - char *argv[1]; + int argc; + char *argv[1]; - argc = 0; argv[0] = 0; - MPI_Init(&argc,&argv); - MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs); - MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid); + argc = 0; argv[0] = 0; + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid); } void mpi_finalize() { - MPI_Finalize(); + MPI_Finalize(); } #endif #endif