CtlUtil.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "CtlUtil.hpp"
  2. #ifdef ZT_CONTROLLER_USE_LIBPQ
  3. #include <iomanip>
  4. #include <sstream>
  5. namespace ZeroTier {
  6. const char* _timestr()
  7. {
  8. time_t t = time(0);
  9. char* ts = ctime(&t);
  10. char* p = ts;
  11. if (! p)
  12. return "";
  13. while (*p) {
  14. if (*p == '\n') {
  15. *p = (char)0;
  16. break;
  17. }
  18. ++p;
  19. }
  20. return ts;
  21. }
  22. std::vector<std::string> split(std::string str, char delim)
  23. {
  24. std::istringstream iss(str);
  25. std::vector<std::string> tokens;
  26. std::string item;
  27. while (std::getline(iss, item, delim)) {
  28. tokens.push_back(item);
  29. }
  30. return tokens;
  31. }
  32. std::string url_encode(const std::string& value)
  33. {
  34. std::ostringstream escaped;
  35. escaped.fill('0');
  36. escaped << std::hex;
  37. for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
  38. std::string::value_type c = (*i);
  39. // Keep alphanumeric and other accepted characters intact
  40. if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
  41. escaped << c;
  42. continue;
  43. }
  44. // Any other characters are percent-encoded
  45. escaped << std::uppercase;
  46. escaped << '%' << std::setw(2) << int((unsigned char)c);
  47. escaped << std::nouppercase;
  48. }
  49. return escaped.str();
  50. }
  51. } // namespace ZeroTier
  52. #endif