Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Tokenizer Class Reference

Tokenizer. More...

#include <SString.h>

List of all members.

Public Types

enum  { MAX_DELIMITERS = 16, MAX_PRESERVES = 16 }

Public Methods

 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. More...


Protected Methods

int is_delimiter (ACE_TCHAR d, int &replace, ACE_TCHAR &r)
 Returns 1 if <d> is a delimiter, 0 otherwise. If <d> should be replaced with <r>, <replace> is set to 1, otherwise 0. More...

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. More...

int preserves_index_
 Pointer to the next free spot in preserves_. More...

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

int delimiter_index_
 Pointer to the next free space in delimiters_. More...


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 258 of file SString.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
MAX_DELIMITERS 
MAX_PRESERVES 

Definition at line 368 of file SString.h.

00368        {
00369     MAX_DELIMITERS=16,
00370     MAX_PRESERVES=16
00371   };


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 21 of file SString.cpp.

References ACE_TCHAR.

00022   : buffer_ (buffer),
00023     index_ (0),
00024     preserves_index_ (0),
00025     delimiter_index_ (0)
00026 {
00027 }


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 30 of file SString.cpp.

References ACE_TCHAR, delimiter_index_, delimiters_, and MAX_DELIMITERS.

Referenced by ACE_Sock_Connect::get_ip_interfaces.

00031 {
00032   if (delimiter_index_ == MAX_DELIMITERS)
00033     return -1;
00034 
00035   delimiters_[delimiter_index_].delimiter_ = d;
00036   delimiters_[delimiter_index_].replace_ = 0;
00037   delimiter_index_++;
00038   return 0;
00039 }

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 42 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.

00044 {
00045   // Make it possible to replace delimiters on-the-fly, e.g., parse
00046   // string until certain token count and then copy rest of the
00047   // original string.
00048   for (int i = 0; i < delimiter_index_; i++)
00049     if (delimiters_[i].delimiter_ == d)
00050       {
00051         delimiters_[i].replacement_ = replacement;
00052         delimiters_[i].replace_ = 1;
00053         return 0;
00054       }
00055 
00056   if (delimiter_index_ >= MAX_DELIMITERS)
00057     return -1;
00058 
00059   delimiters_[delimiter_index_].delimiter_ = d;
00060   delimiters_[delimiter_index_].replacement_ = replacement;
00061   delimiters_[delimiter_index_].replace_ = 1;
00062   delimiter_index_++;
00063   return 0;
00064 }

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

Returns 1 if <d> is a delimiter, 0 otherwise. If <d> should be replaced with <r>, <replace> is set to 1, otherwise 0.

Definition at line 82 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.

00085 {
00086   replace = 0;
00087 
00088   for (int x = 0; x < delimiter_index_; x++)
00089     if (delimiters_[x].delimiter_ == d)
00090       {
00091         if (delimiters_[x].replace_)
00092           {
00093             r = delimiters_[x].replacement_;
00094             replace = 1;
00095           }
00096         return 1;
00097       }
00098 
00099   return 0;
00100 }

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

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

Definition at line 103 of file SString.cpp.

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

Referenced by next.

00106 {
00107   for (int x = 0; x < preserves_index_; x++)
00108     if (preserves_[x].start_ == start)
00109       {
00110         stop = preserves_[x].stop_;
00111         strip = preserves_[x].strip_;
00112         return 1;
00113       }
00114 
00115   return 0;
00116 }

ACE_TCHAR * ACE_Tokenizer::next void   
 

Returns the next token.

Definition at line 119 of file SString.cpp.

References ACE_TCHAR, buffer_, index_, is_delimiter, and is_preserve_designator.

Referenced by ACE_Process_Options::command_line_argv, ACE_Configuration::expand_path, and ACE_Sock_Connect::get_ip_interfaces.

00120 {
00121   // Check if the previous pass was the last one in the buffer.
00122   if (index_ == -1)
00123     {
00124       index_ = 0;
00125       return 0;
00126     }
00127 
00128   ACE_TCHAR replacement;
00129   int replace;
00130   ACE_TCHAR *next_token;
00131 
00132   // Skip all leading delimiters.
00133   for (;;)
00134     {
00135       // Check for end of string.
00136       if (buffer_[index_] == '\0')
00137         {
00138           // If we hit EOS at the start, return 0.
00139           index_ = 0;
00140           return 0;
00141         }
00142 
00143       if (this->is_delimiter (buffer_[index_],
00144                               replace,
00145                               replacement))
00146         index_++;
00147       else
00148         break;
00149     }
00150 
00151   // When we reach this point, buffer_[index_] is a non-delimiter and
00152   // not EOS - the start of our next_token.
00153   next_token = buffer_ + index_;
00154 
00155   // A preserved region is it's own token.
00156   ACE_TCHAR stop;
00157   int strip;
00158   if (this->is_preserve_designator (buffer_[index_],
00159                                     stop,
00160                                     strip))
00161     {
00162       while (++index_)
00163         {
00164           if (buffer_[index_] == '\0')
00165             {
00166               index_ = -1;
00167               goto EXIT_LABEL;
00168             }
00169 
00170           if (buffer_[index_] == stop)
00171             break;
00172         }
00173 
00174       if (strip)
00175         {
00176           // Skip start preserve designator.
00177           next_token += 1;
00178           // Zap the stop preserve designator.
00179           buffer_[index_] = '\0';
00180           // Increment to the next token.
00181           index_++;
00182         }
00183 
00184       goto EXIT_LABEL;
00185     }
00186 
00187   // Step through finding the next delimiter or EOS.
00188   for (;;)
00189     {
00190       // Advance pointer.
00191       index_++;
00192 
00193       // Check for delimiter.
00194       if (this->is_delimiter (buffer_[index_],
00195                               replace,
00196                               replacement))
00197         {
00198           // Replace the delimiter.
00199           if (replace != 0)
00200             buffer_[index_] = replacement;
00201 
00202           // Move the pointer up and return.
00203           index_++;
00204           goto EXIT_LABEL;
00205         }
00206 
00207       // A preserve designator signifies the end of this token.
00208       if (this->is_preserve_designator (buffer_[index_],
00209                                         stop,
00210                                         strip))
00211         goto EXIT_LABEL;
00212 
00213       // Check for end of string.
00214       if (buffer_[index_] == '\0')
00215         {
00216           index_ = -1;
00217           goto EXIT_LABEL;
00218         }
00219     }
00220 
00221 EXIT_LABEL:
00222   return next_token;
00223 }

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 67 of file SString.cpp.

References ACE_TCHAR, MAX_PRESERVES, preserves_, and preserves_index_.

Referenced by ACE_Process_Options::command_line_argv.

00070 {
00071   if (preserves_index_ == MAX_PRESERVES)
00072     return -1;
00073 
00074   preserves_[preserves_index_].start_ = start;
00075   preserves_[preserves_index_].stop_ = stop;
00076   preserves_[preserves_index_].strip_ = strip;
00077   preserves_index_++;
00078   return 0;
00079 }


Member Data Documentation

ACE_TCHAR* ACE_Tokenizer::buffer_ [protected]
 

Definition at line 385 of file SString.h.

Referenced by next.

int ACE_Tokenizer::delimiter_index_ [protected]
 

Pointer to the next free space in delimiters_.

Definition at line 445 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 442 of file SString.h.

Referenced by delimiter, delimiter_replace, and is_delimiter.

int ACE_Tokenizer::index_ [protected]
 

Definition at line 386 of file SString.h.

Referenced by next.

Preserve_Entry ACE_Tokenizer::preserves_[MAX_PRESERVES] [protected]
 

The application can specify MAX_PRESERVES preserve designators.

Definition at line 414 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 417 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 Mon Jun 16 12:58:32 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002