2015-06-30 18:25:40 +00:00
|
|
|
/* Standard I/O implementation for OctaSTD.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OCTA_IO_HH
|
|
|
|
#define OCTA_IO_HH
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-07-01 19:59:05 +00:00
|
|
|
#include "octa/platform.hh"
|
2015-06-30 18:25:40 +00:00
|
|
|
#include "octa/string.hh"
|
|
|
|
#include "octa/stream.hh"
|
2015-07-01 01:22:42 +00:00
|
|
|
#include "octa/format.hh"
|
2015-06-30 18:25:40 +00:00
|
|
|
|
|
|
|
namespace octa {
|
|
|
|
|
|
|
|
enum class StreamMode {
|
|
|
|
read, write, append,
|
|
|
|
update = 1 << 2
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
static const char *filemodes[] = {
|
|
|
|
"rb", "wb", "ab", nullptr, "rb+", "wb+", "ab+"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileStream: Stream {
|
|
|
|
FileStream(): p_f(), p_owned(false) {}
|
|
|
|
FileStream(const FileStream &) = delete;
|
|
|
|
FileStream(FileStream &&s): p_f(s.p_f), p_owned(s.p_owned) {
|
|
|
|
s.p_f = nullptr;
|
|
|
|
s.p_owned = false;
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
FileStream(const char *path, StreamMode mode): p_f() {
|
|
|
|
open(path, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A>
|
2015-07-05 22:59:36 +00:00
|
|
|
FileStream(const AnyString<A> &path, StreamMode mode): p_f() {
|
2015-06-30 18:25:40 +00:00
|
|
|
open(path, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileStream(FILE *f): p_f(f), p_owned(false) {}
|
|
|
|
|
|
|
|
~FileStream() { close(); }
|
|
|
|
|
|
|
|
FileStream &operator=(const FileStream &) = delete;
|
|
|
|
FileStream &operator=(FileStream &&s) {
|
|
|
|
close();
|
|
|
|
swap(s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
bool open(const char *path, StreamMode mode) {
|
2015-06-30 18:25:40 +00:00
|
|
|
if (p_f) return false;
|
2015-07-05 22:59:36 +00:00
|
|
|
p_f = fopen(path, detail::filemodes[Size(mode)]);
|
2015-06-30 18:25:40 +00:00
|
|
|
p_owned = true;
|
|
|
|
return is_open();
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
template<typename A>
|
2015-07-05 22:59:36 +00:00
|
|
|
bool open(const AnyString<A> &path, StreamMode mode) {
|
2015-07-01 19:09:02 +00:00
|
|
|
return open(path.data(), mode);
|
|
|
|
}
|
|
|
|
|
2015-06-30 18:25:40 +00:00
|
|
|
bool open(FILE *f) {
|
|
|
|
if (p_f) return false;
|
|
|
|
p_f = f;
|
|
|
|
p_owned = false;
|
|
|
|
return is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_open() const { return p_f != nullptr; }
|
|
|
|
bool is_owned() const { return p_owned; }
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
if (p_f && p_owned) fclose(p_f);
|
|
|
|
p_f = nullptr;
|
|
|
|
p_owned = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end() const {
|
|
|
|
return feof(p_f) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool seek(StreamOffset pos, StreamSeek whence = StreamSeek::set) {
|
2015-07-01 19:59:05 +00:00
|
|
|
#ifndef OCTA_PLATFORM_WIN32
|
2015-06-30 18:25:40 +00:00
|
|
|
return fseeko(p_f, pos, int(whence)) >= 0;
|
2015-07-01 19:59:05 +00:00
|
|
|
#else
|
|
|
|
return _fseeki64(p_f, pos, int(whence)) >= 0;
|
|
|
|
#endif
|
2015-06-30 18:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamOffset tell() const {
|
2015-07-01 19:59:05 +00:00
|
|
|
#ifndef OCTA_PLATFORM_WIN32
|
2015-06-30 18:25:40 +00:00
|
|
|
return ftello(p_f);
|
2015-07-01 19:59:05 +00:00
|
|
|
#else
|
|
|
|
return _ftelli64(p_f);
|
|
|
|
#endif
|
2015-06-30 18:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool flush() { return !fflush(p_f); }
|
|
|
|
|
2015-07-05 22:59:36 +00:00
|
|
|
Size read_bytes(void *buf, Size count) {
|
2015-06-30 18:25:40 +00:00
|
|
|
return fread(buf, 1, count, p_f);
|
|
|
|
}
|
|
|
|
|
2015-07-05 22:59:36 +00:00
|
|
|
Size write_bytes(const void *buf, Size count) {
|
2015-06-30 18:25:40 +00:00
|
|
|
return fwrite(buf, 1, count, p_f);
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:59:05 +00:00
|
|
|
int getchar() {
|
|
|
|
return fgetc(p_f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool putchar(int c) {
|
|
|
|
return fputc(c, p_f) != EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write(const char *s) {
|
|
|
|
return fputs(s, p_f) != EOF;
|
|
|
|
}
|
|
|
|
|
2015-06-30 18:25:40 +00:00
|
|
|
void swap(FileStream &s) {
|
|
|
|
octa::swap(p_f, s.p_f);
|
|
|
|
octa::swap(p_owned, s.p_owned);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *get_file() { return p_f; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE *p_f;
|
|
|
|
bool p_owned;
|
|
|
|
};
|
|
|
|
|
|
|
|
static FileStream in(::stdin);
|
|
|
|
static FileStream out(::stdout);
|
|
|
|
static FileStream err(::stderr);
|
|
|
|
|
|
|
|
/* no need to call anything from FileStream, prefer simple calls... */
|
|
|
|
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void write(const char *s) {
|
2015-06-30 18:25:40 +00:00
|
|
|
fputs(s, ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
template<typename A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void write(const AnyString<A> &s) {
|
2015-06-30 18:25:40 +00:00
|
|
|
fwrite(s.data(), 1, s.size(), ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:41:01 +00:00
|
|
|
template<typename T>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void write(const T &v) {
|
2015-07-05 22:59:36 +00:00
|
|
|
write(octa::to_string(v));
|
2015-06-30 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename ...A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void write(const T &v, const A &...args) {
|
2015-07-05 22:59:36 +00:00
|
|
|
write(v);
|
2015-06-30 21:41:01 +00:00
|
|
|
write(args...);
|
|
|
|
}
|
|
|
|
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writeln(const char *s) {
|
2015-07-05 22:59:36 +00:00
|
|
|
write(s);
|
2015-06-30 18:25:40 +00:00
|
|
|
putc('\n', ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
template<typename A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writeln(const AnyString<A> &s) {
|
2015-07-05 22:59:36 +00:00
|
|
|
write(s);
|
2015-06-30 18:25:40 +00:00
|
|
|
putc('\n', ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:41:01 +00:00
|
|
|
template<typename T>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writeln(const T &v) {
|
2015-07-05 22:59:36 +00:00
|
|
|
writeln(octa::to_string(v));
|
2015-06-30 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename ...A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writeln(const T &v, const A &...args) {
|
2015-07-05 22:59:36 +00:00
|
|
|
write(v);
|
2015-06-30 21:41:01 +00:00
|
|
|
write(args...);
|
|
|
|
putc('\n', ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-07-03 17:21:05 +00:00
|
|
|
namespace detail {
|
2015-07-05 22:59:36 +00:00
|
|
|
struct UnsafeWritefRange: OutputRange<UnsafeWritefRange, char> {
|
2015-07-03 17:21:05 +00:00
|
|
|
UnsafeWritefRange(char *p): p_ptr(p) {}
|
|
|
|
bool put(char c) {
|
|
|
|
*p_ptr++ = c;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
char *p_ptr;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-07-01 01:22:42 +00:00
|
|
|
template<typename ...A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writef(const char *fmt, const A &...args) {
|
2015-07-01 17:51:39 +00:00
|
|
|
char buf[512];
|
2015-07-05 22:59:36 +00:00
|
|
|
Ptrdiff need = format(detail::FormatOutRange<sizeof(buf)>(buf),
|
|
|
|
fmt, args...);
|
2015-07-03 20:13:54 +00:00
|
|
|
if (need < 0) return;
|
2015-07-05 22:59:36 +00:00
|
|
|
else if (Size(need) < sizeof(buf)) {
|
2015-07-01 17:51:39 +00:00
|
|
|
fwrite(buf, 1, need, ::stdout);
|
2015-07-01 01:22:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-05 22:59:36 +00:00
|
|
|
Vector<char> s;
|
2015-07-01 17:51:39 +00:00
|
|
|
s.reserve(need);
|
2015-07-05 22:59:36 +00:00
|
|
|
format(detail::UnsafeWritefRange(s.data()), fmt, args...);
|
2015-07-01 17:51:39 +00:00
|
|
|
fwrite(s.data(), 1, need, ::stdout);
|
2015-07-01 01:22:42 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
template<typename AL, typename ...A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writef(const AnyString<AL> &fmt,
|
2015-07-01 19:09:02 +00:00
|
|
|
const A &...args) {
|
2015-07-01 17:51:39 +00:00
|
|
|
writef(fmt.data(), args...);
|
2015-07-01 01:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ...A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writefln(const char *fmt, const A &...args) {
|
2015-07-01 17:51:39 +00:00
|
|
|
writef(fmt, args...);
|
2015-07-01 01:22:42 +00:00
|
|
|
putc('\n', ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:09:02 +00:00
|
|
|
template<typename AL, typename ...A>
|
2015-07-11 15:07:52 +00:00
|
|
|
inline void writefln(const AnyString<AL> &fmt,
|
2015-07-01 19:09:02 +00:00
|
|
|
const A &...args) {
|
2015-07-01 17:51:39 +00:00
|
|
|
writef(fmt, args...);
|
2015-07-01 01:22:42 +00:00
|
|
|
putc('\n', ::stdout);
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:46:01 +00:00
|
|
|
} /* namespace octa */
|
2015-06-30 18:25:40 +00:00
|
|
|
|
|
|
|
#endif
|