OR-Tools  9.6
base/file.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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"
30 
31 // This file defines some IO interfaces for compatibility with Google
32 // IO specifications.
33 class File {
34  public:
35  // Opens file "name" with flags specified by "flag".
36  // Flags are defined by fopen(), that is "r", "r+", "w", "w+". "a", and "a+".
37  static File* Open(const char* const name, const char* const flag);
38 
39 #ifndef SWIG // no overloading
40  inline static File* Open(const absl::string_view& name,
41  const char* const mode) {
42  return Open(name.data(), mode);
43  }
44 #endif // SWIG
45 
46  // Opens file "name" with flags specified by "flag".
47  // If open failed, program will exit.
48  static File* OpenOrDie(const char* const name, const char* const flag);
49 
50 #ifndef SWIG // no overloading
51  inline static File* OpenOrDie(const absl::string_view& name,
52  const char* const flag) {
53  return OpenOrDie(name.data(), flag);
54  }
55 #endif // SWIG
56 
57  // Reads "size" bytes to buff from file, buff should be pre-allocated.
58  size_t Read(void* const buff, size_t size);
59 
60  // Reads "size" bytes to buff from file, buff should be pre-allocated.
61  // If read failed, program will exit.
62  void ReadOrDie(void* const buff, size_t size);
63 
64  // Reads a line from file to a string.
65  // Each line must be no more than max_length bytes.
66  char* ReadLine(char* const output, uint64_t max_length);
67 
68  // Reads the whole file to a string, with a maximum length of 'max_length'.
69  // Returns the number of bytes read.
70  int64_t ReadToString(std::string* const line, uint64_t max_length);
71 
72  // Writes "size" bytes of buff to file, buff should be pre-allocated.
73  size_t Write(const void* const buff, size_t size);
74 
75  // Writes "size" bytes of buff to file, buff should be pre-allocated.
76  // If write failed, program will exit.
77  void WriteOrDie(const void* const buff, size_t size);
78 
79  // Writes a string to file.
80  size_t WriteString(const std::string& line);
81 
82  // Writes a string to file and append a "\n".
83  bool WriteLine(const std::string& line);
84 
85  // Closes the file.
86  bool Close();
87  absl::Status Close(int flags);
88 
89  // Flushes buffer.
90  bool Flush();
91 
92  // Returns file size.
93  size_t Size();
94 
95  // Inits internal data structures.
96  static void Init();
97 
98  // Returns the file name.
99  absl::string_view filename() const;
100 
101  // Deletes a file.
102  static bool Delete(const char* const name);
103  static bool Delete(const absl::string_view& name) {
104  return Delete(name.data());
105  }
106 
107  // Tests if a file exists.
108  static bool Exists(const char* const name);
109 
110  bool Open() const;
111 
112  private:
113  File(FILE* const descriptor, const absl::string_view& name);
114 
115  FILE* f_;
116  const absl::string_view name_;
117 };
118 
119 namespace file {
120 
121 using Options = int;
122 
123 inline Options Defaults() { return 0xBABA; }
124 
125 // As of 2016-01, these methods can only be used with flags = file::Defaults().
126 absl::Status Open(const absl::string_view& filename,
127  const absl::string_view& mode, File** f, int flags);
128 File* OpenOrDie(const absl::string_view& filename,
129  const absl::string_view& mode, int flags);
130 absl::Status GetTextProto(const absl::string_view& filename,
131  google::protobuf::Message* proto, int flags);
132 template <typename T>
133 absl::StatusOr<T> GetTextProto(absl::string_view filename, int flags) {
134  T proto;
135  RETURN_IF_ERROR(GetTextProto(filename, &proto, flags));
136  return proto;
137 }
138 absl::Status SetTextProto(const absl::string_view& filename,
139  const google::protobuf::Message& proto, int flags);
140 absl::Status GetBinaryProto(absl::string_view filename,
141  google::protobuf::Message* proto, int flags);
142 template <typename T>
143 absl::StatusOr<T> GetBinaryProto(absl::string_view filename, int flags) {
144  T proto;
145  RETURN_IF_ERROR(GetBinaryProto(filename, &proto, flags));
146  return proto;
147 }
148 absl::Status SetBinaryProto(const absl::string_view& filename,
149  const google::protobuf::Message& proto, int flags);
150 absl::Status SetContents(const absl::string_view& filename,
151  const absl::string_view& contents, int flags);
152 absl::Status GetContents(const absl::string_view& filename, std::string* output,
153  int flags);
154 absl::Status WriteString(File* file, const absl::string_view& contents,
155  int flags);
156 
157 bool ReadFileToString(const absl::string_view& file_name, std::string* output);
158 bool WriteStringToFile(const std::string& data,
159  const absl::string_view& file_name);
160 bool ReadFileToProto(const absl::string_view& file_name,
161  google::protobuf::Message* proto);
162 void ReadFileToProtoOrDie(const absl::string_view& file_name,
163  google::protobuf::Message* proto);
164 bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
165  const absl::string_view& file_name);
166 void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
167  const absl::string_view& file_name);
168 bool WriteProtoToFile(const google::protobuf::Message& proto,
169  const absl::string_view& file_name);
170 void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
171  const absl::string_view& file_name);
172 
173 absl::Status Delete(const absl::string_view& path, int flags);
174 absl::Status Exists(const absl::string_view& path, int flags);
175 
176 } // namespace file
177 
178 #endif // OR_TOOLS_BASE_FILE_H_
#define RETURN_IF_ERROR(expr)
Definition: base/file.h:33
bool Open() const
Definition: base/file.cc:138
static File * OpenOrDie(const absl::string_view &name, const char *const flag)
Definition: base/file.h:51
static File * OpenOrDie(const char *const name, const char *const flag)
Definition: base/file.cc:82
static void Init()
Definition: base/file.cc:140
int64_t ReadToString(std::string *const line, uint64_t max_length)
Definition: base/file.cc:103
char * ReadLine(char *const output, uint64_t max_length)
Definition: base/file.cc:99
static bool Exists(const char *const name)
Definition: base/file.cc:39
void WriteOrDie(const void *const buff, size_t size)
Definition: base/file.cc:75
bool Flush()
Definition: base/file.cc:47
void ReadOrDie(void *const buff, size_t size)
Definition: base/file.cc:67
size_t Write(const void *const buff, size_t size)
Definition: base/file.cc:78
size_t Size()
Definition: base/file.cc:41
size_t Read(void *const buff, size_t size)
Definition: base/file.cc:71
bool WriteLine(const std::string &line)
Definition: base/file.cc:131
static bool Delete(const char *const name)
Definition: base/file.cc:37
absl::string_view filename() const
Definition: base/file.cc:136
size_t WriteString(const std::string &line)
Definition: base/file.cc:127
static File * Open(const absl::string_view &name, const char *const mode)
Definition: base/file.h:40
static bool Delete(const absl::string_view &name)
Definition: base/file.h:103
bool Close()
Definition: base/file.cc:49
CpModelProto proto
const std::string name
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Definition: base/file.cc:194
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: base/file.cc:164
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *const proto, const int flags)
Definition: base/file.cc:309
bool ReadFileToString(const absl::string_view &file_name, std::string *output)
Definition: base/file.cc:215
absl::Status Delete(const absl::string_view &path, int flags)
Definition: base/file.cc:332
File * OpenOrDie(const absl::string_view &filename, const absl::string_view &mode, int flags)
Definition: base/file.cc:155
int Options
Definition: base/file.h:121
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:272
bool WriteProtoToFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:277
absl::Status GetTextProto(const absl::string_view &filename, google::protobuf::Message *proto, int flags)
Definition: base/file.cc:289
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:284
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:265
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: base/file.cc:299
Options Defaults()
Definition: base/file.h:123
bool WriteStringToFile(const std::string &data, const absl::string_view &file_name)
Definition: base/file.cc:219
absl::Status SetBinaryProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: base/file.cc:322
bool ReadFileToProto(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: base/file.cc:231
absl::Status Exists(const absl::string_view &path, int flags)
Definition: base/file.cc:340
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
Definition: base/file.cc:143
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: base/file.cc:205
void ReadFileToProtoOrDie(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: base/file.cc:260
int line
Definition: parse_proto.cc:31