#include "remoteBase.h" //ip #include #include #include #include #include #include using boost::asio::ip::tcp; #include #include //ini write #include #include namespace fs = boost::filesystem; bool Base_DeleteFile(std::string filePath) { if (fs::exists(filePath)) return fs::remove(filePath); return false; } bool Base_Exist(std::string filePath) { return fs::exists(filePath); } uintmax_t Base_filesize(std::string filePath) { return fs::file_size(filePath); } std::pair Base_readFile(std::string filePath, uintmax_t start, char* readBuf, int readSize) { auto cfgFile = fopen(filePath.c_str(), "r"); if (nullptr == cfgFile) return std::make_pair(false, 0); else { if (0 != fseek(cfgFile, start, SEEK_SET)) { fclose(cfgFile); return std::make_pair(false, 0); } char* pBuf = new char[readSize]; memset(pBuf, 0, readSize); int nHasRead = fread(readBuf, readSize, 1, cfgFile); if (nHasRead <= 0) return std::make_pair(false, 0); delete[] pBuf; fclose(cfgFile); return std::make_pair(true, nHasRead); } } bool Base_GetHostIPAddr(BYTE addr[4]) { boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(boost::asio::ip::host_name(), ""); tcp::resolver::iterator iter = resolver.resolve(query); tcp::resolver::iterator end; // End marker. while (iter != end) { tcp::endpoint ep = *iter++; if (ep.address().is_v4()) { auto ipaddr = ep.address().to_string(); std::vector vec; boost::split(vec, ipaddr, boost::is_any_of(".")); if (4 == vec.size()) { for (auto i = 0; i < vec.size(); i++) { int it = boost::lexical_cast(vec[i].c_str()); addr[i] = it; } return true; } } } return false; } bool Base_CopyFile(std::string src, std::string dst) { boost::system::error_code ec; if (fs::exists(src)) { fs::copy_file(src, dst, boost::filesystem::copy_option::overwrite_if_exists, ec); if (!ec.failed()) return true; } else return false; } //替换所有需要更换日期的地方 std::string getCurData() { boost::gregorian::date d(boost::gregorian::day_clock::local_day()); return (boost::format("%04d%02d%02d") % d.year() % d.month().as_number() % d.day()).str(); } //统一切换stricmp,Linux和windows部分都切换,因为很多使用了CSimpleString来比较 bool iequals(const std::string& a, const std::string& b) { unsigned int sz = a.size(); if (b.size() != sz) return false; for (unsigned int i = 0; i < sz; ++i) if (tolower(a[i]) != tolower(b[i])) return false; return true; } unsigned int get_proc_mem(unsigned int pid) {//获取进程占用内存 auto VMRSS_LINE = 17; char file_name[64] = { 0 }; FILE* fd; char line_buff[512] = { 0 }; sprintf(file_name, "/proc/%d/status", pid); fd = fopen(file_name, "r"); if (nullptr == fd) { return 0; } char name[64]; int vmrss; for (int i = 0; i < VMRSS_LINE - 1; i++) { fgets(line_buff, sizeof(line_buff), fd); } fgets(line_buff, sizeof(line_buff), fd); sscanf(line_buff, "%s %d", name, &vmrss); fclose(fd); return vmrss; } std::pair getPathName(std::string filePath) { return std::make_pair(fs::path(filePath).filename().string(), fs::path(filePath).parent_path().string()); } bool unix2dos(std::string filename) { char c; std::ifstream is(filename.c_str(), std::ios::binary); std::ofstream os("temp.txt", std::ios::binary); while (is.get(c)) { switch (c) { case 0x0d: break; case 0x0a: os.put((char)0x0d); os.put((char)0x0a); break; default: os.put(c); break; } } is.close(); os.close(); std::string command = "mv temp.txt " + filename; system(command.c_str()); Base_DeleteFile("temp.txt"); return EXIT_SUCCESS; } void Base_writeIni(std::string section, std::string key, std::string value, std::string cfgPath) { boost::property_tree::ptree lvptProperties; boost::property_tree::ini_parser::read_ini(cfgPath, lvptProperties); if (lvptProperties.find(section) == lvptProperties.not_found()) {//add new section lvptProperties.put(section + "." + key, value); Dbg("modify %s:%s -- %s", section.c_str(), key.c_str(), value.c_str()); } else { auto curPro = lvptProperties.get_child(section); if (curPro.find(key) == curPro.not_found()) lvptProperties.add(section + "." + key, value); else lvptProperties.put(section + "." + key, value); } /* auto curPro = lvptProperties.get_child(section); if (curPro.find(key) == curPro.not_found()) lvptProperties.add(section + "." + key, value); else lvptProperties.put(section + "." + key, value); */ boost::property_tree::ini_parser::write_ini(cfgPath, lvptProperties); unix2dos(cfgPath); } bool Base_isDirectory(std::string filePath) { return fs::is_directory(filePath); }