• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "lang_id/common/file/file-utils.h"
18 
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 #include <string>
25 
26 #include "lang_id/common/lite_base/logging.h"
27 #include "lang_id/common/lite_strings/stringpiece.h"
28 
29 namespace libtextclassifier3 {
30 namespace mobile {
31 
32 namespace file_utils {
33 
GetFileContent(const std::string & filename,std::string * content)34 bool GetFileContent(const std::string &filename, std::string *content) {
35   ScopedMmap scoped_mmap(filename);
36   const MmapHandle &handle = scoped_mmap.handle();
37   if (!handle.ok()) {
38     SAFTM_LOG(ERROR) << "Error opening " << filename;
39     return false;
40   }
41   StringPiece sp = handle.to_stringpiece();
42   content->assign(sp.data(), sp.size());
43   return true;
44 }
45 
FileExists(const std::string & filename)46 bool FileExists(const std::string &filename) {
47   struct stat s = {0};
48   if (!stat(filename.c_str(), &s)) {
49     return s.st_mode & S_IFREG;
50   } else {
51     return false;
52   }
53 }
54 
DirectoryExists(const std::string & dirpath)55 bool DirectoryExists(const std::string &dirpath) {
56   struct stat s = {0};
57   if (!stat(dirpath.c_str(), &s)) {
58     return s.st_mode & S_IFDIR;
59   } else {
60     return false;
61   }
62 }
63 
64 }  // namespace file_utils
65 
66 }  // namespace mobile
67 }  // namespace nlp_saft
68