Merge branch '2134_am_silent_rules'
[kaloumi3.git] / src / viewer / inlines.h
blob6edbb015d02a61d50f555c30edc0ef2dc4fe4776
1 #ifndef MC_VIEWER_INLINES_H
2 #define MC_VIEWER_INLINES_H
4 /*** typedefs(not structures) and defined constants ********************/
6 /*** enums *************************************************************/
8 /*** structures declarations (and typedefs of structures)***************/
10 /*** global variables defined in .c file *******************************/
12 /*** declarations of public functions **********************************/
14 /*** inline functions ****************************************************************************/
16 static inline off_t
17 mcview_offset_doz (off_t a, off_t b)
19 return (a >= b) ? a - b : 0;
22 /* --------------------------------------------------------------------------------------------- */
24 static inline off_t
25 mcview_offset_rounddown (off_t a, off_t b)
27 assert (b != 0);
28 return a - a % b;
31 /* --------------------------------------------------------------------------------------------- */
33 /* difference or zero */
34 static inline screen_dimen
35 mcview_dimen_doz (screen_dimen a, screen_dimen b)
37 return (a >= b) ? a - b : 0;
40 /* --------------------------------------------------------------------------------------------- */
42 static inline screen_dimen
43 mcview_dimen_min (screen_dimen a, screen_dimen b)
45 return (a < b) ? a : b;
48 /* --------------------------------------------------------------------------------------------- */
50 /* {{{ Simple Primitive Functions for mcview_t }}} */
51 static inline gboolean
52 mcview_is_in_panel (mcview_t * view)
54 return (view->dpy_frame_size != 0);
57 /* --------------------------------------------------------------------------------------------- */
59 static inline gboolean
60 mcview_may_still_grow (mcview_t * view)
62 return (view->growbuf_in_use && !view->growbuf_finished);
65 /* --------------------------------------------------------------------------------------------- */
67 /* returns TRUE if the idx lies in the half-open interval
68 * [offset; offset + size), FALSE otherwise.
70 static inline gboolean
71 mcview_already_loaded (off_t offset, off_t idx, size_t size)
73 return (offset <= idx && idx - offset < (off_t) size);
76 /* --------------------------------------------------------------------------------------------- */
78 static inline gboolean
79 mcview_get_byte_file (mcview_t * view, off_t byte_index, int *retval)
81 assert (view->datasource == DS_FILE);
83 mcview_file_load_data (view, byte_index);
84 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
86 if (retval)
87 *retval = view->ds_file_data[byte_index - view->ds_file_offset];
88 return TRUE;
90 if (retval)
91 *retval = -1;
92 return FALSE;
95 /* --------------------------------------------------------------------------------------------- */
97 static inline gboolean
98 mcview_get_byte (mcview_t * view, off_t offset, int *retval)
100 switch (view->datasource)
102 case DS_STDIO_PIPE:
103 case DS_VFS_PIPE:
104 return mcview_get_byte_growing_buffer (view, offset, retval);
105 case DS_FILE:
106 return mcview_get_byte_file (view, offset, retval);
107 case DS_STRING:
108 return mcview_get_byte_string (view, offset, retval);
109 case DS_NONE:
110 return mcview_get_byte_none (view, offset, retval);
112 assert (!"Unknown datasource type");
113 return -1;
116 /* --------------------------------------------------------------------------------------------- */
118 static inline gboolean
119 mcview_get_byte_indexed (mcview_t * view, off_t base, off_t ofs, int *retval)
121 if (base <= OFFSETTYPE_MAX - ofs)
123 return mcview_get_byte (view, base + ofs, retval);
125 if (retval)
126 *retval = -1;
127 return FALSE;
130 /* --------------------------------------------------------------------------------------------- */
132 static inline int
133 mcview_count_backspaces (mcview_t * view, off_t offset)
135 int backspaces = 0;
136 int c;
137 while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
138 && c == '\b')
139 backspaces++;
140 return backspaces;
143 /* --------------------------------------------------------------------------------------------- */
145 static inline gboolean
146 mcview_is_nroff_sequence (mcview_t * view, off_t offset)
148 int c0, c1, c2;
150 /* The following commands are ordered to speed up the calculation. */
152 if (!mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
153 return FALSE;
155 if (!mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
156 return FALSE;
158 if (!mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
159 return FALSE;
161 return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));
164 /* --------------------------------------------------------------------------------------------- */
166 #endif