Google OR-Tools v9.14
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 <cstddef>
18#include <cstdint>
19#include <string>
20
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/string_view.h"
24#include "google/protobuf/message.h"
26
27// This file defines some IO interfaces for compatibility with Google
28// IO specifications.
29class File {
30 public:
31#ifndef SWIG // no overloading
32 // Opens file "name" with flags specified by "mode".
33 // Flags are defined by fopen(), that is "r", "r+", "w", "w+". "a", and "a+".
34 // The caller should call Close() to free the File after closing it.
35 static File* Open(absl::string_view file_name, absl::string_view mode);
36
37 // Opens file "name" with flags specified by "mode".
38 // If open failed, program will exit.
39 // The caller should call Close() to free the File after closing it.
40 static File* OpenOrDie(absl::string_view file_name, absl::string_view mode);
41#endif // SWIG
42
43 explicit File(absl::string_view name);
44 virtual ~File() = default;
45
46 // Reads "size" bytes to buf from file, buff should be pre-allocated.
47 virtual size_t Read(void* buf, size_t size) = 0;
48
49 // Writes "size" bytes of buf to file, buff should be pre-allocated.
50 virtual size_t Write(const void* buf, size_t size) = 0;
51
52 // Closes the file and delete the underlying FILE* descriptor.
53 virtual absl::Status Close(int flags) = 0;
54
55 // Flushes buffer.
56 virtual bool Flush() = 0;
57
58 // Returns file size.
59 virtual size_t Size() = 0;
60
61 // Returns whether the file is currently open.
62 virtual bool Open() const = 0;
63
64 // Reads the whole file to a string, with a maximum length of 'max_length'.
65 // Returns the number of bytes read.
66 int64_t ReadToString(std::string* line, uint64_t max_length);
67
68 // Writes a string to file.
69 size_t WriteString(absl::string_view str);
70
71 // Inits internal data structures.
72 static void Init();
73
74 // Returns the file name.
75 absl::string_view filename() const;
76
77 protected:
78 std::string name_;
79};
80
81namespace file {
82
83using Options = int;
84
85inline Options Defaults() { return 0xBABA; }
86
87// As of 2016-01, these methods can only be used with flags = file::Defaults().
88
89// ---- File API ----
90
91// The caller should free the File after closing it by passing *f to delete.
92absl::Status Open(absl::string_view file_name, absl::string_view mode, File** f,
93 Options options);
94// The caller should free the File after closing it by passing the returned
95// pointer to delete.
96File* OpenOrDie(absl::string_view file_name, absl::string_view mode,
97 Options options);
98
99absl::Status Delete(absl::string_view path, Options options);
100absl::Status Exists(absl::string_view path, Options options);
101
102// ---- Content API ----
103
104absl::StatusOr<std::string> GetContents(absl::string_view path,
105 Options options);
106
107absl::Status GetContents(absl::string_view file_name, std::string* output,
108 Options options);
109
110absl::Status SetContents(absl::string_view file_name,
111 absl::string_view contents, Options options);
112
113absl::Status WriteString(File* file, absl::string_view contents,
114 Options options);
115
116// ---- Protobuf API ----
117
118absl::Status GetTextProto(absl::string_view file_name,
119 google::protobuf::Message* proto, Options options);
120
121template <typename T>
122absl::StatusOr<T> GetTextProto(absl::string_view file_name, Options options) {
123 T proto;
124 RETURN_IF_ERROR(GetTextProto(file_name, &proto, options));
125 return proto;
126}
127
128absl::Status SetTextProto(absl::string_view file_name,
129 const google::protobuf::Message& proto,
130 Options options);
131
132absl::Status GetBinaryProto(absl::string_view file_name,
133 google::protobuf::Message* proto, Options options);
134template <typename T>
135
136absl::StatusOr<T> GetBinaryProto(absl::string_view file_name, Options options) {
137 T proto;
138 RETURN_IF_ERROR(GetBinaryProto(file_name, &proto, options));
139 return proto;
140}
141
142absl::Status SetBinaryProto(absl::string_view file_name,
143 const google::protobuf::Message& proto,
144 Options options);
145
146} // namespace file
147
148#endif // OR_TOOLS_BASE_FILE_H_
#define RETURN_IF_ERROR(expr)
Definition file.h:29
absl::string_view filename() const
Returns the file name.
Definition file.cc:322
virtual bool Flush()=0
Flushes buffer.
virtual absl::Status Close(int flags)=0
Closes the file and delete the underlying FILE* descriptor.
virtual ~File()=default
virtual size_t Write(const void *buf, size_t size)=0
Writes "size" bytes of buf to file, buff should be pre-allocated.
virtual size_t Read(void *buf, size_t size)=0
Reads "size" bytes to buf from file, buff should be pre-allocated.
static void Init()
Inits internal data structures.
Definition file.cc:324
static File * OpenOrDie(absl::string_view file_name, absl::string_view mode)
Definition file.cc:253
std::string name_
Definition file.h:78
size_t WriteString(absl::string_view str)
Writes a string to file.
Definition file.cc:318
File(absl::string_view name)
Definition file.cc:251
virtual bool Open() const =0
Returns whether the file is currently open.
int64_t ReadToString(std::string *line, uint64_t max_length)
Definition file.cc:294
virtual size_t Size()=0
Returns file size.
Definition file.cc:326
int Options
Definition file.h:83
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
-— Content API -—
Definition file.cc:348
absl::Status Exists(absl::string_view path, Options options)
Definition file.cc:499
absl::Status SetBinaryProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
Definition file.cc:475
absl::Status SetTextProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
Definition file.cc:447
absl::Status GetTextProto(absl::string_view file_name, google::protobuf::Message *proto, Options options)
-— Protobuf API -—
Definition file.cc:408
File * OpenOrDie(absl::string_view file_name, absl::string_view mode, Options options)
Definition file.cc:339
absl::Status WriteString(File *file, absl::string_view contents, Options options)
Definition file.cc:377
absl::Status GetBinaryProto(const absl::string_view file_name, google::protobuf::Message *proto, Options options)
Definition file.cc:462
Options Defaults()
Definition file.h:85
absl::Status Delete(absl::string_view path, Options options)
Definition file.cc:490
absl::Status Open(absl::string_view file_name, 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:327
absl::Status SetContents(absl::string_view file_name, absl::string_view contents, Options options)
Definition file.cc:388