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
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
,
36 close_on_delete_ (close_on_delete
),
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
,
49 stream_ (ACE_OS::fdopen (handle
, ACE_TEXT ("r"))),
50 close_on_delete_ (close_on_delete
),
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
74 ACE_Read_Buffer::read (int term
, int search
, int replace
)
76 ACE_TRACE ("ACE_Read_Buffer::read");
77 this->occurrences_
= 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
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.
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.
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...
112 ACE_OS::ungetc (c
, this->stream_
);
118 // Check for possible substitutions.
121 ++this->occurrences_
;
127 buf
[slot
++] = (char) c
;
129 // Substitutions must be made before checking for termination.
134 // Increment the number of bytes.
137 // Don't bother going any farther if the total size is 0.
138 if (this->size_
== 0)
143 // Recurse, when the recursion bottoms out, allocate the result
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);
156 result
+= this->size_
;
158 // Null terminate the buffer.
161 else if ((result
= this->rec_read (term
, search
, replace
)) == 0)
164 // Copy buf into the appropriate location starting from end of
165 // buffer. Peter says this is confusing and that we should use
167 for (size_t j
= slot
; j
> 0; j
--)
168 *--result
= buf
[j
- 1];
173 ACE_END_VERSIONED_NAMESPACE_DECL