OR-Tools  8.2
base/file.h
Go to the documentation of this file.
1// Copyright 2010-2018 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 <cstdio>
18#include <cstdlib>
19#include <string>
20
21#include "absl/status/status.h"
22#include "absl/strings/string_view.h"
23#include "google/protobuf/descriptor.h"
24#include "google/protobuf/io/tokenizer.h"
25#include "google/protobuf/message.h"
26#include "google/protobuf/text_format.h"
29
30// This file defines some IO interfaces for compatibility with Google
31// IO specifications.
32class File {
33 public:
34 // Opens file "name" with flags specified by "flag".
35 // Flags are defined by fopen(), that is "r", "r+", "w", "w+". "a", and "a+".
36 static File* Open(const char* const name, const char* const flag);
37
38#ifndef SWIG // no overloading
39 inline static File* Open(const absl::string_view& name,
40 const char* const mode) {
41 return Open(name.data(), mode);
42 }
43#endif // SWIG
44
45 // Opens file "name" with flags specified by "flag".
46 // If open failed, program will exit.
47 static File* OpenOrDie(const char* const name, const char* const flag);
48
49#ifndef SWIG // no overloading
50 inline static File* OpenOrDie(const absl::string_view& name,
51 const char* const flag) {
52 return OpenOrDie(name.data(), flag);
53 }
54#endif // SWIG
55
56 // Reads "size" bytes to buff from file, buff should be pre-allocated.
57 size_t Read(void* const buff, size_t size);
58
59 // Reads "size" bytes to buff from file, buff should be pre-allocated.
60 // If read failed, program will exit.
61 void ReadOrDie(void* const buff, size_t size);
62
63 // Reads a line from file to a string.
64 // Each line must be no more than max_length bytes.
65 char* ReadLine(char* const output, uint64 max_length);
66
67 // Reads the whole file to a string, with a maximum length of 'max_length'.
68 // Returns the number of bytes read.
69 int64 ReadToString(std::string* const line, uint64 max_length);
70
71 // Writes "size" bytes of buff to file, buff should be pre-allocated.
72 size_t Write(const void* const buff, size_t size);
73
74 // Writes "size" bytes of buff to file, buff should be pre-allocated.
75 // If write failed, program will exit.
76 void WriteOrDie(const void* const buff, size_t size);
77
78 // Writes a string to file.
79 size_t WriteString(const std::string& line);
80
81 // Writes a string to file and append a "\n".
82 bool WriteLine(const std::string& line);
83
84 // Closes the file.
85 bool Close();
86 absl::Status Close(int flags);
87
88 // Flushes buffer.
89 bool Flush();
90
91 // Returns file size.
92 size_t Size();
93
94 // Inits internal data structures.
95 static void Init();
96
97 // Returns the file name.
98 absl::string_view filename() const;
99
100 // Deletes a file.
101 static bool Delete(const char* const name);
102 static bool Delete(const absl::string_view& name) {
103 return Delete(name.data());
104 }
105
106 // Tests if a file exists.
107 static bool Exists(const char* const name);
108
109 bool Open() const;
110
111 private:
112 File(FILE* const descriptor, const absl::string_view& name);
113
114 FILE* f_;
115 const absl::string_view name_;
116};
117
118namespace file {
119inline int Defaults() { return 0xBABA; }
120
121// As of 2016-01, these methods can only be used with flags = file::Defaults().
122absl::Status Open(const absl::string_view& filename,
123 const absl::string_view& mode, File** f, int flags);
124File* OpenOrDie(const absl::string_view& filename,
125 const absl::string_view& mode, int flags);
126absl::Status GetTextProto(const absl::string_view& filename,
127 google::protobuf::Message* proto, int flags);
128absl::Status SetTextProto(const absl::string_view& filename,
129 const google::protobuf::Message& proto, int flags);
130absl::Status SetBinaryProto(const absl::string_view& filename,
131 const google::protobuf::Message& proto, int flags);
132absl::Status SetContents(const absl::string_view& filename,
133 const absl::string_view& contents, int flags);
134absl::Status GetContents(const absl::string_view& filename, std::string* output,
135 int flags);
136absl::Status WriteString(File* file, const absl::string_view& contents,
137 int flags);
138
139bool ReadFileToString(const absl::string_view& file_name, std::string* output);
140bool WriteStringToFile(const std::string& data,
141 const absl::string_view& file_name);
142bool ReadFileToProto(const absl::string_view& file_name,
143 google::protobuf::Message* proto);
144void ReadFileToProtoOrDie(const absl::string_view& file_name,
145 google::protobuf::Message* proto);
146bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
147 const absl::string_view& file_name);
148void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
149 const absl::string_view& file_name);
150bool WriteProtoToFile(const google::protobuf::Message& proto,
151 const absl::string_view& file_name);
152void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
153 const absl::string_view& file_name);
154
155absl::Status Delete(const absl::string_view& path, int flags);
156absl::Status Exists(const absl::string_view& path, int flags);
157
158} // namespace file
159
160#endif // OR_TOOLS_BASE_FILE_H_
Definition: base/file.h:32
static File * OpenOrDie(const absl::string_view &name, const char *const flag)
Definition: base/file.h:50
bool Open() const
Definition: file.cc:137
static File * OpenOrDie(const char *const name, const char *const flag)
Definition: file.cc:81
static void Init()
Definition: file.cc:139
static bool Exists(const char *const name)
Definition: file.cc:38
void WriteOrDie(const void *const buff, size_t size)
Definition: file.cc:74
bool Flush()
Definition: file.cc:46
void ReadOrDie(void *const buff, size_t size)
Definition: file.cc:66
size_t Write(const void *const buff, size_t size)
Definition: file.cc:77
char * ReadLine(char *const output, uint64 max_length)
Definition: file.cc:98
size_t Size()
Definition: file.cc:40
size_t Read(void *const buff, size_t size)
Definition: file.cc:70
bool WriteLine(const std::string &line)
Definition: file.cc:130
static bool Delete(const char *const name)
Definition: file.cc:36
absl::string_view filename() const
Definition: file.cc:135
int64 ReadToString(std::string *const line, uint64 max_length)
Definition: file.cc:102
size_t WriteString(const std::string &line)
Definition: file.cc:126
static File * Open(const absl::string_view &name, const char *const mode)
Definition: base/file.h:39
static bool Delete(const absl::string_view &name)
Definition: base/file.h:102
bool Close()
Definition: file.cc:48
CpModelProto proto
const std::string name
int64_t int64
uint64_t uint64
Definition: file.cc:141
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Definition: file.cc:184
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: file.cc:163
bool ReadFileToString(const absl::string_view &file_name, std::string *output)
Definition: file.cc:201
absl::Status Delete(const absl::string_view &path, int flags)
Definition: file.cc:305
File * OpenOrDie(const absl::string_view &filename, const absl::string_view &mode, int flags)
Definition: file.cc:154
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:258
bool WriteProtoToFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:263
absl::Status GetTextProto(const absl::string_view &filename, google::protobuf::Message *proto, int flags)
Definition: file.cc:275
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:270
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
Definition: file.cc:251
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: file.cc:285
bool WriteStringToFile(const std::string &data, const absl::string_view &file_name)
Definition: file.cc:205
absl::Status SetBinaryProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: file.cc:295
bool ReadFileToProto(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: file.cc:217
absl::Status Exists(const absl::string_view &path, int flags)
Definition: file.cc:313
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
Definition: file.cc:142
int Defaults()
Definition: base/file.h:119
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: file.cc:196
void ReadFileToProtoOrDie(const absl::string_view &file_name, google::protobuf::Message *proto)
Definition: file.cc:246