remoteBase.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "remoteBase.h"
  2. //ip
  3. #include <vector>
  4. #include <string>
  5. #include <boost/asio.hpp>
  6. #include <boost/algorithm/string.hpp>
  7. #include <boost/lexical_cast.hpp>
  8. #include <cstdio>
  9. using boost::asio::ip::tcp;
  10. #include <boost/format.hpp>
  11. #include <boost/date_time/gregorian/gregorian.hpp>
  12. //ini write
  13. #include <boost/property_tree/ptree.hpp>
  14. #include <boost/property_tree/ini_parser.hpp>
  15. namespace fs = boost::filesystem;
  16. bool Base_DeleteFile(std::string filePath)
  17. {
  18. if (fs::exists(filePath))
  19. return fs::remove(filePath);
  20. return false;
  21. }
  22. bool Base_Exist(std::string filePath)
  23. {
  24. return fs::exists(filePath);
  25. }
  26. uintmax_t Base_filesize(std::string filePath)
  27. {
  28. return fs::file_size(filePath);
  29. }
  30. std::pair<bool, int> Base_readFile(std::string filePath, uintmax_t start, char* readBuf, int readSize)
  31. {
  32. auto cfgFile = fopen(filePath.c_str(), "r");
  33. if (nullptr == cfgFile)
  34. return std::make_pair(false, 0);
  35. else
  36. {
  37. if (0 != fseek(cfgFile, start, SEEK_SET)) {
  38. fclose(cfgFile);
  39. return std::make_pair(false, 0);
  40. }
  41. char* pBuf = new char[readSize];
  42. memset(pBuf, 0, readSize);
  43. int nHasRead = fread(readBuf, readSize, 1, cfgFile);
  44. if (nHasRead <= 0)
  45. return std::make_pair(false, 0);
  46. delete[] pBuf;
  47. fclose(cfgFile);
  48. return std::make_pair(true, nHasRead);
  49. }
  50. }
  51. bool Base_GetHostIPAddr(BYTE addr[4])
  52. {
  53. boost::asio::io_service io_service;
  54. tcp::resolver resolver(io_service);
  55. tcp::resolver::query query(boost::asio::ip::host_name(), "");
  56. tcp::resolver::iterator iter = resolver.resolve(query);
  57. tcp::resolver::iterator end; // End marker.
  58. while (iter != end)
  59. {
  60. tcp::endpoint ep = *iter++;
  61. if (ep.address().is_v4())
  62. {
  63. auto ipaddr = ep.address().to_string();
  64. std::vector<std::string> vec;
  65. boost::split(vec, ipaddr, boost::is_any_of("."));
  66. if (4 == vec.size())
  67. {
  68. for (auto i = 0; i < vec.size(); i++)
  69. {
  70. int it = boost::lexical_cast<int>(vec[i].c_str());
  71. addr[i] = it;
  72. }
  73. return true;
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. bool Base_CopyFile(std::string src, std::string dst)
  80. {
  81. boost::system::error_code ec;
  82. if (fs::exists(src))
  83. {
  84. fs::copy_file(src, dst, boost::filesystem::copy_option::overwrite_if_exists, ec);
  85. if (!ec.failed())
  86. return true;
  87. }
  88. else
  89. return false;
  90. }
  91. //替换所有需要更换日期的地方
  92. std::string getCurData()
  93. {
  94. boost::gregorian::date d(boost::gregorian::day_clock::local_day());
  95. return (boost::format("%04d%02d%02d") % d.year() % d.month().as_number() % d.day()).str();
  96. }
  97. //统一切换stricmp,Linux和windows部分都切换,因为很多使用了CSimpleString来比较
  98. bool iequals(const std::string& a, const std::string& b)
  99. {
  100. unsigned int sz = a.size();
  101. if (b.size() != sz)
  102. return false;
  103. for (unsigned int i = 0; i < sz; ++i)
  104. if (tolower(a[i]) != tolower(b[i]))
  105. return false;
  106. return true;
  107. }
  108. unsigned int get_proc_mem(unsigned int pid) {//获取进程占用内存
  109. auto VMRSS_LINE = 17;
  110. char file_name[64] = { 0 };
  111. FILE* fd;
  112. char line_buff[512] = { 0 };
  113. sprintf(file_name, "/proc/%d/status", pid);
  114. fd = fopen(file_name, "r");
  115. if (nullptr == fd) {
  116. return 0;
  117. }
  118. char name[64];
  119. int vmrss;
  120. for (int i = 0; i < VMRSS_LINE - 1; i++) {
  121. fgets(line_buff, sizeof(line_buff), fd);
  122. }
  123. fgets(line_buff, sizeof(line_buff), fd);
  124. sscanf(line_buff, "%s %d", name, &vmrss);
  125. fclose(fd);
  126. return vmrss;
  127. }
  128. std::pair<std::string, std::string> getPathName(std::string filePath)
  129. {
  130. return std::make_pair(fs::path(filePath).filename().string(), fs::path(filePath).parent_path().string());
  131. }
  132. bool unix2dos(std::string filename)
  133. {
  134. char c;
  135. std::ifstream is(filename.c_str(), std::ios::binary);
  136. std::ofstream os("temp.txt", std::ios::binary);
  137. while (is.get(c)) {
  138. switch (c) {
  139. case 0x0d: break;
  140. case 0x0a: os.put((char)0x0d); os.put((char)0x0a); break;
  141. default: os.put(c); break;
  142. }
  143. }
  144. is.close(); os.close();
  145. std::string command = "mv temp.txt " + filename;
  146. system(command.c_str());
  147. Base_DeleteFile("temp.txt");
  148. return EXIT_SUCCESS;
  149. }
  150. void Base_writeIni(std::string section, std::string key, std::string value, std::string cfgPath)
  151. {
  152. boost::property_tree::ptree lvptProperties;
  153. boost::property_tree::ini_parser::read_ini(cfgPath, lvptProperties);
  154. if (lvptProperties.find(section) == lvptProperties.not_found())
  155. {//add new section
  156. lvptProperties.put<std::string>(section + "." + key, value);
  157. Dbg("modify %s:%s -- %s", section.c_str(), key.c_str(), value.c_str());
  158. }
  159. else {
  160. auto curPro = lvptProperties.get_child(section);
  161. if (curPro.find(key) == curPro.not_found())
  162. lvptProperties.add<std::string>(section + "." + key, value);
  163. else
  164. lvptProperties.put<std::string>(section + "." + key, value);
  165. }
  166. /*
  167. auto curPro = lvptProperties.get_child(section);
  168. if (curPro.find(key) == curPro.not_found())
  169. lvptProperties.add<std::string>(section + "." + key, value);
  170. else
  171. lvptProperties.put<std::string>(section + "." + key, value);
  172. */
  173. boost::property_tree::ini_parser::write_ini(cfgPath, lvptProperties);
  174. unix2dos(cfgPath);
  175. }
  176. bool Base_isDirectory(std::string filePath)
  177. {
  178. return fs::is_directory(filePath);
  179. }