BPicture: Fix archive constructor.
[haiku.git] / src / tools / restest / OffsetFile.cpp
blob852d5cc50d52fb18e2f0e2cb0c38c4299b0b70a2
1 // OffsetFile.cpp
3 #include <stdio.h>
5 #include "OffsetFile.h"
7 // constructor
8 OffsetFile::OffsetFile()
9 : fFile(),
10 fOffset(0),
11 fCurrentPosition(0)
15 // constructor
16 OffsetFile::OffsetFile(const BFile& file, off_t offset)
17 : fFile(),
18 fOffset(0),
19 fCurrentPosition(0)
21 SetTo(file, offset);
24 // destructor
25 OffsetFile::~OffsetFile()
29 // SetTo
30 status_t
31 OffsetFile::SetTo(const BFile& file, off_t offset)
33 Unset();
34 fFile = file;
35 fOffset = offset;
36 return fFile.InitCheck();
39 // Unset
40 void
41 OffsetFile::Unset()
43 fFile.Unset();
44 fOffset = 0;
45 fCurrentPosition = 0;
48 // InitCheck
49 status_t
50 OffsetFile::InitCheck() const
52 return fFile.InitCheck();
55 // Read
56 //ssize_t
57 //OffsetFile::Read(void *buffer, size_t size)
58 //{
59 // return fFile.Read(buffer, size);
60 //}
62 // Write
63 //ssize_t
64 //OffsetFile::Write(const void *buffer, size_t size)
65 //{
66 // return fFile.Write(buffer, size);
67 //}
69 // ReadAt
70 ssize_t
71 OffsetFile::ReadAt(off_t pos, void *buffer, size_t size)
73 //printf("ReadAt(%Lx + %Lx, %lu)\n", pos, fOffset, size);
74 return fFile.ReadAt(pos + fOffset, buffer, size);
77 // WriteAt
78 ssize_t
79 OffsetFile::WriteAt(off_t pos, const void *buffer, size_t size)
81 return fFile.WriteAt(pos + fOffset, buffer, size);
84 // Seek
85 off_t
86 OffsetFile::Seek(off_t position, uint32 seekMode)
88 // off_t result = fFile.Seek(position + fOffset, seekMode);
89 // if (result >= 0)
90 // result -= fOffset;
91 // return result;
92 off_t result = B_BAD_VALUE;
93 switch (seekMode) {
94 case SEEK_SET:
95 if (position >= 0)
96 result = fCurrentPosition = position;
97 break;
98 case SEEK_END:
100 off_t size;
101 status_t error = GetSize(&size);
102 if (size + position >= 0)
103 result = fCurrentPosition = size + position;
104 else
105 result = error;
106 break;
108 case SEEK_CUR:
109 if (fCurrentPosition + position >= 0)
110 result = fCurrentPosition += position;
111 break;
112 default:
113 break;
115 return result;
118 // Position
119 off_t
120 OffsetFile::Position() const
122 // off_t result = fFile.Position();
123 // if (result >= 0)
124 // result -= fOffset;
125 // return result;
126 return fCurrentPosition;
129 // SetSize
130 status_t
131 OffsetFile::SetSize(off_t size)
133 status_t error = (size >= 0 ? B_OK : B_BAD_VALUE );
134 if (error == B_OK)
135 error = fFile.SetSize(size + fOffset);
136 return error;
139 // GetSize
140 status_t
141 OffsetFile::GetSize(off_t* size)
143 status_t error = fFile.GetSize(size);
144 if (error == B_OK)
145 *size -= fOffset;
146 return error;
149 // GetOffset
150 off_t
151 OffsetFile::GetOffset() const
153 return fOffset;