buddyinfo.cpp
1 //------------------------------------------------------------------------------
2 // buddyinfo.cpp
3 //
4 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
5 //------------------------------------------------------------------------------
6 #include "buddyinfo.h"
7 #include "scanner.h"
8 #include "strings.h"
9 
10 QString BuddyInfo::createConfigEntry(const QVector<BuddyInfo>& buddies)
11 {
12  QString settingValue;
13 
14  foreach(const BuddyInfo& buddyInfo, buddies)
15  {
16  QString pattern = buddyInfo.pattern.pattern();
17  settingValue.append((buddyInfo.patternType == PT_BASIC ? "basic \"" : "advanced \"") + Strings::escape(pattern) + "\";");
18  }
19 
20  return settingValue;
21 }
22 #include <QDebug>
23 void BuddyInfo::readConfigEntry(const QString& configEntry, QVector<BuddyInfo>& outVector)
24 {
25  outVector.clear();
26 
27  Scanner listReader(configEntry.toUtf8().constData(), configEntry.length());
28  // Syntax: {basic|advanced} "pattern";...
29  while(listReader.tokensLeft())
30  {
31  if(!listReader.checkToken(TK_Identifier))
32  {
33  break; // Invalid so lets just use what we have.
34  }
35 
36  BuddyInfo::PatternType type = BuddyInfo::PT_BASIC;
37  if(listReader->str().compare("basic") == 0)
38  {
39  type = PT_BASIC;
40  }
41  else if(listReader->str().compare("advanced") == 0)
42  {
43  type = PT_ADVANCED;
44  }
45 
46  if(!listReader.checkToken(TK_StringConst))
47  {
48  break;
49  }
50 
51  QRegExp pattern(listReader->str(), Qt::CaseInsensitive, type == PT_BASIC ? QRegExp::Wildcard : QRegExp::RegExp);
52  if(pattern.isValid())
53  {
54  BuddyInfo buddyInfo;
55 
56  buddyInfo.pattern = pattern;
57  buddyInfo.patternType = type;
58 
59  outVector.append(buddyInfo);
60  }
61 
62  if(!listReader.checkToken(';'))
63  {
64  break;
65  }
66  }
67 }
static const QString & escape(QString &str)
Adds escape characters to a string.
Definition: strings.cpp:196
Scanner reads scripts by checking individual tokens.
Definition: scanner.h:75