]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/io/fbuf.c
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
= 512; /* Default size. */
42 u
->fbuf
= get_mem (sizeof (fbuf
));
43 u
->fbuf
->buf
= get_mem (len
);
45 u
->fbuf
->act
= u
->fbuf
->flushed
= u
->fbuf
->pos
= 0;
50 fbuf_reset (gfc_unit
* u
)
52 u
->fbuf
->act
= u
->fbuf
->flushed
= u
->fbuf
->pos
= 0;
57 fbuf_destroy (gfc_unit
* u
)
62 free_mem (u
->fbuf
->buf
);
67 /* Return a pointer to the current position in the buffer, and increase
68 the pointer by len. Makes sure that the buffer is big enough,
69 reallocating if necessary. If the buffer is not big enough, there are
70 three cases to consider:
71 1. If we haven't flushed anything, realloc
72 2. If we have flushed enough that by discarding the flushed bytes
73 the request fits into the buffer, do that.
74 3. Else allocate a new buffer, memcpy unflushed active bytes from old
78 fbuf_alloc (gfc_unit
* u
, size_t len
)
82 if (u
->fbuf
->pos
+ len
> u
->fbuf
->len
)
84 if (u
->fbuf
->flushed
== 0)
86 /* Round up to nearest multiple of the current buffer length. */
87 newlen
= ((u
->fbuf
->pos
+ len
) / u
->fbuf
->len
+ 1) * u
->fbuf
->len
;
88 dest
= realloc (u
->fbuf
->buf
, newlen
);
92 u
->fbuf
->len
= newlen
;
94 else if (u
->fbuf
->act
- u
->fbuf
->flushed
+ len
< u
->fbuf
->len
)
96 memmove (u
->fbuf
->buf
, u
->fbuf
->buf
+ u
->fbuf
->flushed
,
97 u
->fbuf
->act
- u
->fbuf
->flushed
);
98 u
->fbuf
->act
-= u
->fbuf
->flushed
;
99 u
->fbuf
->pos
-= u
->fbuf
->flushed
;
100 u
->fbuf
->flushed
= 0;
104 /* Most general case, flushed != 0, request doesn't fit. */
105 newlen
= ((u
->fbuf
->pos
- u
->fbuf
->flushed
+ len
)
106 / u
->fbuf
->len
+ 1) * u
->fbuf
->len
;
107 dest
= get_mem (newlen
);
108 memcpy (dest
, u
->fbuf
->buf
+ u
->fbuf
->flushed
,
109 u
->fbuf
->act
- u
->fbuf
->flushed
);
110 u
->fbuf
->act
-= u
->fbuf
->flushed
;
111 u
->fbuf
->pos
-= u
->fbuf
->flushed
;
112 u
->fbuf
->flushed
= 0;
114 u
->fbuf
->len
= newlen
;
118 dest
= u
->fbuf
->buf
+ u
->fbuf
->pos
;
120 if (u
->fbuf
->pos
> u
->fbuf
->act
)
121 u
->fbuf
->act
= u
->fbuf
->pos
;
129 fbuf_flush (gfc_unit
* u
, int record_done
)
136 if (u
->fbuf
->act
- u
->fbuf
->flushed
!= 0)
139 nbytes
= u
->fbuf
->act
- u
->fbuf
->flushed
;
141 nbytes
= u
->fbuf
->pos
- u
->fbuf
->flushed
;
142 status
= swrite (u
->s
, u
->fbuf
->buf
+ u
->fbuf
->flushed
, &nbytes
);
143 u
->fbuf
->flushed
+= nbytes
;
154 fbuf_seek (gfc_unit
* u
, gfc_offset off
)
156 gfc_offset pos
= u
->fbuf
->pos
+ off
;
157 /* Moving to the left past the flushed marked would imply moving past
158 the left tab limit, which is never allowed. So return error if
159 that is attempted. */
160 if (pos
< (gfc_offset
) u
->fbuf
->flushed
)