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

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.8     ! ohara      47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ws_fileio.c,v 1.7 2009/02/07 22:41:28 ohara 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)
1.7       ohara     162: #if _MSC_VER < 1500
1.1       noro      163:                _fileno(&rst->fp) = -1;
1.7       ohara     164: #else
                    165:                (&rst->fp)->_file = -1;
                    166: #endif
1.6       noro      167: #elif defined(MPI)
1.1       noro      168: #if defined(sparc)
                    169:                (&rst->fp)->_file = -1;
                    170: #else
                    171:                fileno(&rst->fp) = -1;
                    172: #endif
                    173: #endif
                    174:                rst->fildes = fildes;
                    175:
                    176:                if (mode[0] == 'r') {
                    177:                        rst->read_flag = 1;
                    178:                        rst->p = WSIO_STRING_LENGTH;
                    179:                } else {
                    180:                        rst->read_flag = 0;
                    181:                        rst->p = 0;
                    182:                }
                    183:                rst->max_buf_size = rst->buf_size = WSIO_STRING_LENGTH;
                    184:                rst->buf = (char*)malloc(WSIO_STRING_LENGTH);
                    185:                if (!rst->buf) {
                    186:                        free (rst);
                    187:                        rst = 0;
                    188:                }
                    189:                rst->eof = 0;
                    190:                rst->error = 0;
                    191:        }
                    192:        return rst;
                    193: }
                    194:
                    195: int WSIO_read (data, count, s)
                    196: char* data;
                    197: int count;
                    198: STREAM* s;
                    199: {
                    200:        int size;
                    201:
                    202:        if (!s)
                    203:                return 0;
                    204:
                    205: #if defined(VISUAL)
                    206:        size = recv(s->fildes,data,count,0);
1.6       noro      207: #elif defined(MPI)
1.1       noro      208:        {
                    209:                MPI_Status status;
                    210:
                    211:                MPI_Recv(&size,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD,&status);
                    212:                if ( size > count )
                    213:                        size = count;
                    214:                MPI_Recv(data,size,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD,&status);
                    215:        }
                    216: #endif
                    217:        return size;
                    218: }
                    219:
                    220: int WSIO_write (data, count, s)
                    221: char* data;
                    222: int count;
                    223: STREAM* s;
                    224: {
                    225:        int size;
                    226:
                    227:        if (!s || !count)
                    228:                return 0;
                    229:
                    230: #if defined(VISUAL)
                    231:        size = send(s->fildes,data,count,0);
                    232:        return size;
1.6       noro      233: #elif defined(MPI)
1.1       noro      234:        MPI_Ssend(&count,1,MPI_INT,s->fildes,0,MPI_COMM_WORLD);
                    235:        MPI_Ssend(data,count,MPI_CHAR,s->fildes,0,MPI_COMM_WORLD);
                    236:        return count;
                    237: #endif
                    238: }
                    239:
1.6       noro      240: #if defined(MPI)
1.1       noro      241: int mpi_nprocs,mpi_myid;
                    242:
                    243: void mpi_init()
                    244: {
                    245:        int argc;
                    246:        char *argv[1];
                    247:
                    248:        argc = 0; argv[0] = 0;
                    249:        MPI_Init(&argc,&argv);
                    250:        MPI_Comm_size(MPI_COMM_WORLD,&mpi_nprocs);
                    251:        MPI_Comm_rank(MPI_COMM_WORLD,&mpi_myid);
                    252: }
                    253:
                    254: void mpi_finalize()
                    255: {
                    256:        MPI_Finalize();
                    257: }
                    258: #endif
                    259: #endif

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