2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / io / fbuf.c
bloba0d033bf8758f754e93d1e8ae8dcb92512232015
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)
9 any later version.
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
18 executable.)
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. */
31 #include "io.h"
32 #include <string.h>
33 #include <stdlib.h>
36 void
37 fbuf_init (gfc_unit * u, size_t len)
39 if (len == 0)
40 len = 512; /* Default size. */
42 u->fbuf = get_mem (sizeof (fbuf));
43 u->fbuf->buf = get_mem (len);
44 u->fbuf->len = len;
45 u->fbuf->act = u->fbuf->flushed = u->fbuf->pos = 0;
49 void
50 fbuf_reset (gfc_unit * u)
52 u->fbuf->act = u->fbuf->flushed = u->fbuf->pos = 0;
56 void
57 fbuf_destroy (gfc_unit * u)
59 if (u->fbuf == NULL)
60 return;
61 if (u->fbuf->buf)
62 free_mem (u->fbuf->buf);
63 free_mem (u->fbuf);
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
75 buffer. */
77 char *
78 fbuf_alloc (gfc_unit * u, size_t len)
80 size_t newlen;
81 char *dest;
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);
89 if (dest == NULL)
90 return NULL;
91 u->fbuf->buf = dest;
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;
102 else
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;
113 u->fbuf->buf = dest;
114 u->fbuf->len = newlen;
118 dest = u->fbuf->buf + u->fbuf->pos;
119 u->fbuf->pos += len;
120 if (u->fbuf->pos > u->fbuf->act)
121 u->fbuf->act = u->fbuf->pos;
122 return dest;
129 fbuf_flush (gfc_unit * u, int record_done)
131 int status;
132 size_t nbytes;
134 if (!u->fbuf)
135 return 0;
136 if (u->fbuf->act - u->fbuf->flushed != 0)
138 if (record_done)
139 nbytes = u->fbuf->act - u->fbuf->flushed;
140 else
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;
145 else
146 status = 0;
147 if (record_done)
148 fbuf_reset (u);
149 return status;
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 < u->fbuf->flushed)
161 return -1;
162 u->fbuf->pos = pos;
163 return 0;