From 459ef0665fcab3ebb97f2c7f63301253f8a8efc7 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Tue, 25 Aug 2009 10:32:47 +0300 Subject: [PATCH] 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 --- src/viewer/actions_cmd.c | 6 ++--- src/viewer/coord_cache.c | 4 +-- src/viewer/datasource.c | 25 ++++++++++++------- src/viewer/growbuf.c | 26 +++++++++++++------- src/viewer/hex.c | 4 +-- src/viewer/inlines.h | 53 +++++++++++++++++++++++----------------- src/viewer/internal.h | 6 ++--- src/viewer/move.c | 6 +++-- src/viewer/nroff.c | 20 +++++++-------- src/viewer/plain.c | 6 +++-- src/viewer/search.c | 3 +-- 11 files changed, 91 insertions(+), 68 deletions(-) diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index f14aac992..7a0fe584a 100644 --- a/src/viewer/actions_cmd.c +++ b/src/viewer/actions_cmd.c @@ -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 ")); diff --git a/src/viewer/coord_cache.c b/src/viewer/coord_cache.c index 13bdac553..66b8ad522 100644 --- a/src/viewer/coord_cache.c +++ b/src/viewer/coord_cache.c @@ -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 (¤t, 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 diff --git a/src/viewer/datasource.c b/src/viewer/datasource.c index ad62281e4..2a3fea4c1 100644 --- a/src/viewer/datasource.c +++ b/src/viewer/datasource.c @@ -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. */ diff --git a/src/viewer/growbuf.c b/src/viewer/growbuf.c index bd864cae7..c4f1d1f82 100644 --- a/src/viewer/growbuf.c +++ b/src/viewer/growbuf.c @@ -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; } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/viewer/hex.c b/src/viewer/hex.c index a2b22ca25..acaac289f 100644 --- a/src/viewer/hex.c +++ b/src/viewer/hex.c @@ -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() */ diff --git a/src/viewer/inlines.h b/src/viewer/inlines.h index 6cc72f848..224f8e632 100644 --- a/src/viewer/inlines.h +++ b/src/viewer/inlines.h @@ -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')); diff --git a/src/viewer/internal.h b/src/viewer/internal.h index c2ebafa46..c8cfe3c6d 100644 --- a/src/viewer/internal.h +++ b/src/viewer/internal.h @@ -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: */ diff --git a/src/viewer/move.c b/src/viewer/move.c index e0ba72a49..96455e84f 100644 --- a/src/viewer/move.c +++ b/src/viewer/move.c @@ -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); diff --git a/src/viewer/nroff.c b/src/viewer/nroff.c index bdc2cba64..4892c6cbe 100644 --- a/src/viewer/nroff.c +++ b/src/viewer/nroff.c @@ -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 == '_') { diff --git a/src/viewer/plain.c b/src/viewer/plain.c index 61ef402e6..40d400741 100644 --- a/src/viewer/plain.c +++ b/src/viewer/plain.c @@ -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; diff --git a/src/viewer/search.c b/src/viewer/search.c index 2034ab079..6129d3d4d 100644 --- a/src/viewer/search.c +++ b/src/viewer/search.c @@ -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;