-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathStdIOCallback.cpp
More file actions
140 lines (113 loc) · 2.88 KB
/
StdIOCallback.cpp
File metadata and controls
140 lines (113 loc) · 2.88 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright © 2002-2004 Ingo Ralf Blum.
// SPDX-License-Identifier: LGPL-2.1-or-later
/*!
\file
\author Steve Lhomme <robux4 @ users.sf.net>
\author Moritz Bunkus <moritz @ bunkus.org>
*/
#include <cassert>
#include <limits>
#include <sstream>
#include <cstring>
#include <ios>
#include "ebml/StdIOCallback.h"
using namespace std;
namespace libebml {
StdIOCallback::StdIOCallback(const char*Path, const open_mode aMode)
{
assert(Path!=nullptr);
const char *Mode;
switch (aMode) {
case MODE_READ:
Mode = "rb";
break;
case MODE_SAFE:
Mode = "rb+";
break;
case MODE_WRITE:
Mode = "wb";
break;
case MODE_CREATE:
Mode = "wb+";
break;
default:
throw std::invalid_argument("Invalid file mode supplied.");
}
File=fopen(Path,Mode);
if(File==nullptr) {
stringstream Msg;
Msg<<"Can't open stdio file \""<<Path<<"\" in mode \""<<Mode<<"\"";
throw ios_base::failure(Msg.str(), error_code{errno, std::system_category()});
}
mCurrentPosition = 0;
}
StdIOCallback::~StdIOCallback() noexcept
{
close();
}
std::size_t StdIOCallback::read(void*Buffer,std::size_t Size)
{
assert(File!=nullptr);
const std::size_t result = fread(Buffer, 1, Size, File);
if (feof(File) || ferror(File))
return 0;
mCurrentPosition += result;
return result;
}
void StdIOCallback::setFilePointer(std::int64_t Offset,seek_mode Mode)
{
assert(File!=nullptr);
assert(Offset <= (std::numeric_limits<long>::max)());
assert(Offset >= (std::numeric_limits<long>::min)());
assert(Mode==SEEK_CUR||Mode==SEEK_END||Mode==SEEK_SET);
if(fseek(File,Offset,Mode)!=0) {
ostringstream Msg;
Msg<<"Failed to seek file "<<File<<" to offset "<<Offset<<" in mode "<<Mode;
throw ios_base::failure(Msg.str(), error_code{errno, std::system_category()});
}
switch ( Mode ) {
case SEEK_CUR:
mCurrentPosition += Offset;
break;
case SEEK_END:
mCurrentPosition = ftell(File);
break;
case SEEK_SET:
mCurrentPosition = Offset;
break;
}
}
std::size_t StdIOCallback::write(const void*Buffer,std::size_t Size)
{
assert(File!=nullptr);
const size_t Result = fwrite(Buffer,1,Size,File);
if (feof(File) || ferror(File))
return 0;
mCurrentPosition += Result;
return Result;
}
std::uint64_t StdIOCallback::getFilePointer()
{
assert(File!=nullptr);
#if 0
long Result=ftell(File);
if(Result<0) {
stringstream Msg;
Msg<<"Can't tell the current file pointer position for "<<File;
throw ios_base::failure(Msg.str(), error_code{errno, std::system_category()});
}
#endif
return mCurrentPosition;
}
void StdIOCallback::close()
{
if(File==nullptr)
return;
if(fclose(File)!=0) {
stringstream Msg;
Msg<<"Can't close file "<<File;
throw ios_base::failure(Msg.str(), error_code{errno, std::system_category()});
}
File=nullptr;
}
} // namespace libebml