Google OR-Tools v9.15
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// emulates g3/file/base/file.h
15#ifndef ORTOOLS_BASE_FILE_H_
16#define ORTOOLS_BASE_FILE_H_
17
18#include <cstddef>
19#include <cstdint>
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 file_name, 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 file_name, absl::string_view mode);
42#endif // SWIG
43
44 explicit File(absl::string_view name);
45 virtual ~File() = default;
46
47 // Reads "size" bytes to buf from file, buff should be pre-allocated.
48 virtual size_t Read(void* buf, size_t size) = 0;
49
50 // Writes "size" bytes of buf to file, buff should be pre-allocated.
51 virtual size_t Write(const void* buf, size_t size) = 0;
52
53 // Closes the file and delete the underlying FILE* descriptor.
54 virtual absl::Status Close(int flags) = 0;
55
56 // Flushes buffer.
57 virtual bool Flush() = 0;
58
59 // Returns file size.
60 virtual size_t Size() = 0;
61
62 // Returns whether the file is currently open.
63 virtual bool Open() const = 0;
64
65 // Reads the whole file to a string, with a maximum length of 'max_length'.
66 // Returns the number of bytes read.
67 int64_t ReadToString(std::string* line, uint64_t max_length);
68
69 // Writes a string to file.
70 size_t WriteString(absl::string_view str);
71
72 // Inits internal data structures.
73 static void Init();
74
75 // Returns the file name.
76 absl::string_view filename() const;
77
78 protected:
79 std::string name_;
80};
81
82namespace file {
83
84using Options = int;
85
86inline Options Defaults() { return 0xBABA; }
87
88// As of 2016-01, these methods can only be used with flags = file::Defaults().
89
90// ---- File API ----
91
92// The caller should free the File after closing it by passing *f to delete.
93absl::Status Open(absl::string_view file_name, absl::string_view mode, File** f,
94 Options options);
95// The caller should free the File after closing it by passing the returned
96// pointer to delete.
97File* OpenOrDie(absl::string_view file_name, absl::string_view mode,
98 Options options);
99
100absl::Status Delete(absl::string_view path, Options options);
101absl::Status Exists(absl::string_view path, Options options);
102
103// ---- Content API ----
104
105absl::StatusOr<std::string> GetContents(absl::string_view path,
106 Options options);
107
108absl::Status GetContents(absl::string_view file_name, std::string* output,
109 Options options);
110
111absl::Status SetContents(absl::string_view file_name,
112 absl::string_view contents, Options options);
113
114absl::Status WriteString(File* file, absl::string_view contents,
115 Options options);
116
117// ---- Protobuf API ----
118
119absl::Status GetTextProto(absl::string_view file_name,
120 google::protobuf::Message* proto, Options options);
121
122template <typename T>
123absl::StatusOr<T> GetTextProto(absl::string_view file_name, Options options) {
124 T proto;
125 RETURN_IF_ERROR(GetTextProto(file_name, &proto, options));
126 return proto;
127}
128
129absl::Status SetTextProto(absl::string_view file_name,
130 const google::protobuf::Message& proto,
131 Options options);
132
133absl::Status GetBinaryProto(absl::string_view file_name,
134 google::protobuf::Message* proto, Options options);
135template <typename T>
136
137absl::StatusOr<T> GetBinaryProto(absl::string_view file_name, Options options) {
138 T proto;
139 RETURN_IF_ERROR(GetBinaryProto(file_name, &proto, options));
140 return proto;
141}
142
143absl::Status SetBinaryProto(absl::string_view file_name,
144 const google::protobuf::Message& proto,
145 Options options);
146
147} // namespace file
148
149#endif // ORTOOLS_BASE_FILE_H_
#define RETURN_IF_ERROR(expr)
Definition file.h:30
absl::string_view filename() const
Definition file.cc:323
virtual bool Flush()=0
virtual absl::Status Close(int flags)=0
virtual ~File()=default
virtual size_t Write(const void *buf, size_t size)=0
virtual size_t Read(void *buf, size_t size)=0
static void Init()
Definition file.cc:325
static File * OpenOrDie(absl::string_view file_name, absl::string_view mode)
Definition file.cc:254
std::string name_
Definition file.h:79
size_t WriteString(absl::string_view str)
Definition file.cc:319
File(absl::string_view name)
Definition file.cc:252
virtual bool Open() const =0
int64_t ReadToString(std::string *line, uint64_t max_length)
Definition file.cc:295
virtual size_t Size()=0
Definition file.cc:327
int Options
Definition file.h:84
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
Definition file.cc:349
absl::Status Exists(absl::string_view path, Options options)
Definition file.cc:500
absl::Status SetBinaryProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
Definition file.cc:476
absl::Status SetTextProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
Definition file.cc:448
absl::Status GetTextProto(absl::string_view file_name, google::protobuf::Message *proto, Options options)
Definition file.cc:409
File * OpenOrDie(absl::string_view file_name, absl::string_view mode, Options options)
Definition file.cc:340
absl::Status WriteString(File *file, absl::string_view contents, Options options)
Definition file.cc:378
absl::Status GetBinaryProto(const absl::string_view file_name, google::protobuf::Message *proto, Options options)
Definition file.cc:463
Options Defaults()
Definition file.h:86
absl::Status Delete(absl::string_view path, Options options)
Definition file.cc:491
absl::Status Open(absl::string_view file_name, absl::string_view mode, File **f, Options options)
Definition file.cc:328
absl::Status SetContents(absl::string_view file_name, absl::string_view contents, Options options)
Definition file.cc:389