aboutsummaryrefslogtreecommitdiff
path: root/lib/Wrap/file_wrapper_input.cpp
diff options
context:
space:
mode:
authorDerek Schuff <dschuff@chromium.org>2012-07-03 15:48:25 -0700
committerDerek Schuff <dschuff@chromium.org>2012-07-03 15:48:25 -0700
commit4f429c8b4e06d750b5464b6eafdd102af5196bdd (patch)
tree22a752c4654e3ab9e94c09739f7fb8f9e705433d /lib/Wrap/file_wrapper_input.cpp
parente91f926f3b76774aa7ed4c327fbde6a39e42c87f (diff)
Diff from hg rev 0b098ca44de7
Diffstat (limited to 'lib/Wrap/file_wrapper_input.cpp')
-rw-r--r--lib/Wrap/file_wrapper_input.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/Wrap/file_wrapper_input.cpp b/lib/Wrap/file_wrapper_input.cpp
new file mode 100644
index 0000000000..fc592e0246
--- /dev/null
+++ b/lib/Wrap/file_wrapper_input.cpp
@@ -0,0 +1,53 @@
+/* Copyright 2012 The Native Client Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can
+ * be found in the LICENSE file.
+ */
+
+#include <sys/stat.h>
+#include <stdlib.h>
+
+#include "llvm/Wrap/file_wrapper_input.h"
+
+FileWrapperInput::FileWrapperInput(const std::string& name) :
+ _name(name), _at_eof(false), _size_found(false), _size(0) {
+ _file = fopen(name.c_str(), "rb");
+ if (NULL == _file) {
+ fprintf(stderr, "Unable to open: %s\n", name.c_str());
+ exit(1);
+ }
+}
+
+FileWrapperInput::~FileWrapperInput() {
+ fclose(_file);
+}
+
+size_t FileWrapperInput::Read(uint8_t* buffer, size_t wanted) {
+ size_t found = fread((char*) buffer, 1, wanted, _file);
+ if (feof(_file) || ferror(_file)) {
+ _at_eof = true;
+ }
+ return found;
+}
+
+bool FileWrapperInput::AtEof() {
+ return _at_eof;
+}
+
+off_t FileWrapperInput::Size() {
+ if (_size_found) return _size;
+ struct stat st;
+ if (0 == stat(_name.c_str(), &st)) {
+ _size_found = true;
+ _size = st.st_size;
+ return _size;
+ } else {
+ fprintf(stderr, "Unable to compute file size: %s\n", _name.c_str());
+ exit(1);
+ }
+ // NOT REACHABLE.
+ return 0;
+}
+
+bool FileWrapperInput::Seek(uint32_t pos) {
+ return 0 == fseek(_file, (long) pos, SEEK_SET);
+}