2 Internal file viewer for the Midnight Commander
3 Function for work with coordinate cache (ccache)
5 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
6 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
8 Written by: 1994, 1995, 1998 Miguel de Icaza
9 1994, 1995 Janne Kukonlehto
14 2004 Roland Illig <roland.illig@gmx.de>
15 2005 Roland Illig <roland.illig@gmx.de>
16 2009 Slava Zanko <slavazanko@google.com>
17 2009 Andrew Borodin <aborodin@vmail.ru>
18 2009 Ilia Maslakov <il.smind@gmail.com>
20 This file is part of the Midnight Commander.
22 The Midnight Commander is free software; you can redistribute it
23 and/or modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be
28 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
39 This cache provides you with a fast lookup to map file offsets into
40 line/column pairs and vice versa. The interface to the mapping is
41 provided by the functions mcview_coord_to_offset() and
42 mcview_offset_to_coord().
44 The cache is implemented as a simple sorted array holding entries
45 that map some of the offsets to their line/column pair. Entries that
46 are not cached themselves are interpolated (exactly) from their
47 neighbor entries. The algorithm used for determining the line/column
48 for a specific offset needs to be kept synchronized with the one used
54 #include "../src/global.h"
55 #include "../src/tty/tty.h"
58 #define VIEW_COORD_CACHE_GRANUL 1024
60 /*** global variables ****************************************************************************/
62 /*** file scope macro definitions ****************************************************************/
64 /*** file scope type declarations ****************************************************************/
66 /*** file scope variables ************************************************************************/
68 /*** file scope functions ************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
72 /* Find and return the index of the last cache entry that is
73 * smaller than ''coord'', according to the criterion ''sort_by''. */
75 mcview_ccache_find (mcview_t
* view
, const struct coord_cache_entry
*cache
,
76 const struct coord_cache_entry
*coord
, enum ccache_type sort_by
)
80 limit
= view
->coord_cache
->len
;
86 if (mcview_coord_cache_entry_less (coord
, &cache
[i
], sort_by
, view
->text_nroff_mode
)) {
87 /* continue the search in the lower half of the cache */
89 /* continue the search in the upper half of the cache */
92 limit
= (limit
+ 1) / 2;
98 /* --------------------------------------------------------------------------------------------- */
100 /*** public functions ****************************************************************************/
102 /* --------------------------------------------------------------------------------------------- */
105 mcview_coord_cache_entry_less (const struct coord_cache_entry
*a
,
106 const struct coord_cache_entry
*b
, enum ccache_type crit
,
109 if (crit
== CCACHE_OFFSET
)
110 return (a
->cc_offset
< b
->cc_offset
);
112 if (a
->cc_line
< b
->cc_line
)
115 if (a
->cc_line
== b
->cc_line
) {
117 return (a
->cc_nroff_column
< b
->cc_nroff_column
);
119 return (a
->cc_column
< b
->cc_column
);
125 /* --------------------------------------------------------------------------------------------- */
127 #ifdef MC_ENABLE_DEBUGGING_CODE
130 mcview_ccache_dump (mcview_t
* view
)
133 off_t offset
, line
, column
, nextline_offset
, filesize
;
135 const struct coord_cache_entry
*cache
;
137 assert (view
->coord_cache
!= NULL
);
139 filesize
= mcview_get_filesize (view
);
140 cache
= &(g_array_index (view
->coord_cache
, struct coord_cache_entry
, 0));
142 f
= fopen ("mcview-ccache.out", "w");
145 (void) setvbuf (f
, NULL
, _IONBF
, 0);
148 for (i
= 0; i
< view
->coord_cache
->len
; i
++) {
151 "offset %8" OFFSETTYPE_PRId
" "
152 "line %8" OFFSETTYPE_PRId
" "
153 "column %8" OFFSETTYPE_PRId
" "
154 "nroff_column %8" OFFSETTYPE_PRId
"\n",
155 (unsigned int) i
, cache
[i
].cc_offset
, cache
[i
].cc_line
,
156 cache
[i
].cc_column
, cache
[i
].cc_nroff_column
);
158 (void) fprintf (f
, "\n");
160 /* offset -> line/column translation */
161 for (offset
= 0; offset
< filesize
; offset
++) {
162 mcview_offset_to_coord (view
, &line
, &column
, offset
);
164 "offset %8" OFFSETTYPE_PRId
" "
165 "line %8" OFFSETTYPE_PRId
" "
166 "column %8" OFFSETTYPE_PRId
"\n", offset
, line
, column
);
169 /* line/column -> offset translation */
170 for (line
= 0; TRUE
; line
++) {
171 mcview_coord_to_offset (view
, &nextline_offset
, line
+ 1, 0);
172 (void) fprintf (f
, "nextline_offset %8" OFFSETTYPE_PRId
"\n", nextline_offset
);
174 for (column
= 0; TRUE
; column
++) {
175 mcview_coord_to_offset (view
, &offset
, line
, column
);
176 if (offset
>= nextline_offset
)
180 "line %8" OFFSETTYPE_PRId
" column %8" OFFSETTYPE_PRId
" offset %8"
181 OFFSETTYPE_PRId
"\n", line
, column
, offset
);
184 if (nextline_offset
>= filesize
- 1)
192 /* --------------------------------------------------------------------------------------------- */
195 /* Look up the missing components of ''coord'', which are given by
196 * ''lookup_what''. The function returns the smallest value that
197 * matches the existing components of ''coord''.
200 mcview_ccache_lookup (mcview_t
* view
, struct coord_cache_entry
*coord
,
201 enum ccache_type lookup_what
)
204 struct coord_cache_entry
*cache
, current
, next
, entry
;
205 enum ccache_type sorter
;
213 if (!view
->coord_cache
) {
214 view
->coord_cache
= g_array_new (FALSE
, FALSE
, sizeof (struct coord_cache_entry
));
215 current
.cc_offset
= 0;
217 current
.cc_column
= 0;
218 current
.cc_nroff_column
= 0;
219 g_array_append_val (view
->coord_cache
, current
);
222 sorter
= (lookup_what
== CCACHE_OFFSET
) ? CCACHE_LINECOL
: CCACHE_OFFSET
;
224 tty_enable_interrupt_key ();
227 /* find the two neighbor entries in the cache */
228 cache
= &(g_array_index (view
->coord_cache
, struct coord_cache_entry
, 0));
229 i
= mcview_ccache_find (view
, cache
, coord
, sorter
);
230 /* now i points to the lower neighbor in the cache */
233 if (i
+ 1 < view
->coord_cache
->len
)
234 limit
= cache
[i
+ 1].cc_offset
;
236 limit
= current
.cc_offset
+ VIEW_COORD_CACHE_GRANUL
;
239 nroff_state
= NROFF_START
;
240 for (; current
.cc_offset
< limit
; current
= next
) {
243 if (! mcview_get_byte (view
, current
.cc_offset
, &c
))
246 if (!mcview_coord_cache_entry_less (¤t
, coord
, sorter
, view
->text_nroff_mode
)) {
247 if (lookup_what
== CCACHE_OFFSET
&& view
->text_nroff_mode
&& nroff_state
!= NROFF_START
) {
248 /* don't break here */
254 /* Provide useful default values for ''next'' */
255 next
.cc_offset
= current
.cc_offset
+ 1;
256 next
.cc_line
= current
.cc_line
;
257 next
.cc_column
= current
.cc_column
+ 1;
258 next
.cc_nroff_column
= current
.cc_nroff_column
+ 1;
260 /* and override some of them as necessary. */
262 mcview_get_byte_indexed (view
, current
.cc_offset
, 1, &nextc
);
264 /* Ignore '\r' if it is followed by '\r' or '\n'. If it is
265 * followed by anything else, it is a Mac line ending and
266 * produces a line break.
268 if (nextc
== '\r' || nextc
== '\n') {
269 next
.cc_column
= current
.cc_column
;
270 next
.cc_nroff_column
= current
.cc_nroff_column
;
272 next
.cc_line
= current
.cc_line
+ 1;
274 next
.cc_nroff_column
= 0;
277 } else if (nroff_state
== NROFF_BACKSPACE
) {
278 next
.cc_nroff_column
= current
.cc_nroff_column
- 1;
280 } else if (c
== '\t') {
281 next
.cc_column
= mcview_offset_rounddown (current
.cc_column
, 8) + 8;
282 next
.cc_nroff_column
= mcview_offset_rounddown (current
.cc_nroff_column
, 8) + 8;
284 } else if (c
== '\n') {
285 next
.cc_line
= current
.cc_line
+ 1;
287 next
.cc_nroff_column
= 0;
290 /* Use all default values from above */
293 switch (nroff_state
) {
295 case NROFF_CONTINUATION
:
296 if (mcview_is_nroff_sequence (view
, current
.cc_offset
))
297 nroff_state
= NROFF_BACKSPACE
;
299 nroff_state
= NROFF_START
;
301 case NROFF_BACKSPACE
:
302 nroff_state
= NROFF_CONTINUATION
;
306 /* Cache entries must guarantee that for each i < j,
307 * line[i] <= line[j] and column[i] < column[j]. In the case of
308 * nroff sequences and '\r' characters, this is not guaranteed,
309 * so we cannot save them. */
310 if (nroff_state
== NROFF_START
&& c
!= '\r')
314 if (i
+ 1 == view
->coord_cache
->len
&& entry
.cc_offset
!= cache
[i
].cc_offset
) {
315 g_array_append_val (view
->coord_cache
, entry
);
317 if (!tty_got_interrupt ())
321 tty_disable_interrupt_key ();
323 if (lookup_what
== CCACHE_OFFSET
) {
324 coord
->cc_offset
= current
.cc_offset
;
326 coord
->cc_line
= current
.cc_line
;
327 coord
->cc_column
= current
.cc_column
;
328 coord
->cc_nroff_column
= current
.cc_nroff_column
;
332 /* --------------------------------------------------------------------------------------------- */