version 1.1, 1999/12/03 07:39:11 |
version 1.13, 2018/09/09 08:13:42 |
|
|
/* $OpenXM: OpenXM/src/asir99/io/ws_fileio.c,v 1.2 1999/11/19 10:41:08 noro Exp $ */ |
/* |
#if defined(VISUAL) || MPI |
* Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED |
|
* All rights reserved. |
|
* |
|
* FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited, |
|
* non-exclusive and royalty-free license to use, copy, modify and |
|
* redistribute, solely for non-commercial and non-profit purposes, the |
|
* computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and |
|
* conditions of this Agreement. For the avoidance of doubt, you acquire |
|
* only a limited right to use the SOFTWARE hereunder, and FLL or any |
|
* third party developer retains all rights, including but not limited to |
|
* copyrights, in and to the SOFTWARE. |
|
* |
|
* (1) FLL does not grant you a license in any way for commercial |
|
* purposes. You may use the SOFTWARE only for non-commercial and |
|
* non-profit purposes only, such as academic, research and internal |
|
* business use. |
|
* (2) The SOFTWARE is protected by the Copyright Law of Japan and |
|
* international copyright treaties. If you make copies of the SOFTWARE, |
|
* with or without modification, as permitted hereunder, you shall affix |
|
* to all such copies of the SOFTWARE the above copyright notice. |
|
* (3) An explicit reference to this SOFTWARE and its copyright owner |
|
* shall be made on your publication or presentation in any form of the |
|
* results obtained by use of the SOFTWARE. |
|
* (4) In the event that you modify the SOFTWARE, you shall notify FLL by |
|
* e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification |
|
* for such modification or the source code of the modified part of the |
|
* SOFTWARE. |
|
* |
|
* THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL |
|
* MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND |
|
* EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS |
|
* FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES' |
|
* RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY |
|
* MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY. |
|
* UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT, |
|
* OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY |
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL |
|
* DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES |
|
* ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES |
|
* FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY |
|
* DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF |
|
* SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART |
|
* 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.12 2018/03/29 01:32:54 noro Exp $ |
|
*/ |
|
#if defined(VISUAL) || defined(__MINGW32__) || defined(MPI) |
#include<stdio.h> |
#include<stdio.h> |
#include"wsio.h" |
#include"wsio.h" |
|
|
STREAM* WSIO_open(int, char *); |
STREAM* WSIO_open(int, char *); |
|
int WSIO_fileno(STREAM *); |
int WSIO_read (char *, int, STREAM *); |
int WSIO_read (char *, int, STREAM *); |
int WSIO_write (char *, int, STREAM *); |
int WSIO_write (char *, int, STREAM *); |
int WSIO_flushbuf(STREAM *); |
int WSIO_flushbuf(STREAM *); |
Line 15 unsigned element_size; |
|
Line 63 unsigned element_size; |
|
int count; |
int count; |
STREAM* s; |
STREAM* s; |
{ |
{ |
int length,total,r; |
int length,total,r; |
char *p; |
char *p; |
|
|
if (!s || !s->read_flag || !data |
if (!s || !s->read_flag || !data |
|| !element_size || !count) |
|| !element_size || !count) |
return 0; |
return 0; |
|
|
length = element_size * count; |
length = element_size * count; |
|
|
total = length; |
total = length; |
p = data; |
p = data; |
while ( length > (r = s->buf_size - s->p) ) { |
while ( length > (r = s->buf_size - s->p) ) { |
memcpy((void *)p,(void *)(s->buf+s->p),r); |
memcpy((void *)p,(void *)(s->buf+s->p),r); |
s->p += r; |
s->p += r; |
p += r; |
p += r; |
length -= r; |
length -= r; |
if ( !WSIO_fillbuf(s) ) |
if ( !WSIO_fillbuf(s) ) |
return (total-length)/ element_size; |
return (total-length)/ element_size; |
} |
} |
memcpy((void *)p,(void *)(s->buf+s->p),length); |
memcpy((void *)p,(void *)(s->buf+s->p),length); |
s->p += length; |
s->p += length; |
return count; |
return count; |
} |
} |
|
|
int cwrite (data, element_size, count, s) |
int cwrite (data, element_size, count, s) |
Line 45 unsigned element_size; |
|
Line 93 unsigned element_size; |
|
int count; |
int count; |
STREAM* s; |
STREAM* s; |
{ |
{ |
int length,total,r; |
int length,total,r; |
char *p; |
char *p; |
|
|
if (!s || s->read_flag || !data |
if (!s || s->read_flag || !data |
|| !element_size || !count) |
|| !element_size || !count) |
return 0; |
return 0; |
|
|
length = element_size * count; |
length = element_size * count; |
|
|
total = length; |
total = length; |
p = data; |
p = data; |
while ( length > (r = s->buf_size - s->p) ) { |
while ( length > (r = s->buf_size - s->p) ) { |
memcpy((void *)(s->buf+s->p),(void *)p,r); |
memcpy((void *)(s->buf+s->p),(void *)p,r); |
s->p += r; |
s->p += r; |
p += r; |
p += r; |
length -= r; |
length -= r; |
if ( !WSIO_flushbuf(s) ) |
if ( !WSIO_flushbuf(s) ) |
return (total-length)/ element_size; |
return (total-length)/ element_size; |
} |
} |
memcpy((void *)(s->buf+s->p),(void *)p,length); |
memcpy((void *)(s->buf+s->p),(void *)p,length); |
s->p += length; |
s->p += length; |
return count; |
return count; |
} |
} |
|
|
int cflush(s) |
int cflush(s) |
STREAM* s; |
STREAM* s; |
{ |
{ |
if (!s || s->read_flag) |
if (!s || s->read_flag) |
return EOF; |
return EOF; |
|
|
if (!WSIO_flushbuf(s)) |
if (!WSIO_flushbuf(s)) |
return EOF; |
return EOF; |
else |
else |
return 0; |
return 0; |
} |
} |
|
|
int WSIO_fillbuf(s) |
int WSIO_fillbuf(s) |
STREAM* s; |
STREAM* s; |
{ |
{ |
s->buf_size = WSIO_read(s->buf, s->max_buf_size, s); |
s->buf_size = WSIO_read(s->buf, s->max_buf_size, s); |
s->p = 0; |
s->p = 0; |
|
|
return s->buf_size; |
return s->buf_size; |
} |
} |
|
|
int WSIO_flushbuf(s) |
int WSIO_flushbuf(s) |
STREAM* s; |
STREAM* s; |
{ |
{ |
int rst; |
int rst; |
|
|
|
|
rst = WSIO_write(s->buf, s->p, s); |
rst = WSIO_write(s->buf, s->p, s); |
s->p = 0; |
s->p = 0; |
|
|
return rst; |
return rst; |
} |
} |
|
|
STREAM* WSIO_open(fildes, mode) |
STREAM* WSIO_open(fildes, mode) |
int fildes; |
int fildes; |
char* mode; |
char* mode; |
{ |
{ |
STREAM* rst = 0; |
STREAM* rst = 0; |
int flag = 0; |
int flag = 0; |
|
|
rst = (STREAM*)malloc(sizeof(STREAM)); |
rst = (STREAM*)malloc(sizeof(STREAM)); |
if (rst) { |
if (rst) { |
#if defined(VISUAL) |
#if defined(VISUAL) |
_fileno(&rst->fp) = -1; |
rst->_p = NULL; |
#elif MPI |
#elif defined(__MINGW32__) |
#if defined(sparc) |
(&rst->fp)->_file = -1; |
(&rst->fp)->_file = -1; |
#elif defined(MPI) |
|
#if defined(sparc) || defined(__FreeBSD__) |
|
(&rst->fp)->_file = -1; |
|
#elif defined(linux) |
|
(&rst->fp)->_fileno = -1; |
#else |
#else |
fileno(&rst->fp) = -1; |
fileno(&rst->fp) = -1; |
#endif |
#endif |
#endif |
#endif |
rst->fildes = fildes; |
rst->fildes = fildes; |
|
|
if (mode[0] == 'r') { |
if (mode[0] == 'r') { |
rst->read_flag = 1; |
rst->read_flag = 1; |
rst->p = WSIO_STRING_LENGTH; |
rst->p = WSIO_STRING_LENGTH; |
} else { |
} else { |
rst->read_flag = 0; |
rst->read_flag = 0; |
rst->p = 0; |
rst->p = 0; |
} |
} |
rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH; |
rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH; |
rst->buf = (char*)malloc(WSIO_STRING_LENGTH); |
rst->buf = (char*)malloc(WSIO_STRING_LENGTH); |
if (!rst->buf) { |
if (!rst->buf) { |
free (rst); |
free (rst); |
rst = 0; |
rst = 0; |
} |
} |
rst->eof = 0; |
rst->eof = 0; |
rst->error = 0; |
rst->error = 0; |
} |
} |
return rst; |
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) |
int WSIO_read (data, count, s) |
char* data; |
char* data; |
int count; |
int count; |
STREAM* s; |
STREAM* s; |
{ |
{ |
int size; |
int size; |
|
|
if (!s) |
if (!s) |
return 0; |
return 0; |
|
|
#if defined(VISUAL) |
#if defined(VISUAL) || defined(__MINGW32__) |
size = recv(s->fildes,data,count,0); |
size = recv(s->fildes,data,count,0); |
#elif MPI |
#elif defined(MPI) |
{ |
{ |
MPI_Status status; |
MPI_Status status; |
|
|
MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status); |
MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status); |
if ( size > count ) |
if ( size > count ) |
size = count; |
size = count; |
MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status); |
MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status); |
} |
} |
#endif |
#endif |
return size; |
return size; |
} |
} |
|
|
int WSIO_write (data, count, s) |
int WSIO_write (data, count, s) |
|
|
int count; |
int count; |
STREAM* s; |
STREAM* s; |
{ |
{ |
int size; |
int size; |
|
|
if (!s || !count) |
if (!s || !count) |
return 0; |
return 0; |
|
|
#if defined(VISUAL) |
#if defined(VISUAL) || defined(__MINGW32__) |
size = send(s->fildes,data,count,0); |
size = send(s->fildes,data,count,0); |
return size; |
return size; |
#elif MPI |
#elif defined(MPI) |
MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD); |
MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD); |
MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD); |
MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD); |
return count; |
return count; |
#endif |
#endif |
} |
} |
|
|
#if MPI |
#if defined(MPI) |
int mpi_nprocs,mpi_myid; |
int mpi_nprocs,mpi_myid; |
|
|
void mpi_init() |
void mpi_init() |
{ |
{ |
int argc; |
int argc; |
char *argv[1]; |
char *argv[1]; |
|
|
argc = 0; argv[0] = 0; |
argc = 0; argv[0] = 0; |
MPI_Init(&argc,&argv); |
MPI_Init(&argc,&argv); |
MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs); |
MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs); |
MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid); |
MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid); |
} |
} |
|
|
void mpi_finalize() |
void mpi_finalize() |
{ |
{ |
MPI_Finalize(); |
MPI_Finalize(); |
} |
} |
#endif |
#endif |
#endif |
#endif |