xref: /haiku/src/kits/support/DataPositionIOWrapper.cpp (revision 3369e03d5cde9709c8aa70c99bfe6ce24ba65bf9)
1 /*
2  * Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <DataPositionIOWrapper.h>
8 
9 #include <stdio.h>
10 
11 
12 BDataPositionIOWrapper::BDataPositionIOWrapper(BDataIO* io)
13 	:
14 	BPositionIO(),
15 	fIO(io),
16 	fPosition(0)
17 {
18 }
19 
20 
21 BDataPositionIOWrapper::~BDataPositionIOWrapper()
22 {
23 }
24 
25 
26 ssize_t
27 BDataPositionIOWrapper::Read(void* buffer, size_t size)
28 {
29 	ssize_t bytesRead = fIO->Read(buffer, size);
30 	if (bytesRead > 0)
31 		fPosition += bytesRead;
32 
33 	return bytesRead;
34 }
35 
36 
37 ssize_t
38 BDataPositionIOWrapper::Write(const void* buffer, size_t size)
39 {
40 	ssize_t bytesWritten = fIO->Write(buffer, size);
41 	if (bytesWritten > 0)
42 		fPosition += bytesWritten;
43 
44 	return bytesWritten;
45 }
46 
47 
48 ssize_t
49 BDataPositionIOWrapper::ReadAt(off_t position, void* buffer, size_t size)
50 {
51 	if (position != fPosition)
52 		return B_NOT_SUPPORTED;
53 
54 	return Read(buffer, size);
55 }
56 
57 
58 ssize_t
59 BDataPositionIOWrapper::WriteAt(off_t position, const void* buffer,
60 	size_t size)
61 {
62 	if (position != fPosition)
63 		return B_NOT_SUPPORTED;
64 
65 	return Write(buffer, size);
66 }
67 
68 
69 off_t
70 BDataPositionIOWrapper::Seek(off_t position, uint32 seekMode)
71 {
72 	switch (seekMode) {
73 		case SEEK_CUR:
74 			return position == 0 ? B_OK : B_NOT_SUPPORTED;
75 		case SEEK_SET:
76 			return position == fPosition ? B_OK : B_NOT_SUPPORTED;
77 		case SEEK_END:
78 			return B_NOT_SUPPORTED;
79 		default:
80 			return B_BAD_VALUE;
81 	}
82 }
83 
84 
85 off_t
86 BDataPositionIOWrapper::Position() const
87 {
88 	return fPosition;
89 }
90 
91 
92 status_t
93 BDataPositionIOWrapper::SetSize(off_t size)
94 {
95 	return size == fPosition ? B_OK : B_NOT_SUPPORTED;
96 }
97 
98 
99 status_t
100 BDataPositionIOWrapper::GetSize(off_t* size) const
101 {
102 	return B_NOT_SUPPORTED;
103 }
104