Google OR-Tools v9.9
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 int flags);
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, int flags);
119absl::Status GetTextProto(absl::string_view filename,
120 google::protobuf::Message* proto, int flags);
121template <typename T>
122absl::StatusOr<T> GetTextProto(absl::string_view filename, int flags) {
123 T proto;
124 RETURN_IF_ERROR(GetTextProto(filename, &proto, flags));
125 return proto;
126}
127absl::Status SetTextProto(absl::string_view filename,
128 const google::protobuf::Message& proto, int flags);
129absl::Status GetBinaryProto(absl::string_view filename,
130 google::protobuf::Message* proto, int flags);
131template <typename T>
132absl::StatusOr<T> GetBinaryProto(absl::string_view filename, int flags) {
133 T proto;
134 RETURN_IF_ERROR(GetBinaryProto(filename, &proto, flags));
135 return proto;
136}
137absl::Status SetBinaryProto(absl::string_view filename,
138 const google::protobuf::Message& proto, int flags);
139absl::Status SetContents(absl::string_view filename, absl::string_view contents,
140 int flags);
141absl::Status GetContents(absl::string_view filename, std::string* output,
142 int flags);
143absl::Status WriteString(File* file, absl::string_view contents, int flags);
144
145bool ReadFileToString(absl::string_view file_name, std::string* output);
146bool WriteStringToFile(absl::string_view data, absl::string_view file_name);
147bool ReadFileToProto(absl::string_view file_name,
148 google::protobuf::Message* proto);
149void ReadFileToProtoOrDie(absl::string_view file_name,
150 google::protobuf::Message* proto);
151bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
152 absl::string_view file_name);
153void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
154 absl::string_view file_name);
155bool WriteProtoToFile(const google::protobuf::Message& proto,
156 absl::string_view file_name);
157void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
158 absl::string_view file_name);
159
160absl::Status Delete(absl::string_view path, int flags);
161absl::Status Exists(absl::string_view path, int flags);
162
163} // namespace file
164
165#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:149
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:144
size_t Write(const void *buff, size_t size)
Writes "size" bytes of buff to file, buff should be pre-allocated.
Definition file.cc:92
size_t Read(void *buff, size_t size)
Reads "size" bytes to buff from file, buff should be pre-allocated.
Definition file.cc:87
static File * OpenOrDie(absl::string_view filename, absl::string_view mode)
Definition file.cc:96
void WriteOrDie(const void *buff, size_t size)
Definition file.cc:89
static void Init()
Inits internal data structures.
Definition file.cc:153
size_t Size()
Returns file size.
Definition file.cc:57
char * ReadLine(char *output, uint64_t max_length)
Definition file.cc:112
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:140
bool Flush()
Flushes buffer.
Definition file.cc:63
bool Close()
Closes the file.
Definition file.cc:65
int64_t ReadToString(std::string *line, uint64_t max_length)
Definition file.cc:116
void ReadOrDie(void *buff, size_t size)
Definition file.cc:83
bool Open() const
Definition file.cc:151
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
Definition file.cc:155
File * OpenOrDie(absl::string_view filename, absl::string_view mode, int flags)
Definition file.cc:168
int Options
Definition file.h:107
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:281
absl::Status Delete(absl::string_view path, int flags)
Definition file.cc:347
bool ReadFileToString(absl::string_view file_name, std::string *output)
Definition file.cc:231
absl::Status SetTextProto(absl::string_view filename, const google::protobuf::Message &proto, int flags)
Definition file.cc:315
bool ReadFileToProto(absl::string_view file_name, google::protobuf::Message *proto)
Definition file.cc:247
absl::Status Exists(absl::string_view path, int flags)
Definition file.cc:356
absl::Status WriteString(File *file, absl::string_view contents, int flags)
Definition file.cc:210
bool WriteStringToFile(absl::string_view data, absl::string_view file_name)
Definition file.cc:235
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:288
absl::Status GetContents(absl::string_view filename, std::string *output, int flags)
Definition file.cc:176
bool WriteProtoToFile(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:293
absl::Status GetTextProto(absl::string_view filename, google::protobuf::Message *proto, int flags)
Definition file.cc:305
void ReadFileToProtoOrDie(absl::string_view file_name, google::protobuf::Message *proto)
Definition file.cc:276
absl::Status Open(absl::string_view filename, absl::string_view mode, File **f, int flags)
As of 2016-01, these methods can only be used with flags = file::Defaults().
Definition file.cc:156
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, absl::string_view file_name)
Definition file.cc:300
Options Defaults()
Definition file.h:109
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *proto, const int flags)
Definition file.cc:325
absl::Status SetContents(absl::string_view filename, absl::string_view contents, int flags)
Definition file.cc:220
absl::Status SetBinaryProto(absl::string_view filename, const google::protobuf::Message &proto, int flags)
Definition file.cc:337
int line