Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cTextPos.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cTextPos_H
7 #define _INC_cTextPos_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cStreamProgress.h"
13 #include "StrT.h"
14 
15 namespace Gray
16 {
18  {
24 
25  public:
26  static const cTextPos k_Invalid;
27  static const cTextPos k_Zero;
28 
29  protected:
33  // m_iLineNum = -1 or -2 can be used to indicate errors.
34 
35  public:
37  : m_lOffset(lOffset)
38  , m_iLineNum(iLineNum)
39  , m_iColNum(iColNum)
40  {
41  }
42 
43  void InitTop() noexcept
44  {
45  m_lOffset = 0;
46  m_iLineNum = 0;
47  m_iColNum = 0;
48  }
49  bool isTopLine() const noexcept
50  {
52  return(m_lOffset == 0 && m_iLineNum == 0);
53  }
54 
55  bool isValidPos() const noexcept
56  {
58  return(m_iLineNum >= 0); // m_lOffset >= 0
59  }
60  STREAM_POS_t get_Offset() const noexcept
61  {
63  return m_lOffset;
64  }
65  ITERATE_t get_LineNum() const noexcept
66  {
67  return this->m_iLineNum;
68  }
69  ITERATE_t get_Line1() const noexcept
70  {
71  return this->m_iLineNum + 1;
72  }
73  StrLen_t get_Column1() const noexcept
74  {
75  return this->m_iColNum + 1;
76  }
77 
78  void IncOffset(StrLen_t nLenOffsetSrc) noexcept
79  {
80  m_lOffset += nLenOffsetSrc;
81  m_iColNum += nLenOffsetSrc;
82  }
83  void IncOffset(StrLen_t nLenOffsetSrc, StrLen_t nLenCol) noexcept
84  {
85  // nLenCol = 0 = invisible chars don't count.
86  m_lOffset += nLenOffsetSrc;
87  m_iColNum += nLenCol;
88  }
89  void IncChar(StrLen_t nLenChar = 1) noexcept
90  {
91  // Add one single char that is not a newline or tab.
92  m_lOffset += nLenChar; // UTF8 can span multiple bytes.
93  m_iColNum++;
94  }
95  void IncLine(StrLen_t nLenChar = 1) noexcept
96  {
97  // CRLF or LF
98  m_lOffset += nLenChar; // UTF8 can span multiple bytes.
99  ++m_iLineNum;
100  m_iColNum = 0;
101  }
102 
103  StrLen_t GetStr2(OUT char* pszOut, StrLen_t nLenOut) const;
104  };
105 
107  {
112  protected:
113  const char* m_pszStart;
115 
116  public:
118 
119  public:
120  cTextReader(const char* pszStart, StrLen_t nLenMax = StrT::k_LEN_MAX, StrLen_t nTabSize = cStrConst::k_TabSize)
121  : cTextPos(0, 0, 0)
122  , m_pszStart(pszStart)
123  , m_nLenMax(nLenMax)
124  , m_iTabSize(nTabSize)
125  {
126  }
127 
128  StrLen_t get_LenMax() const noexcept
129  {
130  return m_nLenMax;
131  }
132  StrLen_t get_LenRemaining() const noexcept
133  {
134  if (m_nLenMax < (StrLen_t)m_lOffset)
135  return 0;
136  return m_nLenMax - (StrLen_t)m_lOffset;
137  }
138  bool isValidIndex() const noexcept
139  {
140  return ((UINT)m_lOffset) < (UINT)m_nLenMax; // includes m_lOffset >= 0
141  }
142  bool isValidPos() const noexcept
143  {
145  return m_pszStart != nullptr && isValidIndex(); // includes m_lOffset >= 0
146  }
147  const char* get_CursorPtr() const noexcept
148  {
149  ASSERT(isValidPos());
150  return m_pszStart + this->m_lOffset;
151  }
152  char get_CursorChar() const noexcept
153  {
154  ASSERT(m_pszStart != nullptr);
155  if (!isValidIndex())
156  return '\0';
157  return m_pszStart[this->m_lOffset];
158  }
159 
160  void IncToks(StrLen_t nLen = 1)
161  {
162  // Advance the cursor x.
163  // Skip over some known token. It is not a new line. It has no tabs. It is not past the end of the data.
164 #ifdef _DEBUG
165  ASSERT(isValidPos());
166  char ch = get_CursorChar();
167  ASSERT(!StrChar::IsSpaceX(ch) && ch != '\0');
168 #endif
169  IncOffset(nLen); // eat chars
170  }
171  void IncTab(StrLen_t nLenChar = 1)
172  {
173  // Skip to next tab stop
174  m_lOffset += nLenChar; // eat tab
175  if (m_iTabSize <= 0)
176  m_iColNum++;
177  else
178  m_iColNum = (m_iColNum / m_iTabSize + 1) * m_iTabSize;
179  }
181  {
182  // Check for \r\n sequence, and treat this as a single character
183  IncLine(nLenChar); // bump down to the next line
184  StrLen_t nLen = 1;
185  if (get_CursorChar() == '\n')
186  {
187  m_lOffset += nLenChar; // eat combo char
188  nLen++;
189  }
190  return nLen;
191  }
192 
193  bool isEOF() const
194  {
195  return(get_CursorChar() == '\0');
196  }
197 
198  void SetStartPtr(const char* pszStart, StrLen_t nLenMax = StrT::k_LEN_MAX)
199  {
200  m_pszStart = pszStart;
201  m_nLenMax = nLenMax;
202  InitTop();
203  }
204  };
205 }
206 
207 #endif
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define ASSERT(exp)
Definition: cDebugAssert.h:87
static const StrLen_t k_TabSize
default desired spaces for a tab.
Definition: StrConst.h:47
Definition: cTextPos.h:18
ITERATE_t get_LineNum() const noexcept
< Get 0 based line.
Definition: cTextPos.h:65
void IncLine(StrLen_t nLenChar=1) noexcept
Definition: cTextPos.h:95
bool isTopLine() const noexcept
Definition: cTextPos.h:49
static const cTextPos k_Zero
Top of file.
Definition: cTextPos.h:27
StrLen_t get_Column1() const noexcept
< Get 1 based column.
Definition: cTextPos.h:73
void IncChar(StrLen_t nLenChar=1) noexcept
Definition: cTextPos.h:89
void InitTop() noexcept
Definition: cTextPos.h:43
ITERATE_t m_iLineNum
0 based row/line, for debug purposes if there is an error.
Definition: cTextPos.h:31
static const cTextPos k_Invalid
Set to invalid values.
Definition: cTextPos.h:26
void IncOffset(StrLen_t nLenOffsetSrc) noexcept
Definition: cTextPos.h:78
STREAM_POS_t get_Offset() const noexcept
Definition: cTextPos.h:60
StrLen_t m_iColNum
0 based column number. if used. # of characters, not bytes. UTF can have multi bytes per char.
Definition: cTextPos.h:32
STREAM_POS_t m_lOffset
byte offset into the file. 0 based
Definition: cTextPos.h:30
void IncOffset(StrLen_t nLenOffsetSrc, StrLen_t nLenCol) noexcept
Definition: cTextPos.h:83
ITERATE_t get_Line1() const noexcept
< Get 1 based line.
Definition: cTextPos.h:69
bool isValidPos() const noexcept
Definition: cTextPos.h:55
cTextPos(STREAM_POS_t lOffset=(STREAM_POS_t) k_ITERATE_BAD, ITERATE_t iLineNum=k_ITERATE_BAD, StrLen_t iColNum=k_StrLen_UNK) noexcept
Definition: cTextPos.h:36
Definition: cTextPos.h:107
StrLen_t get_LenMax() const noexcept
Definition: cTextPos.h:128
const char * get_CursorPtr() const noexcept
Definition: cTextPos.h:147
char get_CursorChar() const noexcept
Definition: cTextPos.h:152
void IncToks(StrLen_t nLen=1)
Definition: cTextPos.h:160
bool isValidIndex() const noexcept
Definition: cTextPos.h:138
bool isEOF() const
Definition: cTextPos.h:193
cTextReader(const char *pszStart, StrLen_t nLenMax=StrT::k_LEN_MAX, StrLen_t nTabSize=cStrConst::k_TabSize)
Definition: cTextPos.h:120
const char * m_pszStart
starting read position in the data parsing stream/buffer. cTextPos cursor = m_pszStart + m_lOffset.
Definition: cTextPos.h:113
StrLen_t m_nLenMax
don't advance cTextPos::m_lOffset past this.
Definition: cTextPos.h:114
StrLen_t get_LenRemaining() const noexcept
Definition: cTextPos.h:132
bool isValidPos() const noexcept
Definition: cTextPos.h:142
void SetStartPtr(const char *pszStart, StrLen_t nLenMax=StrT::k_LEN_MAX)
Definition: cTextPos.h:198
const StrLen_t m_iTabSize
for proper tracking of the column number on errors. and m_CursorPos. 0 = not used/don't care.
Definition: cTextPos.h:117
void IncTab(StrLen_t nLenChar=1)
Definition: cTextPos.h:171
StrLen_t IncLineCR(StrLen_t nLenChar=1)
Definition: cTextPos.h:180
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
const StrLen_t k_StrLen_UNK
use the default/current length of the string argument.
Definition: StrConst.h:34
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
int ITERATE_t
like size_t but signed
Definition: Index.h:28
ULONG_PTR STREAM_POS_t
NOT same as FILE_SIZE_t in 32 bit. Why not ?
Definition: cOSHandle.h:54
const ITERATE_t k_ITERATE_BAD
Definition: Index.h:30
static bool IsSpaceX(wchar_t ch) noexcept
Definition: StrChar.h:94
static const StrLen_t k_LEN_MAX
arbitrary max size for Format() etc. NOTE: _MSC_VER says stack frame should be at least 16384
Definition: StrT.h:75