Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Heafield <github@kheafield.com>2013-01-17 15:58:58 +0400
committerKenneth Heafield <github@kheafield.com>2013-01-17 15:58:58 +0400
commita7731ce432347981bcc42962e07275a310972bbf (patch)
tree852bb4a0a7065a765351cd1e963968cac8fc4a6f /util/file.hh
parent8af1d6a017bfebe134ade38780648771fab4ae69 (diff)
KenLM 014bced6
Diffstat (limited to 'util/file.hh')
-rw-r--r--util/file.hh52
1 files changed, 42 insertions, 10 deletions
diff --git a/util/file.hh b/util/file.hh
index c24580d60..471198b1c 100644
--- a/util/file.hh
+++ b/util/file.hh
@@ -1,6 +1,8 @@
#ifndef UTIL_FILE__
#define UTIL_FILE__
+#include "util/exception.hh"
+
#include <cstddef>
#include <cstdio>
#include <string>
@@ -17,7 +19,7 @@ class scoped_fd {
~scoped_fd();
- void reset(int to) {
+ void reset(int to = -1) {
scoped_fd other(fd_);
fd_ = to;
}
@@ -63,6 +65,32 @@ class scoped_FILE {
std::FILE *file_;
};
+/* Thrown for any operation where the fd is known. */
+class FDException : public ErrnoException {
+ public:
+ explicit FDException(int fd) throw();
+
+ virtual ~FDException() throw();
+
+ // This may no longer be valid if the exception was thrown past open.
+ int FD() const { return fd_; }
+
+ // Guess from NameFromFD.
+ const std::string &NameGuess() const { return name_guess_; }
+
+ private:
+ int fd_;
+
+ std::string name_guess_;
+};
+
+// End of file reached.
+class EndOfFileException : public Exception {
+ public:
+ EndOfFileException() throw();
+ ~EndOfFileException() throw();
+};
+
// Open for read only.
int OpenReadOrThrow(const char *name);
// Create file if it doesn't exist, truncate if it does. Opened for write.
@@ -71,12 +99,15 @@ int CreateOrThrow(const char *name);
// Return value for SizeFile when it can't size properly.
const uint64_t kBadSize = (uint64_t)-1;
uint64_t SizeFile(int fd);
+uint64_t SizeOrThrow(int fd);
void ResizeOrThrow(int fd, uint64_t to);
std::size_t PartialRead(int fd, void *to, std::size_t size);
void ReadOrThrow(int fd, void *to, std::size_t size);
std::size_t ReadOrEOF(int fd, void *to_void, std::size_t size);
+// Positioned: unix only for now.
+void PReadOrThrow(int fd, void *to, std::size_t size, uint64_t off);
void WriteOrThrow(int fd, const void *data_void, std::size_t size);
void WriteOrThrow(FILE *to, const void *data, std::size_t size);
@@ -91,17 +122,18 @@ void SeekEnd(int fd);
std::FILE *FDOpenOrThrow(scoped_fd &file);
std::FILE *FDOpenReadOrThrow(scoped_fd &file);
-class TempMaker {
- public:
- explicit TempMaker(const std::string &prefix);
+// Temporary files
+int MakeTemp(const std::string &prefix);
+std::FILE *FMakeTemp(const std::string &prefix);
- // These will already be unlinked for you.
- int Make() const;
- std::FILE *MakeFile() const;
+// dup an fd.
+int DupOrThrow(int fd);
- private:
- std::string base_;
-};
+/* Attempt get file name from fd. This won't always work (i.e. on Windows or
+ * a pipe). The file might have been renamed. It's intended for diagnostics
+ * and logging only.
+ */
+std::string NameFromFD(int fd);
} // namespace util