forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofstream.cc
182 lines (156 loc) · 4.2 KB
/
ofstream.cc
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
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#include "ofstream.h"
#include "ustring.h"
#include "uexception.h"
#include <stdio.h>
namespace ustl {
//----------------------------------------------------------------------
ifstream cin (STDIN_FILENO);
ofstream cout (STDOUT_FILENO);
ofstream cerr (STDERR_FILENO);
//----------------------------------------------------------------------
/// Default constructor.
ofstream::ofstream (void)
: ostringstream()
,_file()
{
reserve (default_stream_buffer_size);
}
/// Constructs a stream for writing to \p ofd
ofstream::ofstream (int ofd)
: ostringstream()
,_file (ofd)
{
clear (_file.rdstate());
reserve (default_stream_buffer_size);
}
/// Constructs a stream for writing to \p filename.
ofstream::ofstream (const char* filename, openmode mode)
: ostringstream()
,_file (filename, mode)
{
clear (_file.rdstate());
}
/// Default destructor.
ofstream::~ofstream (void) noexcept
{
try { flush(); } catch (...) {}
if (_file.fd() <= STDERR_FILENO) // Do not close cin,cout,cerr
_file.detach();
}
/// Flushes the buffer and closes the file.
void ofstream::close (void)
{
clear (_file.rdstate());
flush();
_file.close();
}
/// Flushes the buffer to the file.
ostream& ofstream::flush (void)
{
while (good() && pos() && overflow (capacity())) {}
return *this;
}
/// Seeks to \p p based on \p d.
ofstream& ofstream::seekp (off_t p, seekdir d)
{
flush();
_file.seekp (p, d);
clear (_file.rdstate());
return *this;
}
/// Called when more buffer space (\p n bytes) is needed.
ofstream::size_type ofstream::overflow (size_type n)
{
if (_file.good() && n > capacity() - pos()) {
size_type bw = _file.write (cdata(), pos());
erase (begin(), bw);
}
return ostringstream::overflow (n);
}
//----------------------------------------------------------------------
/// Constructs an unattached stream
ifstream::ifstream (void)
: istringstream()
,_buffer()
,_file()
{
set_buffer_size (default_stream_buffer_size);
}
/// Constructs a stream to read from \p ifd.
ifstream::ifstream (int ifd)
: istringstream()
,_buffer()
,_file (ifd)
{
set_buffer_size (default_stream_buffer_size);
}
/// Constructs a stream to read from \p filename.
ifstream::ifstream (const char* filename, openmode mode)
: istringstream()
,_buffer()
,_file (filename, mode)
{
set_buffer_size (default_stream_buffer_size);
clear (_file.rdstate());
}
/// Set the size of the input buffer
void ifstream::set_buffer_size (size_type sz)
{
_buffer.resize (sz);
#ifndef NDEBUG
fill (_buffer.begin(), _buffer.end(), 0xcd);
#endif
link (_buffer.data(), streamsize(0));
}
/// Reads at least \p n more bytes and returns available bytes.
ifstream::size_type ifstream::underflow (size_type n)
{
if (!_file.eof()) {
const ssize_t freeSpace = _buffer.size() - pos();
const ssize_t neededFreeSpace = max (n, _buffer.size() / 2);
const size_t oughtToErase = Align (max (0, neededFreeSpace - freeSpace));
const size_type nToErase = min (pos(), oughtToErase);
_buffer.memlink::erase (_buffer.begin(), nToErase);
const uoff_t oldPos (pos() - nToErase);
size_type br = oldPos;
if (_buffer.size() - br < n) {
_buffer.resize (br + neededFreeSpace);
link (_buffer.data(), streamsize(0));
}
if (_file.fd() == STDIN_FILENO)
cout.flush();
size_type brn = 1;
for (; br < oldPos + n && brn && _file.good(); br += brn)
brn = _file.readsome (_buffer.begin() + br, _buffer.size() - br);
clear (_file.rdstate());
_buffer[br] = 0;
link (_buffer.data(), br);
seek (oldPos);
}
if (_file.eof())
verify_remaining ("read", _file.name(), n);
return remaining();
}
/// Flushes the input.
int ifstream::sync (void)
{
istringstream::sync();
underflow (0U);
clear (_file.rdstate());
return -good();
}
/// Seeks to \p p based on \p d.
ifstream& ifstream::seekg (off_t p, seekdir d)
{
_buffer.clear();
link (_buffer);
_file.seekg (p, d);
clear (_file.rdstate());
return *this;
}
//----------------------------------------------------------------------
} // namespace ustl