Annotation of OpenXM_contrib2/asir2018/io/ws_fileio.c, Revision 1.1
1.1 ! noro 1: /*
! 2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
! 3: * All rights reserved.
! 4: *
! 5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
! 6: * non-exclusive and royalty-free license to use, copy, modify and
! 7: * redistribute, solely for non-commercial and non-profit purposes, the
! 8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
! 9: * conditions of this Agreement. For the avoidance of doubt, you acquire
! 10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
! 11: * third party developer retains all rights, including but not limited to
! 12: * copyrights, in and to the SOFTWARE.
! 13: *
! 14: * (1) FLL does not grant you a license in any way for commercial
! 15: * purposes. You may use the SOFTWARE only for non-commercial and
! 16: * non-profit purposes only, such as academic, research and internal
! 17: * business use.
! 18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
! 19: * international copyright treaties. If you make copies of the SOFTWARE,
! 20: * with or without modification, as permitted hereunder, you shall affix
! 21: * to all such copies of the SOFTWARE the above copyright notice.
! 22: * (3) An explicit reference to this SOFTWARE and its copyright owner
! 23: * shall be made on your publication or presentation in any form of the
! 24: * results obtained by use of the SOFTWARE.
! 25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
! 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
! 27: * for such modification or the source code of the modified part of the
! 28: * SOFTWARE.
! 29: *
! 30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
! 31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
! 32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
! 33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
! 34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
! 35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
! 36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
! 37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
! 38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
! 39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
! 40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
! 41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
! 42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
! 43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
! 44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
! 45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
! 46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
! 47: * $OpenXM$
! 48: */
! 49: #if defined(VISUAL) || defined(__MINGW32__) || defined(MPI)
! 50: #include<stdio.h>
! 51: #include"wsio.h"
! 52:
! 53: STREAM* WSIO_open(int, char *);
! 54: int WSIO_fileno(STREAM *);
! 55: int WSIO_read (char *, int, STREAM *);
! 56: int WSIO_write (char *, int, STREAM *);
! 57: int WSIO_flushbuf(STREAM *);
! 58: int WSIO_fillbuf(STREAM *);
! 59:
! 60: int cread (data, element_size, count, s)
! 61: char* data;
! 62: unsigned element_size;
! 63: int count;
! 64: STREAM* s;
! 65: {
! 66: int length,total,r;
! 67: char *p;
! 68:
! 69: if (!s || !s->read_flag || !data
! 70: || !element_size || !count)
! 71: return 0;
! 72:
! 73: length = element_size * count;
! 74:
! 75: total = length;
! 76: p = data;
! 77: while ( length > (r = s->buf_size - s->p) ) {
! 78: memcpy((void *)p,(void *)(s->buf+s->p),r);
! 79: s->p += r;
! 80: p += r;
! 81: length -= r;
! 82: if ( !WSIO_fillbuf(s) )
! 83: return (total-length)/ element_size;
! 84: }
! 85: memcpy((void *)p,(void *)(s->buf+s->p),length);
! 86: s->p += length;
! 87: return count;
! 88: }
! 89:
! 90: int cwrite (data, element_size, count, s)
! 91: char* data;
! 92: unsigned element_size;
! 93: int count;
! 94: STREAM* s;
! 95: {
! 96: int length,total,r;
! 97: char *p;
! 98:
! 99: if (!s || s->read_flag || !data
! 100: || !element_size || !count)
! 101: return 0;
! 102:
! 103: length = element_size * count;
! 104:
! 105: total = length;
! 106: p = data;
! 107: while ( length > (r = s->buf_size - s->p) ) {
! 108: memcpy((void *)(s->buf+s->p),(void *)p,r);
! 109: s->p += r;
! 110: p += r;
! 111: length -= r;
! 112: if ( !WSIO_flushbuf(s) )
! 113: return (total-length)/ element_size;
! 114: }
! 115: memcpy((void *)(s->buf+s->p),(void *)p,length);
! 116: s->p += length;
! 117: return count;
! 118: }
! 119:
! 120: int cflush(s)
! 121: STREAM* s;
! 122: {
! 123: if (!s || s->read_flag)
! 124: return EOF;
! 125:
! 126: if (!WSIO_flushbuf(s))
! 127: return EOF;
! 128: else
! 129: return 0;
! 130: }
! 131:
! 132: int WSIO_fillbuf(s)
! 133: STREAM* s;
! 134: {
! 135: s->buf_size = WSIO_read(s->buf, s->max_buf_size, s);
! 136: s->p = 0;
! 137:
! 138: return s->buf_size;
! 139: }
! 140:
! 141: int WSIO_flushbuf(s)
! 142: STREAM* s;
! 143: {
! 144: int rst;
! 145:
! 146:
! 147: rst = WSIO_write(s->buf, s->p, s);
! 148: s->p = 0;
! 149:
! 150: return rst;
! 151: }
! 152:
! 153: STREAM* WSIO_open(fildes, mode)
! 154: int fildes;
! 155: char* mode;
! 156: {
! 157: STREAM* rst = 0;
! 158: int flag = 0;
! 159:
! 160: rst = (STREAM*)malloc(sizeof(STREAM));
! 161: if (rst) {
! 162: #if defined(VISUAL)
! 163: rst->_p = NULL;
! 164: #elif defined(__MINGW32__)
! 165: (&rst->fp)->_file = -1;
! 166: #elif defined(MPI)
! 167: #if defined(sparc) || defined(__FreeBSD__)
! 168: (&rst->fp)->_file = -1;
! 169: #elif defined(linux)
! 170: (&rst->fp)->_fileno = -1;
! 171: #else
! 172: fileno(&rst->fp) = -1;
! 173: #endif
! 174: #endif
! 175: rst->fildes = fildes;
! 176:
! 177: if (mode[0] == 'r') {
! 178: rst->read_flag = 1;
! 179: rst->p = WSIO_STRING_LENGTH;
! 180: } else {
! 181: rst->read_flag = 0;
! 182: rst->p = 0;
! 183: }
! 184: rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH;
! 185: rst->buf = (char*)malloc(WSIO_STRING_LENGTH);
! 186: if (!rst->buf) {
! 187: free (rst);
! 188: rst = 0;
! 189: }
! 190: rst->eof = 0;
! 191: rst->error = 0;
! 192: }
! 193: return rst;
! 194: }
! 195:
! 196: int WSIO_fileno(STREAM *s)
! 197: {
! 198: #if defined(VISUAL)
! 199: return s->_p==NULL? -1: _fileno((FILE *)s);
! 200: #elif defined(__MINGW32__)
! 201: return _fileno((FILE *)s);
! 202: #endif
! 203: return (int)fileno((FILE *)s);
! 204: }
! 205:
! 206: int WSIO_read (data, count, s)
! 207: char* data;
! 208: int count;
! 209: STREAM* s;
! 210: {
! 211: int size;
! 212:
! 213: if (!s)
! 214: return 0;
! 215:
! 216: #if defined(VISUAL) || defined(__MINGW32__)
! 217: size = recv(s->fildes,data,count,0);
! 218: #elif defined(MPI)
! 219: {
! 220: MPI_Status status;
! 221:
! 222: MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status);
! 223: if ( size > count )
! 224: size = count;
! 225: MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status);
! 226: }
! 227: #endif
! 228: return size;
! 229: }
! 230:
! 231: int WSIO_write (data, count, s)
! 232: char* data;
! 233: int count;
! 234: STREAM* s;
! 235: {
! 236: int size;
! 237:
! 238: if (!s || !count)
! 239: return 0;
! 240:
! 241: #if defined(VISUAL) || defined(__MINGW32__)
! 242: size = send(s->fildes,data,count,0);
! 243: return size;
! 244: #elif defined(MPI)
! 245: MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD);
! 246: MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD);
! 247: return count;
! 248: #endif
! 249: }
! 250:
! 251: #if defined(MPI)
! 252: int mpi_nprocs,mpi_myid;
! 253:
! 254: void mpi_init()
! 255: {
! 256: int argc;
! 257: char *argv[1];
! 258:
! 259: argc = 0; argv[0] = 0;
! 260: MPI_Init(&argc,&argv);
! 261: MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs);
! 262: MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid);
! 263: }
! 264:
! 265: void mpi_finalize()
! 266: {
! 267: MPI_Finalize();
! 268: }
! 269: #endif
! 270: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>