ACE_Tokenizer Class Reference

Tokenizer. More...

#include <SString.h>

Collaboration diagram for ACE_Tokenizer:

Collaboration graph
[legend]
List of all members.

Public Types

enum  { MAX_DELIMITERS = 16, MAX_PRESERVES = 16 }

Public Member Functions

 ACE_Tokenizer (ACE_TCHAR *buffer)
int delimiter (ACE_TCHAR d)
int delimiter_replace (ACE_TCHAR d, ACE_TCHAR replacement)
int preserve_designators (ACE_TCHAR start, ACE_TCHAR stop, int strip=1)
ACE_TCHARnext (void)
 Returns the next token.


Protected Member Functions

int is_delimiter (ACE_TCHAR d, int &replace, ACE_TCHAR &r)
int is_preserve_designator (ACE_TCHAR start, ACE_TCHAR &stop, int &strip)

Protected Attributes

ACE_TCHARbuffer_
int index_
Preserve_Entry preserves_ [MAX_PRESERVES]
 The application can specify MAX_PRESERVES preserve designators.

int preserves_index_
 Pointer to the next free spot in preserves_.

Delimiter_Entry delimiters_ [MAX_DELIMITERS]
 The tokenizer allows MAX_DELIMITERS number of delimiters.

int delimiter_index_
 Pointer to the next free space in delimiters_.


Detailed Description

Tokenizer.

Tokenizes a buffer. Allows application to set delimiters and preserve designators. Does not allow special characters, yet (e.g., printf ("\"like a quoted string\"")).

Definition at line 265 of file SString.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
MAX_DELIMITERS 
MAX_PRESERVES 

Definition at line 375 of file SString.h.

00375        {
00376     MAX_DELIMITERS=16,
00377     MAX_PRESERVES=16
00378   };


Constructor & Destructor Documentation

ACE_Tokenizer::ACE_Tokenizer ACE_TCHAR buffer  ) 
 

buffer will be parsed. Notice that ACE_Tokenizer will modify buffer if you use delimiter_replace or preserve_designators to do character substitution.

Note:
You should NOT pass a constant string or string literal to this constructor, since ACE_Tokenizer will try to modify the string.
See also:
preserve_designators

preserve_designators

Definition at line 337 of file SString.cpp.

References ACE_TCHAR.

00338   : buffer_ (buffer),
00339     index_ (0),
00340     preserves_index_ (0),
00341     delimiter_index_ (0)
00342 {
00343 }


Member Function Documentation

int ACE_Tokenizer::delimiter ACE_TCHAR  d  ) 
 

d is a delimiter.

Returns:
Returns 0 on success, -1 if there is no memory left.
Example:
char buf[30]; ACE_OS::strcpy(buf, "William/Joseph/Hagins"); ACE_Tokenizer tok (buf); tok.delimiter ('/'); for (char *p = tok.next (); p; p = tok.next ()) cout << p << endl;

This will print out:

William/Joseph/Hagins Joseph/Hagins Hagins

Definition at line 346 of file SString.cpp.

References ACE_TCHAR, ACE_Tokenizer::Delimiter_Entry::delimiter_, delimiter_index_, delimiters_, MAX_DELIMITERS, and ACE_Tokenizer::Delimiter_Entry::replace_.

Referenced by ACE::get_ip_interfaces().

00347 {
00348   if (delimiter_index_ == MAX_DELIMITERS)
00349     return -1;
00350 
00351   delimiters_[delimiter_index_].delimiter_ = d;
00352   delimiters_[delimiter_index_].replace_ = 0;
00353   delimiter_index_++;
00354   return 0;
00355 }

int ACE_Tokenizer::delimiter_replace ACE_TCHAR  d,
ACE_TCHAR  replacement
 

d is a delimiter and, when found, will be replaced by replacement.

Returns:
0 on success, -1 if there is no memory left.
Example:
char buf[30]; ACE_OS::strcpy(buf, "William/Joseph/Hagins"); ACE_Tokenizer tok (buf); tok.delimiter_replace ('/', 0); for (char *p = tok.next (); p; p = tok.next ()) cout << p << endl;

This will print out:

William Joseph Hagins

Definition at line 358 of file SString.cpp.

References ACE_TCHAR, ACE_Tokenizer::Delimiter_Entry::delimiter_, delimiter_index_, delimiters_, MAX_DELIMITERS, ACE_Tokenizer::Delimiter_Entry::replace_, and ACE_Tokenizer::Delimiter_Entry::replacement_.

Referenced by ACE_Process_Options::command_line_argv(), and ACE_Configuration::expand_path().

00360 {
00361   // Make it possible to replace delimiters on-the-fly, e.g., parse
00362   // string until certain token count and then copy rest of the
00363   // original string.
00364   for (int i = 0; i < delimiter_index_; i++)
00365     if (delimiters_[i].delimiter_ == d)
00366       {
00367         delimiters_[i].replacement_ = replacement;
00368         delimiters_[i].replace_ = 1;
00369         return 0;
00370       }
00371 
00372   if (delimiter_index_ >= MAX_DELIMITERS)
00373     return -1;
00374 
00375   delimiters_[delimiter_index_].delimiter_ = d;
00376   delimiters_[delimiter_index_].replacement_ = replacement;
00377   delimiters_[delimiter_index_].replace_ = 1;
00378   delimiter_index_++;
00379   return 0;
00380 }

int ACE_Tokenizer::is_delimiter ACE_TCHAR  d,
int &  replace,
ACE_TCHAR r
[protected]
 

Returns 1 if is a delimiter, 0 otherwise. If should be replaced with , is set to 1, otherwise 0.

Definition at line 398 of file SString.cpp.

References ACE_TCHAR, ACE_Tokenizer::Delimiter_Entry::delimiter_, delimiter_index_, delimiters_, ACE_Tokenizer::Delimiter_Entry::replace_, and ACE_Tokenizer::Delimiter_Entry::replacement_.

Referenced by next().

00401 {
00402   replace = 0;
00403 
00404   for (int x = 0; x < delimiter_index_; x++)
00405     if (delimiters_[x].delimiter_ == d)
00406       {
00407         if (delimiters_[x].replace_)
00408           {
00409             r = delimiters_[x].replacement_;
00410             replace = 1;
00411           }
00412         return 1;
00413       }
00414 
00415   return 0;
00416 }

int ACE_Tokenizer::is_preserve_designator ACE_TCHAR  start,
ACE_TCHAR stop,
int &  strip
[protected]
 

If is a start preserve designator, returns 1 and sets to the stop designator. Returns 0 if is not a preserve designator.

Definition at line 419 of file SString.cpp.

References ACE_TCHAR, preserves_, preserves_index_, ACE_Tokenizer::Preserve_Entry::start_, and ACE_Tokenizer::Preserve_Entry::stop_.

Referenced by next().

00422 {
00423   for (int x = 0; x < preserves_index_; x++)
00424     if (preserves_[x].start_ == start)
00425       {
00426         stop = preserves_[x].stop_;
00427         strip = preserves_[x].strip_;
00428         return 1;
00429       }
00430 
00431   return 0;
00432 }

ACE_TCHAR * ACE_Tokenizer::next void   ) 
 

Returns the next token.

Definition at line 435 of file SString.cpp.

References ACE_TCHAR, is_delimiter(), and is_preserve_designator().

Referenced by ACE_Process_Options::command_line_argv(), ACE_Configuration::expand_path(), and ACE::get_ip_interfaces().

00436 {
00437   // Check if the previous pass was the last one in the buffer.
00438   if (index_ == -1)
00439     {
00440       index_ = 0;
00441       return 0;
00442     }
00443 
00444   ACE_TCHAR replacement = 0;
00445   int replace;
00446   ACE_TCHAR *next_token;
00447 
00448   // Skip all leading delimiters.
00449   for (;;)
00450     {
00451       // Check for end of string.
00452       if (buffer_[index_] == '\0')
00453         {
00454           // If we hit EOS at the start, return 0.
00455           index_ = 0;
00456           return 0;
00457         }
00458 
00459       if (this->is_delimiter (buffer_[index_],
00460                               replace,
00461                               replacement))
00462         index_++;
00463       else
00464         break;
00465     }
00466 
00467   // When we reach this point, buffer_[index_] is a non-delimiter and
00468   // not EOS - the start of our next_token.
00469   next_token = buffer_ + index_;
00470 
00471   // A preserved region is it's own token.
00472   ACE_TCHAR stop;
00473   int strip;
00474   if (this->is_preserve_designator (buffer_[index_],
00475                                     stop,
00476                                     strip))
00477     {
00478       while (++index_)
00479         {
00480           if (buffer_[index_] == '\0')
00481             {
00482               index_ = -1;
00483               goto EXIT_LABEL;
00484             }
00485 
00486           if (buffer_[index_] == stop)
00487             break;
00488         }
00489 
00490       if (strip)
00491         {
00492           // Skip start preserve designator.
00493           next_token += 1;
00494           // Zap the stop preserve designator.
00495           buffer_[index_] = '\0';
00496           // Increment to the next token.
00497           index_++;
00498         }
00499 
00500       goto EXIT_LABEL;
00501     }
00502 
00503   // Step through finding the next delimiter or EOS.
00504   for (;;)
00505     {
00506       // Advance pointer.
00507       index_++;
00508 
00509       // Check for delimiter.
00510       if (this->is_delimiter (buffer_[index_],
00511                               replace,
00512                               replacement))
00513         {
00514           // Replace the delimiter.
00515           if (replace != 0)
00516             buffer_[index_] = replacement;
00517 
00518           // Move the pointer up and return.
00519           index_++;
00520           goto EXIT_LABEL;
00521         }
00522 
00523       // A preserve designator signifies the end of this token.
00524       if (this->is_preserve_designator (buffer_[index_],
00525                                         stop,
00526                                         strip))
00527         goto EXIT_LABEL;
00528 
00529       // Check for end of string.
00530       if (buffer_[index_] == '\0')
00531         {
00532           index_ = -1;
00533           goto EXIT_LABEL;
00534         }
00535     }
00536 
00537 EXIT_LABEL:
00538   return next_token;
00539 }

int ACE_Tokenizer::preserve_designators ACE_TCHAR  start,
ACE_TCHAR  stop,
int  strip = 1
 

Extract string between a pair of designator characters. For instance, quotes, or '(' and ')'. start specifies the begin designator. stop specifies the end designator. strip If strip == 1, then the preserve designators will be stripped from the tokens returned by next.

Returns:
0 on success, -1 if there is no memory left.
Example with strip = 0:
char buf[30]; ACE_OS::strcpy(buf, "William(Joseph)Hagins"); ACE_Tokenizer tok (buf); tok.preserve_designators ('(', ')', 0); for (char *p = tok.next (); p; p = tok.next ()) cout << p << endl;

This will print out:

William(Joseph)Hagins (Joseph)Hagins )Hagins

Example with strip = 1:

char buf[30]; ACE_OS::strcpy(buf, "William(Joseph)Hagins"); ACE_Tokenizer tok (buf); tok.preserve_designators ('(', ')', 1); for (char *p = tok.next (); p; p = tok.next ()) cout << p << endl;

This will print out:

William Joseph Hagins

Definition at line 383 of file SString.cpp.

References ACE_TCHAR, MAX_PRESERVES, preserves_, preserves_index_, ACE_Tokenizer::Preserve_Entry::start_, ACE_Tokenizer::Preserve_Entry::stop_, and ACE_Tokenizer::Preserve_Entry::strip_.

Referenced by ACE_Process_Options::command_line_argv().

00386 {
00387   if (preserves_index_ == MAX_PRESERVES)
00388     return -1;
00389 
00390   preserves_[preserves_index_].start_ = start;
00391   preserves_[preserves_index_].stop_ = stop;
00392   preserves_[preserves_index_].strip_ = strip;
00393   preserves_index_++;
00394   return 0;
00395 }


Member Data Documentation

ACE_TCHAR* ACE_Tokenizer::buffer_ [protected]
 

Definition at line 392 of file SString.h.

int ACE_Tokenizer::delimiter_index_ [protected]
 

Pointer to the next free space in delimiters_.

Definition at line 452 of file SString.h.

Referenced by delimiter(), delimiter_replace(), and is_delimiter().

Delimiter_Entry ACE_Tokenizer::delimiters_[MAX_DELIMITERS] [protected]
 

The tokenizer allows MAX_DELIMITERS number of delimiters.

Definition at line 449 of file SString.h.

Referenced by delimiter(), delimiter_replace(), and is_delimiter().

int ACE_Tokenizer::index_ [protected]
 

Definition at line 393 of file SString.h.

Preserve_Entry ACE_Tokenizer::preserves_[MAX_PRESERVES] [protected]
 

The application can specify MAX_PRESERVES preserve designators.

Definition at line 421 of file SString.h.

Referenced by is_preserve_designator(), and preserve_designators().

int ACE_Tokenizer::preserves_index_ [protected]
 

Pointer to the next free spot in preserves_.

Definition at line 424 of file SString.h.

Referenced by is_preserve_designator(), and preserve_designators().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:31:51 2006 for ACE by doxygen 1.3.6