Use override/default for RTPortableServer
[ACE_TAO.git] / ACE / ace / Read_Buffer.cpp
blobba1740651814d2b65824f1068cf2786ed344726d
1 #include "ace/Read_Buffer.h"
3 #include "ace/config-all.h"
5 #if !defined (__ACE_INLINE__)
6 #include "ace/Read_Buffer.inl"
7 #endif /* __ACE_INLINE__ */
9 #include "ace/Log_Category.h"
10 #include "ace/Malloc_Base.h"
11 #include "ace/Service_Config.h"
12 #include "ace/OS_NS_stdio.h"
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 void
17 ACE_Read_Buffer::dump () const
19 #if defined (ACE_HAS_DUMP)
20 ACE_TRACE ("ACE_Read_Buffer::dump");
21 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
22 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("size_ = %d"), this->size_));
23 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\noccurrences_ = %d"), this->occurrences_));
24 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nstream_ = %x"), this->stream_));
25 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nallocator_ = %x"), this->allocator_));
26 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
27 #endif /* ACE_HAS_DUMP */
30 ACE_Read_Buffer::ACE_Read_Buffer (FILE *fp,
31 bool close_on_delete,
32 ACE_Allocator *alloc)
33 : size_ (0),
34 occurrences_ (0),
35 stream_ (fp),
36 close_on_delete_ (close_on_delete),
37 allocator_ (alloc)
39 ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer");
40 if (this->allocator_ == 0)
41 this->allocator_ = ACE_Allocator::instance ();
44 ACE_Read_Buffer::ACE_Read_Buffer (ACE_HANDLE handle,
45 bool close_on_delete,
46 ACE_Allocator *alloc)
47 : size_ (0),
48 occurrences_ (0),
49 stream_ (ACE_OS::fdopen (handle, ACE_TEXT ("r"))),
50 close_on_delete_ (close_on_delete),
51 allocator_ (alloc)
53 ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer");
55 if (this->allocator_ == 0)
56 this->allocator_ = ACE_Allocator::instance ();
59 ACE_Read_Buffer::~ACE_Read_Buffer ()
61 ACE_TRACE ("ACE_Read_Buffer::~ACE_Read_Buffer");
63 if (this->close_on_delete_)
64 ACE_OS::fclose (this->stream_);
67 // Input: term the character to terminate on
68 // search the character to search for
69 // replace the character with which to replace search
70 // Output: a buffer containing the contents of stream
71 // Method: call the recursive helper function read_helper
73 char *
74 ACE_Read_Buffer::read (int term, int search, int replace)
76 ACE_TRACE ("ACE_Read_Buffer::read");
77 this->occurrences_ = 0;
78 this->size_ = 0;
79 return this->rec_read (term, search, replace);
82 // Input: term the termination character
83 // search the character to search for
84 // replace the character with which to replace search
85 // Purpose: read in a file to a buffer using only a single dynamic
86 // allocation.
87 // Method: read until the local buffer is full and then recurse.
88 // Must continue until the termination character is reached.
89 // Allocate the final buffer based on the number of local
90 // buffers read and as the recursive calls bottom out,
91 // copy them in reverse order into the allocated buffer.
93 char *
94 ACE_Read_Buffer::rec_read (int term, int search, int replace)
96 ACE_TRACE ("ACE_Read_Buffer::rec_read");
97 // This is our temporary workspace.
98 char buf[BUFSIZ];
100 int c = EOF;
101 size_t slot = 0;
102 int done = 0;
104 // Read in the file char by char
105 while (slot < BUFSIZ)
107 c = ACE_OS::getc (this->stream_);
109 // Don't insert EOF into the buffer...
110 if (c == EOF)
112 ACE_OS::ungetc (c, this->stream_);
113 break;
115 else if (c == term)
116 done = 1;
118 // Check for possible substitutions.
119 if (c == search)
121 ++this->occurrences_;
123 if (replace >= 0)
124 c = replace;
127 buf[slot++] = (char) c;
129 // Substitutions must be made before checking for termination.
130 if (done)
131 break;
134 // Increment the number of bytes.
135 this->size_ += slot;
137 // Don't bother going any farther if the total size is 0.
138 if (this->size_ == 0)
139 return 0;
141 char *result = 0;
143 // Recurse, when the recursion bottoms out, allocate the result
144 // buffer.
145 if (done || c == EOF)
147 // Use the allocator to acquire the memory. The + 1 allows
148 // space for the null terminator.
149 result = (char *) this->allocator_->malloc (this->size_ + 1);
151 if (result == 0)
153 errno = ENOMEM;
154 return 0;
156 result += this->size_;
158 // Null terminate the buffer.
159 *result = '\0';
161 else if ((result = this->rec_read (term, search, replace)) == 0)
162 return 0;
164 // Copy buf into the appropriate location starting from end of
165 // buffer. Peter says this is confusing and that we should use
166 // memcpy() ;-)
167 for (size_t j = slot; j > 0; j--)
168 *--result = buf[j - 1];
170 return result;
173 ACE_END_VERSIONED_NAMESPACE_DECL