|
@@ -0,0 +1,167 @@
|
|
|
+#include "remoteBase.h"
|
|
|
+//ip
|
|
|
+#include <vector>
|
|
|
+#include <string>
|
|
|
+#include <boost/asio.hpp>
|
|
|
+#include <boost/algorithm/string.hpp>
|
|
|
+#include <boost/lexical_cast.hpp>
|
|
|
+#include <cstdio>
|
|
|
+using boost::asio::ip::tcp;
|
|
|
+
|
|
|
+#include <boost/format.hpp>
|
|
|
+#include <boost/date_time/gregorian/gregorian.hpp>
|
|
|
+
|
|
|
+//ini write
|
|
|
+#include <boost/property_tree/ptree.hpp>
|
|
|
+#include <boost/property_tree/ini_parser.hpp>
|
|
|
+
|
|
|
+
|
|
|
+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<bool, int> 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))
|
|
|
+ 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<std::string> 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<int>(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("%.4d%.2d%.2d") % 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::string getPathName(std::string filePath)
|
|
|
+{
|
|
|
+ return fs::path(filePath).filename().string();
|
|
|
+}
|
|
|
+
|
|
|
+template<typename T>
|
|
|
+void Base_writeIni(std::string section, std::string key, T value, std::string cfgPath)
|
|
|
+{
|
|
|
+ boost::property_tree::ptree lvptProperties;
|
|
|
+ boost::property_tree::ini_parser::read_ini(cfgPath, lvptProperties);
|
|
|
+ boost::property_tree::basic_ptree<std::string, std::string> lvbtItems = lvptProperties.get_child(section);
|
|
|
+
|
|
|
+
|
|
|
+ lvptProperties.add<T>(key, value);
|
|
|
+
|
|
|
+ boost::property_tree::ini_parser::write_ini(cfgPath, lvptProperties);
|
|
|
+}
|