#include <Read_Buffer.h>
Collaboration diagram for ACE_Read_Buffer:

Public Methods | |
| ACE_Read_Buffer (FILE *fp, int close_on_delete=0, ACE_Allocator *=0) | |
| Read from a FILE *. More... | |
| ACE_Read_Buffer (ACE_HANDLE handle, int close_on_delete=0, ACE_Allocator *=0) | |
| Read from an open HANDLE. More... | |
| ~ACE_Read_Buffer (void) | |
| Closes the FILE *. More... | |
| char * | read (int terminator=EOF, int search='\n', int replace='\0') |
| size_t | replaced (void) const |
| Returns the number of characters replaced during a <read>. More... | |
| size_t | size (void) const |
| Returns the size of the allocated buffer obtained during a <read>, not including the null terminator. More... | |
| ACE_Allocator * | alloc (void) const |
| Returns a pointer to its allocator. More... | |
| void | dump (void) const |
| Dump the state of the object. More... | |
Private Methods | |
| char * | rec_read (int term, int search, int replace) |
| Recursive helper method that does the work... More... | |
| void | operator= (const ACE_Read_Buffer &) |
| ACE_Read_Buffer (const ACE_Read_Buffer &) | |
Private Attributes | |
| size_t | size_ |
| The total number of characters in the buffer. More... | |
| size_t | occurrences_ |
| The total number of characters replaced. More... | |
| FILE * | stream_ |
| The stream we are reading from. More... | |
| int | close_on_delete_ |
| Keeps track of whether we should close the FILE in the destructor. More... | |
| ACE_Allocator * | allocator_ |
| Pointer to the allocator. More... | |
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 43 of file Read_Buffer.h.
|
||||||||||||||||
|
Read from a FILE *.
Definition at line 27 of file Read_Buffer.cpp. References ACE_TRACE, allocator_, and ACE_Allocator::instance.
00030 : stream_ (fp), 00031 close_on_delete_ (close_on_delete), 00032 allocator_ (alloc) 00033 { 00034 ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer"); 00035 if (this->allocator_ == 0) 00036 this->allocator_ = ACE_Allocator::instance (); 00037 } |
|
||||||||||||||||
|
Read from an open HANDLE.
Definition at line 40 of file Read_Buffer.cpp. References ACE_LIB_TEXT, ACE_TRACE, allocator_, and ACE_Allocator::instance.
00043 : stream_ (ACE_OS::fdopen (handle, ACE_LIB_TEXT ("r"))), 00044 close_on_delete_ (close_on_delete), 00045 allocator_ (alloc) 00046 { 00047 ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer"); 00048 00049 if (this->allocator_ == 0) 00050 this->allocator_ = ACE_Allocator::instance (); 00051 } |
|
|
Closes the FILE *.
Definition at line 54 of file Read_Buffer.cpp. References ACE_TRACE, close_on_delete_, and ACE_OS::fclose.
00055 {
00056 ACE_TRACE ("ACE_Read_Buffer::~ACE_Read_Buffer");
00057
00058 if (this->close_on_delete_)
00059 ACE_OS::fclose (this->stream_);
00060 }
|
|
|
|
|
|
Returns a pointer to its allocator.
Definition at line 23 of file Read_Buffer.i. References ACE_TRACE, and allocator_.
00024 {
00025 ACE_TRACE ("ACE_Read_Buffer::alloc");
00026 return this->allocator_;
00027 }
|
|
|
Dump the state of the object.
Definition at line 16 of file Read_Buffer.cpp. References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_TRACE, and LM_DEBUG.
00017 {
00018 ACE_TRACE ("ACE_Read_Buffer::dump");
00019 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00020 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("size_ = %d"), this->size_));
00021 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\noccurrences_ = %d"), this->occurrences_));
00022 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nstream_ = %x"), this->stream_));
00023 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nallocator_ = %x"), this->allocator_));
00024 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00025 }
|
|
|
|
|
||||||||||||||||
|
Returns a pointer dynamically allocated with <ACE_Allocator::malloc> to data from the input stream up to (and including) the <terminator>. If <search> is >= 0 then all occurrences of the <search> value are substituted with the <replace> value. The last of the byte of data is a 0, so that <strlen> 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 69 of file Read_Buffer.cpp. References ACE_TRACE, occurrences_, rec_read, and size_.
00070 {
00071 ACE_TRACE ("ACE_Read_Buffer::read");
00072 this->occurrences_ = 0;
00073 this->size_ = 0;
00074 return this->rec_read (term, search, replace);
00075 }
|
|
||||||||||||||||
|
Recursive helper method that does the work...
Definition at line 89 of file Read_Buffer.cpp. References ACE_TRACE, allocator_, ACE_Allocator::malloc, occurrences_, and size_. Referenced by read.
00090 {
00091 ACE_TRACE ("ACE_Read_Buffer::rec_read");
00092 // This is our temporary workspace.
00093 char buf[BUFSIZ];
00094
00095 int c = EOF;
00096 size_t slot = 0;
00097 int done = 0;
00098
00099 // Read in the file char by char
00100 while (slot < BUFSIZ)
00101 {
00102 c = getc (this->stream_);
00103
00104 // Don't insert EOF into the buffer...
00105 if (c == EOF)
00106 {
00107 ungetc (c, this->stream_);
00108 break;
00109 }
00110 else if (c == term)
00111 done = 1;
00112
00113 // Check for possible substitutions.
00114 if (c == search)
00115 {
00116 this->occurrences_++;
00117
00118 if (replace >= 0)
00119 c = replace;
00120 }
00121
00122 buf[slot++] = (char) c;
00123
00124 // Substitutions must be made before checking for termination.
00125 if (done)
00126 break;
00127 }
00128
00129 // Increment the number of bytes.
00130 this->size_ += slot;
00131
00132 // Don't bother going any farther if the total size is 0.
00133 if (this->size_ == 0)
00134 return 0;
00135
00136 char *result;
00137
00138 // Recurse, when the recursion bottoms out, allocate the result
00139 // buffer.
00140 if (done || c == EOF)
00141 {
00142 // Use the allocator to acquire the memory. The + 1 allows
00143 // space for the null terminator.
00144 result = (char *) this->allocator_->malloc (this->size_ + 1);
00145
00146 if (result == 0)
00147 {
00148 errno = ENOMEM;
00149 return 0;
00150 }
00151 result += this->size_;
00152
00153 // Null terminate the buffer.
00154 *result = '\0';
00155 }
00156 else if ((result = this->rec_read (term, search, replace)) == 0)
00157 return 0;
00158
00159 // Copy buf into the appropriate location starting from end of
00160 // buffer. Peter says this is confusing and that we should use
00161 // memcpy() ;-)
00162 for (size_t j = slot; j > 0; j--)
00163 *--result = buf[j - 1];
00164
00165 return result;
00166 }
|
|
|
Returns the number of characters replaced during a <read>.
Definition at line 16 of file Read_Buffer.i. References ACE_TRACE, and occurrences_.
00017 {
00018 ACE_TRACE ("ACE_Read_Buffer::replaced");
00019 return this->occurrences_;
00020 }
|
|
|
Returns the size of the allocated buffer obtained during a <read>, not including the null terminator.
Definition at line 7 of file Read_Buffer.i. References ACE_TRACE, and size_.
|
|
|
Pointer to the allocator.
Definition at line 109 of file Read_Buffer.h. Referenced by ACE_Read_Buffer, alloc, and rec_read. |
|
|
Keeps track of whether we should close the FILE in the destructor.
Definition at line 106 of file Read_Buffer.h. Referenced by ~ACE_Read_Buffer. |
|
|
The total number of characters replaced.
Definition at line 99 of file Read_Buffer.h. |
|
|
The total number of characters in the buffer.
Definition at line 96 of file Read_Buffer.h. |
|
|
The stream we are reading from.
Definition at line 102 of file Read_Buffer.h. |
1.2.14 written by Dimitri van Heesch,
© 1997-2002