1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include "libplatform/impl.h"
namespace mp4v2 { namespace platform { namespace io {
///////////////////////////////////////////////////////////////////////////////
class StandardFileProvider : public FileProvider
{
public:
StandardFileProvider();
bool open( std::string name, Mode mode );
bool seek( Size pos );
bool read( void* buffer, Size size, Size& nin, Size maxChunkSize );
bool write( const void* buffer, Size size, Size& nout, Size maxChunkSize );
bool close();
private:
bool _seekg;
bool _seekp;
std::fstream _fstream;
};
///////////////////////////////////////////////////////////////////////////////
StandardFileProvider::StandardFileProvider()
: _seekg ( false )
, _seekp ( false )
{
}
bool
StandardFileProvider::open( std::string name, Mode mode )
{
ios::openmode om = ios::binary;
switch( mode ) {
case MODE_UNDEFINED:
case MODE_READ:
default:
om |= ios::in;
_seekg = true;
_seekp = false;
break;
case MODE_MODIFY:
om |= ios::in | ios::out;
_seekg = true;
_seekp = true;
break;
case MODE_CREATE:
om |= ios::in | ios::out | ios::trunc;
_seekg = true;
_seekp = true;
break;
}
_fstream.open( name.c_str(), om );
return _fstream.fail();
}
bool
StandardFileProvider::seek( Size pos )
{
if( _seekg )
_fstream.seekg( pos, ios::beg );
if( _seekp )
_fstream.seekp( pos, ios::beg );
return _fstream.fail();
}
bool
StandardFileProvider::read( void* buffer, Size size, Size& nin, Size maxChunkSize )
{
_fstream.read( (char*)buffer, size );
if( _fstream.fail() )
return true;
nin = _fstream.gcount();
return false;
}
bool
StandardFileProvider::write( const void* buffer, Size size, Size& nout, Size maxChunkSize )
{
_fstream.write( (const char*)buffer, size );
if( _fstream.fail() )
return true;
nout = size;
return false;
}
bool
StandardFileProvider::close()
{
_fstream.close();
return _fstream.fail();
}
///////////////////////////////////////////////////////////////////////////////
FileProvider&
FileProvider::standard()
{
return *new StandardFileProvider();
}
///////////////////////////////////////////////////////////////////////////////
}}} // namespace mp4v2::platform::io
|