1 /*
2 * Copyright (C) 2018 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 NLP_SAFT_COMPONENTS_COMMON_MOBILE_FILE_FILE_UTILS_H_
18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_FILE_FILE_UTILS_H_
19
20 #include <stddef.h>
21
22 #include <string>
23
24 #include "lang_id/common/file/mmap.h"
25 #include "lang_id/common/lite_strings/stringpiece.h"
26
27 namespace libtextclassifier3 {
28 namespace mobile {
29
30 namespace file_utils {
31
32 // Reads the entire content of a file into a string. Returns true on success,
33 // false on error.
34 bool GetFileContent(const std::string &filename, std::string *content);
35
36 // Parses a proto from its serialized representation in memory. That
37 // representation starts at address |data| and should contain exactly
38 // |num_bytes| bytes. Returns true on success, false otherwise.
39 template <class Proto>
ParseProtoFromMemory(const char * data,size_t num_bytes,Proto * proto)40 bool ParseProtoFromMemory(const char *data, size_t num_bytes, Proto *proto) {
41 if (data == nullptr) {
42 // Avoid passing a nullptr to ParseFromArray below.
43 return false;
44 }
45 return proto->ParseFromArray(data, num_bytes);
46 }
47
48 // Convenience StringPiece-based version of ParseProtoFromMemory.
49 template <class Proto>
ParseProtoFromMemory(StringPiece sp,Proto * proto)50 inline bool ParseProtoFromMemory(StringPiece sp, Proto *proto) {
51 return ParseProtoFromMemory(sp.data(), sp.size(), proto);
52 }
53
54 // Parses a proto from a file. Returns true on success, false otherwise.
55 //
56 // Note: the entire content of the file should be the binary (not
57 // human-readable) serialization of a protocol buffer.
58 //
59 // Note: when we compile for Android, the proto parsing methods need to know the
60 // type of the message they are parsing. We use template polymorphism for that.
61 template <class Proto>
ReadProtoFromFile(const std::string & filename,Proto * proto)62 bool ReadProtoFromFile(const std::string &filename, Proto *proto) {
63 ScopedMmap scoped_mmap(filename);
64 const MmapHandle &handle = scoped_mmap.handle();
65 if (!handle.ok()) {
66 return false;
67 }
68 return ParseProtoFromMemory(handle.to_stringpiece(), proto);
69 }
70
71 // Returns true if filename is the name of an existing file, and false
72 // otherwise.
73 bool FileExists(const std::string &filename);
74
75 // Returns true if dirpath is the path to an existing directory, and false
76 // otherwise.
77 bool DirectoryExists(const std::string &dirpath);
78
79 } // namespace file_utils
80
81 } // namespace mobile
82 } // namespace nlp_saft
83
84 #endif // NLP_SAFT_COMPONENTS_COMMON_MOBILE_FILE_FILE_UTILS_H_
85