Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / Read_Buffer.cpp
blobdc246a466b0cd2e520b8d068afc1bb16f4b79870
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
21 void
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,
36 bool close_on_delete,
37 ACE_Allocator *alloc)
38 : stream_ (fp),
39 close_on_delete_ (close_on_delete),
40 allocator_ (alloc)
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,
49 bool close_on_delete,
50 ACE_Allocator *alloc)
51 : stream_ (ACE_OS::fdopen (handle, ACE_TEXT ("r"))),
52 close_on_delete_ (close_on_delete),
53 allocator_ (alloc)
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
76 char *
77 ACE_Read_Buffer::read (int term, int search, int replace)
79 ACE_TRACE ("ACE_Read_Buffer::read");
80 this->occurrences_ = 0;
81 this->size_ = 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
89 // allocation.
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.
96 char *
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.
101 char buf[BUFSIZ];
103 int c = EOF;
104 size_t slot = 0;
105 int done = 0;
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...
113 if (c == EOF)
115 ACE_OS::ungetc (c, this->stream_);
116 break;
118 else if (c == term)
119 done = 1;
121 // Check for possible substitutions.
122 if (c == search)
124 ++this->occurrences_;
126 if (replace >= 0)
127 c = replace;
130 buf[slot++] = (char) c;
132 // Substitutions must be made before checking for termination.
133 if (done)
134 break;
137 // Increment the number of bytes.
138 this->size_ += slot;
140 // Don't bother going any farther if the total size is 0.
141 if (this->size_ == 0)
142 return 0;
144 char *result = 0;
146 // Recurse, when the recursion bottoms out, allocate the result
147 // buffer.
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);
154 if (result == 0)
156 errno = ENOMEM;
157 return 0;
159 result += this->size_;
161 // Null terminate the buffer.
162 *result = '\0';
164 else if ((result = this->rec_read (term, search, replace)) == 0)
165 return 0;
167 // Copy buf into the appropriate location starting from end of
168 // buffer. Peter says this is confusing and that we should use
169 // memcpy() ;-)
170 for (size_t j = slot; j > 0; j--)
171 *--result = buf[j - 1];
173 return result;
176 ACE_END_VERSIONED_NAMESPACE_DECL