Go to the documentation of this file.
9 #ifndef CNOID_UTIL_TOKENIZER_H
10 #define CNOID_UTIL_TOKENIZER_H
25 template<class Char, class Traits = typename std::basic_string<Char>::traits_type>
29 typedef std::basic_string<Char,Traits> string_type;
32 char_eq(Char e):e_(e) { }
34 return Traits::eq(e_,c);
42 bool is_escape(Char e){
44 return std::find_if(escape_.begin(),escape_.end(),f)!=escape_.end();
49 return std::find_if(c_.begin(),c_.end(),f)!=c_.end();
52 bool is_quote(Char e){
54 return std::find_if(quote_.begin(),quote_.end(),f)!=quote_.end();
57 template <
typename iterator,
typename Token>
58 void do_escape(iterator& next,iterator end,Token& tok){
62 if(Traits::eq(*next,
'n')){
65 }
else if(is_quote(*next)){
68 }
else if(is_c(*next)){
71 }
else if (is_escape(*next)){
81 : escape_(1,e), c_(1,c), quote_(1,q), last_(false) { }
84 : escape_(e), c_(c), quote_(q), last_(false) { }
88 template <
typename InputIterator,
typename Token>
89 bool operator()(InputIterator& next,InputIterator end,Token& tok) {
90 bool bInQuote =
false;
102 for(; next != end; ++next){
103 if(is_escape(*next)){
104 do_escape(next, end, tok);
105 }
else if(is_c(*next)){
116 else if (is_quote(*next)){
129 template <typename Char, typename Tr = typename std::basic_string<Char>::traits_type>
132 typedef std::basic_string<Char,Tr> string_type;
137 const Char* kept_delims = 0,
139 : m_dropped_delims(dropped_delims),
140 m_use_ispunct(false),
141 m_use_isspace(false),
142 m_empty_tokens(empty_tokens),
147 m_kept_delims = kept_delims;
154 : m_use_ispunct(true),
157 m_output_done(false) { }
161 template <
typename InputIterator,
typename Token>
162 bool operator()(InputIterator& next, InputIterator end, Token& tok)
166 for(; next != end && is_dropped(*next); ++next){ }
169 InputIterator start(next);
180 for(; next != end && !is_dropped(*next) && !is_kept(*next); ++next);
186 if(m_output_done ==
false){
187 m_output_done =
true;
188 tok.assign(start, next);
196 if(m_output_done ==
false){
197 m_output_done =
true;
200 m_output_done =
false;
202 }
else if(m_output_done ==
false && is_dropped(*next)){
203 m_output_done =
true;
205 if(is_dropped(*next)){
208 for(; next != end && !is_dropped(*next) && !is_kept(*next); ++next);
209 m_output_done =
true;
212 tok.assign(start, next);
217 string_type m_kept_delims;
218 string_type m_dropped_delims;
224 bool is_kept(Char E)
const
226 if(m_kept_delims.length()){
227 return m_kept_delims.find(E) != string_type::npos;
228 }
else if(m_use_ispunct){
229 return std::ispunct(E) != 0;
234 bool is_dropped(Char E)
const
236 if(m_dropped_delims.length()){
237 return m_dropped_delims.find(E) != string_type::npos;
238 }
else if(m_use_isspace){
239 return std::isspace(E) != 0;
247 template <
class TokenizerFunc,
class Iterator,
class Type>
260 valid_ = f_(begin_,end_,tok_);
263 const Type& dereference()
const {
268 template<
class Other>
269 bool equal(
const Other& a)
const {
270 return (a.valid_ && valid_)
271 ?( (a.begin_==begin_) && (a.end_ == end_) )
279 valid_ = (begin_ != end_)?
280 f_(begin_,end_,tok_):
false;
287 : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
290 : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
308 return !(*
this == other);
312 return dereference();
316 return &dereference();
319 Iterator
base()
const{
return begin_;}
321 Iterator
end()
const{
return end_;}
332 class TokenizerFunc = CharSeparator<char>,
333 class Iterator = std::string::const_iterator,
334 class Type = std::string
345 template<
class Type,
class Iterator,
class TokenizerFunc>
350 return ret_type(fun,begin,end);
355 typename TokenizerFunc = CharSeparator<char>,
356 typename Iterator = std::string::const_iterator,
357 typename Type = std::string
379 Tokenizer(Iterator first, Iterator last,
const TokenizerFunc& f = TokenizerFunc())
380 : first_(first), last_(last), f_(f) { }
382 template <
typename Container>
384 : first_(c.
begin()), last_(c.
end()), f_() { }
386 template <
typename Container>
388 : first_(c.
begin()), last_(c.
end()), f_(f) { }
390 void assign(Iterator first, Iterator last){
395 void assign(Iterator first, Iterator last,
const TokenizerFunc& f){
400 template <
typename Container>
402 assign(c.begin(),c.end());
405 template <
typename Container>
406 void assign(
const Container& c,
const TokenizerFunc& f){
407 assign(c.begin(),c.end(),f);
Definition: Tokenizer.h:130
EmptyTokenPolicy
Definition: Tokenizer.h:127
bool operator!=(const TokenIterator &other) const
Definition: Tokenizer.h:307
bool operator()(InputIterator &next, InputIterator end, Token &tok)
Definition: Tokenizer.h:89
void assign(Iterator first, Iterator last)
Definition: Tokenizer.h:390
iter const_iterator
Definition: Tokenizer.h:367
bool at_end() const
Definition: Tokenizer.h:327
Definition: Tokenizer.h:359
@ KEEP_EMPTY_TOKENS
Definition: Tokenizer.h:127
Tokenizer(Iterator first, Iterator last, const TokenizerFunc &f=TokenizerFunc())
Definition: Tokenizer.h:379
TokenIterator(Iterator begin, Iterator e=Iterator())
Definition: Tokenizer.h:289
CharSeparator(const Char *dropped_delims, const Char *kept_delims=0, EmptyTokenPolicy empty_tokens=DROP_EMPTY_TOKENS)
Definition: Tokenizer.h:136
bool operator()(InputIterator &next, InputIterator end, Token &tok)
Definition: Tokenizer.h:162
void reset()
Definition: Tokenizer.h:86
Iterator base() const
Definition: Tokenizer.h:319
const typedef pointer const_pointer
Definition: Tokenizer.h:372
TokenIterator(TokenizerFunc f, Iterator begin, Iterator e=Iterator())
Definition: Tokenizer.h:286
const typedef value_type & const_reference
Definition: Tokenizer.h:370
TokenIteratorGenerator< TokenizerFunc, Iterator, Type >::type make_token_iterator(Iterator begin, Iterator end, const TokenizerFunc &fun)
Definition: Tokenizer.h:347
@ DROP_EMPTY_TOKENS
Definition: Tokenizer.h:127
Tokenizer(const TokenizerFunc &f)
Definition: Tokenizer.h:376
const Type & operator*() const
Definition: Tokenizer.h:311
TokenizerFunc tokenizer_function() const
Definition: Tokenizer.h:323
Definition: Tokenizer.h:336
Definition: Tokenizer.h:20
friend class iterator_core_access
Definition: Tokenizer.h:250
void size_type
Definition: Tokenizer.h:373
void assign(const Container &c, const TokenizerFunc &f)
Definition: Tokenizer.h:406
Definition: AbstractSceneLoader.h:11
Definition: Tokenizer.h:248
TokenIterator & operator++()
Definition: Tokenizer.h:292
void difference_type
Definition: Tokenizer.h:374
Iterator end() const
Definition: Tokenizer.h:321
Tokenizer(const Container &c)
Definition: Tokenizer.h:383
void assign(const Container &c)
Definition: Tokenizer.h:401
iter end() const
Definition: Tokenizer.h:411
void assign(Iterator first, Iterator last, const TokenizerFunc &f)
Definition: Tokenizer.h:395
Type current_token() const
Definition: Tokenizer.h:325
bool operator==(const TokenIterator &other) const
Definition: Tokenizer.h:303
Tokenizer(const Container &c, const TokenizerFunc &f)
Definition: Tokenizer.h:387
iter begin() const
Definition: Tokenizer.h:410
escaped_list_error(const std::string &what_arg)
Definition: Tokenizer.h:21
value_type * pointer
Definition: Tokenizer.h:371
iter iterator
Definition: Tokenizer.h:366
const Type * operator->() const
Definition: Tokenizer.h:315
TokenIterator operator++(int)
Definition: Tokenizer.h:297
TokenIterator< TokenizerFunc, Iterator, Type > type
Definition: Tokenizer.h:339
EscapedListSeparator(string_type e, string_type c, string_type q)
Definition: Tokenizer.h:83
TokenIterator()
Definition: Tokenizer.h:284
CharSeparator()
Definition: Tokenizer.h:153
value_type & reference
Definition: Tokenizer.h:369
void reset()
Definition: Tokenizer.h:159
Type value_type
Definition: Tokenizer.h:368
Definition: Tokenizer.h:26
EscapedListSeparator(Char e='\\', Char c=',', Char q='\"')
Definition: Tokenizer.h:80