-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_gr_fdio.h
195 lines (180 loc) · 5.83 KB
/
_gr_fdio.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#pragma once
// Copyright David Lawrence Bien 1997 - 2021.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt).
// _gr_fdio.h
// graph I/O through files - originally through file descriptors then ported to Windows files.
// dbien: 21APR2020
#include <fcntl.h>
#ifndef WIN32
#include <unistd.h>
#endif //!WIN32
#include "_compat.h"
#include "_gr_inc.h"
__DGRAPH_BEGIN_NAMESPACE
// Specialize for fdout - default version just writes raw memory:
template < class t_TyWrite >
__INLINE void
_RawWriteGraphEl( vtyFileHandle _hFile, t_TyWrite const & _rEl )
{
if ( sizeof( _rEl ) )
{
uint64_t u64Written;
int iWriteResult = FileWrite(_hFile, &_rEl, sizeof( _rEl ), &u64Written );
__THROWPT( e_ttFileOutput );
if ( !!iWriteResult || ( u64Written != sizeof( _rEl ) ) )
THROWNAMEDEXCEPTIONERRNO( GetLastErrNo(), ( u64Written != sizeof( _rEl ) ) ? "Didn't write all the data? WTF?" : "FileWrite() failed." );
}
}
// Specialize for fdin - default version just reads raw memory:
template < class t_TyRead >
__INLINE void
_RawReadGraphEl( vtyFileHandle _hFile, t_TyRead & _rEl )
{
if ( sizeof( _rEl ) )
{
uint64_t u64Read;
int iReadResult = FileRead(_hFile, &_rEl, sizeof( _rEl ), &u64Read );
__THROWPT( e_ttFileInput );
if ( !!iReadResult || ( u64Read != sizeof( _rEl ) ) )
THROWNAMEDEXCEPTIONERRNO( GetLastErrNo(), ( u64Read != sizeof( _rEl ) ) ? "EOF before end of value." : "FileRead() failed." );
}
}
struct _file_RawElIO
{
template < class t_TyEl >
void Write( vtyFileHandle _hFile, t_TyEl const & _rel )
{
_RawWriteGraphEl( _hFile, _rel );
}
template < class t_TyEl >
void Read( vtyFileHandle _hFile, t_TyEl & _rel )
{
_RawReadGraphEl( _hFile, _rel );
}
};
template < class t_TyOutputNodeEl,
class t_TyOutputLinkEl = t_TyOutputNodeEl >
struct _file_out_object
{
typedef vtyFileHandle _TyInitArg;
typedef vtySeekOffset _TyStreamPos;
typedef t_TyOutputNodeEl _TyIONodeEl;
typedef t_TyOutputLinkEl _TyIOLinkEl;
vtyFileHandle m_hFile{vkhInvalidFileHandle}; // This object doesn't own the lifetime of the open file.
t_TyOutputNodeEl m_one;
t_TyOutputLinkEl m_ole;
_file_out_object( _file_out_object const & ) = delete;
_file_out_object() = delete;
_file_out_object( vtyFileHandle _hFile,
t_TyOutputNodeEl const & _rone,
t_TyOutputLinkEl const & _role )
: m_hFile( _hFile ),
m_one( _rone ),
m_ole( _role )
{
__THROWPT( e_ttMemory ); // in the cases where where are dynamic members within m_one or m_ole.
}
_file_out_object( vtyFileHandle _hFile,
t_TyOutputNodeEl && _rrone,
t_TyOutputLinkEl && _rrole )
: m_hFile( _hFile ),
m_one( std::move( _rrone ) ),
m_ole( std::move( _rrole ) )
{
}
_TyStreamPos TellP() const
{
__THROWPT( e_ttFileOutput );
return NFileSeekAndThrow( m_hFile, 0, vkSeekCur );
}
void SeekP( _TyStreamPos _sp )
{
int iSeekResult = FileSeek( m_hFile, _sp, vkSeekBegin );
__THROWPT( e_ttFileOutput );
if ( !!iSeekResult )
THROWNAMEDEXCEPTIONERRNO( GetLastErrNo(), "FileSeek() failed." );
}
void Write( const void * _pv, size_t _st )
{
if ( _st )
{
uint64_t u64Written;
int iWrite = FileWrite( m_hFile, _pv, _st, &u64Written );
__THROWPT( e_ttFileOutput );
if ( !!iWrite || ( u64Written != _st ) )
THROWNAMEDEXCEPTIONERRNO( GetLastErrNo(), ( u64Written != _st ) ? "Didn't write all the data? WTF?" : "FileWrite() failed." );
}
}
template < class t_TyEl >
void WriteNodeEl( t_TyEl const & _rel )
{
m_one.Write( m_hFile, _rel );
}
template < class t_TyEl >
void WriteLinkEl( t_TyEl const & _rel )
{
m_ole.Write( m_hFile, _rel );
}
};
template < class t_TyInputNodeEl,
class t_TyInputLinkEl = t_TyInputNodeEl >
struct _file_in_object
{
typedef vtyFileHandle _TyInitArg;
typedef vtySeekOffset _TyStreamPos;
typedef t_TyInputNodeEl _TyIONodeEl;
typedef t_TyInputLinkEl _TyIOLinkEl;
vtyFileHandle m_hFile{vkhInvalidFileHandle}; // This object doesn't own the lifetime of the open file.
t_TyInputNodeEl m_ine;
t_TyInputLinkEl m_ile;
_file_in_object( vtyFileHandle _hFile,
t_TyInputNodeEl const & _rine,
t_TyInputLinkEl const & _rile )
: m_hFile( _hFile ),
m_ine( _rine ),
m_ile( _rile )
{
__THROWPT( e_ttMemory ); // in the cases where where are dynamic members within m_ine or m_ile.
}
_file_in_object( vtyFileHandle _hFile,
t_TyInputNodeEl && _rrine,
t_TyInputLinkEl && _rrile )
: m_hFile( _hFile ),
m_ine( std::move( _rrine ) ),
m_ile( std::move( _rrile ) )
{
}
_TyStreamPos TellG() const
{
__THROWPT( e_ttFileInput | e_ttFatal );
return NFileSeekAndThrow( m_hFile, 0, vkSeekCur );
}
void SeekG( _TyStreamPos _sp )
{
int iSeekResult = FileSeek( m_hFile, _sp, vkSeekBegin );
__THROWPT( e_ttFileInput | e_ttFatal );
if ( !!iSeekResult )
THROWNAMEDEXCEPTIONERRNO( GetLastErrNo(), "FileSeek() failed." );
}
void Read( void * _pv, size_t _st )
{
uint64_t u64Read;
int iRead = FileRead(m_hFile, _pv, _st, &u64Read);
__THROWPT( e_ttFileInput );
if ( !!iRead || (u64Read != _st ) )
THROWNAMEDEXCEPTIONERRNO( GetLastErrNo(), ( u64Read != _st ) ? "EOF before all data read." : "FileRead() failed.");
}
template < class t_TyEl >
void ReadNodeEl( t_TyEl & _rel )
{
m_ine.Read( m_hFile, _rel );
}
template < class t_TyEl >
void ReadLinkEl( t_TyEl & _rel )
{
m_ile.Read( m_hFile, _rel );
}
};
__DGRAPH_END_NAMESPACE