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