OR-Tools  9.3
base/file.h
Go to the documentation of this file.
1// Copyright 2010-2021 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"
30
31// This file defines some IO interfaces for compatibility with Google
32// IO specifications.
33class 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
119namespace file {
120inline int Defaults() { return 0xBABA; }
121
122// As of 2016-01, these methods can only be used with flags = file::Defaults().
123absl::Status Open(const absl::string_view& filename,
124 const absl::string_view& mode, File** f, int flags);
125File* OpenOrDie(const absl::string_view& filename,
126 const absl::string_view& mode, int flags);
127absl::Status GetTextProto(const absl::string_view& filename,
128 google::protobuf::Message* proto, int flags);
129template <typename T>
130absl::StatusOr<T> GetTextProto(absl::string_view filename, int flags) {
131 T proto;
132 RETURN_IF_ERROR(GetTextProto(filename, &proto, flags));
133 return proto;
134}
135absl::Status SetTextProto(const absl::string_view& filename,
136 const google::protobuf::Message& proto, int flags);
137absl::Status GetBinaryProto(absl::string_view filename,
138 google::protobuf::Message* proto, int flags);
139template <typename T>
140absl::StatusOr<T> GetBinaryProto(absl::string_view filename, int flags) {
141 T proto;
142 RETURN_IF_ERROR(GetBinaryProto(filename, &proto, flags));
143 return proto;
144}
145absl::Status SetBinaryProto(const absl::string_view& filename,
146 const google::protobuf::Message& proto, int flags);
147absl::Status SetContents(const absl::string_view& filename,
148 const absl::string_view& contents, int flags);
149absl::Status GetContents(const absl::string_view& filename, std::string* output,
150 int flags);
151absl::Status WriteString(File* file, const absl::string_view& contents,
152 int flags);
153
154bool ReadFileToString(const absl::string_view& file_name, std::string* output);
155bool WriteStringToFile(const std::string& data,
156 const absl::string_view& file_name);
157bool ReadFileToProto(const absl::string_view& file_name,
158 google::protobuf::Message* proto);
159void ReadFileToProtoOrDie(const absl::string_view& file_name,
160 google::protobuf::Message* proto);
161bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
162 const absl::string_view& file_name);
163void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
164 const absl::string_view& file_name);
165bool WriteProtoToFile(const google::protobuf::Message& proto,
166 const absl::string_view& file_name);
167void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
168 const absl::string_view& file_name);
169
170absl::Status Delete(const absl::string_view& path, int flags);
171absl::Status Exists(const absl::string_view& path, int flags);
172
173} // namespace file
174
175#endif // OR_TOOLS_BASE_FILE_H_
#define RETURN_IF_ERROR(expr)
Definition: base/file.h:33
static File * OpenOrDie(const absl::string_view &name, const char *const flag)
Definition: base/file.h:51
bool Open() const
Definition: base/file.cc:137
static File * OpenOrDie(const char *const name, const char *const flag)
Definition: base/file.cc:81
static void Init()
Definition: base/file.cc:139
int64_t ReadToString(std::string *const line, uint64_t max_length)
Definition: base/file.cc:102
char * ReadLine(char *const output, uint64_t max_length)
Definition: base/file.cc:98
static bool Exists(const char *const name)
Definition: base/file.cc:38
void WriteOrDie(const void *const buff, size_t size)
Definition: base/file.cc:74
bool Flush()
Definition: base/file.cc:46
void ReadOrDie(void *const buff, size_t size)
Definition: base/file.cc:66
size_t Write(const void *const buff, size_t size)
Definition: base/file.cc:77
size_t Size()
Definition: base/file.cc:40
size_t Read(void *const buff, size_t size)
Definition: base/file.cc:70
bool WriteLine(const std::string &line)
Definition: base/file.cc:130
static bool Delete(const char *const name)
Definition: base/file.cc:36
absl::string_view filename() const
Definition: base/file.cc:135
size_t WriteString(const std::string &line)
Definition: base/file.cc:126
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:48
CpModelProto proto
const std::string name
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Definition: base/file.cc:184
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: base/file.cc:163
int Defaults()
Definition: base/file.h:120
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *const proto, const int flags)
Definition: base/file.cc:295
bool ReadFileToString(const absl::string_view &file_name, std::string *output)
Definition: base/file.cc:201
absl::Status Delete(const absl::string_view &path, int flags)
Definition: base/file.cc:318
File * OpenOrDie(const absl::string_view &filename, const absl::string_view &mode, int flags)
Definition: base/file.cc:154
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:258
bool WriteProtoToFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:263
absl::Status GetTextProto(const absl::string_view &filename, google::protobuf::Message *proto, int flags)
Definition: base/file.cc:275
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:270
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: base/file.cc:251
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: base/file.cc:285
bool WriteStringToFile(const std::string &data, const absl::string_view &file_name)
Definition: base/file.cc:205
absl::Status SetBinaryProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: base/file.cc:308
bool ReadFileToProto(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: base/file.cc:217
absl::Status Exists(const absl::string_view &path, int flags)
Definition: base/file.cc:326
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
Definition: base/file.cc:142
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: base/file.cc:196
void ReadFileToProtoOrDie(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: base/file.cc:246