dmpserverutils.h 2.8 KB

/**************************************************************************
* file:              dmpserverutils.h

* Author:            wanzhongping
* Date:              2021-03-17 16:05:31
* Email:             zhongpingw@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/

#ifndef __dmpserverutils_h__
#define __dmpserverutils_h__

#include "dmap_server.h"
#include <boost/filesystem.hpp>
#include <unordered_map>

class SERVER_EXPORT DmpServerUtils
{
  public:
    //获取目录下的所有dll或so库文件
    static int GetLibraryPaths(const boost::filesystem::path &dir, std::vector<boost::filesystem::path> &locations);
};

  class CaseInsensitiveEqual {
    public:
      bool operator()(const std::string &str1, const std::string &str2) const noexcept {
        return str1.size() == str2.size() &&
           std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) {
             return tolower(a) == tolower(b);
           });
      }
  };
  
  class CaseInsensitiveHash {
    public:
      std::size_t operator()(const std::string &str) const noexcept {
        std::size_t h = 0;
        std::hash<int> hash;
        for(auto c : str)
          h ^= hash(tolower(c)) + 0x9e3779b9 + (h << 6) + (h >> 2);
        return h;
      }
  };


  using CaseInsensitiveMultimap = std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual>;

  // Percent encoding and decoding
  class Percent {
    public:
      static std::string encode(const std::string &value) noexcept;
      static std::string decode(const std::string &value) noexcept; 
  };

 
//自定义大小写不敏感的map
struct ci_less : std::binary_function<std::string, std::string, bool>
{
  // case-independent (ci) compare_less binary function
  struct nocase_compare : public std::binary_function<unsigned char, unsigned char, bool>
  {
    bool operator()(const unsigned char &c1, const unsigned char &c2) const
    {
      return tolower(c1) < tolower(c2);
    }
  };
  bool operator()(const std::string &s1, const std::string &s2) const
  {
    return std::lexicographical_compare(s1.begin(), s1.end(), // source range
                                        s2.begin(), s2.end(), // dest range
                                        nocase_compare());    // comparison
  }
};

using CIMap = std::map<std::string, std::string, ci_less>;

 // Query string creation and parsing
  class QueryString {
    public:
      // Returns query string created from given field names and values
      static std::string create(const CIMap &fields) noexcept;
      // Returns query keys with percent-decoded values.
      static CIMap parse(const std::string &query_string) noexcept;
  };


#endif //__dmpserverutils_h__