libostd/octa/stream.h

158 lines
3.3 KiB
C
Raw Normal View History

/* Generic stream implementation for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
#ifndef OCTA_STREAM_H
#define OCTA_STREAM_H
2015-06-26 03:30:48 +02:00
#include <stdio.h>
#include <sys/types.h>
#include "octa/types.h"
#include "octa/range.h"
2015-06-26 21:18:40 +02:00
#include "octa/string.h"
#include "octa/type_traits.h"
2015-06-26 03:30:48 +02:00
namespace octa {
2015-06-26 03:30:48 +02:00
/* off_t is POSIX - will also work on windows with mingw/clang, but FIXME */
using StreamOffset = off_t;
2015-06-26 21:18:40 +02:00
enum class StreamSeek {
2015-06-26 03:30:48 +02:00
cur = SEEK_CUR,
end = SEEK_END,
set = SEEK_SET
};
2015-06-26 22:14:54 +02:00
struct StreamRange;
2015-06-26 21:41:11 +02:00
struct Stream: InputRange<
Stream, octa::InputRangeTag, char, char, octa::Size, StreamOffset
2015-06-26 21:18:40 +02:00
> {
2015-06-26 03:30:48 +02:00
using Offset = StreamOffset;
2015-06-26 21:18:40 +02:00
virtual ~Stream() {}
2015-06-26 03:30:48 +02:00
virtual void close() = 0;
2015-06-26 21:18:40 +02:00
virtual bool end() const = 0;
2015-06-26 03:30:48 +02:00
virtual Offset size() {
Offset p = tell();
2015-06-26 21:18:40 +02:00
if ((p < 0) || !seek(0, StreamSeek::end)) return -1;
2015-06-26 03:30:48 +02:00
Offset e = tell();
2015-06-26 21:18:40 +02:00
return (p == e) || (seek(p, StreamSeek::set) ? e : -1);
2015-06-26 03:30:48 +02:00
}
2015-06-26 21:18:40 +02:00
virtual bool seek(Offset, StreamSeek = StreamSeek::set) {
return false;
}
2015-06-26 03:30:48 +02:00
2015-06-26 21:18:40 +02:00
virtual Offset tell() const { return -1; }
2015-06-26 03:30:48 +02:00
virtual bool flush() { return true; }
2015-06-26 21:18:40 +02:00
2015-06-26 21:41:11 +02:00
virtual octa::Size read(char *, octa::Size) { return 0; }
virtual octa::Size write(const char *, octa::Size) { return 0; }
2015-06-26 21:18:40 +02:00
2015-06-26 22:14:54 +02:00
StreamRange iter();
};
struct StreamRange: InputRange<
Stream, octa::InputRangeTag, char, char, octa::Size, StreamOffset
> {
StreamRange(Stream &s): p_stream(&s) {}
StreamRange(const StreamRange &r): p_stream(r.p_stream) {}
2015-06-26 21:18:40 +02:00
2015-06-26 22:14:54 +02:00
bool empty() const { return p_stream->end(); }
2015-06-26 21:18:40 +02:00
bool pop_front() {
if (empty()) return false;
2015-06-26 21:41:11 +02:00
char val;
2015-06-26 22:14:54 +02:00
return !!p_stream->read(&val, 1);
2015-06-26 21:18:40 +02:00
}
2015-06-26 21:41:11 +02:00
char front() const {
char val;
2015-06-26 22:14:54 +02:00
p_stream->seek(-p_stream->read(&val, 1), StreamSeek::cur);
2015-06-26 21:18:40 +02:00
return val;
}
2015-06-26 22:14:54 +02:00
virtual bool equals_front(const StreamRange &s) const {
return p_stream->tell() == s.p_stream->tell();
2015-06-26 21:18:40 +02:00
}
2015-06-26 21:41:11 +02:00
void put(const char &val) {
2015-06-26 22:14:54 +02:00
p_stream->write(&val, 1);
2015-06-26 21:18:40 +02:00
}
2015-06-26 22:14:54 +02:00
private:
Stream *p_stream;
2015-06-26 21:18:40 +02:00
};
2015-06-26 22:14:54 +02:00
inline StreamRange Stream::iter() {
return StreamRange(*this);
}
2015-06-26 21:18:40 +02:00
enum class StreamMode {
read, write, append,
update = 1 << 2
};
namespace detail {
static const char *filemodes[] = {
"rb", "wb", "ab", nullptr, "rb+", "wb+", "ab+"
};
}
2015-06-26 21:41:11 +02:00
struct FileStream: Stream {
2015-06-26 22:14:54 +02:00
FileStream(): p_f() {}
2015-06-26 22:20:05 +02:00
FileStream(const FileStream &s) = delete;
2015-06-26 21:18:40 +02:00
2015-06-26 22:14:54 +02:00
FileStream(const octa::String &path, StreamMode mode): p_f() {
2015-06-26 21:18:40 +02:00
open(path, mode);
}
~FileStream() { close(); }
bool open(const octa::String &path, StreamMode mode) {
2015-06-26 22:14:54 +02:00
if (p_f) return false;
2015-06-26 21:18:40 +02:00
p_f = fopen(path.data(), octa::detail::filemodes[octa::Size(mode)]);
return p_f != nullptr;
}
void close() {
2015-06-26 22:14:54 +02:00
if (p_f) fclose(p_f);
2015-06-26 21:18:40 +02:00
p_f = nullptr;
}
bool end() const {
return feof(p_f) != 0;
}
bool seek(StreamOffset pos, StreamSeek whence = StreamSeek::set) {
return fseeko(p_f, pos, int(whence)) >= 0;
}
StreamOffset tell() const {
return ftello(p_f);
}
bool flush() { return !fflush(p_f); }
2015-06-26 21:41:11 +02:00
octa::Size read(char *buf, octa::Size count) {
return fread(buf, 1, count, p_f);
2015-06-26 21:18:40 +02:00
}
2015-06-26 21:41:11 +02:00
octa::Size write(const char *buf, octa::Size count) {
return fwrite(buf, 1, count, p_f);
2015-06-26 21:18:40 +02:00
}
private:
FILE *p_f;
2015-06-26 03:30:48 +02:00
};
}
#endif