ACE_Tokenizer Class Reference

Tokenizer. More...

#include <SString.h>

Collaboration diagram for ACE_Tokenizer:

Collaboration graph
[legend]
List of all members.

Public Types

 MAX_DELIMITERS = 16
 MAX_PRESERVES = 16
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_.

Classes

class  Delimiter_Entry
 Delimiter Entry. More...
class  Preserve_Entry
 Preserve Entry. 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 271 of file SString.h.


Member Enumeration Documentation

anonymous enum

Enumerator:
MAX_DELIMITERS 
MAX_PRESERVES 

Definition at line 381 of file SString.h.

00381        {
00382     MAX_DELIMITERS=16,
00383     MAX_PRESERVES=16
00384   };


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

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


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

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

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

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

References 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().

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

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

References delimiter_index_, and delimiters_.

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

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

References preserves_, preserves_index_, and ACE_Tokenizer::Preserve_Entry::strip_.

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

ACE_TCHAR * ACE_Tokenizer::next ( void   ) 

Returns the next token.

Definition at line 437 of file SString.cpp.

References buffer_, and index_.

Referenced by ACE_Process_Options::command_line_argv().

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

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

References 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().

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


Member Data Documentation

ACE_TCHAR* ACE_Tokenizer::buffer_ [protected]

Definition at line 398 of file SString.h.

Referenced by next().

int ACE_Tokenizer::delimiter_index_ [protected]

Pointer to the next free space in delimiters_.

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

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

int ACE_Tokenizer::index_ [protected]

Definition at line 399 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 427 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 430 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 Tue Feb 2 17:35:49 2010 for ACE by  doxygen 1.4.7