[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.13

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.13    ! ohara      47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ws_fileio.c,v 1.12 2018/03/29 01:32:54 noro 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 *);
1.13    ! ohara      54: int WSIO_fileno(STREAM *);
1.1       noro       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: {
1.12      noro       66:   int length,total,r;
                     67:   char *p;
1.1       noro       68:
1.12      noro       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;
1.1       noro       88: }
                     89:
                     90: int cwrite (data, element_size, count, s)
                     91: char* data;
                     92: unsigned element_size;
                     93: int count;
                     94: STREAM* s;
                     95: {
1.12      noro       96:   int length,total,r;
                     97:   char *p;
1.1       noro       98:
1.12      noro       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;
1.1       noro      118: }
                    119:
                    120: int cflush(s)
                    121: STREAM* s;
                    122: {
1.12      noro      123:   if (!s || s->read_flag)
                    124:     return EOF;
1.1       noro      125:
1.12      noro      126:   if (!WSIO_flushbuf(s))
                    127:     return EOF;
                    128:   else
                    129:     return 0;
1.1       noro      130: }
                    131:
                    132: int WSIO_fillbuf(s)
                    133: STREAM* s;
                    134: {
1.12      noro      135:   s->buf_size = WSIO_read(s->buf, s->max_buf_size, s);
                    136:   s->p = 0;
1.1       noro      137:
1.12      noro      138:   return s->buf_size;
1.1       noro      139: }
                    140:
                    141: int WSIO_flushbuf(s)
                    142: STREAM* s;
                    143: {
1.12      noro      144:   int rst;
1.1       noro      145:
                    146:
1.12      noro      147:   rst = WSIO_write(s->buf, s->p, s);
                    148:   s->p = 0;
1.1       noro      149:
1.12      noro      150:   return rst;
1.1       noro      151: }
                    152:
                    153: STREAM* WSIO_open(fildes, mode)
                    154: int fildes;
                    155: char* mode;
                    156: {
1.12      noro      157:   STREAM* rst = 0;
                    158:   int flag = 0;
1.1       noro      159:
1.12      noro      160:   rst = (STREAM*)malloc(sizeof(STREAM));
                    161:   if (rst) {
1.1       noro      162: #if defined(VISUAL)
1.13    ! ohara     163:     rst->_p = NULL;
        !           164: #elif defined(__MINGW32__)
1.12      noro      165:     (&rst->fp)->_file = -1;
1.6       noro      166: #elif defined(MPI)
1.9       ohara     167: #if defined(sparc) || defined(__FreeBSD__)
1.12      noro      168:     (&rst->fp)->_file = -1;
1.9       ohara     169: #elif defined(linux)
1.12      noro      170:     (&rst->fp)->_fileno = -1;
1.1       noro      171: #else
1.12      noro      172:     fileno(&rst->fp) = -1;
1.1       noro      173: #endif
                    174: #endif
1.12      noro      175:     rst->fildes = fildes;
1.1       noro      176:
1.12      noro      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;
1.1       noro      194: }
                    195:
1.13    ! ohara     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:
1.1       noro      206: int WSIO_read (data, count, s)
                    207: char* data;
                    208: int count;
                    209: STREAM* s;
                    210: {
1.12      noro      211:   int size;
1.1       noro      212:
1.12      noro      213:   if (!s)
                    214:     return 0;
1.1       noro      215:
1.11      fujimoto  216: #if defined(VISUAL) || defined(__MINGW32__)
1.12      noro      217:   size = recv(s->fildes,data,count,0);
1.6       noro      218: #elif defined(MPI)
1.12      noro      219:   {
                    220:     MPI_Status status;
1.1       noro      221:
1.12      noro      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:   }
1.1       noro      227: #endif
1.12      noro      228:   return size;
1.1       noro      229: }
                    230:
                    231: int WSIO_write (data, count, s)
                    232: char* data;
                    233: int count;
                    234: STREAM* s;
                    235: {
1.12      noro      236:   int size;
1.1       noro      237:
1.12      noro      238:   if (!s || !count)
                    239:     return 0;
1.1       noro      240:
1.11      fujimoto  241: #if defined(VISUAL) || defined(__MINGW32__)
1.12      noro      242:   size = send(s->fildes,data,count,0);
                    243:   return size;
1.6       noro      244: #elif defined(MPI)
1.12      noro      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;
1.1       noro      248: #endif
                    249: }
                    250:
1.6       noro      251: #if defined(MPI)
1.1       noro      252: int mpi_nprocs,mpi_myid;
                    253:
                    254: void mpi_init()
                    255: {
1.12      noro      256:   int argc;
                    257:   char *argv[1];
1.1       noro      258:
1.12      noro      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);
1.1       noro      263: }
                    264:
                    265: void mpi_finalize()
                    266: {
1.12      noro      267:   MPI_Finalize();
1.1       noro      268: }
                    269: #endif
                    270: #endif

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