1 // OffsetFile.cpp
2
3 #include <stdio.h>
4
5 #include "OffsetFile.h"
6
7 // constructor
OffsetFile()8 OffsetFile::OffsetFile()
9 : fFile(),
10 fOffset(0),
11 fCurrentPosition(0)
12 {
13 }
14
15 // constructor
OffsetFile(const BFile & file,off_t offset)16 OffsetFile::OffsetFile(const BFile& file, off_t offset)
17 : fFile(),
18 fOffset(0),
19 fCurrentPosition(0)
20 {
21 SetTo(file, offset);
22 }
23
24 // destructor
~OffsetFile()25 OffsetFile::~OffsetFile()
26 {
27 }
28
29 // SetTo
30 status_t
SetTo(const BFile & file,off_t offset)31 OffsetFile::SetTo(const BFile& file, off_t offset)
32 {
33 Unset();
34 fFile = file;
35 fOffset = offset;
36 return fFile.InitCheck();
37 }
38
39 // Unset
40 void
Unset()41 OffsetFile::Unset()
42 {
43 fFile.Unset();
44 fOffset = 0;
45 fCurrentPosition = 0;
46 }
47
48 // InitCheck
49 status_t
InitCheck() const50 OffsetFile::InitCheck() const
51 {
52 return fFile.InitCheck();
53 }
54
55 // Read
56 //ssize_t
57 //OffsetFile::Read(void *buffer, size_t size)
58 //{
59 // return fFile.Read(buffer, size);
60 //}
61
62 // Write
63 //ssize_t
64 //OffsetFile::Write(const void *buffer, size_t size)
65 //{
66 // return fFile.Write(buffer, size);
67 //}
68
69 // ReadAt
70 ssize_t
ReadAt(off_t pos,void * buffer,size_t size)71 OffsetFile::ReadAt(off_t pos, void *buffer, size_t size)
72 {
73 //printf("ReadAt(%Lx + %Lx, %lu)\n", pos, fOffset, size);
74 return fFile.ReadAt(pos + fOffset, buffer, size);
75 }
76
77 // WriteAt
78 ssize_t
WriteAt(off_t pos,const void * buffer,size_t size)79 OffsetFile::WriteAt(off_t pos, const void *buffer, size_t size)
80 {
81 return fFile.WriteAt(pos + fOffset, buffer, size);
82 }
83
84 // Seek
85 off_t
Seek(off_t position,uint32 seekMode)86 OffsetFile::Seek(off_t position, uint32 seekMode)
87 {
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:
99 {
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;
107 }
108 case SEEK_CUR:
109 if (fCurrentPosition + position >= 0)
110 result = fCurrentPosition += position;
111 break;
112 default:
113 break;
114 }
115 return result;
116 }
117
118 // Position
119 off_t
Position() const120 OffsetFile::Position() const
121 {
122 // off_t result = fFile.Position();
123 // if (result >= 0)
124 // result -= fOffset;
125 // return result;
126 return fCurrentPosition;
127 }
128
129 // SetSize
130 status_t
SetSize(off_t size)131 OffsetFile::SetSize(off_t size)
132 {
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;
137 }
138
139 // GetSize
140 status_t
GetSize(off_t * size)141 OffsetFile::GetSize(off_t* size)
142 {
143 status_t error = fFile.GetSize(size);
144 if (error == B_OK)
145 *size -= fOffset;
146 return error;
147 }
148
149 // GetOffset
150 off_t
GetOffset() const151 OffsetFile::GetOffset() const
152 {
153 return fOffset;
154 }
155
156