ACE_Read_Buffer Class Reference

Efficiently reads an artibrarily large buffer from an input stream up to and including a termination character. Also performs search/replace on single occurrences a character in the buffer using the principles of Integrated Layer Processing. More...

#include <Read_Buffer.h>

Collaboration diagram for ACE_Read_Buffer:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Read_Buffer (FILE *fp, int close_on_delete=0, ACE_Allocator *=0)
 Read from a FILE *.

 ACE_Read_Buffer (ACE_HANDLE handle, int close_on_delete=0, ACE_Allocator *=0)
 Read from an open HANDLE.

 ~ACE_Read_Buffer (void)
 Closes the FILE *.

char * read (int terminator=EOF, int search= '\n', int replace= '\0')
size_t replaced (void) const
 Returns the number of characters replaced during a .

size_t size (void) const
ACE_Allocatoralloc (void) const
 Returns a pointer to its allocator.

void dump (void) const
 Dump the state of the object.


Private Member Functions

void operator= (const ACE_Read_Buffer &)
 ACE_Read_Buffer (const ACE_Read_Buffer &)
char * rec_read (int term, int search, int replace)
 Recursive helper method that does the work...


Private Attributes

size_t size_
 The total number of characters in the buffer.

size_t occurrences_
 The total number of characters replaced.

FILE * stream_
 The stream we are reading from.

int close_on_delete_
ACE_Allocatorallocator_
 Pointer to the allocator.


Detailed Description

Efficiently reads an artibrarily large buffer from an input stream up to and including a termination character. Also performs search/replace on single occurrences a character in the buffer using the principles of Integrated Layer Processing.

This implementation is optimized to do a single dynamic allocation and make only one copy of the data. It uses recursion and the run-time stack to accomplish this efficiently.

Definition at line 46 of file Read_Buffer.h.


Constructor & Destructor Documentation

ACE_Read_Buffer::ACE_Read_Buffer FILE *  fp,
int  close_on_delete = 0,
ACE_Allocator = 0
 

Read from a FILE *.

Definition at line 35 of file Read_Buffer.cpp.

References ACE_TRACE, and ACE_Allocator::instance().

00038   : stream_ (fp),
00039     close_on_delete_ (close_on_delete),
00040     allocator_ (alloc)
00041 {
00042   ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer");
00043   if (this->allocator_ == 0)
00044     this->allocator_ = ACE_Allocator::instance ();
00045 }

ACE_Read_Buffer::ACE_Read_Buffer ACE_HANDLE  handle,
int  close_on_delete = 0,
ACE_Allocator = 0
 

Read from an open HANDLE.

Definition at line 48 of file Read_Buffer.cpp.

References ACE_LIB_TEXT, ACE_TRACE, and ACE_Allocator::instance().

00051   : stream_ (ACE_OS::fdopen (handle, ACE_LIB_TEXT ("r"))),
00052     close_on_delete_ (close_on_delete),
00053     allocator_ (alloc)
00054 {
00055   ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer");
00056 
00057   if (this->allocator_ == 0)
00058     this->allocator_ = ACE_Allocator::instance ();
00059 }

ACE_Read_Buffer::~ACE_Read_Buffer void   ) 
 

Closes the FILE *.

Definition at line 62 of file Read_Buffer.cpp.

References ACE_TRACE, close_on_delete_, and ACE_OS::fclose().

00063 {
00064   ACE_TRACE ("ACE_Read_Buffer::~ACE_Read_Buffer");
00065 
00066   if (this->close_on_delete_)
00067     ACE_OS::fclose (this->stream_);
00068 }

ACE_Read_Buffer::ACE_Read_Buffer const ACE_Read_Buffer  )  [private]
 


Member Function Documentation

ACE_INLINE ACE_Allocator * ACE_Read_Buffer::alloc void   )  const
 

Returns a pointer to its allocator.

Definition at line 26 of file Read_Buffer.inl.

References ACE_TRACE.

00027 {
00028   ACE_TRACE ("ACE_Read_Buffer::alloc");
00029   return this->allocator_;
00030 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_Read_Buffer::dump void   )  const
 

Dump the state of the object.

Definition at line 22 of file Read_Buffer.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_TRACE, and LM_DEBUG.

00023 {
00024 #if defined (ACE_HAS_DUMP)
00025   ACE_TRACE ("ACE_Read_Buffer::dump");
00026   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00027   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("size_ = %d"), this->size_));
00028   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\noccurrences_ = %d"), this->occurrences_));
00029   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\nstream_ = %x"), this->stream_));
00030   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\nallocator_ = %x"), this->allocator_));
00031   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00032 #endif /* ACE_HAS_DUMP */
00033 }

void ACE_Read_Buffer::operator= const ACE_Read_Buffer  )  [private]
 

char * ACE_Read_Buffer::read int  terminator = EOF,
int  search = '\n',
int  replace = '\0'
 

Returns a pointer dynamically allocated with <ACE_Allocator::malloc> to data from the input stream up to (and including) the . If is >= 0 then all occurrences of the value are substituted with the value. The last of the byte of data is a 0, so that can be used on it. The caller is responsible for freeing the pointer returned from this method using the <ACE_Allocator::free>.

Definition at line 77 of file Read_Buffer.cpp.

References ACE_TRACE, occurrences_, and rec_read().

00078 {
00079   ACE_TRACE ("ACE_Read_Buffer::read");
00080   this->occurrences_ = 0;
00081   this->size_ = 0;
00082   return this->rec_read (term, search, replace);
00083 }

char * ACE_Read_Buffer::rec_read int  term,
int  search,
int  replace
[private]
 

Recursive helper method that does the work...

Definition at line 97 of file Read_Buffer.cpp.

References ACE_TRACE, ACE_Allocator::malloc(), and occurrences_.

Referenced by read().

00098 {
00099   ACE_TRACE ("ACE_Read_Buffer::rec_read");
00100   // This is our temporary workspace.
00101   char buf[BUFSIZ];
00102 
00103   int c = EOF;
00104   size_t slot = 0;
00105   int done = 0;
00106 
00107   // Read in the file char by char
00108   while (slot < BUFSIZ)
00109     {
00110       c = getc (this->stream_);
00111 
00112       // Don't insert EOF into the buffer...
00113       if (c == EOF)
00114         {
00115           ungetc (c, this->stream_);
00116           break;
00117         }
00118       else if (c == term)
00119         done = 1;
00120 
00121       // Check for possible substitutions.
00122       if (c == search)
00123         {
00124           this->occurrences_++;
00125 
00126           if (replace >= 0)
00127             c = replace;
00128         }
00129 
00130       buf[slot++] = (char) c;
00131 
00132       // Substitutions must be made before checking for termination.
00133       if (done)
00134         break;
00135     }
00136 
00137   // Increment the number of bytes.
00138   this->size_ += slot;
00139 
00140   // Don't bother going any farther if the total size is 0.
00141   if (this->size_ == 0)
00142     return 0;
00143 
00144   char *result;
00145 
00146   // Recurse, when the recursion bottoms out, allocate the result
00147   // buffer.
00148   if (done || c == EOF)
00149     {
00150       // Use the allocator to acquire the memory.  The + 1 allows
00151       // space for the null terminator.
00152       result = (char *) this->allocator_->malloc (this->size_ + 1);
00153 
00154       if (result == 0)
00155         {
00156           errno = ENOMEM;
00157           return 0;
00158         }
00159       result += this->size_;
00160 
00161       // Null terminate the buffer.
00162       *result = '\0';
00163     }
00164   else if ((result = this->rec_read (term, search, replace)) == 0)
00165     return 0;
00166 
00167   // Copy buf into the appropriate location starting from end of
00168   // buffer.  Peter says this is confusing and that we should use
00169   // memcpy() ;-)
00170   for (size_t j = slot; j > 0; j--)
00171     *--result = buf[j - 1];
00172 
00173   return result;
00174 }

ACE_INLINE size_t ACE_Read_Buffer::replaced void   )  const
 

Returns the number of characters replaced during a .

Definition at line 19 of file Read_Buffer.inl.

References ACE_TRACE, and occurrences_.

00020 {
00021   ACE_TRACE ("ACE_Read_Buffer::replaced");
00022   return this->occurrences_;
00023 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE size_t ACE_Read_Buffer::size void   )  const
 

Returns the size of the allocated buffer obtained during a read, not including the null terminator.

Definition at line 10 of file Read_Buffer.inl.

References ACE_TRACE.

00011 {
00012   ACE_TRACE ("ACE_Read_Buffer::size");
00013   return this->size_;
00014 }


Member Data Documentation

ACE_Allocator* ACE_Read_Buffer::allocator_ [private]
 

Pointer to the allocator.

Definition at line 118 of file Read_Buffer.h.

int ACE_Read_Buffer::close_on_delete_ [private]
 

Keeps track of whether we should close the FILE in the destructor.

Definition at line 115 of file Read_Buffer.h.

Referenced by ~ACE_Read_Buffer().

size_t ACE_Read_Buffer::occurrences_ [private]
 

The total number of characters replaced.

Definition at line 108 of file Read_Buffer.h.

Referenced by read(), rec_read(), and replaced().

size_t ACE_Read_Buffer::size_ [private]
 

The total number of characters in the buffer.

Definition at line 105 of file Read_Buffer.h.

FILE* ACE_Read_Buffer::stream_ [private]
 

The stream we are reading from.

Definition at line 111 of file Read_Buffer.h.


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