1 /***************************************************************************
2 * Copyright (C) 2006 by Kita Developers *
3 * ikemo@users.sourceforge.jp *
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 ***************************************************************************/
12 #include <QtCore/QTextCodec>
18 m_readCodec = QTextCodec::codecForName("Shift-JIS");
19 m_writeCodec = QTextCodec::codecForName("Shift-JIS");
22 QString Bbs::datToUnicode(const QByteArray& ba) const
24 return m_readCodec->toUnicode(ba);
27 QByteArray Bbs::datFromUnicode(const QString& str) const
29 return m_readCodec->fromUnicode(str);
32 QString Bbs::postToUnicode(const QByteArray& ba) const
34 return m_writeCodec->toUnicode(ba);
37 QByteArray Bbs::postFromUnicode(const QString& str) const
39 return m_writeCodec->fromUnicode(str);
43 // 0 encode both @ and /
44 // 1 encode @ but not /
45 // 2 encode neither @ or /
46 QString Bbs::encode(const QString& segment, int encoding_offset,
47 int encoding_hint, bool isRawURI)
49 const char *encode_string = "/@<>#\"&?={}|^~[]\'`\\:+%";
50 encode_string += encoding_offset;
54 local = segment.toLocal8Bit();
57 QTextCodec * textCodec = QTextCodec::codecForMib(encoding_hint);
59 local = segment.toLocal8Bit();
61 local = textCodec->fromUnicode(segment);
64 int old_length = isRawURI ? local.size() - 1 : local.length();
67 return segment.isEmpty() ? QString() : QString(""); // differentiate null and empty
69 // a worst case approximation
70 QChar *new_segment = new QChar[ old_length * 3 + 1 ];
73 for (int i = 0; i < old_length; i++)
75 // 'unsave' and 'reserved' characters
76 // according to RFC 1738,
77 // 2.2. URL Character Encoding Issues (pp. 3-4)
78 // WABA: Added non-ascii
79 unsigned char character = local[i];
80 if ((character <= 32) || (character >= 127) ||
81 strchr(encode_string, character))
83 new_segment[ new_length++ ] = '%';
85 unsigned int c = character / 16;
86 c += (c > 9) ? ('A' - 10) : '0';
87 new_segment[ new_length++ ] = c;
90 c += (c > 9) ? ('A' - 10) : '0';
91 new_segment[ new_length++ ] = c;
95 unsigned char character = local[i];
96 new_segment[ new_length++ ] = character;
100 QString result = QString(new_segment, new_length);
101 delete [] new_segment;
105 QString Bbs::encodeString(const QString &str, int encoding_hint)
107 return encode(str, 1, encoding_hint);