FairRoot/PandaRoot
StrTok.cxx
Go to the documentation of this file.
1 // StrTok.cpp: implementation of the CStrTok class.
2 //
4 
5 #include "StrTok.h"
6 #include "string.h"
7 
8 #ifdef _DEBUG
9 #undef THIS_FILE
10 static char THIS_FILE[]=__FILE__;
11 #endif
12 
14 // Construction/Destruction
16 
18 {
20  m_chDelimiter = 0;
21  m_lpszNext = NULL;
22 }
23 
25 {
26 
27 }
28 
29 char* CStrTok::GetFirst(char* lpsz, const char* lpcszDelimiters)
30 {
31  Break();
32 
33  m_lpszNext = lpsz;
34  return GetNext(lpcszDelimiters);
35 }
36 
37 char* CStrTok::GetNext(const char* lpcszDelimiters)
38 {
39  if(m_lpszNext == NULL)
40  return NULL;
41 
42  // return the saved delimiter
43  if(m_chDelimiter != 0)
45 
46  char* lpsz = m_lpszNext;
48  // increment the next pointer to the next delimiter or token
50  {
51  int nLength = strlen(lpcszDelimiters);
52  if(memcmp(lpcszDelimiters, m_lpszNext, nLength) == 0)
53  m_lpszNext += nLength;
54  else
55  m_lpszNext = strstr(m_lpszNext, lpcszDelimiters); // may be NULL
56  }
57  else
58  {
59  if(strchr(lpcszDelimiters, *m_lpszNext))
60  m_lpszNext++;
61  else
62  m_lpszNext += strcspn(m_lpszNext, lpcszDelimiters);
63  }
64  else
66  {
67  int nLength = strlen(lpcszDelimiters);
68  // increment the token pointer
69  while(memcmp(lpcszDelimiters, lpsz, nLength) == 0)
70  {
71  lpsz += nLength;
73  break;
74  }
75  // increment the next pointer after the end of the token
76  m_lpszNext = strstr(lpsz, lpcszDelimiters); // may be NULL
77  }
78  else
79  {
80  // increment the token pointer to the start of the token
82  lpsz += strspn(lpsz, lpcszDelimiters);
83  else
84  if(strchr(lpcszDelimiters, *lpsz) && m_chDelimiter)
85  lpsz++;
86  // increment the next pointer after the end of the token
87  m_lpszNext = lpsz + strcspn(lpsz, lpcszDelimiters);
88  }
89 
90  if(m_lpszNext == NULL || *m_lpszNext == 0)
91  { // reach the end of the buffer
92  m_chDelimiter = 0;
93  m_lpszNext = NULL;
94  }
95  else
96  { // save the delimiter and terminate the token by null
98  *m_lpszNext = 0;
99  }
100  if(*lpsz == 0 && !m_bOneByOneDelimiter)
101  return NULL;
102  return lpsz;
103 }
104 
105 void CStrTok::SetNext(const char* lpcszNext)
106 {
107  if(m_chDelimiter != 0)
109  m_chDelimiter = 0;
110  m_lpszNext = (char*)lpcszNext;
111 }
112 
114 {
115  return m_lpszNext == NULL;
116 }
117 
118 bool CStrTok::IsDelimiter(char ch, const char* lpcszDelimiters)
119 {
120  return strchr(lpcszDelimiters, ch) != NULL;
121 }
122 
124 {
125  SetNext(NULL);
126 }
127 
128 void CStrTok::TrimLeft(char* &lpsz, const char* lpcszDelimiters /*= NULL*/)
129 {
130  if(m_lpszNext == NULL)
131  return;
132  if(lpcszDelimiters == NULL)
133  lpcszDelimiters = " \t\r\n";
134  while(strchr(lpcszDelimiters, *lpsz))
135  lpsz++;
136 }
137 
138 void CStrTok::TrimRight(const char* lpcszDelimiters /*= NULL*/)
139 {
140  if(m_lpszNext == NULL)
141  return;
142  if(lpcszDelimiters == NULL)
143  lpcszDelimiters = " \t\r\n";
144  char* pNext = m_lpszNext-1;
145  while(strchr(lpcszDelimiters, *pNext))
146  *pNext = 0, pNext--;
147 }
148 
char m_chDelimiter
Definition: StrTok.h:24
bool m_bDelimiterAsToken
Definition: StrTok.h:19
void TrimRight(const char *lpcszDelimiters=0)
Definition: StrTok.cxx:138
char * m_lpszNext
Definition: StrTok.h:23
bool m_bOneByOneDelimiter
Definition: StrTok.h:20
void TrimLeft(char *&lpsz, const char *lpcszDelimiters=0)
Definition: StrTok.cxx:128
char * GetFirst(char *lpsz, const char *lpcszDelimiters)
Definition: StrTok.cxx:29
void SetNext(const char *lpcszNext)
Definition: StrTok.cxx:105
void Break()
Definition: StrTok.cxx:123
bool m_bDelimitersInSequence
Definition: StrTok.h:21
virtual ~CStrTok()
Definition: StrTok.cxx:24
static bool IsDelimiter(char ch, const char *lpcszDelimiters)
Definition: StrTok.cxx:118
bool IsEOB()
Definition: StrTok.cxx:113
CStrTok()
Definition: StrTok.cxx:17
char * GetNext(const char *lpcszDelimiters)
Definition: StrTok.cxx:37