00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_tokenizer.hpp"
00012
00013 #include "xrb_util.hpp"
00014
00015 namespace Xrb
00016 {
00017
00018 Tokenizer::Tokenizer (
00019 char *const string_to_tokenize,
00020 char const *const delimiters)
00021 {
00022 ASSERT1(string_to_tokenize != NULL);
00023 ASSERT1(delimiters != NULL);
00024
00025 m_string_to_tokenize = string_to_tokenize;
00026 m_string_to_tokenize_requires_deletion = false;
00027 m_delimiters = delimiters;
00028 m_end_of_token = NULL;
00029 }
00030
00031 Tokenizer::Tokenizer (
00032 const char *const string_to_tokenize,
00033 char const *const delimiters)
00034 {
00035 ASSERT1(string_to_tokenize != NULL);
00036 ASSERT1(delimiters != NULL);
00037
00038 m_string_to_tokenize = Util::StringDuplicate(string_to_tokenize);
00039 m_string_to_tokenize_requires_deletion = false;
00040 m_delimiters = delimiters;
00041 m_end_of_token = NULL;
00042 }
00043
00044 Tokenizer::~Tokenizer ()
00045 {
00046 if (m_string_to_tokenize_requires_deletion)
00047 Delete(m_string_to_tokenize);
00048 }
00049
00050 char const *Tokenizer::GetToken () const
00051 {
00052
00053
00054 if (m_end_of_token)
00055 {
00056
00057
00058 if (m_replaced_delimiter == '\0')
00059 return 0;
00060
00061 *m_end_of_token = m_replaced_delimiter;
00062 }
00063
00064 else
00065 {
00066 ASSERT1(m_replaced_delimiter == '\0');
00067 m_end_of_token = m_string_to_tokenize;
00068 }
00069
00070
00071
00072
00073 while (*m_end_of_token != '\0' &&
00074 IsCharADelimiter(*m_end_of_token))
00075 {
00076 ++m_end_of_token;
00077 }
00078
00079
00080
00081
00082 if (*m_end_of_token == '\0')
00083 {
00084 m_replaced_delimiter = '\0';
00085 return 0;
00086 }
00087
00088
00089
00090
00091 char const *token_to_return = m_end_of_token;
00092
00093
00094
00095
00096 while (*m_end_of_token != '\0' &&
00097 !IsCharADelimiter(*m_end_of_token))
00098 {
00099 ++m_end_of_token;
00100 }
00101
00102
00103
00104
00105 if (*m_end_of_token == '\0')
00106 {
00107 m_replaced_delimiter = '\0';
00108 return token_to_return;
00109 }
00110
00111
00112
00113
00114 ASSERT1(IsCharADelimiter(*m_end_of_token));
00115 m_replaced_delimiter = *m_end_of_token;
00116 *m_end_of_token = 0;
00117
00118
00119 return token_to_return;
00120 }
00121
00122 bool Tokenizer::IsCharADelimiter (char const c) const
00123 {
00124 ASSERT1(m_delimiters != NULL);
00125
00126 char const *delimiter = m_delimiters;
00127 while (*delimiter)
00128 {
00129 if (c == *delimiter)
00130 return true;
00131
00132 ++delimiter;
00133 }
00134
00135 return false;
00136 }
00137
00138 }