Go to the documentation of this file.00001
00002
00003 #ifndef ACE_TOKENIZER_T_CPP
00004 #define ACE_TOKENIZER_T_CPP
00005
00006 #include "ace/ACE.h"
00007 #include "ace/Malloc_Base.h"
00008 #include "ace/String_Base.h"
00009 #include "ace/Auto_Ptr.h"
00010 #include "ace/OS_NS_string.h"
00011
00012 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00013
00014 template <class ACE_CHAR_T>
00015 ACE_Tokenizer_T<ACE_CHAR_T>::ACE_Tokenizer_T (ACE_CHAR_T *buffer)
00016 : buffer_ (buffer),
00017 index_ (0),
00018 preserves_index_ (0),
00019 delimiter_index_ (0)
00020 {
00021 }
00022
00023 template <class ACE_CHAR_T>
00024 int
00025 ACE_Tokenizer_T<ACE_CHAR_T>::delimiter (ACE_CHAR_T d)
00026 {
00027 if (delimiter_index_ == MAX_DELIMITERS)
00028 return -1;
00029
00030 delimiters_[delimiter_index_].delimiter_ = d;
00031 delimiters_[delimiter_index_].replace_ = 0;
00032 ++delimiter_index_;
00033 return 0;
00034 }
00035
00036 template <class ACE_CHAR_T>
00037 int
00038 ACE_Tokenizer_T<ACE_CHAR_T>::delimiter_replace (ACE_CHAR_T d,
00039 ACE_CHAR_T replacement)
00040 {
00041
00042
00043
00044 for (int i = 0; i < delimiter_index_; i++)
00045 if (delimiters_[i].delimiter_ == d)
00046 {
00047 delimiters_[i].replacement_ = replacement;
00048 delimiters_[i].replace_ = 1;
00049 return 0;
00050 }
00051
00052 if (delimiter_index_ >= MAX_DELIMITERS)
00053 return -1;
00054
00055 delimiters_[delimiter_index_].delimiter_ = d;
00056 delimiters_[delimiter_index_].replacement_ = replacement;
00057 delimiters_[delimiter_index_].replace_ = 1;
00058 ++delimiter_index_;
00059 return 0;
00060 }
00061
00062 template <class ACE_CHAR_T>
00063 int
00064 ACE_Tokenizer_T<ACE_CHAR_T>::preserve_designators (ACE_CHAR_T start,
00065 ACE_CHAR_T stop,
00066 int strip)
00067 {
00068 if (preserves_index_ == MAX_PRESERVES)
00069 return -1;
00070
00071 preserves_[preserves_index_].start_ = start;
00072 preserves_[preserves_index_].stop_ = stop;
00073 preserves_[preserves_index_].strip_ = strip;
00074 ++preserves_index_;
00075 return 0;
00076 }
00077
00078 template <class ACE_CHAR_T>
00079 int
00080 ACE_Tokenizer_T<ACE_CHAR_T>::is_delimiter (ACE_CHAR_T d,
00081 int &replace,
00082 ACE_CHAR_T &r)
00083 {
00084 replace = 0;
00085
00086 for (int x = 0; x < delimiter_index_; x++)
00087 if (delimiters_[x].delimiter_ == d)
00088 {
00089 if (delimiters_[x].replace_)
00090 {
00091 r = delimiters_[x].replacement_;
00092 replace = 1;
00093 }
00094 return 1;
00095 }
00096
00097 return 0;
00098 }
00099
00100 template <class ACE_CHAR_T>
00101 int
00102 ACE_Tokenizer_T<ACE_CHAR_T>::is_preserve_designator (ACE_CHAR_T start,
00103 ACE_CHAR_T &stop,
00104 int &strip)
00105 {
00106 for (int x = 0; x < preserves_index_; x++)
00107 if (preserves_[x].start_ == start)
00108 {
00109 stop = preserves_[x].stop_;
00110 strip = preserves_[x].strip_;
00111 return 1;
00112 }
00113
00114 return 0;
00115 }
00116
00117 template <class ACE_CHAR_T>
00118 ACE_CHAR_T *
00119 ACE_Tokenizer_T<ACE_CHAR_T>::next (void)
00120 {
00121
00122 if (index_ == -1)
00123 {
00124 index_ = 0;
00125 return 0;
00126 }
00127
00128
00129 if (!buffer_)
00130 {
00131 return 0;
00132 }
00133
00134 ACE_CHAR_T replacement = 0;
00135 int replace;
00136 ACE_CHAR_T *next_token = 0;
00137
00138
00139 for (;;)
00140 {
00141
00142 if (buffer_[index_] == '\0')
00143 {
00144
00145 index_ = 0;
00146 return 0;
00147 }
00148
00149 if (this->is_delimiter (buffer_[index_],
00150 replace,
00151 replacement))
00152 ++index_;
00153 else
00154 break;
00155 }
00156
00157
00158
00159 next_token = buffer_ + index_;
00160
00161
00162 ACE_CHAR_T stop;
00163 int strip;
00164 if (this->is_preserve_designator (buffer_[index_],
00165 stop,
00166 strip))
00167 {
00168 while (++index_)
00169 {
00170 if (buffer_[index_] == '\0')
00171 {
00172 index_ = -1;
00173 goto EXIT_LABEL;
00174 }
00175
00176 if (buffer_[index_] == stop)
00177 break;
00178 }
00179
00180 if (strip)
00181 {
00182
00183 next_token += 1;
00184
00185 buffer_[index_] = '\0';
00186
00187 ++index_;
00188 }
00189
00190 goto EXIT_LABEL;
00191 }
00192
00193
00194 for (;;)
00195 {
00196
00197 ++index_;
00198
00199
00200 if (this->is_delimiter (buffer_[index_],
00201 replace,
00202 replacement))
00203 {
00204
00205 if (replace != 0)
00206 buffer_[index_] = replacement;
00207
00208
00209 ++index_;
00210 goto EXIT_LABEL;
00211 }
00212
00213
00214
00215
00216 if (this->is_preserve_designator (buffer_[index_],
00217 stop,
00218 strip))
00219 {
00220 ++index_;
00221 while (('\0' != buffer_[index_]) && (stop != buffer_[index_]))
00222 ++index_;
00223 }
00224
00225
00226 if (buffer_[index_] == '\0')
00227 {
00228 index_ = -1;
00229 goto EXIT_LABEL;
00230 }
00231 }
00232
00233 EXIT_LABEL:
00234 return next_token;
00235 }
00236
00237
00238
00239
00240 ACE_END_VERSIONED_NAMESPACE_DECL
00241
00242 #endif