x86_64: Cleanup of code for master
[midnight-commander.git] / src / viewer / inlines.h
blob88708dac9fff8ef07814fc8f1ae32fa9c6ddc27d
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)) {
85 if (retval)
86 *retval = view->ds_file_data[byte_index - view->ds_file_offset];
87 return TRUE;
89 if (retval)
90 *retval = -1;
91 return FALSE;
94 /* --------------------------------------------------------------------------------------------- */
96 static inline gboolean
97 mcview_get_byte (mcview_t * view, off_t offset, int *retval)
99 switch (view->datasource) {
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);
110 assert (!"Unknown datasource type");
111 return -1;
114 /* --------------------------------------------------------------------------------------------- */
116 static inline gboolean
117 mcview_get_byte_indexed (mcview_t * view, off_t base, off_t ofs, int *retval)
119 if (base <= OFFSETTYPE_MAX - ofs) {
120 return mcview_get_byte (view, base + ofs, retval);
122 if (retval)
123 *retval = -1;
124 return FALSE;
127 /* --------------------------------------------------------------------------------------------- */
129 static inline int
130 mcview_count_backspaces (mcview_t * view, off_t offset)
132 int backspaces = 0;
133 int c;
134 while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
135 && c == '\b')
136 backspaces++;
137 return backspaces;
140 /* --------------------------------------------------------------------------------------------- */
142 static inline gboolean
143 mcview_is_nroff_sequence (mcview_t * view, off_t offset)
145 int c0, c1, c2;
147 /* The following commands are ordered to speed up the calculation. */
149 if (! mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
150 return FALSE;
152 if (! mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
153 return FALSE;
155 if (! mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
156 return FALSE;
158 return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));
161 /* --------------------------------------------------------------------------------------------- */
163 #endif