2 Internal file viewer for the Midnight Commander
3 Functions for datasources
5 Copyright (C) 1994-2016
6 Free Software Foundation, Inc.
9 Miguel de Icaza, 1994, 1995, 1998
10 Janne Kukonlehto, 1994, 1995
12 Joseph M. Hinkle, 1996
15 Roland Illig <roland.illig@gmx.de>, 2004, 2005
16 Slava Zanko <slavazanko@google.com>, 2009
17 Andrew Borodin <aborodin@vmail.ru>, 2009
18 Ilia Maslakov <il.smind@gmail.com>, 2009
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 3 of the License,
25 or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU 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, see <http://www.gnu.org/licenses/>.
37 The data source provides the viewer with data from either a file, a
38 string or the output of a command. The mcview_get_byte() function can be
39 used to get the value of a byte at a specific offset. If the offset
40 is out of range, -1 is returned. The function mcview_get_byte_indexed(a,b)
41 returns the byte at the offset a+b, or -1 if a+b is out of range.
43 The mcview_set_byte() function has the effect that later calls to
44 mcview_get_byte() will return the specified byte for this offset. This
45 function is designed only for use by the hexedit component after
46 saving its changes. Inspect the source before you want to use it for
49 The mcview_get_filesize() function returns the current size of the
50 data source. If the growing buffer is used, this size may increase
51 later on. Use the mcview_may_still_grow() function when you want to
52 know if the size can change later.
57 #include "lib/global.h"
58 #include "lib/vfs/vfs.h"
60 #include "lib/widget.h" /* D_NORMAL, D_ERROR */
64 /*** global variables ****************************************************************************/
66 /*** file scope macro definitions ****************************************************************/
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
73 /*** file scope functions ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
77 mcview_set_datasource_stdio_pipe (WView
* view
, mc_pipe_t
* p
)
79 p
->out
.len
= MC_PIPE_BUFSIZE
;
80 p
->out
.null_term
= FALSE
;
81 p
->err
.len
= MC_PIPE_BUFSIZE
;
82 p
->err
.null_term
= TRUE
;
83 view
->datasource
= DS_STDIO_PIPE
;
84 view
->ds_stdio_pipe
= p
;
85 view
->pipe_first_err_msg
= TRUE
;
87 mcview_growbuf_init (view
);
90 /* --------------------------------------------------------------------------------------------- */
91 /*** public functions ****************************************************************************/
92 /* --------------------------------------------------------------------------------------------- */
95 mcview_set_datasource_none (WView
* view
)
97 view
->datasource
= DS_NONE
;
100 /* --------------------------------------------------------------------------------------------- */
103 mcview_get_filesize (WView
* view
)
105 switch (view
->datasource
)
111 return mcview_growbuf_filesize (view
);
113 return view
->ds_file_filesize
;
115 return view
->ds_string_len
;
118 assert (!"Unknown datasource type");
124 /* --------------------------------------------------------------------------------------------- */
127 mcview_update_filesize (WView
* view
)
129 if (view
->datasource
== DS_FILE
)
132 if (mc_fstat (view
->ds_file_fd
, &st
) != -1)
133 view
->ds_file_filesize
= st
.st_size
;
137 /* --------------------------------------------------------------------------------------------- */
140 mcview_get_ptr_file (WView
* view
, off_t byte_index
)
143 assert (view
->datasource
== DS_FILE
);
146 mcview_file_load_data (view
, byte_index
);
147 if (mcview_already_loaded (view
->ds_file_offset
, byte_index
, view
->ds_file_datalen
))
148 return (char *) (view
->ds_file_data
+ (byte_index
- view
->ds_file_offset
));
152 /* --------------------------------------------------------------------------------------------- */
155 mcview_get_utf (WView
* view
, off_t byte_index
, int *ch
, int *ch_len
)
159 gchar utf8buf
[UTF8_CHAR_LEN
+ 1];
161 switch (view
->datasource
)
165 str
= mcview_get_ptr_growing_buffer (view
, byte_index
);
168 str
= mcview_get_ptr_file (view
, byte_index
);
171 str
= mcview_get_ptr_string (view
, byte_index
);
183 res
= g_utf8_get_char_validated (str
, -1);
187 /* Retry with explicit bytes to make sure it's not a buffer boundary */
190 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
192 if (mcview_get_byte (view
, byte_index
+ i
, &res
))
200 utf8buf
[UTF8_CHAR_LEN
] = '\0';
202 res
= g_utf8_get_char_validated (str
, -1);
207 *ch
= (unsigned char) (*str
);
212 gchar
*next_ch
= NULL
;
215 /* Calculate UTF-8 char length */
216 next_ch
= g_utf8_next_char (str
);
217 *ch_len
= next_ch
- str
;
223 /* --------------------------------------------------------------------------------------------- */
226 mcview_get_ptr_string (WView
* view
, off_t byte_index
)
229 assert (view
->datasource
== DS_STRING
);
231 if (byte_index
>= 0 && byte_index
< (off_t
) view
->ds_string_len
)
232 return (char *) (view
->ds_string_data
+ byte_index
);
236 /* --------------------------------------------------------------------------------------------- */
239 mcview_get_byte_string (WView
* view
, off_t byte_index
, int *retval
)
246 p
= mcview_get_ptr_string (view
, byte_index
);
251 *retval
= (unsigned char) (*p
);
255 /* --------------------------------------------------------------------------------------------- */
258 mcview_get_byte_none (WView
* view
, off_t byte_index
, int *retval
)
264 assert (view
->datasource
== DS_NONE
);
272 /* --------------------------------------------------------------------------------------------- */
275 mcview_set_byte (WView
* view
, off_t offset
, byte b
)
278 #ifndef HAVE_ASSERT_H
281 assert (offset
< mcview_get_filesize (view
));
282 assert (view
->datasource
== DS_FILE
);
285 view
->ds_file_datalen
= 0; /* just force reloading */
288 /* --------------------------------------------------------------------------------------------- */
292 mcview_file_load_data (WView
* view
, off_t byte_index
)
299 assert (view
->datasource
== DS_FILE
);
302 if (mcview_already_loaded (view
->ds_file_offset
, byte_index
, view
->ds_file_datalen
))
305 if (byte_index
>= view
->ds_file_filesize
)
308 blockoffset
= mcview_offset_rounddown (byte_index
, view
->ds_file_datasize
);
309 if (mc_lseek (view
->ds_file_fd
, blockoffset
, SEEK_SET
) == -1)
313 while (bytes_read
< view
->ds_file_datasize
)
316 mc_read (view
->ds_file_fd
, view
->ds_file_data
+ bytes_read
,
317 view
->ds_file_datasize
- bytes_read
);
322 bytes_read
+= (size_t) res
;
324 view
->ds_file_offset
= blockoffset
;
325 if ((off_t
) bytes_read
> view
->ds_file_filesize
- view
->ds_file_offset
)
327 /* the file has grown in the meantime -- stick to the old size */
328 view
->ds_file_datalen
= view
->ds_file_filesize
- view
->ds_file_offset
;
332 view
->ds_file_datalen
= bytes_read
;
337 view
->ds_file_datalen
= 0;
340 /* --------------------------------------------------------------------------------------------- */
343 mcview_close_datasource (WView
* view
)
345 switch (view
->datasource
)
350 if (view
->ds_stdio_pipe
!= NULL
)
352 mcview_growbuf_done (view
);
353 mcview_display (view
);
355 mcview_growbuf_free (view
);
358 if (view
->ds_vfs_pipe
!= -1)
359 mcview_growbuf_done (view
);
360 mcview_growbuf_free (view
);
363 (void) mc_close (view
->ds_file_fd
);
364 view
->ds_file_fd
= -1;
365 MC_PTR_FREE (view
->ds_file_data
);
368 MC_PTR_FREE (view
->ds_string_data
);
372 assert (!"Unknown datasource type")
376 view
->datasource
= DS_NONE
;
379 /* --------------------------------------------------------------------------------------------- */
382 mcview_set_datasource_file (WView
* view
, int fd
, const struct stat
*st
)
384 view
->datasource
= DS_FILE
;
385 view
->ds_file_fd
= fd
;
386 view
->ds_file_filesize
= st
->st_size
;
387 view
->ds_file_offset
= 0;
388 view
->ds_file_data
= g_malloc (4096);
389 view
->ds_file_datalen
= 0;
390 view
->ds_file_datasize
= 4096;
393 /* --------------------------------------------------------------------------------------------- */
396 mcview_load_command_output (WView
* view
, const char *command
)
399 GError
*error
= NULL
;
401 mcview_close_datasource (view
);
403 p
= mc_popen (command
, &error
);
406 mcview_display (view
);
407 mcview_show_error (view
, error
->message
);
408 g_error_free (error
);
412 /* Check if filter produced any output */
413 mcview_set_datasource_stdio_pipe (view
, p
);
414 if (!mcview_get_byte (view
, 0, NULL
))
416 mcview_close_datasource (view
);
417 mcview_display (view
);
424 /* --------------------------------------------------------------------------------------------- */
427 mcview_set_datasource_vfs_pipe (WView
* view
, int fd
)
432 view
->datasource
= DS_VFS_PIPE
;
433 view
->ds_vfs_pipe
= fd
;
435 mcview_growbuf_init (view
);
438 /* --------------------------------------------------------------------------------------------- */
441 mcview_set_datasource_string (WView
* view
, const char *s
)
443 view
->datasource
= DS_STRING
;
444 view
->ds_string_len
= strlen (s
);
445 view
->ds_string_data
= (byte
*) g_strndup (s
, view
->ds_string_len
);
448 /* --------------------------------------------------------------------------------------------- */