OSDN Git Service

* Chill runtime moved into toplevel libchill.
[pf3gnuchains/gcc-fork.git] / libchill / writerecord.c
1 /* Implement Input/Output runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser, et al
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include <setjmp.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26
27 #include "fileio.h"
28
29 static
30 void
31 doWrite( Access_Mode* the_access, void* buf, size_t nbyte )
32 {
33   size_t nwrit;
34
35   nwrit = write( the_access->association->handle, buf, nbyte );
36
37   if( nwrit < nbyte )
38   {
39     the_access->association->syserrno = errno;
40     RWEXCEPTION( WRITEFAIL, OS_IO_ERROR );
41   }
42 }
43
44
45 void
46 __writerecord( Access_Mode*  the_access,
47                signed long   the_index,
48                char*         the_val_addr,
49                unsigned long the_val_len,
50                char*         file,
51                int           line )
52
53 {
54   Association_Mode* the_assoc;
55   unsigned long  info;
56   char*          actaddr;
57   unsigned short actlen;
58   off_t          filepos;
59
60   if( !the_access )
61     CHILLEXCEPTION( file, line, EMPTY, NULL_ACCESS );
62
63   if( !(the_assoc = the_access->association) )
64     CHILLEXCEPTION( file, line, NOTCONNECTED, IS_NOT_CONNECTED );
65
66   /* Usage must no be ReadOnly */
67   if( the_assoc->usage == ReadOnly )
68     CHILLEXCEPTION( file, line, WRITEFAIL, BAD_USAGE );
69
70   /*
71    *  Positioning
72    */
73   if( TEST_FLAG( the_access, IO_INDEXED ) )
74   {
75     /* index expression must be within bounds of index mode */
76     if( the_index < the_access->lowindex
77         || the_access->highindex < the_index )
78       CHILLEXCEPTION( file, line, RANGEFAIL, BAD_INDEX );
79     filepos = the_access->base + 
80               (the_index - the_access->lowindex) * the_access->reclength;
81
82     if( lseek( the_assoc->handle, filepos, SEEK_SET ) == -1L )
83       CHILLEXCEPTION( file, line, WRITEFAIL, LSEEK_FAILS );
84   }
85
86   if( (info = setjmp( __rw_exception )) )
87     CHILLEXCEPTION( file, line, info>>16, info & 0xffff );
88
89   if( TEST_FLAG( the_access, IO_TEXTIO ) )
90   {
91     if( TEST_FLAG( the_access, IO_INDEXED ) )
92     {
93       int nspace = the_access->reclength - the_val_len;
94       memset( the_val_addr + 2 + the_val_len, ' ', nspace );
95       actlen = the_access->reclength - 2;
96       MOV2(the_val_addr,&actlen);
97       doWrite( the_access, the_val_addr, the_access->reclength );
98     }
99     else
100     { 
101       if( the_assoc->ctl_pre )
102         write( the_assoc->handle, &the_assoc->ctl_pre, 1 );
103       MOV2(&actlen,the_val_addr);
104       write( the_assoc->handle, the_val_addr + 2, actlen );
105       if( the_assoc->ctl_post )
106         write( the_assoc->handle, &the_assoc->ctl_post, 1 );
107       the_assoc->ctl_pre  = '\0';
108       the_assoc->ctl_post = '\n';
109     }
110   }
111   else
112   {
113     switch( the_access->rectype )
114     {
115     case Fixed:
116       if( TEST_FLAG( the_assoc, IO_VARIABLE ) )
117       {
118         actlen = the_access->reclength;
119         doWrite( the_access, &actlen, sizeof(actlen) );
120       }
121       doWrite( the_access, the_val_addr, the_val_len );
122       break;
123     case VaryingChars:
124       MOV2(&actlen,the_val_addr);
125       if( actlen > the_access->reclength - 2 )
126         CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
127       actlen = TEST_FLAG( the_access, IO_INDEXED ) 
128                ? the_access->reclength : actlen + 2;
129       doWrite( the_access, the_val_addr, actlen );
130       break;
131     }
132   }
133 }