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

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.6     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ws_fileio.c,v 1.5 2003/03/07 03:12:28 noro Exp $
1.2       noro       48: */
1.6     ! noro       49: #if defined(VISUAL) || 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: {
                     65:        int length,total,r;
                     66:        char *p;
                     67:
                     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;
                     87: }
                     88:
                     89: int cwrite (data, element_size, count, s)
                     90: char* data;
                     91: unsigned element_size;
                     92: int count;
                     93: STREAM* s;
                     94: {
                     95:        int length,total,r;
                     96:        char *p;
                     97:
                     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;
                    117: }
                    118:
                    119: int cflush(s)
                    120: STREAM* s;
                    121: {
                    122:        if (!s || s->read_flag)
                    123:                return EOF;
                    124:
                    125:        if (!WSIO_flushbuf(s))
                    126:                return EOF;
                    127:        else
                    128:                return 0;
                    129: }
                    130:
                    131: int WSIO_fillbuf(s)
                    132: STREAM* s;
                    133: {
                    134:        s->buf_size = WSIO_read(s->buf, s->max_buf_size, s);
                    135:        s->p = 0;
                    136:
                    137:        return s->buf_size;
                    138: }
                    139:
                    140: int WSIO_flushbuf(s)
                    141: STREAM* s;
                    142: {
                    143:        int rst;
                    144:
                    145:
                    146:        rst = WSIO_write(s->buf, s->p, s);
                    147:        s->p = 0;
                    148:
                    149:        return rst;
                    150: }
                    151:
                    152: STREAM* WSIO_open(fildes, mode)
                    153: int fildes;
                    154: char* mode;
                    155: {
                    156:        STREAM* rst = 0;
                    157:        int flag = 0;
                    158:
                    159:        rst = (STREAM*)malloc(sizeof(STREAM));
                    160:        if (rst) {
                    161: #if defined(VISUAL)
                    162:                _fileno(&rst->fp) = -1;
1.6     ! noro      163: #elif defined(MPI)
1.1       noro      164: #if defined(sparc)
                    165:                (&rst->fp)->_file = -1;
                    166: #else
                    167:                fileno(&rst->fp) = -1;
                    168: #endif
                    169: #endif
                    170:                rst->fildes = fildes;
                    171:
                    172:                if (mode[0] == 'r') {
                    173:                        rst->read_flag = 1;
                    174:                        rst->p = WSIO_STRING_LENGTH;
                    175:                } else {
                    176:                        rst->read_flag = 0;
                    177:                        rst->p = 0;
                    178:                }
                    179:                rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH;
                    180:                rst->buf = (char*)malloc(WSIO_STRING_LENGTH);
                    181:                if (!rst->buf) {
                    182:                        free (rst);
                    183:                        rst = 0;
                    184:                }
                    185:                rst->eof = 0;
                    186:                rst->error = 0;
                    187:        }
                    188:        return rst;
                    189: }
                    190:
                    191: int WSIO_read (data, count, s)
                    192: char* data;
                    193: int count;
                    194: STREAM* s;
                    195: {
                    196:        int size;
                    197:
                    198:        if (!s)
                    199:                return 0;
                    200:
                    201: #if defined(VISUAL)
                    202:        size = recv(s->fildes,data,count,0);
1.6     ! noro      203: #elif defined(MPI)
1.1       noro      204:        {
                    205:                MPI_Status status;
                    206:
                    207:                MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status);
                    208:                if ( size > count )
                    209:                        size = count;
                    210:                MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status);
                    211:        }
                    212: #endif
                    213:        return size;
                    214: }
                    215:
                    216: int WSIO_write (data, count, s)
                    217: char* data;
                    218: int count;
                    219: STREAM* s;
                    220: {
                    221:        int size;
                    222:
                    223:        if (!s || !count)
                    224:                return 0;
                    225:
                    226: #if defined(VISUAL)
                    227:        size = send(s->fildes,data,count,0);
                    228:        return size;
1.6     ! noro      229: #elif defined(MPI)
1.1       noro      230:        MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD);
                    231:        MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD);
                    232:        return count;
                    233: #endif
                    234: }
                    235:
1.6     ! noro      236: #if defined(MPI)
1.1       noro      237: int mpi_nprocs,mpi_myid;
                    238:
                    239: void mpi_init()
                    240: {
                    241:        int argc;
                    242:        char *argv[1];
                    243:
                    244:        argc = 0; argv[0] = 0;
                    245:        MPI_Init(&argc,&argv);
                    246:        MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs);
                    247:        MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid);
                    248: }
                    249:
                    250: void mpi_finalize()
                    251: {
                    252:        MPI_Finalize();
                    253: }
                    254: #endif
                    255: #endif

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