Annotation of OpenXM_contrib2/asir2000/io/ws_fileio.c, Revision 1.12
1.2 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
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 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.
1.12 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ws_fileio.c,v 1.11 2015/08/14 13:51:55 fujimoto Exp $
1.2 noro 48: */
1.11 fujimoto 49: #if defined(VISUAL) || defined(__MINGW32__) || defined(MPI)
1.1 noro 50: #include<stdio.h>
51: #include"wsio.h"
52:
53: STREAM* WSIO_open(int, char *);
54: int WSIO_read (char *, int, STREAM *);
55: int WSIO_write (char *, int, STREAM *);
56: int WSIO_flushbuf(STREAM *);
57: int WSIO_fillbuf(STREAM *);
58:
59: int cread (data, element_size, count, s)
60: char* data;
61: unsigned element_size;
62: int count;
63: STREAM* s;
64: {
1.12 ! noro 65: int length,total,r;
! 66: char *p;
1.1 noro 67:
1.12 ! noro 68: if (!s || !s->read_flag || !data
! 69: || !element_size || !count)
! 70: return 0;
! 71:
! 72: length = element_size * count;
! 73:
! 74: total = length;
! 75: p = data;
! 76: while ( length > (r = s->buf_size - s->p) ) {
! 77: memcpy((void *)p,(void *)(s->buf+s->p),r);
! 78: s->p += r;
! 79: p += r;
! 80: length -= r;
! 81: if ( !WSIO_fillbuf(s) )
! 82: return (total-length)/ element_size;
! 83: }
! 84: memcpy((void *)p,(void *)(s->buf+s->p),length);
! 85: s->p += length;
! 86: return count;
1.1 noro 87: }
88:
89: int cwrite (data, element_size, count, s)
90: char* data;
91: unsigned element_size;
92: int count;
93: STREAM* s;
94: {
1.12 ! noro 95: int length,total,r;
! 96: char *p;
1.1 noro 97:
1.12 ! noro 98: if (!s || s->read_flag || !data
! 99: || !element_size || !count)
! 100: return 0;
! 101:
! 102: length = element_size * count;
! 103:
! 104: total = length;
! 105: p = data;
! 106: while ( length > (r = s->buf_size - s->p) ) {
! 107: memcpy((void *)(s->buf+s->p),(void *)p,r);
! 108: s->p += r;
! 109: p += r;
! 110: length -= r;
! 111: if ( !WSIO_flushbuf(s) )
! 112: return (total-length)/ element_size;
! 113: }
! 114: memcpy((void *)(s->buf+s->p),(void *)p,length);
! 115: s->p += length;
! 116: return count;
1.1 noro 117: }
118:
119: int cflush(s)
120: STREAM* s;
121: {
1.12 ! noro 122: if (!s || s->read_flag)
! 123: return EOF;
1.1 noro 124:
1.12 ! noro 125: if (!WSIO_flushbuf(s))
! 126: return EOF;
! 127: else
! 128: return 0;
1.1 noro 129: }
130:
131: int WSIO_fillbuf(s)
132: STREAM* s;
133: {
1.12 ! noro 134: s->buf_size = WSIO_read(s->buf, s->max_buf_size, s);
! 135: s->p = 0;
1.1 noro 136:
1.12 ! noro 137: return s->buf_size;
1.1 noro 138: }
139:
140: int WSIO_flushbuf(s)
141: STREAM* s;
142: {
1.12 ! noro 143: int rst;
1.1 noro 144:
145:
1.12 ! noro 146: rst = WSIO_write(s->buf, s->p, s);
! 147: s->p = 0;
1.1 noro 148:
1.12 ! noro 149: return rst;
1.1 noro 150: }
151:
152: STREAM* WSIO_open(fildes, mode)
153: int fildes;
154: char* mode;
155: {
1.12 ! noro 156: STREAM* rst = 0;
! 157: int flag = 0;
1.1 noro 158:
1.12 ! noro 159: rst = (STREAM*)malloc(sizeof(STREAM));
! 160: if (rst) {
1.1 noro 161: #if defined(VISUAL)
1.7 ohara 162: #if _MSC_VER < 1500
1.12 ! noro 163: _fileno(&rst->fp) = -1;
1.7 ohara 164: #else
1.12 ! noro 165: (&rst->fp)->_file = -1;
1.7 ohara 166: #endif
1.6 noro 167: #elif defined(MPI)
1.9 ohara 168: #if defined(sparc) || defined(__FreeBSD__)
1.12 ! noro 169: (&rst->fp)->_file = -1;
1.9 ohara 170: #elif defined(linux)
1.12 ! noro 171: (&rst->fp)->_fileno = -1;
1.1 noro 172: #else
1.12 ! noro 173: fileno(&rst->fp) = -1;
1.1 noro 174: #endif
175: #endif
1.12 ! noro 176: rst->fildes = fildes;
1.1 noro 177:
1.12 ! noro 178: if (mode[0] == 'r') {
! 179: rst->read_flag = 1;
! 180: rst->p = WSIO_STRING_LENGTH;
! 181: } else {
! 182: rst->read_flag = 0;
! 183: rst->p = 0;
! 184: }
! 185: rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH;
! 186: rst->buf = (char*)malloc(WSIO_STRING_LENGTH);
! 187: if (!rst->buf) {
! 188: free (rst);
! 189: rst = 0;
! 190: }
! 191: rst->eof = 0;
! 192: rst->error = 0;
! 193: }
! 194: return rst;
1.1 noro 195: }
196:
197: int WSIO_read (data, count, s)
198: char* data;
199: int count;
200: STREAM* s;
201: {
1.12 ! noro 202: int size;
1.1 noro 203:
1.12 ! noro 204: if (!s)
! 205: return 0;
1.1 noro 206:
1.11 fujimoto 207: #if defined(VISUAL) || defined(__MINGW32__)
1.12 ! noro 208: size = recv(s->fildes,data,count,0);
1.6 noro 209: #elif defined(MPI)
1.12 ! noro 210: {
! 211: MPI_Status status;
1.1 noro 212:
1.12 ! noro 213: MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status);
! 214: if ( size > count )
! 215: size = count;
! 216: MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status);
! 217: }
1.1 noro 218: #endif
1.12 ! noro 219: return size;
1.1 noro 220: }
221:
222: int WSIO_write (data, count, s)
223: char* data;
224: int count;
225: STREAM* s;
226: {
1.12 ! noro 227: int size;
1.1 noro 228:
1.12 ! noro 229: if (!s || !count)
! 230: return 0;
1.1 noro 231:
1.11 fujimoto 232: #if defined(VISUAL) || defined(__MINGW32__)
1.12 ! noro 233: size = send(s->fildes,data,count,0);
! 234: return size;
1.6 noro 235: #elif defined(MPI)
1.12 ! noro 236: MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD);
! 237: MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD);
! 238: return count;
1.1 noro 239: #endif
240: }
241:
1.6 noro 242: #if defined(MPI)
1.1 noro 243: int mpi_nprocs,mpi_myid;
244:
245: void mpi_init()
246: {
1.12 ! noro 247: int argc;
! 248: char *argv[1];
1.1 noro 249:
1.12 ! noro 250: argc = 0; argv[0] = 0;
! 251: MPI_Init(&argc,&argv);
! 252: MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs);
! 253: MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid);
1.1 noro 254: }
255:
256: void mpi_finalize()
257: {
1.12 ! noro 258: MPI_Finalize();
1.1 noro 259: }
260: #endif
261: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>