2
0

CtlUtil.cpp 1.2 KB

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