Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1// Copyright 2010-2024 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 <cstdint>
18#include <cstdio>
19#include <cstdlib>
20#include <string>
21
22#include "absl/status/status.h"
23#include "absl/status/statusor.h"
24#include "absl/strings/string_view.h"
25#include "google/protobuf/message.h"
27
28// This file defines some IO interfaces for compatibility with Google
29// IO specifications.
30class File {
31 public:
32#ifndef SWIG // no overloading
33 // Opens file "name" with flags specified by "mode".
34 // Flags are defined by fopen(), that is "r", "r+", "w", "w+". "a", and "a+".
35 // The caller should free the File after closing it by passing the returned
36 // pointer to delete.
37 static File* Open(absl::string_view filename, absl::string_view mode);
38
39 // Opens file "name" with flags specified by "mode".
40 // If open failed, program will exit.
41 // The caller should free the File after closing it by passing the returned
42 // pointer to delete.
43 static File* OpenOrDie(absl::string_view filename, absl::string_view mode);
44#endif // SWIG
45
46 // Reads "size" bytes to buff from file, buff should be pre-allocated.
47 size_t Read(void* buff, size_t size);
48
49 // Reads "size" bytes to buff from file, buff should be pre-allocated.
50 // If read failed, program will exit.
51 void ReadOrDie(void* buff, size_t size);
52
53 // Reads a line from file to a string.
54 // Each line must be no more than max_length bytes.
55 char* ReadLine(char* output, uint64_t max_length);
56
57 // Reads the whole file to a string, with a maximum length of 'max_length'.
58 // Returns the number of bytes read.
59 int64_t ReadToString(std::string* line, uint64_t max_length);
60
61 // Writes "size" bytes of buff to file, buff should be pre-allocated.
62 size_t Write(const void* buff, size_t size);
63
64 // Writes "size" bytes of buff to file, buff should be pre-allocated.
65 // If write failed, program will exit.
66 void WriteOrDie(const void* buff, size_t size);
67
68 // Writes a string to file.
69 size_t WriteString(absl::string_view str);
70
71 // Writes a string to file and append a "\n".
72 bool WriteLine(absl::string_view line);
73
74 // Closes the file.
75 bool Close();
76 absl::Status Close(int flags);
77
78 // Flushes buffer.
79 bool Flush();
80
81 // Returns file size.
82 size_t Size();
83
84 // Inits internal data structures.
85 static void Init();
86
87 // Returns the file name.
88 absl::string_view filename() const;
89
90 // Deletes a file.
91 static bool Delete(absl::string_view filename);
92
93 // Tests if a file exists.
94 static bool Exists(absl::string_view filename);
95
96 bool Open() const;
97
98 private:
99 File(FILE* descriptor, absl::string_view name);
100
101 FILE* f_;
102 std::string name_;
103};
104
105namespace file {
106
107using Options = int;
108
109inline Options Defaults() { return 0xBABA; }
110
111// As of 2016-01, these methods can only be used with flags = file::Defaults().
112
113// The caller should free the File after closing it by passing *f to delete.
114absl::Status Open(absl::string_view filename, absl::string_view mode, File** f,
115 Options options);
116// The caller should free the File after closing it by passing the returned
117// pointer to delete.
118File* OpenOrDie(absl::string_view filename, absl::string_view mode,
119 Options options);
120absl::Status GetTextProto(absl::string_view filename,
121 google::protobuf::Message* proto, Options options);
122template <typename T>
123absl::StatusOr<T> GetTextProto(absl::string_view filename, Options options) {
124 T proto;
125 RETURN_IF_ERROR(GetTextProto(filename, &proto, options));
126 return proto;
127}
128absl::Status SetTextProto(absl::string_view filename,
129 const google::protobuf::Message& proto,
130 Options options);
131absl::Status GetBinaryProto(absl::string_view filename,
132 google::protobuf::Message* proto, Options options);
133template <typename T>
134absl::StatusOr<T> GetBinaryProto(absl::string_view filename, Options options) {
135 T proto;
136 RETURN_IF_ERROR(GetBinaryProto(filename, &proto, options));
137 return proto;
138}
139absl::Status SetBinaryProto(absl::string_view filename,
140 const google::protobuf::Message& proto,
141 Options options);
142absl::Status SetContents(absl::string_view filename, absl::string_view contents,
143 Options options);
144absl::StatusOr<std::string> GetContents(absl::string_view path,
145 Options options);
146absl::Status GetContents(absl::string_view filename, std::string* output,
147 Options options);
148absl::Status WriteString(File* file, absl::string_view contents,
149 Options options);
150
151bool ReadFileToString(absl::string_view file_name, std::string* output);
152bool WriteStringToFile(absl::string_view data, absl::string_view file_name);
153bool ReadFileToProto(absl::string_view file_name,
154 google::protobuf::Message* proto);
155void ReadFileToProtoOrDie(absl::string_view file_name,
156 google::protobuf::Message* proto);
157bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
158 absl::string_view file_name);
159void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
160 absl::string_view file_name);
161bool WriteProtoToFile(const google::protobuf::Message& proto,
162 absl::string_view file_name);
163void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
164 absl::string_view file_name);
165
166absl::Status Delete(absl::string_view path, Options options);
167absl::Status Exists(absl::string_view path, Options options);
168
169} // namespace file
170
171#endif // OR_TOOLS_BASE_FILE_H_
IntegerValue size
#define RETURN_IF_ERROR(expr)
Definition file.h:30
absl::string_view filename() const
Returns the file name.
Definition file.cc:163
static bool Delete(absl::string_view filename)
Deletes a file.
Definition file.cc:47
bool WriteLine(absl::string_view line)
Writes a string to file and append a "\n".
Definition file.cc:158
size_t Write(const void *buff, size_t size)
Writes "size" bytes of buff to file, buff should be pre-allocated.
Definition file.cc:106
size_t Read(void *buff, size_t size)
Reads "size" bytes to buff from file, buff should be pre-allocated.
Definition file.cc:101
static File * OpenOrDie(absl::string_view filename, absl::string_view mode)
Definition file.cc:110
void WriteOrDie(const void *buff, size_t size)
Definition file.cc:103
static void Init()
Inits internal data structures.
Definition file.cc:167
size_t Size()
Returns file size.
Definition file.cc:57
char * ReadLine(char *output, uint64_t max_length)
Definition file.cc:126
static bool Exists(absl::string_view filename)
Tests if a file exists.
Definition file.cc:52
size_t WriteString(absl::string_view str)
Writes a string to file.
Definition file.cc:154
bool Flush()
Flushes buffer.
Definition file.cc:63
bool Close()
Closes the file.
Definition file.cc:66
int64_t ReadToString(std::string *line, uint64_t max_length)
Definition file.cc:130
void ReadOrDie(void *buff, size_t size)
Definition file.cc:97
bool Open() const
Definition file.cc:165
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
Definition file.cc:169
absl::Status SetTextProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
Definition file.cc:337
int Options
Definition file.h:107
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
Definition file.cc:191
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:303
absl::Status Exists(absl::string_view path, Options options)
Definition file.cc:380
absl::Status Open(absl::string_view filename, absl::string_view mode, File **f, Options options)
As of 2016-01, these methods can only be used with flags = file::Defaults().
Definition file.cc:170
absl::Status SetBinaryProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
Definition file.cc:360
bool ReadFileToString(absl::string_view file_name, std::string *output)
Definition file.cc:252
bool ReadFileToProto(absl::string_view file_name, google::protobuf::Message *proto)
Definition file.cc:269
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
Definition file.cc:242
bool WriteStringToFile(absl::string_view data, absl::string_view file_name)
Definition file.cc:256
absl::Status WriteString(File *file, absl::string_view contents, Options options)
Definition file.cc:231
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:310
bool WriteProtoToFile(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:315
void ReadFileToProtoOrDie(absl::string_view file_name, google::protobuf::Message *proto)
Definition file.cc:298
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:322
Options Defaults()
Definition file.h:109
absl::Status Delete(absl::string_view path, Options options)
Definition file.cc:371
absl::Status GetTextProto(absl::string_view filename, google::protobuf::Message *proto, Options options)
Definition file.cc:327
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *proto, Options options)
Definition file.cc:348
File * OpenOrDie(absl::string_view filename, absl::string_view mode, Options options)
Definition file.cc:182
int line