-
Notifications
You must be signed in to change notification settings - Fork 63
/
ofstream.cc
169 lines (144 loc) · 4.02 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
// 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 <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
namespace ustl {
//----------------------------------------------------------------------
ifstream cin (STDIN_FILENO);
ofstream cout (STDOUT_FILENO);
ofstream cerr (STDERR_FILENO);
//----------------------------------------------------------------------
/// Default constructor.
ofstream::ofstream (void)
: ostringstream (),
m_File ()
{
reserve (255);
}
/// Constructs a stream for writing to \p Fd.
ofstream::ofstream (int Fd)
: ostringstream (),
m_File (Fd)
{
clear (m_File.rdstate());
reserve (255);
}
/// Constructs a stream for writing to \p filename.
ofstream::ofstream (const char* filename, openmode mode)
: ostringstream (),
m_File (filename, mode)
{
clear (m_File.rdstate());
}
/// Default destructor.
ofstream::~ofstream (void) noexcept
{
try { flush(); } catch (...) {}
if (m_File.fd() <= STDERR_FILENO) // Do not close cin,cout,cerr
m_File.detach();
}
/// Flushes the buffer and closes the file.
void ofstream::close (void)
{
clear (m_File.rdstate());
flush();
m_File.close();
}
/// Flushes the buffer to the file.
ofstream& ofstream::flush (void)
{
clear();
while (good() && pos() && overflow (remaining())) ;
clear (m_File.rdstate());
return (*this);
}
/// Seeks to \p p based on \p d.
ofstream& ofstream::seekp (off_t p, seekdir d)
{
flush();
m_File.seekp (p, d);
clear (m_File.rdstate());
return (*this);
}
/// Called when more buffer space (\p n bytes) is needed.
ofstream::size_type ofstream::overflow (size_type n)
{
if (eof() || (n > remaining() && n < capacity() - pos()))
return (ostringstream::overflow (n));
size_type bw = m_File.write (cdata(), pos());
clear (m_File.rdstate());
erase (begin(), bw);
if (remaining() < n)
ostringstream::overflow (n);
return (remaining());
}
//----------------------------------------------------------------------
/// Constructs a stream to read from \p Fd.
ifstream::ifstream (int Fd)
: istringstream (),
m_Buffer (255,'\0'),
m_File (Fd)
{
link (m_Buffer.data(), streamsize(0));
}
/// Constructs a stream to read from \p filename.
ifstream::ifstream (const char* filename, openmode mode)
: istringstream (),
m_Buffer (255,'\0'),
m_File (filename, mode)
{
clear (m_File.rdstate());
link (m_Buffer.data(), streamsize(0));
}
/// Reads at least \p n more bytes and returns available bytes.
ifstream::size_type ifstream::underflow (size_type n)
{
if (eof())
return (istringstream::underflow (n));
const ssize_t freeSpace = m_Buffer.size() - pos();
const ssize_t neededFreeSpace = max (n, m_Buffer.size() / 2);
const size_t oughtToErase = Align (max (0, neededFreeSpace - freeSpace));
const size_type nToErase = min (pos(), oughtToErase);
m_Buffer.memlink::erase (m_Buffer.begin(), nToErase);
const uoff_t oldPos (pos() - nToErase);
size_type br = oldPos;
if (m_Buffer.size() - br < n) {
m_Buffer.resize (br + neededFreeSpace);
link (m_Buffer.data(), streamsize(0));
}
cout.flush();
size_type brn = 1;
for (; br < oldPos + n && brn && m_File.good(); br += brn)
brn = m_File.readsome (m_Buffer.begin() + br, m_Buffer.size() - br);
clear (m_File.rdstate());
m_Buffer[br] = 0;
link (m_Buffer.data(), br);
seek (oldPos);
return (remaining());
}
/// Flushes the input.
int ifstream::sync (void)
{
istringstream::sync();
underflow (0U);
clear (m_File.rdstate());
return (-good());
}
/// Seeks to \p p based on \p d.
ifstream& ifstream::seekg (off_t p, seekdir d)
{
m_Buffer.clear();
link (m_Buffer);
m_File.seekg (p, d);
clear (m_File.rdstate());
return (*this);
}
//----------------------------------------------------------------------
} // namespace ustl