global.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "global.h"
  2. #include <QString>
  3. #include <random>
  4. #include <QRegularExpression>
  5. #include <QDebug>
  6. namespace global
  7. {
  8. const char randomtable[60] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  9. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  10. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  11. 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
  12. 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  13. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'};
  14. QString getValue(const QString &string, const QString &key, Type type)
  15. {
  16. if (key.isEmpty())
  17. return QString();
  18. if (type == eHttpHeader) {
  19. if (string.indexOf(QRegularExpression(key + ":")) == -1) {
  20. return QString();
  21. }
  22. }
  23. else if (string.indexOf(QRegularExpression(key + "\\s*=")) == -1) {
  24. return QString();
  25. }
  26. QString result {string};
  27. if (type == eHttpHeader) {
  28. result.remove(QRegularExpression("^.*"+key+":\\s", QRegularExpression::DotMatchesEverythingOption));
  29. }
  30. else {
  31. result.remove(QRegularExpression("^.*"+key+"\\s*=", QRegularExpression::DotMatchesEverythingOption));
  32. }
  33. QString separator {"\n"};
  34. if (type == eForTriggers) {
  35. separator = ":::";
  36. } else if (type == eForWeb) {
  37. separator = " ";
  38. } else if (type == eHttpHeader) {
  39. separator = "\r\n";
  40. }
  41. int valueEnd = result.indexOf(separator);
  42. if (valueEnd != -1) {
  43. result.remove(valueEnd, result.size()-valueEnd);
  44. }
  45. if (type == eForWeb) {
  46. result = QByteArray::fromPercentEncoding(result.toUtf8());
  47. result.replace('+', ' ');
  48. }
  49. else if (type == eHttpHeader) {
  50. result.remove('\"');
  51. }
  52. else {
  53. std::string stdResult {result.toStdString()};
  54. while (stdResult.front() == ' ') stdResult = stdResult.substr(1);
  55. while (stdResult.back() == ' ') stdResult.pop_back();
  56. return stdResult.c_str();
  57. }
  58. return result;
  59. }
  60. QString toLowerAndNoSpaces(const QString &channelName)
  61. {
  62. QString result {channelName};
  63. result.replace(' ', '_');
  64. result = result.toLower();
  65. return result;
  66. }
  67. QString getRandomString(int entropy, int sizeOfLine)
  68. {
  69. QString random_value;
  70. if(entropy <= 0 || entropy > 59)
  71. entropy = 59;
  72. std::random_device rd;
  73. std::uniform_int_distribution<int> dist(0, entropy);
  74. while(random_value.size() < sizeOfLine) {
  75. random_value += randomtable[dist(rd)];
  76. }
  77. return random_value;
  78. }
  79. } // namespace