We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / common / util.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "util.h"
22 #include <QDebug>
23 #include <QTextCodec>
24
25 QString nickFromMask(QString mask) {
26   return mask.section('!', 0, 0);
27 }
28
29 QString userFromMask(QString mask) {
30   QString userhost = mask.section('!', 1);
31   if(userhost.isEmpty()) return QString();
32   return userhost.section('@', 0, 0);
33 }
34
35 QString hostFromMask(QString mask) {
36   QString userhost = mask.section('!', 1);
37   if(userhost.isEmpty()) return QString();
38   return userhost.section('@', 1);
39 }
40
41 bool isChannelName(QString str) {
42   return QString("#&!+").contains(str[0]);
43 }
44
45 QString decodeString(QByteArray input, QString encoding) {
46   // First, we check if it's utf8. It is very improbable to encounter a string that looks like
47   // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
48   // is safe to assume that it is.
49   Q_ASSERT(sizeof(const char) == sizeof(quint8));  // just to make sure
50   bool isUtf8 = true;
51   int cnt = 0;
52   for(int i = 0; i < input.size(); i++) {
53     if(cnt) {
54       // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
55       if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
56       cnt--;
57       continue;
58     }
59     if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
60     if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; }  // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
61     if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; }  // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
62     if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; }  // 2-byte char 110xxxxx 10yyyyyy
63     isUtf8 = false; break;  // 8 bit char, but not utf8!
64   }
65   if(isUtf8 && cnt == 0) {
66     QString s = QString::fromUtf8(input);
67     //qDebug() << "Detected utf8:" << s;
68     return s;
69   }
70   QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
71   if(!codec) {
72     qWarning() << QString("Invalid encoding: %1").arg(encoding);
73     return QString::fromAscii(input);
74   }
75   return codec->toUnicode(input);
76 }
77
78 void writeDataToDevice(QIODevice *dev, const QVariant &item) {
79   QByteArray block;
80   QDataStream out(&block, QIODevice::WriteOnly);
81   out.setVersion(QDataStream::Qt_4_2);
82   out << (quint32)0 << item;
83   out.device()->seek(0);
84   out << (quint32)(block.size() - sizeof(quint32));
85   dev->write(block);
86 }
87
88 bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item) {
89   QDataStream in(dev);
90   in.setVersion(QDataStream::Qt_4_2);
91
92   if(blockSize == 0) {
93     if(dev->bytesAvailable() < (int)sizeof(quint32)) return false;
94     in >> blockSize;
95   }
96   if(dev->bytesAvailable() < blockSize) return false;
97   in >> item;
98   return true;
99 }
100
101
102 uint editingDistance(const QString &s1, const QString &s2) {
103   uint n = s1.size()+1;
104   uint m = s2.size()+1;
105   //uint matrix[n][m];
106   QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
107
108   for(uint i = 0; i < n; i++)
109     matrix[i][0] = i;
110
111   for(uint i = 0; i < m; i++)
112     matrix[0][i] = i;
113
114   uint min;
115   for(uint i = 1; i < n; i++) {
116     for(uint j = 1; j < m; j++) {
117       uint deleteChar = matrix[i-1][j] + 1;
118       uint insertChar = matrix[i][j-1] + 1;
119
120       if(deleteChar < insertChar)
121         min = deleteChar;
122       else
123         min = insertChar;
124       
125       if(s1[i-1] == s2[j-1]) {
126         uint inheritChar = matrix[i-1][j-1];
127         if(inheritChar < min)
128           min = inheritChar;
129       }
130
131       matrix[i][j] = min;
132     }
133   }
134   return matrix[n-1][m-1];
135 }