123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #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)) {
- 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<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("%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<std::string, std::string> 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<std::string>(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<std::string>(section + "." + key, value);
- else
- lvptProperties.put<std::string>(section + "." + key, value);
- }
-
-
- /*
- auto curPro = lvptProperties.get_child(section);
- if (curPro.find(key) == curPro.not_found())
- lvptProperties.add<std::string>(section + "." + key, value);
- else
- lvptProperties.put<std::string>(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);
- }
|