1 // $Id: Read_Buffer.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/Read_Buffer.h"
5 #include "ace/config-all.h"
7 #if !defined (__ACE_INLINE__)
8 #include "ace/Read_Buffer.inl"
9 #endif /* __ACE_INLINE__ */
11 #include "ace/Log_Msg.h"
12 #include "ace/Malloc_Base.h"
13 #include "ace/Service_Config.h"
14 #include "ace/OS_NS_stdio.h"
16 ACE_RCSID(ace
, Read_Buffer
, "$Id: Read_Buffer.cpp 80826 2008-03-04 14:51:23Z wotte $")
19 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
22 ACE_Read_Buffer::dump (void) const
24 #if defined (ACE_HAS_DUMP)
25 ACE_TRACE ("ACE_Read_Buffer::dump");
26 ACE_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
27 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("size_ = %d"), this->size_
));
28 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\noccurrences_ = %d"), this->occurrences_
));
29 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nstream_ = %x"), this->stream_
));
30 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nallocator_ = %x"), this->allocator_
));
31 ACE_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
32 #endif /* ACE_HAS_DUMP */
35 ACE_Read_Buffer::ACE_Read_Buffer (FILE *fp
,
39 close_on_delete_ (close_on_delete
),
42 ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer");
43 if (this->allocator_
== 0)
44 this->allocator_
= ACE_Allocator::instance ();
47 #if !defined (ACE_HAS_WINCE)
48 ACE_Read_Buffer::ACE_Read_Buffer (ACE_HANDLE handle
,
51 : stream_ (ACE_OS::fdopen (handle
, ACE_TEXT ("r"))),
52 close_on_delete_ (close_on_delete
),
55 ACE_TRACE ("ACE_Read_Buffer::ACE_Read_Buffer");
57 if (this->allocator_
== 0)
58 this->allocator_
= ACE_Allocator::instance ();
60 #endif // ACE_HAS_WINCE
62 ACE_Read_Buffer::~ACE_Read_Buffer (void)
64 ACE_TRACE ("ACE_Read_Buffer::~ACE_Read_Buffer");
66 if (this->close_on_delete_
)
67 ACE_OS::fclose (this->stream_
);
70 // Input: term the character to terminate on
71 // search the character to search for
72 // replace the character with which to replace search
73 // Output: a buffer containing the contents of stream
74 // Method: call the recursive helper function read_helper
77 ACE_Read_Buffer::read (int term
, int search
, int replace
)
79 ACE_TRACE ("ACE_Read_Buffer::read");
80 this->occurrences_
= 0;
82 return this->rec_read (term
, search
, replace
);
85 // Input: term the termination character
86 // search the character to search for
87 // replace the character with which to replace search
88 // Purpose: read in a file to a buffer using only a single dynamic
90 // Method: read until the local buffer is full and then recurse.
91 // Must continue until the termination character is reached.
92 // Allocate the final buffer based on the number of local
93 // buffers read and as the recursive calls bottom out,
94 // copy them in reverse order into the allocated buffer.
97 ACE_Read_Buffer::rec_read (int term
, int search
, int replace
)
99 ACE_TRACE ("ACE_Read_Buffer::rec_read");
100 // This is our temporary workspace.
107 // Read in the file char by char
108 while (slot
< BUFSIZ
)
110 c
= ACE_OS::getc (this->stream_
);
112 // Don't insert EOF into the buffer...
115 ACE_OS::ungetc (c
, this->stream_
);
121 // Check for possible substitutions.
124 ++this->occurrences_
;
130 buf
[slot
++] = (char) c
;
132 // Substitutions must be made before checking for termination.
137 // Increment the number of bytes.
140 // Don't bother going any farther if the total size is 0.
141 if (this->size_
== 0)
146 // Recurse, when the recursion bottoms out, allocate the result
148 if (done
|| c
== EOF
)
150 // Use the allocator to acquire the memory. The + 1 allows
151 // space for the null terminator.
152 result
= (char *) this->allocator_
->malloc (this->size_
+ 1);
159 result
+= this->size_
;
161 // Null terminate the buffer.
164 else if ((result
= this->rec_read (term
, search
, replace
)) == 0)
167 // Copy buf into the appropriate location starting from end of
168 // buffer. Peter says this is confusing and that we should use
170 for (size_t j
= slot
; j
> 0; j
--)
171 *--result
= buf
[j
- 1];
176 ACE_END_VERSIONED_NAMESPACE_DECL