Google OR-Tools v9.12
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-2025 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 call Close() to free the File after closing it.
36 static File* Open(absl::string_view filename, absl::string_view mode);
37
38 // Opens file "name" with flags specified by "mode".
39 // If open failed, program will exit.
40 // The caller should call Close() to free the File after closing it.
41 static File* OpenOrDie(absl::string_view filename, absl::string_view mode);
42#endif // SWIG
43
44 // Reads "size" bytes to buff from file, buff should be pre-allocated.
45 size_t Read(void* buff, size_t size);
46
47 // Reads "size" bytes to buff from file, buff should be pre-allocated.
48 // If read failed, program will exit.
49 void ReadOrDie(void* buff, size_t size);
50
51 // Reads a line from file to a string.
52 // Each line must be no more than max_length bytes.
53 char* ReadLine(char* output, uint64_t max_length);
54
55 // Reads the whole file to a string, with a maximum length of 'max_length'.
56 // Returns the number of bytes read.
57 int64_t ReadToString(std::string* line, uint64_t max_length);
58
59 // Writes "size" bytes of buff to file, buff should be pre-allocated.
60 size_t Write(const void* buff, size_t size);
61
62 // Writes "size" bytes of buff to file, buff should be pre-allocated.
63 // If write failed, program will exit.
64 void WriteOrDie(const void* buff, size_t size);
65
66 // Writes a string to file.
67 size_t WriteString(absl::string_view str);
68
69 // Writes a string to file and append a "\n".
70 bool WriteLine(absl::string_view line);
71
72 // Closes the file and delete the underlying FILE* descriptor.
73 bool Close();
74 absl::Status Close(int flags);
75
76 // Flushes buffer.
77 bool Flush();
78
79 // Returns file size.
80 size_t Size();
81
82 // Inits internal data structures.
83 static void Init();
84
85 // Returns the file name.
86 absl::string_view filename() const;
87
88 // Deletes a file.
89 static bool Delete(absl::string_view filename);
90
91 // Tests if a file exists.
92 static bool Exists(absl::string_view filename);
93
94 bool Open() const;
95
96 private:
97 File(FILE* descriptor, absl::string_view name);
98
99 FILE* f_;
100 std::string name_;
101};
102
103namespace file {
104
105using Options = int;
106
107inline Options Defaults() { return 0xBABA; }
108
109// As of 2016-01, these methods can only be used with flags = file::Defaults().
110
111// The caller should free the File after closing it by passing *f to delete.
112absl::Status Open(absl::string_view filename, absl::string_view mode, File** f,
113 Options options);
114// The caller should free the File after closing it by passing the returned
115// pointer to delete.
116File* OpenOrDie(absl::string_view filename, absl::string_view mode,
117 Options options);
118absl::Status GetTextProto(absl::string_view filename,
119 google::protobuf::Message* proto, Options options);
120template <typename T>
121absl::StatusOr<T> GetTextProto(absl::string_view filename, Options options) {
122 T proto;
123 RETURN_IF_ERROR(GetTextProto(filename, &proto, options));
124 return proto;
125}
126absl::Status SetTextProto(absl::string_view filename,
127 const google::protobuf::Message& proto,
128 Options options);
129absl::Status GetBinaryProto(absl::string_view filename,
130 google::protobuf::Message* proto, Options options);
131template <typename T>
132absl::StatusOr<T> GetBinaryProto(absl::string_view filename, Options options) {
133 T proto;
134 RETURN_IF_ERROR(GetBinaryProto(filename, &proto, options));
135 return proto;
136}
137absl::Status SetBinaryProto(absl::string_view filename,
138 const google::protobuf::Message& proto,
139 Options options);
140absl::Status SetContents(absl::string_view filename, absl::string_view contents,
141 Options options);
142absl::StatusOr<std::string> GetContents(absl::string_view path,
143 Options options);
144absl::Status GetContents(absl::string_view filename, std::string* output,
145 Options options);
146absl::Status WriteString(File* file, absl::string_view contents,
147 Options options);
148
149bool ReadFileToString(absl::string_view file_name, std::string* output);
150bool WriteStringToFile(absl::string_view data, absl::string_view file_name);
151bool ReadFileToProto(absl::string_view file_name,
152 google::protobuf::Message* proto);
153void ReadFileToProtoOrDie(absl::string_view file_name,
154 google::protobuf::Message* proto);
155bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
156 absl::string_view file_name);
157void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
158 absl::string_view file_name);
159bool WriteProtoToFile(const google::protobuf::Message& proto,
160 absl::string_view file_name);
161void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
162 absl::string_view file_name);
163
164absl::Status Delete(absl::string_view path, Options options);
165absl::Status Exists(absl::string_view path, Options options);
166
167} // namespace file
168
169#endif // OR_TOOLS_BASE_FILE_H_
#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 and delete the underlying FILE* descriptor.
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
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:105
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:107
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