Change prototype of mcview_get_byte() function (and al related functions)

from

int mcview_get_byte (mcview_t *, off_t)

into

gboolean mcview_get_byte (mcview_t, off_t, int *)

Now splitted return code(success/fail) and readed value from datasource value.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2009-08-25 10:32:47 +03:00
parent dc15da9572
commit 459ef0665f
11 changed files with 91 additions and 68 deletions

View File

@ -91,7 +91,7 @@ static cb_ret_t
mcview_handle_editkey (mcview_t * view, int key)
{
struct hexedit_change_node *node;
byte byte_val;
int byte_val;
/* Has there been a change at this position? */
node = view->change_list;
@ -114,7 +114,7 @@ mcview_handle_editkey (mcview_t * view, int key)
if (node)
byte_val = node->value;
else
byte_val = mcview_get_byte (view, view->hex_cursor);
mcview_get_byte (view, view->hex_cursor, &byte_val);
if (view->hexedit_lownibble) {
byte_val = (byte_val & 0xf0) | (hexvalue);
@ -548,7 +548,7 @@ mcview_moveto_addr_cmd (mcview_t * view)
if (line != NULL) {
if (*line != '\0') {
addr = strtoul (line, &error, 0);
if ((*error == '\0') && mcview_get_byte (view, addr) != -1) {
if ((*error == '\0') && mcview_get_byte (view, addr, NULL) == TRUE) {
mcview_moveto_offset (view, addr);
} else {
message (D_ERROR, _("Warning"), _(" Invalid address "));

View File

@ -234,7 +234,7 @@ mcview_ccache_lookup (mcview_t * view, struct coord_cache_entry *coord,
for (; current.cc_offset < limit; current = next) {
int c, nextc;
if ((c = mcview_get_byte (view, current.cc_offset)) == -1)
if (! mcview_get_byte (view, current.cc_offset, &c))
break;
if (!mcview_coord_cache_entry_less (&current, coord, sorter, view->text_nroff_mode)) {
@ -253,7 +253,7 @@ mcview_ccache_lookup (mcview_t * view, struct coord_cache_entry *coord,
/* and override some of them as necessary. */
if (c == '\r') {
nextc = mcview_get_byte_indexed (view, current.cc_offset, 1);
mcview_get_byte_indexed (view, current.cc_offset, 1, &nextc);
/* Ignore '\r' if it is followed by '\r' or '\n'. If it is
* followed by anything else, it is a Mac line ending and

View File

@ -193,24 +193,31 @@ mcview_get_utf (mcview_t * view, off_t byte_index, int *char_width, gboolean * r
/* --------------------------------------------------------------------------------------------- */
int
mcview_get_byte_string (mcview_t * view, off_t byte_index)
gboolean
mcview_get_byte_string (mcview_t * view, off_t byte_index, int *retval)
{
assert (view->datasource == DS_STRING);
if (byte_index < view->ds_string_len)
return view->ds_string_data[byte_index];
return -1;
if (byte_index < view->ds_string_len) {
if (retval)
*retval = view->ds_string_data[byte_index];
return TRUE;
}
if (retval)
*retval = -1;
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
int
mcview_get_byte_none (mcview_t * view, off_t byte_index)
gboolean
mcview_get_byte_none (mcview_t * view, off_t byte_index, int *retval)
{
assert (view->datasource == DS_NONE);
(void) &view;
(void) byte_index;
return -1;
if (retval)
*retval = -1;
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
@ -344,7 +351,7 @@ mcview_load_command_output (mcview_t * view, const char *command)
/* First, check if filter produced any output */
mcview_set_datasource_stdio_pipe (view, fp);
if (mcview_get_byte (view, 0) == -1) {
if (! mcview_get_byte (view, 0, NULL)) {
mcview_close_datasource (view);
/* Avoid two messages. Message from stderr has priority. */

View File

@ -165,25 +165,33 @@ mcview_growbuf_read_until (mcview_t * view, off_t ofs)
/* --------------------------------------------------------------------------------------------- */
int
mcview_get_byte_growing_buffer (mcview_t * view, off_t byte_index)
gboolean
mcview_get_byte_growing_buffer (mcview_t * view, off_t byte_index, int *retval)
{
if (retval)
*retval = -1;
off_t pageno = byte_index / VIEW_PAGE_SIZE;
off_t pageindex = byte_index % VIEW_PAGE_SIZE;
assert (view->growbuf_in_use);
if ((size_t) pageno != pageno)
return -1;
return FALSE;
mcview_growbuf_read_until (view, byte_index + 1);
if (view->growbuf_blocks == 0)
return -1;
if (pageno < view->growbuf_blocks - 1)
return view->growbuf_blockptr[pageno][pageindex];
if (pageno == view->growbuf_blocks - 1 && pageindex < view->growbuf_lastindex)
return view->growbuf_blockptr[pageno][pageindex];
return -1;
return FALSE;
if (pageno < view->growbuf_blocks - 1) {
if (retval)
*retval = view->growbuf_blockptr[pageno][pageindex];
return TRUE;
}
if (pageno == view->growbuf_blocks - 1 && pageindex < view->growbuf_lastindex) {
if (retval)
*retval = view->growbuf_blockptr[pageno][pageindex];
return TRUE;
}
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -107,7 +107,7 @@ mcview_display_hex (mcview_t * view)
curr = curr->next;
}
for (row = 0; mcview_get_byte (view, from) != -1 && row < height; row++) {
for (row = 0; mcview_get_byte (view, from, NULL) == TRUE && row < height; row++) {
col = 0;
/* Print the hex offset */
@ -131,7 +131,7 @@ mcview_display_hex (mcview_t * view)
break;
}
#endif
if ((c = mcview_get_byte (view, from)) == -1)
if (! mcview_get_byte (view, from, &c))
break;
/* Save the cursor position for mcview_place_cursor() */

View File

@ -75,32 +75,37 @@ mcview_already_loaded (off_t offset, off_t idx, size_t size)
/* --------------------------------------------------------------------------------------------- */
static inline int
mcview_get_byte_file (mcview_t * view, off_t byte_index)
static inline gboolean
mcview_get_byte_file (mcview_t * view, off_t byte_index, int *retval)
{
assert (view->datasource == DS_FILE);
mcview_file_load_data (view, byte_index);
if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
return view->ds_file_data[byte_index - view->ds_file_offset];
return -1;
if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) {
if (retval)
*retval = view->ds_file_data[byte_index - view->ds_file_offset];
return TRUE;
}
if (retval)
*retval = -1;
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
static inline int
mcview_get_byte (mcview_t * view, off_t offset)
static inline gboolean
mcview_get_byte (mcview_t * view, off_t offset, int *retval)
{
switch (view->datasource) {
case DS_STDIO_PIPE:
case DS_VFS_PIPE:
return mcview_get_byte_growing_buffer (view, offset);
return mcview_get_byte_growing_buffer (view, offset, retval);
case DS_FILE:
return mcview_get_byte_file (view, offset);
return mcview_get_byte_file (view, offset, retval);
case DS_STRING:
return mcview_get_byte_string (view, offset);
return mcview_get_byte_string (view, offset, retval);
case DS_NONE:
return mcview_get_byte_none (view, offset);
return mcview_get_byte_none (view, offset, retval);
}
assert (!"Unknown datasource type");
return -1;
@ -108,12 +113,15 @@ mcview_get_byte (mcview_t * view, off_t offset)
/* --------------------------------------------------------------------------------------------- */
static inline int
mcview_get_byte_indexed (mcview_t * view, off_t base, off_t ofs)
static inline gboolean
mcview_get_byte_indexed (mcview_t * view, off_t base, off_t ofs, int *retval)
{
if (base <= OFFSETTYPE_MAX - ofs)
return mcview_get_byte (view, base + ofs);
return -1;
if (base <= OFFSETTYPE_MAX - ofs) {
return mcview_get_byte (view, base + ofs, retval);
}
if (retval)
*retval = -1;
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
@ -122,7 +130,9 @@ static inline int
mcview_count_backspaces (mcview_t * view, off_t offset)
{
int backspaces = 0;
while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces) == '\b')
int c;
while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
&& c == '\b')
backspaces++;
return backspaces;
}
@ -136,16 +146,13 @@ mcview_is_nroff_sequence (mcview_t * view, off_t offset)
/* The following commands are ordered to speed up the calculation. */
c1 = mcview_get_byte_indexed (view, offset, 1);
if (c1 == -1 || c1 != '\b')
if (! mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
return FALSE;
c0 = mcview_get_byte_indexed (view, offset, 0);
if (c0 == -1 || !g_ascii_isprint (c0))
if (! mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
return FALSE;
c2 = mcview_get_byte_indexed (view, offset, 2);
if (c2 == -1 || !g_ascii_isprint (c2))
if (! mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
return FALSE;
return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));

View File

@ -226,8 +226,8 @@ off_t mcview_get_filesize (mcview_t *);
char *mcview_get_ptr_file (mcview_t *, off_t);
char *mcview_get_ptr_string (mcview_t *, off_t);
int mcview_get_utf (mcview_t *, off_t, int *, gboolean *);
int mcview_get_byte_string (mcview_t *, off_t);
int mcview_get_byte_none (mcview_t *, off_t);
gboolean mcview_get_byte_string (mcview_t *, off_t, int *);
gboolean mcview_get_byte_none (mcview_t *, off_t, int *);
void mcview_set_byte (mcview_t *, off_t, byte);
void mcview_file_load_data (mcview_t *, off_t);
void mcview_close_datasource (mcview_t *);
@ -255,7 +255,7 @@ void mcview_growbuf_init (mcview_t *);
void mcview_growbuf_free (mcview_t *);
off_t mcview_growbuf_filesize (mcview_t *);
void mcview_growbuf_read_until (mcview_t *, off_t);
int mcview_get_byte_growing_buffer (mcview_t *, off_t);
gboolean mcview_get_byte_growing_buffer (mcview_t *, off_t, int *);
char *mcview_get_ptr_growing_buffer (mcview_t *, off_t);
/* hex.c: */

View File

@ -154,6 +154,7 @@ mcview_move_down (mcview_t * view, off_t lines)
} else if (view->text_wrap_mode) {
off_t line, col, i;
int c;
for (i = 0; i < lines; i++) {
off_t new_offset, chk_line, chk_col;
@ -165,7 +166,8 @@ mcview_move_down (mcview_t * view, off_t lines)
/* skip to the next line if the only thing that would be
* displayed is the newline character. */
mcview_offset_to_coord (view, &chk_line, &chk_col, new_offset);
if (chk_line == line && chk_col == col && mcview_get_byte (view, new_offset) == '\n')
if (chk_line == line && chk_col == col && mcview_get_byte (view, new_offset, &c) == TRUE
&& c == '\n')
new_offset++;
view->dpy_start = new_offset;
@ -321,7 +323,7 @@ mcview_moveto_eol (mcview_t * view)
off_t filesize, bol;
bol = mcview_offset_rounddown (view->hex_cursor, view->bytes_per_line);
if (mcview_get_byte_indexed (view, bol, view->bytes_per_line - 1) != -1) {
if (mcview_get_byte_indexed (view, bol, view->bytes_per_line - 1, NULL) == TRUE) {
view->hex_cursor = bol + view->bytes_per_line - 1;
} else {
filesize = mcview_get_filesize (view);

View File

@ -94,7 +94,7 @@ mcview_display_nroff (mcview_t * view)
} else
#endif
{
if ((c = mcview_get_byte (view, from)) == -1)
if (! mcview_get_byte (view, from, &c))
break;
}
from++;
@ -103,8 +103,8 @@ mcview_display_nroff (mcview_t * view)
if (c == '\b') {
if (from > 1) {
c_prev = mcview_get_byte (view, from - 2);
c_next = mcview_get_byte (view, from);
mcview_get_byte (view, from - 2, &c_prev);
mcview_get_byte (view, from, &c_next);
}
if (g_ascii_isprint (c_prev) && g_ascii_isprint (c_prev)
&& (c_prev == c_next || c_prev == '_' || (c_prev == '+' && c_next == 'o'))) {
@ -136,7 +136,7 @@ mcview_display_nroff (mcview_t * view)
}
if (c == '\r') {
c = mcview_get_byte_indexed (view, from, 1);
mcview_get_byte_indexed (view, from, 1, &c);
if (c == '\r' || c == '\n')
continue;
col = 0;
@ -258,19 +258,17 @@ mcview_nroff_seq_info (mcview_nroff_t * nroff)
return NROFF_TYPE_NONE;
nroff->type = NROFF_TYPE_NONE;
nroff->current_char = mcview_get_byte (nroff->view, nroff->index);
if (nroff->current_char == -1 || !g_ascii_isprint (nroff->current_char)) /* FIXME: utf-8 and g_ascii_isprint */
if (! mcview_get_byte (nroff->view, nroff->index, &nroff->current_char)
|| !g_ascii_isprint (nroff->current_char)) /* FIXME: utf-8 and g_ascii_isprint */
return nroff->type;
nroff->char_width = 1;
next = mcview_get_byte (nroff->view, nroff->index + 1);
if (next == -1 || next != '\b')
if (! mcview_get_byte (nroff->view, nroff->index + 1, &next) || next != '\b')
return nroff->type;
next2 = mcview_get_byte (nroff->view, nroff->index + 2);
if (next2 == -1 || !g_ascii_isprint (next2)) /* FIXME: utf-8 and g_ascii_isprint */
if (! mcview_get_byte (nroff->view, nroff->index + 2, &next2)
|| !g_ascii_isprint (next2)) /* FIXME: utf-8 and g_ascii_isprint */
return nroff->type;
if (nroff->current_char == '_' && next2 == '_') {

View File

@ -91,7 +91,7 @@ mcview_display_text (mcview_t * view)
} else
#endif
{
if ((c = mcview_get_byte (view, from)) == -1)
if (! mcview_get_byte (view, from, &c))
break;
}
from++;
@ -106,7 +106,9 @@ mcview_display_text (mcview_t * view)
}
if (c == '\r') {
c = mcview_get_byte_indexed (view, from, 1);
if (! mcview_get_byte_indexed (view, from, 1, &c))
break;
if (c == '\r' || c == '\n')
continue;
col = 0;

View File

@ -118,8 +118,7 @@ mcview_search_cmd_callback (const void *user_data, gsize char_offset)
/* view_read_continue (view, &view->search_onechar_info); *//* AB:FIXME */
if (!view->text_nroff_mode) {
byte = mcview_get_byte (view, char_offset);
if (byte == -1)
if (! mcview_get_byte (view, char_offset, &byte))
return MC_SEARCH_CB_ABORT;
return byte;