Commit 16b19477 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 969dbe4b
...@@ -55,37 +55,37 @@ tuple<File, error> open(const string &path, int flags, mode_t mode) { ...@@ -55,37 +55,37 @@ tuple<File, error> open(const string &path, int flags, mode_t mode) {
} }
error File::close() { error File::close() {
File *f = this; File &f = *this;
int err = ::close(f->_fd); int err = ::close(f._fd);
if (err != 0) if (err != 0)
return f->_errno("close"); return f._errno("close");
f->_fd = -1; f._fd = -1;
return nil; return nil;
} }
tuple<int, error> File::read(void *buf, size_t count) { tuple<int, error> File::read(void *buf, size_t count) {
File *f = this; File &f = *this;
int n; int n;
n = ::read(f->_fd, buf, count); n = ::read(f._fd, buf, count);
if (n == 0) if (n == 0)
return make_tuple(n, io::EOF_); return make_tuple(n, io::EOF_);
if (n < 0) if (n < 0)
return make_tuple(0, f->_errno("read")); return make_tuple(0, f._errno("read"));
return make_tuple(n, nil); return make_tuple(n, nil);
} }
tuple <int, error> File::write(const void *buf, size_t count) { tuple <int, error> File::write(const void *buf, size_t count) {
File *f = this; File &f = *this;
int n, wrote=0; int n, wrote=0;
// NOTE contrary to write(2) we have to write all data as io.Writer requires. // NOTE contrary to write(2) we have to write all data as io.Writer requires.
while (count != 0) { while (count != 0) {
n = ::write(f->_fd, buf, count); n = ::write(f._fd, buf, count);
if (n < 0) if (n < 0)
return make_tuple(wrote, f->_errno("write")); return make_tuple(wrote, f._errno("write"));
wrote += n; wrote += n;
buf = ((const char *)buf) + n; buf = ((const char *)buf) + n;
...@@ -96,19 +96,19 @@ tuple <int, error> File::write(const void *buf, size_t count) { ...@@ -96,19 +96,19 @@ tuple <int, error> File::write(const void *buf, size_t count) {
} }
error File::stat(struct stat *st) { error File::stat(struct stat *st) {
File *f = this; File &f = *this;
int err = fstat(f->_fd, st); int err = fstat(f._fd, st);
if (err != 0) if (err != 0)
return f->_errno("stat"); return f._errno("stat");
return nil; return nil;
} }
// _errno returns error corresponding to op(file) and errno. // _errno returns error corresponding to op(file) and errno.
error File::_errno(const char *op) { error File::_errno(const char *op) {
File *f = this; File &f = *this;
return _pathError(op, f->_path, errno); return _pathError(op, f._path, errno);
} }
// _pathError returns os.PathError-like for op/path and system error // _pathError returns os.PathError-like for op/path and system error
......
...@@ -125,4 +125,41 @@ error errorf (const char *format, ...) ...@@ -125,4 +125,41 @@ error errorf (const char *format, ...)
} // fmt:: } // fmt::
// ---- misc ----
#include <unordered_map>
#include <unordered_set>
// dict wraps unordered_map into ergonomic interface.
template<typename Key, typename Value>
struct dict : std::unordered_map<Key, Value> {
// has returns whether dict has element k.
bool has(const Key &k) const {
const dict &d = *this;
return d.find(k) != d.end();
}
// get implements `d[k] -> (v, ok)`.
tuple<Value, bool> get(const Key &k) const {
const dict &d = *this;
auto _ = d.find(k);
if (_ == d.end())
return make_tuple(Value(), false);
return make_tuple(_->second, true);
}
};
// set wraps unordered_set into ergonomic interface.
template<typename Key>
struct set : std::unordered_set<Key> {
// has returns whether set has element k.
bool has(const Key &k) const {
const set &s = *this;
return s.find(k) != s.end();
}
};
#endif #endif
...@@ -34,8 +34,6 @@ using namespace golang; ...@@ -34,8 +34,6 @@ using namespace golang;
#include <wendelin/bigfile/ram.h> #include <wendelin/bigfile/ram.h>
#include <wendelin/bug.h> #include <wendelin/bug.h>
#include <unordered_map>
#include <unordered_set>
#include <vector> #include <vector>
#include <sys/types.h> #include <sys/types.h>
...@@ -48,27 +46,6 @@ using namespace golang; ...@@ -48,27 +46,6 @@ using namespace golang;
#include "wcfs_misc.h" #include "wcfs_misc.h"
template<typename Key, typename Value>
struct dict : std::unordered_map<Key, Value> {
// has returns whether container d (e.g. dict) has element k.
bool has(const Key &k) const {
const dict *d = this;
return d->find(k) != d->end();
}
// get implements `d[k] -> (v, ok)`.
tuple<Value, bool> get(const Key &k) const {
const dict *d = this;
auto _ = d->find(k);
if (_ == d->end())
return make_tuple(Value(), false);
return make_tuple(_->second, true);
}
};
template<typename Key>
using set = std::unordered_set<Key>;
using std::vector; using std::vector;
typedef uint64_t Tid; typedef uint64_t Tid;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment