提交 ed36be9b80c5037193f8819d3d3b796668dc42fb

作者 LJH 李佳桓
1 个父辈 ab62cdca

add

正在显示 1 个修改的文件 包含 130 行增加0 行删除
  1 +/**************************************************************************
  2 +* file: dmpserverutils.cpp
  3 +
  4 +* Author: wanzhongping
  5 +* Date: 2021-03-17 16:05:37
  6 +* Email: zhongpingw@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +
  10 +#include "dmpserverutils.h"
  11 +
  12 + int DmpServerUtils::GetLibraryPaths(const boost::filesystem::path &dir, std::vector<boost::filesystem::path> &locations)
  13 + {
  14 + if (!boost::filesystem::exists(dir))
  15 + {
  16 + return -1;
  17 + }
  18 +
  19 + std::string dllExt(".dll");
  20 +#ifdef IS_WINDOWS
  21 + dllExt = ".dll";
  22 +#elif IS_LINUX
  23 + dllExt = ".so";
  24 +#else
  25 + dllExt = ".dll";
  26 +#endif
  27 +
  28 + boost::filesystem::directory_iterator end_iter;
  29 + for (boost::filesystem::directory_iterator iter(dir); iter != end_iter; ++iter)
  30 + {
  31 + if (boost::filesystem::is_regular_file(iter->status()) && iter->path().extension() == dllExt)
  32 + {
  33 + locations.push_back(iter->path());
  34 + }
  35 +
  36 + if (boost::filesystem::is_directory(iter->status()))
  37 + {
  38 + GetLibraryPaths(iter->path(), locations);
  39 + }
  40 + }
  41 +
  42 + return locations.size();
  43 + }
  44 +
  45 + std::string Percent::encode(const std::string &value) noexcept {
  46 + static auto hex_chars = "0123456789ABCDEF";
  47 +
  48 + std::string result;
  49 + result.reserve(value.size()); // Minimum size of result
  50 +
  51 + for(auto &chr : value) {
  52 + if(!((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') || chr == '-' || chr == '.' || chr == '_' || chr == '~'))
  53 + result += std::string("%") + hex_chars[static_cast<unsigned char>(chr) >> 4] + hex_chars[static_cast<unsigned char>(chr) & 15];
  54 + else
  55 + result += chr;
  56 + }
  57 +
  58 + return result;
  59 + }
  60 +
  61 + // Returns percent-decoded string
  62 + std::string Percent::decode(const std::string &value) noexcept {
  63 + std::string result;
  64 + result.reserve(value.size() / 3 + (value.size() % 3)); // Minimum size of result
  65 +
  66 + for(std::size_t i = 0; i < value.size(); ++i) {
  67 + auto &chr = value[i];
  68 + if(chr == '%' && i + 2 < value.size()) {
  69 + auto hex = value.substr(i + 1, 2);
  70 + auto decoded_chr = static_cast<char>(std::strtol(hex.c_str(), nullptr, 16));
  71 + result += decoded_chr;
  72 + i += 2;
  73 + }
  74 + else if(chr == '+')
  75 + result += ' ';
  76 + else
  77 + result += chr;
  78 + }
  79 + return result;
  80 + }
  81 +
  82 +
  83 + // Returns query string created from given field names and values
  84 + std::string QueryString::create(const CIMap &fields) noexcept {
  85 + std::string result;
  86 +
  87 + bool first = true;
  88 + for(auto &field : fields) {
  89 + result += (!first ? "&" : "") + field.first + '=' + Percent::encode(field.second);
  90 + first = false;
  91 + }
  92 +
  93 + return result;
  94 + }
  95 +
  96 + // Returns query keys with percent-decoded values.
  97 + CIMap QueryString::parse(const std::string &query_string) noexcept {
  98 + CIMap result;
  99 +
  100 + if(query_string.empty())
  101 + return result;
  102 +
  103 + std::size_t name_pos = 0;
  104 + auto name_end_pos = std::string::npos;
  105 + auto value_pos = std::string::npos;
  106 + for(std::size_t c = 0; c < query_string.size(); ++c) {
  107 + if(query_string[c] == '&') {
  108 + auto name = query_string.substr(name_pos, (name_end_pos == std::string::npos ? c : name_end_pos) - name_pos);
  109 + if(!name.empty()) {
  110 + auto value = value_pos == std::string::npos ? std::string() : query_string.substr(value_pos, c - value_pos);
  111 + result.emplace(std::move(name), Percent::decode(value));
  112 + }
  113 + name_pos = c + 1;
  114 + name_end_pos = std::string::npos;
  115 + value_pos = std::string::npos;
  116 + }
  117 + else if(query_string[c] == '=' && name_end_pos == std::string::npos) {
  118 + name_end_pos = c;
  119 + value_pos = c + 1;
  120 + }
  121 + }
  122 + if(name_pos < query_string.size()) {
  123 + auto name = query_string.substr(name_pos, (name_end_pos == std::string::npos ? std::string::npos : name_end_pos - name_pos));
  124 + if(!name.empty()) {
  125 + auto value = value_pos >= query_string.size() ? std::string() : query_string.substr(value_pos);
  126 + result.emplace(std::move(name), Percent::decode(value));
  127 + }
  128 + }
  129 + return result;
  130 + }
注册登录 后发表评论