OR-Tools  8.1
base/file.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_FILE_H_
15 #define OR_TOOLS_BASE_FILE_H_
16 
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 
21 #include "absl/status/status.h"
22 #include "absl/strings/string_view.h"
23 #include "google/protobuf/descriptor.h"
24 #include "google/protobuf/io/tokenizer.h"
25 #include "google/protobuf/message.h"
26 #include "google/protobuf/text_format.h"
28 #include "ortools/base/logging.h"
29 
30 // This file defines some IO interfaces for compatibility with Google
31 // IO specifications.
32 class File {
33  public:
34  // Opens file "name" with flags specified by "flag".
35  // Flags are defined by fopen(), that is "r", "r+", "w", "w+". "a", and "a+".
36  static File* Open(const char* const name, const char* const flag);
37 
38 #ifndef SWIG // no overloading
39  inline static File* Open(const absl::string_view& name,
40  const char* const mode) {
41  return Open(name.data(), mode);
42  }
43 #endif // SWIG
44 
45  // Opens file "name" with flags specified by "flag".
46  // If open failed, program will exit.
47  static File* OpenOrDie(const char* const name, const char* const flag);
48 
49 #ifndef SWIG // no overloading
50  inline static File* OpenOrDie(const absl::string_view& name,
51  const char* const flag) {
52  return OpenOrDie(name.data(), flag);
53  }
54 #endif // SWIG
55 
56  // Reads "size" bytes to buff from file, buff should be pre-allocated.
57  size_t Read(void* const buff, size_t size);
58 
59  // Reads "size" bytes to buff from file, buff should be pre-allocated.
60  // If read failed, program will exit.
61  void ReadOrDie(void* const buff, size_t size);
62 
63  // Reads a line from file to a string.
64  // Each line must be no more than max_length bytes.
65  char* ReadLine(char* const output, uint64 max_length);
66 
67  // Reads the whole file to a string, with a maximum length of 'max_length'.
68  // Returns the number of bytes read.
69  int64 ReadToString(std::string* const line, uint64 max_length);
70 
71  // Writes "size" bytes of buff to file, buff should be pre-allocated.
72  size_t Write(const void* const buff, size_t size);
73 
74  // Writes "size" bytes of buff to file, buff should be pre-allocated.
75  // If write failed, program will exit.
76  void WriteOrDie(const void* const buff, size_t size);
77 
78  // Writes a string to file.
79  size_t WriteString(const std::string& line);
80 
81  // Writes a string to file and append a "\n".
82  bool WriteLine(const std::string& line);
83 
84  // Closes the file.
85  bool Close();
86  absl::Status Close(int flags);
87 
88  // Flushes buffer.
89  bool Flush();
90 
91  // Returns file size.
92  size_t Size();
93 
94  // Inits internal data structures.
95  static void Init();
96 
97  // Returns the file name.
98  absl::string_view filename() const;
99 
100  // Deletes a file.
101  static bool Delete(const char* const name);
102  static bool Delete(const absl::string_view& name) {
103  return Delete(name.data());
104  }
105 
106  // Tests if a file exists.
107  static bool Exists(const char* const name);
108 
109  bool Open() const;
110 
111  private:
112  File(FILE* const descriptor, const absl::string_view& name);
113 
114  FILE* f_;
115  const absl::string_view name_;
116 };
117 
118 namespace file {
119 inline int Defaults() { return 0xBABA; }
120 
121 // As of 2016-01, these methods can only be used with flags = file::Defaults().
122 absl::Status Open(const absl::string_view& filename,
123  const absl::string_view& mode, File** f, int flags);
124 File* OpenOrDie(const absl::string_view& filename,
125  const absl::string_view& mode, int flags);
126 absl::Status GetTextProto(const absl::string_view& filename,
127  google::protobuf::Message* proto, int flags);
128 absl::Status SetTextProto(const absl::string_view& filename,
129  const google::protobuf::Message& proto, int flags);
130 absl::Status SetBinaryProto(const absl::string_view& filename,
131  const google::protobuf::Message& proto, int flags);
132 absl::Status SetContents(const absl::string_view& filename,
133  const absl::string_view& contents, int flags);
134 absl::Status GetContents(const absl::string_view& filename, std::string* output,
135  int flags);
136 absl::Status WriteString(File* file, const absl::string_view& contents,
137  int flags);
138 
139 bool ReadFileToString(const absl::string_view& file_name, std::string* output);
140 bool WriteStringToFile(const std::string& data,
141  const absl::string_view& file_name);
142 bool ReadFileToProto(const absl::string_view& file_name,
143  google::protobuf::Message* proto);
144 void ReadFileToProtoOrDie(const absl::string_view& file_name,
145  google::protobuf::Message* proto);
146 bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
147  const absl::string_view& file_name);
148 void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
149  const absl::string_view& file_name);
150 bool WriteProtoToFile(const google::protobuf::Message& proto,
151  const absl::string_view& file_name);
152 void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
153  const absl::string_view& file_name);
154 
155 absl::Status Delete(const absl::string_view& path, int flags);
156 absl::Status Exists(const absl::string_view& path, int flags);
157 
158 } // namespace file
159 
160 #endif // OR_TOOLS_BASE_FILE_H_
file::SetContents
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: file.cc:188
integral_types.h
File::Init
static void Init()
Definition: file.cc:139
file::GetContents
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: file.cc:163
File::Close
bool Close()
Definition: file.cc:48
file::WriteString
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Definition: file.cc:176
file::Exists
absl::Status Exists(const absl::string_view &path, int flags)
Definition: file.cc:305
file::SetTextProto
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: file.cc:277
logging.h
File::filename
absl::string_view filename() const
Definition: file.cc:135
file::ReadFileToProto
bool ReadFileToProto(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: file.cc:209
File::Open
static File * Open(const absl::string_view &name, const char *const mode)
Definition: base/file.h:39
File::WriteString
size_t WriteString(const std::string &line)
Definition: file.cc:126
file::GetTextProto
absl::Status GetTextProto(const absl::string_view &filename, google::protobuf::Message *proto, int flags)
Definition: file.cc:267
File::Size
size_t Size()
Definition: file.cc:40
File::Delete
static bool Delete(const char *const name)
Definition: file.cc:36
File::Open
bool Open() const
Definition: file.cc:137
int64
int64_t int64
Definition: integral_types.h:34
file::ReadFileToString
bool ReadFileToString(const absl::string_view &file_name, std::string *output)
Definition: file.cc:193
File::ReadLine
char * ReadLine(char *const output, uint64 max_length)
Definition: file.cc:98
file::WriteStringToFile
bool WriteStringToFile(const std::string &data, const absl::string_view &file_name)
Definition: file.cc:197
File
Definition: base/file.h:32
File::WriteLine
bool WriteLine(const std::string &line)
Definition: file.cc:130
File::ReadOrDie
void ReadOrDie(void *const buff, size_t size)
Definition: file.cc:66
file::SetBinaryProto
absl::Status SetBinaryProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: file.cc:287
File::Read
size_t Read(void *const buff, size_t size)
Definition: file.cc:70
uint64
uint64_t uint64
Definition: integral_types.h:39
file::WriteProtoToFileOrDie
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:262
file::ReadFileToProtoOrDie
void ReadFileToProtoOrDie(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: file.cc:238
file::WriteProtoToFile
bool WriteProtoToFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:255
File::Delete
static bool Delete(const absl::string_view &name)
Definition: base/file.h:102
File::Write
size_t Write(const void *const buff, size_t size)
Definition: file.cc:77
file::Delete
absl::Status Delete(const absl::string_view &path, int flags)
Definition: file.cc:297
file::Defaults
int Defaults()
Definition: base/file.h:119
File::WriteOrDie
void WriteOrDie(const void *const buff, size_t size)
Definition: file.cc:74
file
Definition: file.cc:141
File::OpenOrDie
static File * OpenOrDie(const char *const name, const char *const flag)
Definition: file.cc:81
proto
CpModelProto proto
Definition: cp_model_fz_solver.cc:107
file::Open
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
Definition: file.cc:142
File::ReadToString
int64 ReadToString(std::string *const line, uint64 max_length)
Definition: file.cc:102
file::WriteProtoToASCIIFile
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:243
file::WriteProtoToASCIIFileOrDie
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:250
file::OpenOrDie
File * OpenOrDie(const absl::string_view &filename, const absl::string_view &mode, int flags)
Definition: file.cc:154
name
const std::string name
Definition: default_search.cc:808
File::Flush
bool Flush()
Definition: file.cc:46
File::OpenOrDie
static File * OpenOrDie(const absl::string_view &name, const char *const flag)
Definition: base/file.h:50
File::Exists
static bool Exists(const char *const name)
Definition: file.cc:38