This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_util.cc
84 lines (75 loc) · 3.02 KB
/
file_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/util/file_util.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/file.h"
#include "ortools/base/logging.h"
#include "ortools/base/status_macros.h"
namespace operations_research {
using ::google::protobuf::TextFormat;
util::StatusOr<std::string> ReadFileToString(absl::string_view filename) {
std::string contents;
RETURN_IF_ERROR(file::GetContents(filename, &contents, file::Defaults()));
// Note that gzipped files are currently not supported.
return contents;
}
bool ReadFileToProto(absl::string_view filename,
google::protobuf::Message* proto) {
std::string data;
CHECK_OK(file::GetContents(filename, &data, file::Defaults()));
// Note that gzipped files are currently not supported.
// Try binary format first, then text format, then JSON, then proto3 JSON,
// then give up.
if (proto->ParseFromString(data)) return true;
if (google::protobuf::TextFormat::ParseFromString(data, proto)) return true;
LOG(WARNING) << "Could not parse protocol buffer";
return false;
}
bool WriteProtoToFile(absl::string_view filename,
const google::protobuf::Message& proto,
ProtoWriteFormat proto_write_format, bool gzipped) {
// Note that gzipped files are currently not supported.
gzipped = false;
std::string file_type_suffix;
std::string output_string;
google::protobuf::io::StringOutputStream stream(&output_string);
switch (proto_write_format) {
case ProtoWriteFormat::kProtoBinary:
if (!proto.SerializeToZeroCopyStream(&stream)) {
LOG(WARNING) << "Serialize to stream failed.";
return false;
}
file_type_suffix = ".bin";
break;
case ProtoWriteFormat::kProtoText:
if (!google::protobuf::TextFormat::PrintToString(proto, &output_string)) {
LOG(WARNING) << "Printing to std::string failed.";
return false;
}
break;
}
const std::string output_filename = absl::StrCat(filename, file_type_suffix);
VLOG(1) << "Writing " << output_string.size() << " bytes to "
<< output_filename;
if (!file::SetContents(output_filename, output_string, file::Defaults())
.ok()) {
LOG(WARNING) << "Writing to file failed.";
return false;
}
return true;
}
} // namespace operations_research