btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / bin / makeudfimage / PositionIOStream.cpp
blob5b3b12bea45c16dbdec998e0b6f3b353d2e29ec0
1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
3 // by the MIT License.
4 //
5 // Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6 //----------------------------------------------------------------------
8 /*! \file PositionIOStream.cpp
9 */
11 #include "PositionIOStream.h"
13 #include <stdlib.h>
14 #include <string.h>
16 PositionIOStream::PositionIOStream(BPositionIO &stream)
17 : fStream(stream)
21 ssize_t
22 PositionIOStream::Read(void *buffer, size_t size)
24 return fStream.Read(buffer, size);
27 ssize_t
28 PositionIOStream::ReadAt(off_t pos, void *buffer, size_t size)
30 return fStream.ReadAt(pos, buffer, size);
33 ssize_t
34 PositionIOStream::Write(const void *buffer, size_t size)
36 return fStream.Write(buffer, size);
39 ssize_t
40 PositionIOStream::WriteAt(off_t pos, const void *buffer, size_t size)
42 return fStream.WriteAt(pos, buffer, size);
45 /*! \brief Writes \a size bytes worth of data from \a data at the current
46 position in the file, incrementing the file's position marker as it goes.
48 ssize_t
49 PositionIOStream::Write(BDataIO &data, size_t size)
51 status_t error = kBufferSize > 0 ? B_OK : B_BAD_VALUE;
52 size_t bytes = 0;
53 if (!error) {
54 void *buffer = malloc(kBufferSize);
55 error = buffer ? B_OK : B_NO_MEMORY;
56 if (!error) {
57 // Fudge the buffer size from here out if the requested
58 // number of bytes to write is smaller than the buffer
59 size_t bufferSize = (size < kBufferSize ? size : kBufferSize);
60 // Zero
61 memset(buffer, 0, bufferSize);
62 // Write
63 while (bytes < size) {
64 ssize_t bytesRead = data.Read(buffer, bufferSize);
65 if (bytesRead >= 0) {
66 ssize_t bytesWritten = fStream.Write(buffer, bytesRead);
67 if (bytesWritten >= 0) {
68 bytes += bytesWritten;
69 } else {
70 error = status_t(bytesWritten);
71 break;
73 } else {
74 error = status_t(bytesRead);
75 break;
79 free(buffer);
81 return error ? ssize_t(error) : ssize_t(bytes);
84 /*! \brief Writes \a size bytes worth of data from \a data at position
85 \a pos in the file without incrementing the file's position marker.
87 ssize_t
88 PositionIOStream::WriteAt(off_t pos, BDataIO &data, size_t size)
90 status_t error = kBufferSize > 0 ? B_OK : B_BAD_VALUE;
91 size_t bytes = 0;
92 if (!error) {
93 void *buffer = malloc(kBufferSize);
94 error = buffer ? B_OK : B_NO_MEMORY;
95 if (!error) {
96 // Fudge the buffer size from here out if the requested
97 // number of bytes to write is smaller than the buffer
98 size_t bufferSize = (size < kBufferSize ? size : kBufferSize);
99 // Zero
100 memset(buffer, 0, bufferSize);
101 // Write
102 while (bytes < size) {
103 ssize_t bytesRead = data.Read(buffer, bufferSize);
104 if (bytesRead >= 0) {
105 ssize_t bytesWritten = fStream.WriteAt(pos, buffer, bytesRead);
106 if (bytesWritten >= 0) {
107 bytes += bytesWritten;
108 pos += bytesWritten;
109 } else {
110 error = status_t(bytesWritten);
111 break;
113 } else {
114 error = status_t(bytesRead);
115 break;
119 free(buffer);
121 return error ? ssize_t(error) : ssize_t(bytes);
124 /*! \brief Writes \a size bytes worth of zeros at the current position
125 in the file, incrementing the file's position marker as it goes.
127 ssize_t
128 PositionIOStream::Zero(size_t size)
130 status_t error = kBufferSize > 0 ? B_OK : B_BAD_VALUE;
131 size_t bytes = 0;
132 if (!error) {
133 void *buffer = malloc(kBufferSize);
134 error = buffer ? B_OK : B_NO_MEMORY;
135 if (!error) {
136 // Fudge the buffer size from here out if the requested
137 // number of bytes to write is smaller than the buffer
138 size_t bufferSize = (size < kBufferSize ? size : kBufferSize);
139 // Zero
140 memset(buffer, 0, bufferSize);
141 // Write
142 while (bytes < size) {
143 ssize_t bytesWritten = fStream.Write(buffer, bufferSize);
144 if (bytesWritten >= 0) {
145 bytes += bytesWritten;
146 } else {
147 error = status_t(bytesWritten);
148 break;
152 free(buffer);
154 return error ? ssize_t(error) : ssize_t(bytes);
157 /*! \brief Writes \a size bytes worth of zeros at position \a pos
158 in the file without incrementing the file's position marker.
160 ssize_t
161 PositionIOStream::ZeroAt(off_t pos, size_t size)
163 status_t error = kBufferSize > 0 ? B_OK : B_BAD_VALUE;
164 size_t bytes = 0;
165 if (!error) {
166 void *buffer = malloc(kBufferSize);
167 error = buffer ? B_OK : B_NO_MEMORY;
168 if (!error) {
169 // Fudge the buffer size from here out if the requested
170 // number of bytes to write is smaller than the buffer
171 size_t bufferSize = (size < kBufferSize ? size : kBufferSize);
172 // Zero
173 memset(buffer, 0, bufferSize);
174 // Write
175 while (bytes < size) {
176 ssize_t bytesWritten = fStream.WriteAt(pos, buffer, bufferSize);
177 if (bytesWritten >= 0) {
178 bytes += bytesWritten;
179 pos += bytesWritten;
180 } else {
181 error = status_t(bytesWritten);
182 break;
186 free(buffer);
188 return error ? ssize_t(error) : ssize_t(bytes);
191 off_t
192 PositionIOStream::Seek(off_t position, uint32 seek_mode)
194 return fStream.Seek(position, seek_mode);
197 off_t
198 PositionIOStream::Position() const
200 return fStream.Position();
203 status_t
204 PositionIOStream::SetSize(off_t size)
206 return fStream.SetSize(size);