2 Internal file viewer for the Midnight Commander
3 Function for work with coordinate cache (ccache)
5 Copyright (C) 1994-2016
6 Free Software Foundation, Inc.
9 Miguel de Icaza, 1994, 1995, 1998
10 Janne Kukonlehto, 1994, 1995
12 Joseph M. Hinkle, 1996
15 Roland Illig <roland.illig@gmx.de>, 2004, 2005
16 Slava Zanko <slavazanko@google.com>, 2009
17 Andrew Borodin <aborodin@vmail.ru>, 2009
18 Ilia Maslakov <il.smind@gmail.com>, 2009
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 3 of the License,
25 or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU 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, see <http://www.gnu.org/licenses/>.
37 This cache provides you with a fast lookup to map file offsets into
38 line/column pairs and vice versa. The interface to the mapping is
39 provided by the functions mcview_coord_to_offset() and
40 mcview_offset_to_coord().
42 The cache is implemented as a simple sorted array holding entries
43 that map some of the offsets to their line/column pair. Entries that
44 are not cached themselves are interpolated (exactly) from their
45 neighbor entries. The algorithm used for determining the line/column
46 for a specific offset needs to be kept synchronized with the one used
52 #include <string.h> /* memmove() */
53 #ifdef MC_ENABLE_DEBUGGING_CODE
54 #include <inttypes.h> /* uintmax_t */
57 #include "lib/global.h"
58 #include "lib/tty/tty.h"
61 /*** global variables ****************************************************************************/
63 /*** file scope macro definitions ****************************************************************/
65 #define VIEW_COORD_CACHE_GRANUL 1024
66 #define CACHE_CAPACITY_DELTA 64
68 /*** file scope type declarations ****************************************************************/
70 typedef gboolean (*cmp_func_t
) (const coord_cache_entry_t
* a
, const coord_cache_entry_t
* b
);
72 /*** file scope variables ************************************************************************/
74 /*** file scope functions ************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
77 /* insert new cache entry into the cache */
79 mcview_ccache_add_entry (coord_cache_t
* cache
, size_t pos
, const coord_cache_entry_t
* entry
)
81 if ((cache
== NULL
) || (entry
== NULL
))
84 pos
= MIN (pos
, cache
->size
);
86 /* increase cache capacity if needed */
87 if (cache
->size
== cache
->capacity
)
89 cache
->capacity
+= CACHE_CAPACITY_DELTA
;
90 cache
->cache
= g_realloc (cache
->cache
, cache
->capacity
* sizeof (*cache
->cache
));
93 /* insert new entry */
94 if (pos
!= cache
->size
)
95 memmove (cache
->cache
[pos
+ 1], cache
->cache
[pos
],
96 (cache
->size
- pos
) * sizeof (*cache
->cache
));
97 cache
->cache
[pos
] = g_memdup (entry
, sizeof (*entry
));
101 /* --------------------------------------------------------------------------------------------- */
104 mcview_coord_cache_entry_less_offset (const coord_cache_entry_t
* a
, const coord_cache_entry_t
* b
)
106 return (a
->cc_offset
< b
->cc_offset
);
109 /* --------------------------------------------------------------------------------------------- */
112 mcview_coord_cache_entry_less_plain (const coord_cache_entry_t
* a
, const coord_cache_entry_t
* b
)
114 if (a
->cc_line
< b
->cc_line
)
117 if (a
->cc_line
== b
->cc_line
)
118 return (a
->cc_column
< b
->cc_column
);
124 /* --------------------------------------------------------------------------------------------- */
127 mcview_coord_cache_entry_less_nroff (const coord_cache_entry_t
* a
, const coord_cache_entry_t
* b
)
129 if (a
->cc_line
< b
->cc_line
)
132 if (a
->cc_line
== b
->cc_line
)
133 return (a
->cc_nroff_column
< b
->cc_nroff_column
);
139 /* --------------------------------------------------------------------------------------------- */
140 /** Find and return the index of the last cache entry that is
141 * smaller than ''coord'', according to the criterion ''sort_by''. */
144 mcview_ccache_find (WView
* view
, const coord_cache_entry_t
* coord
, cmp_func_t cmp_func
)
147 size_t limit
= view
->coord_cache
->size
;
157 i
= base
+ limit
/ 2;
158 if (cmp_func (coord
, view
->coord_cache
->cache
[i
]))
160 /* continue the search in the lower half of the cache */
164 /* continue the search in the upper half of the cache */
167 limit
= (limit
+ 1) / 2;
172 /* --------------------------------------------------------------------------------------------- */
173 /*** public functions ****************************************************************************/
174 /* --------------------------------------------------------------------------------------------- */
177 coord_cache_new (void)
179 coord_cache_t
*cache
;
181 cache
= g_new (coord_cache_t
, 1);
183 cache
->capacity
= CACHE_CAPACITY_DELTA
;
184 cache
->cache
= g_malloc0 (cache
->capacity
* sizeof (*cache
->cache
));
189 /* --------------------------------------------------------------------------------------------- */
192 coord_cache_free (coord_cache_t
* cache
)
198 for (i
= 0; i
< cache
->size
; i
++)
199 g_free (cache
->cache
[i
]);
201 g_free (cache
->cache
);
206 /* --------------------------------------------------------------------------------------------- */
208 #ifdef MC_ENABLE_DEBUGGING_CODE
211 mcview_ccache_dump (WView
* view
)
214 off_t offset
, line
, column
, nextline_offset
, filesize
;
216 const coord_cache_t
*cache
= view
->coord_cache
;
219 assert (cache
!= NULL
);
222 filesize
= mcview_get_filesize (view
);
224 f
= fopen ("mcview-ccache.out", "w");
227 (void) setvbuf (f
, NULL
, _IONBF
, 0);
230 for (i
= 0; i
< cache
->size
; i
++)
233 "entry %8u offset %8" PRIuMAX
234 " line %8" PRIuMAX
" column %8" PRIuMAX
235 " nroff_column %8" PRIuMAX
"\n",
237 (uintmax_t) cache
->cache
[i
]->cc_offset
,
238 (uintmax_t) cache
->cache
[i
]->cc_line
,
239 (uintmax_t) cache
->cache
[i
]->cc_column
,
240 (uintmax_t) cache
->cache
[i
]->cc_nroff_column
);
242 (void) fprintf (f
, "\n");
244 /* offset -> line/column translation */
245 for (offset
= 0; offset
< filesize
; offset
++)
247 mcview_offset_to_coord (view
, &line
, &column
, offset
);
249 "offset %8" PRIuMAX
" line %8" PRIuMAX
" column %8" PRIuMAX
"\n",
250 (uintmax_t) offset
, (uintmax_t) line
, (uintmax_t) column
);
253 /* line/column -> offset translation */
254 for (line
= 0; TRUE
; line
++)
256 mcview_coord_to_offset (view
, &nextline_offset
, line
+ 1, 0);
257 (void) fprintf (f
, "nextline_offset %8" PRIuMAX
"\n", (uintmax_t) nextline_offset
);
259 for (column
= 0; TRUE
; column
++)
261 mcview_coord_to_offset (view
, &offset
, line
, column
);
262 if (offset
>= nextline_offset
)
266 "line %8" PRIuMAX
" column %8" PRIuMAX
" offset %8" PRIuMAX
"\n",
267 (uintmax_t) line
, (uintmax_t) column
, (uintmax_t) offset
);
270 if (nextline_offset
>= filesize
- 1)
278 /* --------------------------------------------------------------------------------------------- */
279 /** Look up the missing components of ''coord'', which are given by
280 * ''lookup_what''. The function returns the smallest value that
281 * matches the existing components of ''coord''.
285 mcview_ccache_lookup (WView
* view
, coord_cache_entry_t
* coord
, enum ccache_type lookup_what
)
288 coord_cache_t
*cache
;
289 coord_cache_entry_t current
, next
, entry
;
290 enum ccache_type sorter
;
301 if (view
->coord_cache
== NULL
)
302 view
->coord_cache
= coord_cache_new ();
304 cache
= view
->coord_cache
;
306 if (cache
->size
== 0)
308 current
.cc_offset
= 0;
310 current
.cc_column
= 0;
311 current
.cc_nroff_column
= 0;
312 mcview_ccache_add_entry (cache
, 0, ¤t
);
315 sorter
= (lookup_what
== CCACHE_OFFSET
) ? CCACHE_LINECOL
: CCACHE_OFFSET
;
317 if (sorter
== CCACHE_OFFSET
)
318 cmp_func
= mcview_coord_cache_entry_less_offset
;
319 else if (view
->text_nroff_mode
)
320 cmp_func
= mcview_coord_cache_entry_less_nroff
;
322 cmp_func
= mcview_coord_cache_entry_less_plain
;
325 tty_enable_interrupt_key ();
328 /* find the two neighbor entries in the cache */
329 i
= mcview_ccache_find (view
, coord
, cmp_func
);
330 /* now i points to the lower neighbor in the cache */
332 current
= *cache
->cache
[i
];
333 if (i
+ 1 < view
->coord_cache
->size
)
334 limit
= cache
->cache
[i
+ 1]->cc_offset
;
336 limit
= current
.cc_offset
+ VIEW_COORD_CACHE_GRANUL
;
339 nroff_state
= NROFF_START
;
340 for (; current
.cc_offset
< limit
; current
= next
)
344 if (!mcview_get_byte (view
, current
.cc_offset
, &c
))
347 if (!cmp_func (¤t
, coord
))
349 if (lookup_what
== CCACHE_OFFSET
&& view
->text_nroff_mode
&& nroff_state
!= NROFF_START
)
351 /* don't break here */
359 /* Provide useful default values for ''next'' */
360 next
.cc_offset
= current
.cc_offset
+ 1;
361 next
.cc_line
= current
.cc_line
;
362 next
.cc_column
= current
.cc_column
+ 1;
363 next
.cc_nroff_column
= current
.cc_nroff_column
+ 1;
365 /* and override some of them as necessary. */
370 mcview_get_byte_indexed (view
, current
.cc_offset
, 1, &nextc
);
372 /* Ignore '\r' if it is followed by '\r' or '\n'. If it is
373 * followed by anything else, it is a Mac line ending and
374 * produces a line break.
376 if (nextc
== '\r' || nextc
== '\n')
378 next
.cc_column
= current
.cc_column
;
379 next
.cc_nroff_column
= current
.cc_nroff_column
;
383 next
.cc_line
= current
.cc_line
+ 1;
385 next
.cc_nroff_column
= 0;
389 else if (nroff_state
== NROFF_BACKSPACE
)
391 next
.cc_nroff_column
= current
.cc_nroff_column
- 1;
396 next
.cc_column
= mcview_offset_rounddown (current
.cc_column
, 8) + 8;
397 next
.cc_nroff_column
= mcview_offset_rounddown (current
.cc_nroff_column
, 8) + 8;
402 next
.cc_line
= current
.cc_line
+ 1;
404 next
.cc_nroff_column
= 0;
409 /* Use all default values from above */
415 case NROFF_CONTINUATION
:
416 nroff_state
= mcview_is_nroff_sequence (view
, current
.cc_offset
)
417 ? NROFF_BACKSPACE
: NROFF_START
;
419 case NROFF_BACKSPACE
:
420 nroff_state
= NROFF_CONTINUATION
;
426 /* Cache entries must guarantee that for each i < j,
427 * line[i] <= line[j] and column[i] < column[j]. In the case of
428 * nroff sequences and '\r' characters, this is not guaranteed,
429 * so we cannot save them. */
430 if (nroff_state
== NROFF_START
&& c
!= '\r')
434 if (i
+ 1 == cache
->size
&& entry
.cc_offset
!= cache
->cache
[i
]->cc_offset
)
436 mcview_ccache_add_entry (cache
, cache
->size
, &entry
);
438 if (!tty_got_interrupt ())
442 tty_disable_interrupt_key ();
444 if (lookup_what
== CCACHE_OFFSET
)
446 coord
->cc_offset
= current
.cc_offset
;
450 coord
->cc_line
= current
.cc_line
;
451 coord
->cc_column
= current
.cc_column
;
452 coord
->cc_nroff_column
= current
.cc_nroff_column
;
456 /* --------------------------------------------------------------------------------------------- */