summaryrefslogtreecommitdiff
path: root/src/cursor.hpp
blob: 6b09a248519d97cf8b03de98808caa960a375530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* $Id: cursor.hpp,v 1.4 2006-11-05 04:42:43 ganzhorn Exp $
 * This file is part of lfhex.
 * Copyright (C) 2006 Salem Ganzhorn <eyekode@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef _CURSOR_HPP_
#define _CURSOR_HPP_

#include <sys/types.h>
#include <limits.h>
#include <stdexcept>

#ifdef _LARGEFILE_SOURCE
#define MAX_OFFSET LONG_LONG_MAX
#else
#define MAX_OFFSET LONG_MAX
#endif

class  Cursor {
public:
  Cursor( off_t byteOffset = 0,
	  off_t charOffset = 0,
	  off_t low        = 0,
	  off_t high       = 1,
	  off_t charsPerByte = 2 ); // defaults to hex
  Cursor( const Cursor& c);

  off_t incrByChar( off_t n );
  off_t incrByByte( off_t n );
  off_t decrByChar( off_t n );
  off_t decrByByte( off_t n );

  off_t   byteOffset() const; //returns the current byte offset
  off_t   charOffset() const; //returns the current char offset. (relative)
  off_t   charOffsetAbsolute() const; //returns the current absolute offset.

  bool   setRange( off_t low, off_t high );
  // setCharsPerByte:
  // sideEffect: sets charOffset to min(_charsPerByte-1,_charOffset)
  off_t   setCharsPerByte( off_t charsPerByte );
  void    setOffset( off_t byteOffset, off_t charOffset );

protected:
  // performs: dst = src; and clamps the value to [low,high)
  void assignClamped(off_t& dst,off_t src,off_t low,off_t high);

  enum {
    OutOfRange = -1,
  };
protected:
  off_t _byteOffset;
  off_t _charOffset;
  off_t _low;
  off_t _high;
  off_t _charsPerByte;
};

#endif