github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/fs/util.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 21 #include "fs/util.h" 22 #include <boost/filesystem.hpp> 23 #include <chrono> 24 #include <ctime> 25 26 namespace neb { 27 namespace fs { 28 29 std::string cur_full_path() { 30 return boost::filesystem::current_path().generic_string(); 31 } 32 33 std::string cur_dir() { 34 boost::filesystem::path cur_path = boost::filesystem::current_path(); 35 return cur_path.parent_path().generic_string(); 36 } 37 38 std::string tmp_dir() { 39 return boost::filesystem::temp_directory_path().generic_string(); 40 } 41 42 std::string join_path(const std::string &parent, const std::string &fp) { 43 boost::filesystem::path cur_path(parent); 44 boost::filesystem::path np = cur_path / boost::filesystem::path(fp); 45 return np.generic_string(); 46 } 47 48 std::string parent_dir(const std::string &fp) { 49 boost::filesystem::path cur_path(fp); 50 return cur_path.parent_path().generic_string(); 51 } 52 53 bool is_absolute_path(const std::string &fp) { 54 boost::filesystem::path cur_path(fp); 55 return cur_path.is_absolute(); 56 } 57 58 bool exists(const std::string &p) { 59 return boost::filesystem::exists(boost::filesystem::path(p)); 60 } 61 62 std::string get_user_name() { return std::string("usr"); } 63 64 } // end namespace fs 65 66 std::string now() { 67 auto nt = std::chrono::system_clock::now(); 68 std::time_t tt = std::chrono::system_clock::to_time_t(nt); 69 auto ret = std::string(std::ctime(&tt)); 70 ret.erase(std::remove_if(ret.begin(), ret.end(), 71 [](unsigned char c) { return c == '\n'; })); 72 return ret; 73 } 74 75 } // end namespace neb