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
|
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include <limits>
#include "writer.h"
namespace marisa {
namespace grimoire {
namespace io {
Writer::Writer()
: file_(NULL), fd_(-1), stream_(NULL), needs_fclose_(false) {}
Writer::~Writer() {
if (needs_fclose_) {
::fclose(file_);
}
}
void Writer::open(const char *filename) {
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
Writer temp;
temp.open_(filename);
swap(temp);
}
void Writer::open(std::FILE *file) {
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
Writer temp;
temp.open_(file);
swap(temp);
}
void Writer::open(int fd) {
MARISA_THROW_IF(fd == -1, MARISA_CODE_ERROR);
Writer temp;
temp.open_(fd);
swap(temp);
}
void Writer::open(std::ostream &stream) {
Writer temp;
temp.open_(stream);
swap(temp);
}
void Writer::clear() {
Writer().swap(*this);
}
void Writer::swap(Writer &rhs) {
marisa::swap(file_, rhs.file_);
marisa::swap(fd_, rhs.fd_);
marisa::swap(stream_, rhs.stream_);
marisa::swap(needs_fclose_, rhs.needs_fclose_);
}
void Writer::seek(std::size_t size) {
MARISA_THROW_IF(!is_open(), MARISA_STATE_ERROR);
if (size == 0) {
return;
} else if (size <= 16) {
const char buf[16] = {};
write_data(buf, size);
} else {
const char buf[1024] = {};
do {
const std::size_t count = (size < sizeof(buf)) ? size : sizeof(buf);
write_data(buf, count);
size -= count;
} while (size != 0);
}
}
bool Writer::is_open() const {
return (file_ != NULL) || (fd_ != -1) || (stream_ != NULL);
}
void Writer::open_(const char *filename) {
std::FILE *file = NULL;
#ifdef _MSC_VER
MARISA_THROW_IF(::fopen_s(&file, filename, "wb") != 0, MARISA_IO_ERROR);
#else // _MSC_VER
file = ::fopen(filename, "wb");
MARISA_THROW_IF(file == NULL, MARISA_IO_ERROR);
#endif // _MSC_VER
file_ = file;
needs_fclose_ = true;
}
void Writer::open_(std::FILE *file) {
file_ = file;
}
void Writer::open_(int fd) {
fd_ = fd;
}
void Writer::open_(std::ostream &stream) {
stream_ = &stream;
}
void Writer::write_data(const void *data, std::size_t size) {
MARISA_THROW_IF(!is_open(), MARISA_STATE_ERROR);
if (size == 0) {
return;
} else if (fd_ != -1) {
while (size != 0) {
#ifdef _WIN32
static const std::size_t CHUNK_SIZE =
std::numeric_limits<int>::max();
const unsigned int count = (size < CHUNK_SIZE) ? size : CHUNK_SIZE;
const int size_written = ::_write(fd_, data, count);
#else // _WIN32
static const std::size_t CHUNK_SIZE =
std::numeric_limits< ::ssize_t>::max();
const ::size_t count = (size < CHUNK_SIZE) ? size : CHUNK_SIZE;
const ::ssize_t size_written = ::write(fd_, data, count);
#endif // _WIN32
MARISA_THROW_IF(size_written <= 0, MARISA_IO_ERROR);
data = static_cast<const char *>(data) + size_written;
size -= size_written;
}
} else if (file_ != NULL) {
MARISA_THROW_IF(::fwrite(data, 1, size, file_) != size, MARISA_IO_ERROR);
MARISA_THROW_IF(::fflush(file_) != 0, MARISA_IO_ERROR);
} else if (stream_ != NULL) {
try {
MARISA_THROW_IF(!stream_->write(static_cast<const char *>(data), size),
MARISA_IO_ERROR);
} catch (const std::ios_base::failure &) {
MARISA_THROW(MARISA_IO_ERROR, "std::ios_base::failure");
}
}
}
} // namespace io
} // namespace grimoire
} // namespace marisa
|