global.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. else if (valueEnd == -1 and type == eForWeb) {
  46. separator = ' ';
  47. valueEnd = result.indexOf(separator);
  48. if (valueEnd != -1) {
  49. result.remove(valueEnd, result.size()-valueEnd);
  50. }
  51. }
  52. if (type == eForWeb) {
  53. result = QByteArray::fromPercentEncoding(result.toUtf8());
  54. result.replace('+', ' ');
  55. }
  56. else if (type == eHttpHeader) {
  57. result.remove('\"');
  58. }
  59. else {
  60. std::string stdResult {result.toStdString()};
  61. while (stdResult.front() == ' ') stdResult = stdResult.substr(1);
  62. while (stdResult.back() == ' ') stdResult.pop_back();
  63. return stdResult.c_str();
  64. }
  65. return result;
  66. }
  67. QString toLowerAndNoSpaces(const QString &channelName)
  68. {
  69. QString result {channelName};
  70. result.replace(' ', '_');
  71. result = result.toLower();
  72. return result;
  73. }
  74. QString getRandomString(int entropy, int sizeOfLine)
  75. {
  76. QString random_value;
  77. if(entropy <= 0 || entropy > 59)
  78. entropy = 59;
  79. std::random_device rd;
  80. std::uniform_int_distribution<int> dist(0, entropy);
  81. while(random_value.size() < sizeOfLine) {
  82. random_value += randomtable[dist(rd)];
  83. }
  84. return random_value;
  85. }
  86. } // namespace