Guest User

Untitled

a guest
Jul 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include "mircparser.h"
  2. #include <KDebug>
  3. #include <QTextStream>
  4. using namespace Aki;
  5.  
  6. namespace Aki
  7. {
  8. class MIrcParserPrivate
  9. {
  10. public:
  11. MIrcParserPrivate(Aki::MIrcParser *qq)
  12. : q(qq)
  13. {
  14. }
  15.  
  16. void readServerLine(const QString &str)
  17. {
  18. QString line = str;
  19. Aki::ServerPointer server(new Aki::Server);
  20. QStringList addresses;
  21.  
  22. // Remove the nXX= part from the beginning of the string since
  23. // it is useless.
  24. QString tmp = line.left(line.indexOf('='));
  25. line.remove(0, tmp.length() + 1);
  26.  
  27. QStringList serversSplit = line.split("SERVER:", QString::SkipEmptyParts, Qt::CaseInsensitive);
  28. QString serverName = serversSplit[0];
  29. QString addressGroup = serversSplit[1];
  30.  
  31. server->setName(serverName);
  32.  
  33. QStringList groupSplit = addressGroup.split("GROUP:", QString::SkipEmptyParts, Qt::CaseInsensitive);
  34. QString addressPort = groupSplit[0];
  35. QStringList addressPortSplit = addressPort.split(':');
  36.  
  37. QString address = addressPortSplit[0];
  38. QStringList portSplit = addressPortSplit[1].split('.');
  39.  
  40. QStringListIterator portIter(portSplit);
  41. while (portIter.hasNext()) {
  42. QString ports = portIter.next().trimmed();
  43. if (ports.contains('-')) {
  44. QStringList lowHighSplit = ports.split('-');
  45. int low = lowHighSplit[0].toInt();
  46. int high = lowHighSplit[1].toInt();
  47.  
  48. for (int i = low; i < high + 1; ++i) {
  49. addresses << address + '/' + QString::number(i);
  50. }
  51. } else {
  52. addresses << address + '/' + ports;
  53. }
  54. }
  55.  
  56. if (!addresses.isEmpty()) {
  57. server->setAddressList(addresses);
  58. servers << server;
  59. }
  60. }
  61.  
  62. Aki::MIrcParser *q;
  63. Aki::ServerList servers;
  64. }; // End of class MIrcParserPrivate.
  65. } // End of namespace Aki.
  66.  
  67. MIrcParser::MIrcParser(QObject *parent)
  68. : QObject(parent)
  69. {
  70. d.reset(new Aki::MIrcParserPrivate(this));
  71. }
  72.  
  73. MIrcParser::~MIrcParser()
  74. {
  75. }
  76.  
  77. bool
  78. MIrcParser::read(QIODevice *device)
  79. {
  80. if (!device) {
  81. return false;
  82. }
  83.  
  84. QString line;
  85.  
  86. QTextStream in(device);
  87. while (!in.atEnd()) {
  88. line = in.readLine().trimmed();
  89.  
  90. if (line[0] == 'n') {
  91. d->readServerLine(line);
  92. }
  93. }
  94.  
  95. device->close();
  96.  
  97. return true;
  98. }
  99.  
  100. Aki::ServerList
  101. MIrcParser::servers() const
  102. {
  103. return d->servers;
  104. }
Add Comment
Please, Sign In to add comment