x86_64: Cleanup of code for master
[midnight-commander.git] / src / viewer / growbuf.c
blob8709594dd445032895aee4b6a10843a4edc93659
1 /*
2 Internal file viewer for the Midnight Commander
3 Function for work with growing bufers
5 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
6 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
8 Written by: 1994, 1995, 1998 Miguel de Icaza
9 1994, 1995 Janne Kukonlehto
10 1995 Jakub Jelinek
11 1996 Joseph M. Hinkle
12 1997 Norbert Warmuth
13 1998 Pavel Machek
14 2004 Roland Illig <roland.illig@gmx.de>
15 2005 Roland Illig <roland.illig@gmx.de>
16 2009 Slava Zanko <slavazanko@google.com>
17 2009 Andrew Borodin <aborodin@vmail.ru>
18 2009 Ilia Maslakov <il.smind@gmail.com>
20 This file is part of the Midnight Commander.
22 The Midnight Commander is free software; you can redistribute it
23 and/or modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be
28 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
35 MA 02110-1301, USA.
38 #include <config.h>
39 #include <errno.h>
41 #include "lib/global.h"
42 #include "src/wtools.h"
44 #include "lib/vfs/mc-vfs/vfs.h"
46 #include "internal.h"
48 /* Block size for reading files in parts */
49 #define VIEW_PAGE_SIZE ((size_t) 8192)
51 /*** global variables ****************************************************************************/
53 /*** file scope macro definitions ****************************************************************/
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 /*** file scope functions ************************************************************************/
61 /*** public functions ****************************************************************************/
63 /* --------------------------------------------------------------------------------------------- */
65 void
66 mcview_growbuf_init (mcview_t * view)
68 view->growbuf_in_use = TRUE;
69 view->growbuf_blockptr = g_ptr_array_new ();
70 view->growbuf_lastindex = VIEW_PAGE_SIZE;
71 view->growbuf_finished = FALSE;
74 /* --------------------------------------------------------------------------------------------- */
76 void
77 mcview_growbuf_free (mcview_t * view)
79 assert (view->growbuf_in_use);
81 g_ptr_array_foreach (view->growbuf_blockptr, (GFunc) g_free, NULL);
83 (void) g_ptr_array_free (view->growbuf_blockptr, TRUE);
85 view->growbuf_blockptr = NULL;
86 view->growbuf_in_use = FALSE;
89 /* --------------------------------------------------------------------------------------------- */
91 off_t
92 mcview_growbuf_filesize (mcview_t * view)
94 assert (view->growbuf_in_use);
96 if (view->growbuf_blockptr->len == 0)
97 return 0;
98 else
99 return ((off_t) view->growbuf_blockptr->len - 1) * VIEW_PAGE_SIZE + view->growbuf_lastindex;
102 /* --------------------------------------------------------------------------------------------- */
104 /* Copies the output from the pipe to the growing buffer, until either
105 * the end-of-pipe is reached or the interval [0..ofs) of the growing
106 * buffer is completely filled. */
107 void
108 mcview_growbuf_read_until (mcview_t * view, off_t ofs)
110 ssize_t nread;
111 byte *p;
112 size_t bytesfree;
113 gboolean short_read;
115 assert (view->growbuf_in_use);
117 if (view->growbuf_finished)
118 return;
120 short_read = FALSE;
121 while (mcview_growbuf_filesize (view) < ofs || short_read) {
122 if (view->growbuf_lastindex == VIEW_PAGE_SIZE) {
123 /* Append a new block to the growing buffer */
124 byte *newblock = g_try_malloc (VIEW_PAGE_SIZE);
125 if (newblock == NULL)
126 return;
128 g_ptr_array_add (view->growbuf_blockptr, newblock);
129 view->growbuf_lastindex = 0;
131 p = g_ptr_array_index(
132 view->growbuf_blockptr,
133 view->growbuf_blockptr->len - 1)
134 + view->growbuf_lastindex;
136 bytesfree = VIEW_PAGE_SIZE - view->growbuf_lastindex;
138 if (view->datasource == DS_STDIO_PIPE) {
139 nread = fread (p, 1, bytesfree, view->ds_stdio_pipe);
140 if (nread == 0) {
141 view->growbuf_finished = TRUE;
142 (void) pclose (view->ds_stdio_pipe);
143 mcview_display (view);
144 close_error_pipe (D_NORMAL, NULL);
145 view->ds_stdio_pipe = NULL;
146 return;
148 } else {
149 assert (view->datasource == DS_VFS_PIPE);
150 do {
151 nread = mc_read (view->ds_vfs_pipe, p, bytesfree);
152 } while (nread == -1 && errno == EINTR);
153 if (nread == -1 || nread == 0) {
154 view->growbuf_finished = TRUE;
155 (void) mc_close (view->ds_vfs_pipe);
156 view->ds_vfs_pipe = -1;
157 return;
160 short_read = ((size_t) nread < bytesfree);
161 view->growbuf_lastindex += nread;
165 /* --------------------------------------------------------------------------------------------- */
167 gboolean
168 mcview_get_byte_growing_buffer (mcview_t * view, off_t byte_index, int *retval)
170 off_t pageno;
171 off_t pageindex;
173 if (retval)
174 *retval = -1;
176 pageno = byte_index / VIEW_PAGE_SIZE;
177 pageindex = byte_index % VIEW_PAGE_SIZE;
179 assert (view->growbuf_in_use);
181 if (pageno < 0)
182 return FALSE;
184 mcview_growbuf_read_until (view, byte_index + 1);
185 if (view->growbuf_blockptr->len == 0)
186 return FALSE;
187 if (pageno < view->growbuf_blockptr->len - 1) {
188 if (retval)
189 *retval = *((byte *) (g_ptr_array_index(view->growbuf_blockptr, pageno) + pageindex));
190 return TRUE;
192 if (pageno == view->growbuf_blockptr->len - 1 && pageindex < (off_t) view->growbuf_lastindex) {
193 if (retval)
194 *retval = *((byte *) (g_ptr_array_index(view->growbuf_blockptr, pageno) + pageindex));
195 return TRUE;
197 return FALSE;
200 /* --------------------------------------------------------------------------------------------- */
202 char *
203 mcview_get_ptr_growing_buffer (mcview_t * view, off_t byte_index)
205 off_t pageno = byte_index / VIEW_PAGE_SIZE;
206 off_t pageindex = byte_index % VIEW_PAGE_SIZE;
208 assert (view->growbuf_in_use);
210 if (pageno < 0)
211 return NULL;
213 mcview_growbuf_read_until (view, byte_index + 1);
214 if (view->growbuf_blockptr->len == 0)
215 return NULL;
216 if (pageno < view->growbuf_blockptr->len - 1)
217 return (char *) (g_ptr_array_index(view->growbuf_blockptr, pageno) + pageindex);
218 if (pageno == view->growbuf_blockptr->len - 1 && pageindex < (off_t) view->growbuf_lastindex)
219 return (char *) (g_ptr_array_index(view->growbuf_blockptr, pageno) + pageindex);
220 return NULL;
223 /* --------------------------------------------------------------------------------------------- */