Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / FILE_IO.cpp
blob681b6c53885c631d8a322ab8aa26bd19bb454a00
1 #include "ace/FILE_IO.h"
3 #include "ace/Log_Category.h"
4 #include "ace/OS_NS_sys_stat.h"
5 #include "ace/OS_Memory.h"
6 #include "ace/Truncate.h"
7 #if defined (ACE_HAS_ALLOC_HOOKS)
8 # include "ace/Malloc_Base.h"
9 #endif /* ACE_HAS_ALLOC_HOOKS */
11 #if !defined (__ACE_INLINE__)
12 #include "ace/FILE_IO.inl"
13 #endif /* __ACE_INLINE__ */
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 ACE_ALLOC_HOOK_DEFINE(ACE_FILE_IO)
19 void
20 ACE_FILE_IO::dump () const
22 #if defined (ACE_HAS_DUMP)
23 ACE_TRACE ("ACE_FILE_IO::dump");
25 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
26 this->addr_.dump ();
27 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
28 #endif /* ACE_HAS_DUMP */
31 // Simple-minded do nothing constructor.
33 ACE_FILE_IO::ACE_FILE_IO ()
35 ACE_TRACE ("ACE_FILE_IO::ACE_FILE_IO");
38 // Send N char *ptrs and int lengths. Note that the char *'s precede
39 // the ints (basically, an varargs version of writev). The count N is
40 // the *total* number of trailing arguments, *not* a couple of the
41 // number of tuple pairs!
43 ssize_t
44 ACE_FILE_IO::send (size_t n, ...) const
46 ACE_TRACE ("ACE_FILE_IO::send");
47 #ifdef ACE_LACKS_VA_FUNCTIONS
48 ACE_UNUSED_ARG (n);
49 ACE_NOTSUP_RETURN (-1);
50 #else
51 va_list argp;
52 int total_tuples = ACE_Utils::truncate_cast<int> (n / 2);
53 iovec *iovp = 0;
54 #if defined (ACE_HAS_ALLOCA)
55 iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
56 #else
57 # ifdef ACE_HAS_ALLOC_HOOKS
58 ACE_ALLOCATOR_RETURN (iovp, (iovec *)
59 ACE_Allocator::instance ()->malloc (total_tuples *
60 sizeof (iovec)),
61 -1);
62 # else
63 ACE_NEW_RETURN (iovp,
64 iovec[total_tuples],
65 -1);
66 # endif /* ACE_HAS_ALLOC_HOOKS */
67 #endif /* !defined (ACE_HAS_ALLOCA) */
69 va_start (argp, n);
71 for (int i = 0; i < total_tuples; i++)
73 iovp[i].iov_base = va_arg (argp, char *);
74 iovp[i].iov_len = va_arg (argp, int);
77 ssize_t result = ACE_OS::writev (this->get_handle (),
78 iovp,
79 total_tuples);
80 #if !defined (ACE_HAS_ALLOCA)
81 # ifdef ACE_HAS_ALLOC_HOOKS
82 ACE_Allocator::instance ()->free (iovp);
83 # else
84 delete [] iovp;
85 # endif /* ACE_HAS_ALLOC_HOOKS */
86 #endif /* !defined (ACE_HAS_ALLOCA) */
87 va_end (argp);
88 return result;
89 #endif // ACE_LACKS_VA_FUNCTIONS
92 // This is basically an interface to ACE_OS::readv, that doesn't use
93 // the struct iovec explicitly. The ... can be passed as an arbitrary
94 // number of (char *ptr, int len) tuples. However, the count N is the
95 // *total* number of trailing arguments, *not* a couple of the number
96 // of tuple pairs!
98 ssize_t
99 ACE_FILE_IO::recv (size_t n, ...) const
101 ACE_TRACE ("ACE_FILE_IO::recv");
102 #ifdef ACE_LACKS_VA_FUNCTIONS
103 ACE_UNUSED_ARG (n);
104 ACE_NOTSUP_RETURN (-1);
105 #else
106 va_list argp;
107 int total_tuples = ACE_Utils::truncate_cast<int> (n / 2);
108 iovec *iovp = 0;
109 #if defined (ACE_HAS_ALLOCA)
110 iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
111 #else
112 # ifdef ACE_HAS_ALLOC_HOOKS
113 ACE_ALLOCATOR_RETURN (iovp, (iovec *)
114 ACE_Allocator::instance ()->malloc (total_tuples *
115 sizeof (iovec)),
116 -1);
117 # else
118 ACE_NEW_RETURN (iovp,
119 iovec[total_tuples],
120 -1);
121 # endif /* ACE_HAS_ALLOC_HOOKS */
122 #endif /* !defined (ACE_HAS_ALLOCA) */
124 va_start (argp, n);
126 for (int i = 0; i < total_tuples; i++)
128 iovp[i].iov_base = va_arg (argp, char *);
129 iovp[i].iov_len = va_arg (argp, int);
132 ssize_t const result = ACE_OS::readv (this->get_handle (),
133 iovp,
134 total_tuples);
135 #if !defined (ACE_HAS_ALLOCA)
136 # ifdef ACE_HAS_ALLOC_HOOKS
137 ACE_Allocator::instance ()->free (iovp);
138 # else
139 delete [] iovp;
140 # endif /* ACE_HAS_ALLOC_HOOKS */
141 #endif /* !defined (ACE_HAS_ALLOCA) */
142 va_end (argp);
143 return result;
144 #endif // ACE_LACKS_VA_FUNCTIONS
147 // Allows a client to read from a file without having to provide a
148 // buffer to read. This method determines how much data is in the
149 // file, allocates a buffer of this size, reads in the data, and
150 // returns the number of bytes read.
152 ssize_t
153 ACE_FILE_IO::recvv (iovec *io_vec)
155 ACE_TRACE ("ACE_FILE_IO::recvv");
157 io_vec->iov_base = 0;
158 ACE_OFF_T const length = ACE_OS::filesize (this->get_handle ());
160 if (length > 0)
162 // Restrict to max size we can record in iov_len.
163 size_t len = ACE_Utils::truncate_cast<u_long> (length);
164 #if defined (ACE_HAS_ALLOC_HOOKS)
165 ACE_ALLOCATOR_RETURN (io_vec->iov_base,
166 static_cast<char*>(ACE_Allocator::instance()->malloc(sizeof(char) * len)),
167 -1);
168 #else
169 ACE_NEW_RETURN (io_vec->iov_base,
170 char[len],
171 -1);
172 #endif /* ACE_HAS_ALLOC_HOOKS */
173 io_vec->iov_len = static_cast<u_long> (this->recv_n (io_vec->iov_base,
174 len));
175 return io_vec->iov_len;
177 else
179 return ACE_Utils::truncate_cast<ssize_t> (length);
183 ACE_END_VERSIONED_NAMESPACE_DECL