root/src/viewer/inlines.h

/* [previous][next][first][last][top][bottom][index][help]  */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. mcview_offset_doz
  2. mcview_offset_rounddown
  3. mcview_dimen_doz
  4. mcview_is_in_panel
  5. mcview_may_still_grow
  6. mcview_already_loaded
  7. mcview_get_byte_file
  8. mcview_get_byte
  9. mcview_get_byte_indexed
  10. mcview_count_backspaces
  11. mcview_is_nroff_sequence
  12. mcview_growbuf_read_all_data

   1 #ifndef MC__VIEWER_INLINES_H
   2 #define MC__VIEWER_INLINES_H
   3 
   4 #include <limits.h>             /* CHAR_BIT */
   5 
   6 /*** typedefs(not structures) and defined constants **********************************************/
   7 
   8 #define OFF_T_BITWIDTH  ((unsigned int) (sizeof (off_t) * CHAR_BIT - 1))
   9 #define OFFSETTYPE_MAX (((off_t) 1 << (OFF_T_BITWIDTH - 1)) - 1)
  10 
  11 /*** enums ***************************************************************************************/
  12 
  13 /*** structures declarations (and typedefs of structures)*****************************************/
  14 
  15 /*** global variables defined in .c file *********************************************************/
  16 
  17 /*** declarations of public functions ************************************************************/
  18 
  19 /*** inline functions ****************************************************************************/
  20 
  21 /* difference or zero */
  22 static inline off_t
  23 mcview_offset_doz (off_t a, off_t b)
     /* [previous][next][first][last][top][bottom][index][help]  */
  24 {
  25     return (a >= b) ? a - b : 0;
  26 }
  27 
  28 /* --------------------------------------------------------------------------------------------- */
  29 
  30 static inline off_t
  31 mcview_offset_rounddown (off_t a, off_t b)
     /* [previous][next][first][last][top][bottom][index][help]  */
  32 {
  33     g_assert (b != 0);
  34     return a - a % b;
  35 }
  36 
  37 /* --------------------------------------------------------------------------------------------- */
  38 
  39 /* difference or zero */
  40 static inline screen_dimen
  41 mcview_dimen_doz (screen_dimen a, screen_dimen b)
     /* [previous][next][first][last][top][bottom][index][help]  */
  42 {
  43     return (a >= b) ? a - b : 0;
  44 }
  45 
  46 /* --------------------------------------------------------------------------------------------- */
  47 
  48 /* {{{ Simple Primitive Functions for WView }}} */
  49 static inline gboolean
  50 mcview_is_in_panel (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
  51 {
  52     return (view->dpy_frame_size != 0);
  53 }
  54 
  55 /* --------------------------------------------------------------------------------------------- */
  56 
  57 static inline gboolean
  58 mcview_may_still_grow (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
  59 {
  60     return (view->growbuf_in_use && !view->growbuf_finished);
  61 }
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 
  65 /* returns TRUE if the idx lies in the half-open interval
  66  * [offset; offset + size), FALSE otherwise.
  67  */
  68 static inline gboolean
  69 mcview_already_loaded (off_t offset, off_t idx, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
  70 {
  71     return (offset <= idx && idx - offset < (off_t) size);
  72 }
  73 
  74 /* --------------------------------------------------------------------------------------------- */
  75 
  76 static inline gboolean
  77 mcview_get_byte_file (WView * view, off_t byte_index, int *retval)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79     g_assert (view->datasource == DS_FILE);
  80 
  81     mcview_file_load_data (view, byte_index);
  82     if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
  83     {
  84         if (retval)
  85             *retval = view->ds_file_data[byte_index - view->ds_file_offset];
  86         return TRUE;
  87     }
  88     if (retval)
  89         *retval = -1;
  90     return FALSE;
  91 }
  92 
  93 /* --------------------------------------------------------------------------------------------- */
  94 
  95 static inline gboolean
  96 mcview_get_byte (WView * view, off_t offset, int *retval)
     /* [previous][next][first][last][top][bottom][index][help]  */
  97 {
  98     switch (view->datasource)
  99     {
 100     case DS_STDIO_PIPE:
 101     case DS_VFS_PIPE:
 102         return mcview_get_byte_growing_buffer (view, offset, retval);
 103     case DS_FILE:
 104         return mcview_get_byte_file (view, offset, retval);
 105     case DS_STRING:
 106         return mcview_get_byte_string (view, offset, retval);
 107     case DS_NONE:
 108         return mcview_get_byte_none (view, offset, retval);
 109     default:
 110         return FALSE;
 111     }
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 
 116 static inline gboolean
 117 mcview_get_byte_indexed (WView * view, off_t base, off_t ofs, int *retval)
     /* [previous][next][first][last][top][bottom][index][help]  */
 118 {
 119     if (base <= OFFSETTYPE_MAX - ofs)
 120     {
 121         return mcview_get_byte (view, base + ofs, retval);
 122     }
 123     if (retval)
 124         *retval = -1;
 125     return FALSE;
 126 }
 127 
 128 /* --------------------------------------------------------------------------------------------- */
 129 
 130 static inline int
 131 mcview_count_backspaces (WView * view, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 132 {
 133     int backspaces = 0;
 134     int c;
 135     while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
 136            && c == '\b')
 137         backspaces++;
 138     return backspaces;
 139 }
 140 
 141 /* --------------------------------------------------------------------------------------------- */
 142 
 143 static inline gboolean
 144 mcview_is_nroff_sequence (WView * view, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 145 {
 146     int c0, c1, c2;
 147 
 148     /* The following commands are ordered to speed up the calculation. */
 149 
 150     if (!mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
 151         return FALSE;
 152 
 153     if (!mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
 154         return FALSE;
 155 
 156     if (!mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
 157         return FALSE;
 158 
 159     return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));
 160 }
 161 
 162 /* --------------------------------------------------------------------------------------------- */
 163 
 164 static inline void
 165 mcview_growbuf_read_all_data (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 166 {
 167     mcview_growbuf_read_until (view, OFFSETTYPE_MAX);
 168 }
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 
 172 #endif /* MC__VIEWER_INLINES_H */

/* [previous][next][first][last][top][bottom][index][help]  */