global.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 getRandomString(int entropy, int sizeOfLine)
  15. {
  16. QString random_value;
  17. if(entropy <= 0 || entropy > 59)
  18. entropy = 59;
  19. std::random_device rd;
  20. std::uniform_int_distribution<int> dist(0, entropy);
  21. while(random_value.size() < sizeOfLine) {
  22. random_value += randomtable[dist(rd)];
  23. }
  24. return random_value;
  25. }
  26. QString getValue(const QString &string, const QString &key, Type type)
  27. {
  28. if (key.isEmpty())
  29. return QString();
  30. if (type == eHttpHeader) {
  31. if (string.indexOf(QRegularExpression(key + ":")) == -1) {
  32. return QString();
  33. }
  34. }
  35. else if (string.indexOf(QRegularExpression(key + "\\s*=")) == -1) {
  36. return QString();
  37. }
  38. QString result {string};
  39. if (type == eHttpHeader) {
  40. result.remove(QRegularExpression("^.*"+key+":\\s", QRegularExpression::DotMatchesEverythingOption));
  41. }
  42. else {
  43. result.remove(QRegularExpression("^.*"+key+"\\s*=", QRegularExpression::DotMatchesEverythingOption));
  44. }
  45. QString separator {'\n'};
  46. if (type == eForTriggers) {
  47. separator = ":::";
  48. } else if (type == eForWeb) {
  49. separator = '&';
  50. } else if (type == eHttpHeader) {
  51. separator = "\r\n";
  52. }
  53. int valueEnd = result.indexOf(separator);
  54. if (valueEnd != -1) {
  55. result.remove(valueEnd, result.size()-valueEnd);
  56. }
  57. else if (valueEnd == -1 and type == eForWeb) {
  58. separator = ' ';
  59. valueEnd = result.indexOf(separator);
  60. if (valueEnd != -1) {
  61. result.remove(valueEnd, result.size()-valueEnd);
  62. }
  63. }
  64. if (type == eForWeb) {
  65. result = QByteArray::fromPercentEncoding(result.toUtf8());
  66. result.replace('+', ' ');
  67. }
  68. else if (type == eHttpHeader) {
  69. result.remove('\"');
  70. }
  71. else {
  72. std::string stdResult {result.toStdString()};
  73. while (stdResult.front() == ' ') stdResult = stdResult.substr(1);
  74. while (stdResult.back() == ' ') stdResult.pop_back();
  75. return stdResult.c_str();
  76. }
  77. return result;
  78. }
  79. QString toLowerAndNoSpaces(const QString &channelName)
  80. {
  81. QString result {channelName};
  82. result.replace(' ', '_');
  83. result = result.toLower();
  84. return result;
  85. }
  86. } // namespace