Add fbuf.c
[gcc.git] / libgfortran / io / fbuf.c
1 /* Copyright (C) 2008 Free Software Foundation, Inc.
2 Contributed by Janne Blomqvist
3
4 This file is part of the GNU Fortran runtime library (libgfortran).
5
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.
10
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.)
19
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.
24
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. */
29
30
31 #include "io.h"
32 #include <string.h>
33 #include <stdlib.h>
34
35
36 void
37 fbuf_init (gfc_unit * u, size_t len)
38 {
39 if (len == 0)
40 len = 4096; /* Default size one page. */
41
42 u->fbuf = get_mem (sizeof (fbuf));
43 u->fbuf->buf = u->fbuf->ptr = get_mem (len);
44 u->fbuf->len = len;
45 u->fbuf->act = u->fbuf->flushed = 0;
46 }
47
48
49 void
50 fbuf_reset (gfc_unit * u)
51 {
52 u->fbuf->act = u->fbuf->flushed = 0;
53 u->fbuf->ptr = u->fbuf->buf;
54 }
55
56
57 void
58 fbuf_destroy (gfc_unit * u)
59 {
60 if (u->fbuf == NULL)
61 return;
62 if (u->fbuf->buf)
63 free_mem (u->fbuf->buf);
64 free_mem (u->fbuf);
65 }
66
67
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. */
71
72 char *
73 fbuf_alloc (gfc_unit * u, size_t len)
74 {
75 size_t newlen, ptrpos;
76 char *dest;
77 if (u->fbuf->ptr + len > u->fbuf->buf + u->fbuf->len)
78 {
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);
83 if (dest == NULL)
84 return NULL;
85 u->fbuf->buf = dest;
86 u->fbuf->ptr = dest + ptrpos;
87 u->fbuf->len = newlen;
88 }
89 dest = u->fbuf->ptr;
90 u->fbuf->ptr += len;
91 if ((size_t) (u->fbuf->ptr - u->fbuf->buf) > u->fbuf->act)
92 u->fbuf->act = u->fbuf->ptr - u->fbuf->buf;
93 return dest;
94 }
95
96
97 int
98 fbuf_flush (gfc_unit * u, int record_done)
99 {
100 int status;
101 size_t nbytes;
102
103 if (!u->fbuf)
104 return 0;
105 if (u->fbuf->act - u->fbuf->flushed != 0)
106 {
107 if (record_done)
108 nbytes = u->fbuf->act - u->fbuf->flushed;
109 else
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;
113 }
114 else
115 status = 0;
116 if (record_done)
117 fbuf_reset (u);
118 return status;
119 }
120
121
122 int
123 fbuf_seek (gfc_unit * u, gfc_offset off)
124 {
125 gfc_offset pos = u->fbuf->ptr - u->fbuf->buf + off;
126 if (pos < 0)
127 return -1;
128 u->fbuf->ptr = u->fbuf->buf + pos;
129 if (pos > u->fbuf->act)
130 u->fbuf->act = pos;
131 return 0;
132 }