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

Read_Buffer.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Read_Buffer.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00003 
00004 #include "ace/Read_Buffer.h"
00005 #include "ace/Log_Msg.h"
00006 #include "ace/Malloc_Base.h"
00007 #include "ace/Service_Config.h"
00008 
00009 #if !defined (__ACE_INLINE__)
00010 #include "ace/Read_Buffer.i"
00011 #endif /* __ACE_INLINE__ */
00012 
00013 ACE_RCSID(ace, Read_Buffer, "$Id: Read_Buffer.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00014 
00015 void
00016 ACE_Read_Buffer::dump (void) const
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 }
00026 
00027 ACE_Read_Buffer::ACE_Read_Buffer (FILE *fp,
00028                                   int close_on_delete,
00029                                   ACE_Allocator *alloc)
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 }
00038 
00039 #if !defined (ACE_HAS_WINCE)
00040 ACE_Read_Buffer::ACE_Read_Buffer (ACE_HANDLE handle,
00041                                   int close_on_delete,
00042                                   ACE_Allocator *alloc)
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 }
00052 #endif  // ACE_HAS_WINCE
00053 
00054 ACE_Read_Buffer::~ACE_Read_Buffer (void)
00055 {
00056   ACE_TRACE ("ACE_Read_Buffer::~ACE_Read_Buffer");
00057 
00058   if (this->close_on_delete_)
00059     ACE_OS::fclose (this->stream_);
00060 }
00061 
00062 // Input: term        the character to terminate on
00063 //        search      the character to search for
00064 //        replace     the character with which to replace search
00065 // Output: a buffer containing the contents of stream
00066 // Method: call the recursive helper function read_helper
00067 
00068 char *
00069 ACE_Read_Buffer::read (int term, int search, int replace)
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 }
00076 
00077 // Input: term       the termination character
00078 //        search     the character to search for
00079 //        replace    the character with which to replace search
00080 // Purpose: read in a file to a buffer using only a single dynamic
00081 //          allocation.
00082 // Method: read until the local buffer is full and then recurse.
00083 //         Must continue until the termination character is reached.
00084 //         Allocate the final buffer based on the number of local
00085 //         buffers read and as the recursive calls bottom out,
00086 //         copy them in reverse order into the allocated buffer.
00087 
00088 char *
00089 ACE_Read_Buffer::rec_read (int term, int search, int replace)
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 }

Generated on Mon Jun 16 11:21:02 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002