1 /* Copyright (C) 2008 Free Software Foundation, Inc.
2 Contributed by Janne Blomqvist
4 This file is part of the GNU Fortran runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
37 fbuf_init (gfc_unit
* u
, size_t len
)
40 len
= 4096; /* Default size one page. */
42 u
->fbuf
= get_mem (sizeof (fbuf
));
43 u
->fbuf
->buf
= u
->fbuf
->ptr
= get_mem (len
);
45 u
->fbuf
->act
= u
->fbuf
->flushed
= 0;
50 fbuf_reset (gfc_unit
* u
)
52 u
->fbuf
->act
= u
->fbuf
->flushed
= 0;
53 u
->fbuf
->ptr
= u
->fbuf
->buf
;
58 fbuf_destroy (gfc_unit
* u
)
63 free_mem (u
->fbuf
->buf
);
68 /* Return a pointer to the current position in the buffer, and increase
69 the pointer by len. Makes sure that the buffer is big enough,
70 reallocating if necessary. */
73 fbuf_alloc (gfc_unit
* u
, size_t len
)
75 size_t newlen
, ptrpos
;
77 if (u
->fbuf
->ptr
+ len
> u
->fbuf
->buf
+ u
->fbuf
->len
)
79 /* Round up to nearest multiple of the current buffer length. */
80 ptrpos
= u
->fbuf
->ptr
- u
->fbuf
->buf
;
81 newlen
= ((ptrpos
+ len
) / u
->fbuf
->len
+ 1) * u
->fbuf
->len
;
82 dest
= realloc (u
->fbuf
->buf
, newlen
);
86 u
->fbuf
->ptr
= dest
+ ptrpos
;
87 u
->fbuf
->len
= newlen
;
91 if ((size_t) (u
->fbuf
->ptr
- u
->fbuf
->buf
) > u
->fbuf
->act
)
92 u
->fbuf
->act
= u
->fbuf
->ptr
- u
->fbuf
->buf
;
98 fbuf_flush (gfc_unit
* u
, int record_done
)
105 if (u
->fbuf
->act
- u
->fbuf
->flushed
!= 0)
108 nbytes
= u
->fbuf
->act
- u
->fbuf
->flushed
;
110 nbytes
= u
->fbuf
->ptr
- u
->fbuf
->buf
- u
->fbuf
->flushed
;
111 status
= swrite (u
->s
, u
->fbuf
->buf
+ u
->fbuf
->flushed
, &nbytes
);
112 u
->fbuf
->flushed
+= nbytes
;
123 fbuf_seek (gfc_unit
* u
, gfc_offset off
)
125 gfc_offset pos
= u
->fbuf
->ptr
- u
->fbuf
->buf
+ off
;
128 u
->fbuf
->ptr
= u
->fbuf
->buf
+ pos
;
129 if (pos
> u
->fbuf
->act
)