1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef LIBTEXTCLASSIFIER_COMMON_FILE_UTILS_H_
18 #define LIBTEXTCLASSIFIER_COMMON_FILE_UTILS_H_
19
20 #include <cstddef>
21 #include <memory>
22 #include <string>
23
24 #include "common/config.h"
25
26 #if PORTABLE_SAFT_MOBILE
27 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
28 #endif
29
30 #include "common/mmap.h"
31 #include "util/strings/stringpiece.h"
32
33 namespace libtextclassifier {
34 namespace nlp_core {
35
36 namespace file_utils {
37
38 // Reads the entire content of a file into a string. Returns true on success,
39 // false on error.
40 bool GetFileContent(const std::string &filename, std::string *content);
41
42 // Parses a proto from its serialized representation in memory. That
43 // representation starts at address sp.data() and contains exactly sp.size()
44 // bytes. Returns true on success, false otherwise.
45 template<class Proto>
ParseProtoFromMemory(StringPiece sp,Proto * proto)46 bool ParseProtoFromMemory(StringPiece sp, Proto *proto) {
47 if (!sp.data()) {
48 // Avoid passing a nullptr to ArrayInputStream below.
49 return false;
50 }
51 #if PORTABLE_SAFT_MOBILE
52 ::google::protobuf::io::ArrayInputStream stream(sp.data(), sp.size());
53 return proto->ParseFromZeroCopyStream(&stream);
54 #else
55
56 std::string data(sp.data(), sp.size());
57 return proto->ParseFromString(data);
58 #endif
59 }
60
61 // Parses a proto from a file. Returns true on success, false otherwise.
62 //
63 // Note: the entire content of the file should be the binary (not
64 // human-readable) serialization of a protocol buffer.
65 //
66 // Note: when we compile for Android, the proto parsing methods need to know the
67 // type of the message they are parsing. We use template polymorphism for that.
68 template<class Proto>
ReadProtoFromFile(const std::string & filename,Proto * proto)69 bool ReadProtoFromFile(const std::string &filename, Proto *proto) {
70 ScopedMmap scoped_mmap(filename);
71 const MmapHandle &handle = scoped_mmap.handle();
72 if (!handle.ok()) {
73 return false;
74 }
75 return ParseProtoFromMemory(handle.to_stringpiece(), proto);
76 }
77
78 // Returns true if filename is the name of an existing file, and false
79 // otherwise.
80 bool FileExists(const std::string &filename);
81
82 // Returns true if dirpath is the path to an existing directory, and false
83 // otherwise.
84 bool DirectoryExists(const std::string &dirpath);
85
86 } // namespace file_utils
87
88 } // namespace nlp_core
89 } // namespace libtextclassifier
90
91 #endif // LIBTEXTCLASSIFIER_COMMON_FILE_UTILS_H_
92