KSquare Utilities
KKStrParser.h
Go to the documentation of this file.
1 /* KKStrParser.h -- Class used to parse string into tokens.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #ifndef _KKSTRPARSER_
6 #define _KKSTRPARSER_
7 
8 #include "DateTime.h"
9 #include "KKStr.h"
10 
11 
12 namespace KKB
13 {
14  /**
15  *@class KKStrParser
16  *@brief Class that manages the extraction of tokens from a String without being destructive to the original string.
17  */
19  {
20  public:
21  KKStrParser (const KKStrParser& _strParser);
22  KKStrParser (const char* _str);
23  KKStrParser (const KKStr& _str);
24  KKStrParser (KKStr&& _str);
25 
26  ~KKStrParser ();
27 
28  char GetLastChar ();
29 
30 
31  /**
32  *@brief Extract next Token from string, tokens will be separated by delimiter characters.
33  *@details Removes next Token from string. The token will be terminated by end of string or the first
34  * occurrence of a delimiter character. If no more tokens left will return a Empty KKStr. If you want
35  * to remove leading and trailing whitespace characters you need to call the "TrimWhiteSpace" method.
36  *
37  * Quoted Strings will be treated differently. If the first character in the token is a quote(") or
38  * apostrophe(') character then the token will include all characters until the matching quote
39  * character. The quote characters will be NOT be included in the token. The next character pointer will
40  * be set to the following delimiter character. The quote characters must match. That is if the first
41  * quote character was (') then the terminating quote character also be ('). The special escape sequences
42  * ("\t", "\n", "\r", "\\", '\"', and "\'" will be translated into (tab), (line-feed), (carriage-return),
43  * (back-slash), (quote), and (apostrophe).
44  *
45  *@param[in] delStr List of delimiter characters.
46  *@return Extracted Token.
47  */
48  KKStr GetNextToken (const char* delStr = "\n\t\r ");
49 
50  char GetNextChar ();
51 
52  char GetNextTokenChar (const char* delStr = "\n\t\r ");
53 
54  KKB::DateTime GetNextTokenDateTime (const char* delStr = "\n\t\r ");
55 
56  kkint32 GetNextTokenInt (const char* delStr = "\n\t\r ");
57 
58  long GetNextTokenLong (const char* delStr = "\n\t\r ");
59 
60  double GetNextTokenDouble (const char* delStr = "\n\t\r ");
61 
62  float GetNextTokenFloat (const char* delStr = "\n\t\r ");
63 
64  kkuint32 GetNextTokenUint (const char* delStr = "\n\t\r ");
65 
66  bool GetNextTokenBool (const char* delStr = "\n\t\r ");
67 
69 
71 
72  char LastDelimiter () const {return lastDelimiter;}
73 
74  bool MoreTokens () const {return (nextPos < len);}
75 
76  char PeekLastChar () const;
77 
78  char PeekNextChar () const;
79 
80 
81  /**
82  *@brief Will use the same rules as "GetNextToken" to retrieve the next token n the string but will not
83  * advance the next character pointer.
84  */
85  KKStr PeekNextToken (const char* delStr = "\n\t\r ") const;
86 
87  void Reset ();
88 
89  /**
90  *@brief Advances the next-character pointer to the next NOT white space character.
91  */
92  void SkipWhiteSpace (const char* whiteSpace = " ");
93 
94  VectorKKStr Split (const char* delStr = "\n\t\r ");
95 
96  const char* Str () const {return str;}
97 
98  KKStr SubStrPart (kkuint32 firstChar,
99  kkuint32 lastChar
100  ) const;
101 
102  /**
103  *@brief After this call all leading and trailing whitespace will be trimmed from tokens.
104  *@details The next character pointer will be advanced to the next NON whitespace character.
105  */
106  void TrimWhiteSpace (const char* _whiteSpace = " ");
107 
108  private:
109  char lastDelimiter; /**< The last delimiter character encountered when calling "GetNextToken". */
110  kkuint32 len;
111  kkuint32 nextPos;
112  const char* str;
113  bool trimWhiteSpace;
114  bool weOwnStr;
115  char* whiteSpace;
116  };
117 } /* KKB*/
118 
119 
120 #endif
bool GetNextTokenBool(const char *delStr="\n\t\r ")
KKStr SubStrPart(kkuint32 firstChar, kkuint32 lastChar) const
__int32 kkint32
Definition: KKBaseTypes.h:88
void TrimWhiteSpace(const char *_whiteSpace=" ")
After this call all leading and trailing whitespace will be trimmed from tokens.
Definition: KKStrParser.cpp:95
KKStrParser(const KKStrParser &_strParser)
Definition: KKStrParser.cpp:28
KKStr GetNextToken(const char *delStr="\n\t\r ")
Extract next Token from string, tokens will be separated by delimiter characters. ...
KKStrParser(KKStr &&_str)
Definition: KKStrParser.cpp:66
char LastDelimiter() const
Definition: KKStrParser.h:72
long GetNextTokenLong(const char *delStr="\n\t\r ")
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
float GetNextTokenFloat(const char *delStr="\n\t\r ")
bool MoreTokens() const
Definition: KKStrParser.h:74
char PeekNextChar() const
KKTHread * KKTHreadPtr
KKStrParser(const KKStr &_str)
Definition: KKStrParser.cpp:42
void SkipWhiteSpace(const char *whiteSpace=" ")
Advances the next-character pointer to the next NOT white space character.
char PeekLastChar() const
VectorKKStr Split(const char *delStr="\n\t\r ")
const char * Str() const
Definition: KKStrParser.h:96
KKStr PeekNextToken(const char *delStr="\n\t\r ") const
Will use the same rules as "GetNextToken" to retrieve the next token n the string but will not advanc...
kkuint32 GetNextTokenUint(const char *delStr="\n\t\r ")
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
KKB::DateTime GetNextTokenDateTime(const char *delStr="\n\t\r ")
double GetNextTokenDouble(const char *delStr="\n\t\r ")
kkint32 GetNextTokenInt(const char *delStr="\n\t\r ")
Class that manages the extraction of tokens from a String without being destructive to the original s...
Definition: KKStrParser.h:18
char GetNextTokenChar(const char *delStr="\n\t\r ")
KKStrParser(const char *_str)
Definition: KKStrParser.cpp:54