00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <string.h>
00015 #include <sys/stat.h>
00016 #include <sys/types.h>
00017 #if defined(_MSC_VER)
00018 #include <io.h>
00019 #define access _access
00020 #define F_OK 0
00021 #else
00022 #include <unistd.h>
00023 #endif
00024
00025 #include <string>
00026
00027 #include "base/file.h"
00028 #include "base/logging.h"
00029 #include "base/scoped_ptr.h"
00030
00031 namespace operations_research {
00032
00033 File::File(FILE* const f_des, const std::string& name)
00034 : f_(f_des), name_(name) {}
00035
00036 bool File::Delete(char* const name) {
00037 return remove(name) == 0;
00038 }
00039
00040 bool File::Exists(char* const name) {
00041 return access(name, F_OK) == 0;
00042 }
00043
00044 size_t File::Size() {
00045 struct stat f_stat;
00046 stat(name_.c_str(), &f_stat);
00047 return f_stat.st_size;
00048 }
00049
00050 bool File::Flush() {
00051 return fflush(f_) == 0;
00052 }
00053
00054 bool File::Close() {
00055 if (fclose(f_) == 0) {
00056 f_ = NULL;
00057 return true;
00058 } else {
00059 return false;
00060 }
00061 }
00062
00063 void File::ReadOrDie(void* const buf, size_t size) {
00064 CHECK_EQ(fread(buf, 1, size, f_), size);
00065 }
00066
00067 size_t File::Read(void* const buf, size_t size) {
00068 return fread(buf, 1, size, f_);
00069 }
00070
00071 void File::WriteOrDie(const void* const buf, size_t size) {
00072 CHECK_EQ(fwrite(buf, 1, size, f_), size);
00073 }
00074 size_t File::Write(const void* const buf, size_t size) {
00075 return fwrite(buf, 1, size, f_);
00076 }
00077
00078 File* File::OpenOrDie(const char* const name, const char* const flag) {
00079 FILE* const f_des = fopen(name, flag);
00080 if (f_des == NULL) {
00081 std::cerr << "Cannot open " << name;
00082 exit(1);
00083 }
00084 File* const f = new File(f_des, name);
00085 return f;
00086 }
00087
00088 File* File::Open(const char* const name, const char* const flag) {
00089 FILE* const f_des = fopen(name, flag);
00090 if (f_des == NULL) return NULL;
00091 File* const f = new File(f_des, name);
00092 return f;
00093 }
00094
00095 char* File::ReadLine(char* const output, uint64 max_length) {
00096 return fgets(output, max_length, f_);
00097 }
00098
00099 int64 File::ReadToString(std::string* const output, uint64 max_length) {
00100 CHECK_NOTNULL(output);
00101 output->clear();
00102
00103 if (max_length == 0) return 0;
00104 if (max_length < 0) return -1;
00105
00106 int64 needed = max_length;
00107 int bufsize = (needed < (2 << 20) ? needed : (2 << 20));
00108
00109 scoped_array<char> buf(new char[bufsize]);
00110
00111 int64 nread = 0;
00112 while (needed > 0) {
00113 nread = Read(buf.get(), (bufsize < needed ? bufsize : needed));
00114 if (nread > 0) {
00115 output->append(buf.get(), nread);
00116 needed -= nread;
00117 } else {
00118 break;
00119 }
00120 }
00121 return (nread >= 0 ? static_cast<int64>(output->size()) : -1);
00122 }
00123
00124 size_t File::WriteString(const std::string& line) {
00125 return Write(line.c_str(), line.size());
00126 }
00127
00128 bool File::WriteLine(const std::string& line) {
00129 if (Write(line.c_str(), line.size()) != line.size()) return false;
00130 return Write("\n", 1) == 1;
00131 }
00132
00133 std::string File::CreateFileName() const {
00134 return name_;
00135 }
00136
00137 bool File::Open() const {
00138 return f_ != NULL;
00139 }
00140
00141 void File::Init() {}
00142 }