summaryrefslogtreecommitdiff
path: root/klm/util
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2012-03-09 22:24:05 -0500
committerChris Dyer <cdyer@cs.cmu.edu>2012-03-09 22:24:05 -0500
commit164ef0ee3579202af838c54bdb402ce5559b6044 (patch)
treea163c94ff999bd5920461da75a0e22a6792b35ac /klm/util
parent113317266853abff2e1c0c3e889017d0eee55c93 (diff)
parent249301376865578b7f9678cc97c0f8b6f78623f6 (diff)
Merge branch 'master' of github.com:redpony/cdec
Diffstat (limited to 'klm/util')
-rw-r--r--klm/util/file.cc10
-rw-r--r--klm/util/file.hh3
-rw-r--r--klm/util/mmap.cc14
3 files changed, 13 insertions, 14 deletions
diff --git a/klm/util/file.cc b/klm/util/file.cc
index aee7c77a..176737fa 100644
--- a/klm/util/file.cc
+++ b/klm/util/file.cc
@@ -42,6 +42,16 @@ int OpenReadOrThrow(const char *name) {
return ret;
}
+int CreateOrThrow(const char *name) {
+ int ret;
+#if defined(_WIN32) || defined(_WIN64)
+ UTIL_THROW_IF(-1 == (ret = _open(name, _O_CREAT | _O_TRUNC | _O_RDWR, _S_IREAD | _S_IWRITE)), ErrnoException, "while creating " << name);
+#else
+ UTIL_THROW_IF(-1 == (ret = open(name, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)), ErrnoException, "while creating " << name);
+#endif
+ return ret;
+}
+
uint64_t SizeFile(int fd) {
#if defined(_WIN32) || defined(_WIN64)
__int64 ret = _filelengthi64(fd);
diff --git a/klm/util/file.hh b/klm/util/file.hh
index 5c57e2a9..72c8ea76 100644
--- a/klm/util/file.hh
+++ b/klm/util/file.hh
@@ -65,7 +65,10 @@ class scoped_FILE {
std::FILE *file_;
};
+// Open for read only.
int OpenReadOrThrow(const char *name);
+// Create file if it doesn't exist, truncate if it does. Opened for write.
+int CreateOrThrow(const char *name);
// Return value for SizeFile when it can't size properly.
const uint64_t kBadSize = (uint64_t)-1;
diff --git a/klm/util/mmap.cc b/klm/util/mmap.cc
index a329ce4e..3b1c58b8 100644
--- a/klm/util/mmap.cc
+++ b/klm/util/mmap.cc
@@ -170,20 +170,6 @@ void *MapZeroedWrite(int fd, std::size_t size) {
return MapOrThrow(size, true, kFileFlags, false, fd, 0);
}
-namespace {
-
-int CreateOrThrow(const char *name) {
- int ret;
-#if defined(_WIN32) || defined(_WIN64)
- UTIL_THROW_IF(-1 == (ret = _open(name, _O_CREAT | _O_TRUNC | _O_RDWR, _S_IREAD | _S_IWRITE)), ErrnoException, "while creating " << name);
-#else
- UTIL_THROW_IF(-1 == (ret = open(name, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)), ErrnoException, "while creating " << name);
-#endif
- return ret;
-}
-
-} // namespace
-
void *MapZeroedWrite(const char *name, std::size_t size, scoped_fd &file) {
file.reset(CreateOrThrow(name));
try {