OSDN Git Service

Move the directories
[kita/kita.git] / src / libkita / bbs.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Kita Developers                                 *
3  *   ikemo@users.sourceforge.jp                                            *
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 #include "bbs.h"
11
12 #include <QtCore/QTextCodec>
13
14 using namespace Kita;
15
16 Bbs::Bbs()
17 {
18     m_readCodec = QTextCodec::codecForName("Shift-JIS");
19     m_writeCodec = QTextCodec::codecForName("Shift-JIS");
20 }
21
22 QString Bbs::datToUnicode(const QByteArray& ba) const
23 {
24     return m_readCodec->toUnicode(ba);
25 }
26
27 QByteArray Bbs::datFromUnicode(const QString& str) const
28 {
29     return m_readCodec->fromUnicode(str);
30 }
31
32 QString Bbs::postToUnicode(const QByteArray& ba) const
33 {
34     return m_writeCodec->toUnicode(ba);
35 }
36
37 QByteArray Bbs::postFromUnicode(const QString& str) const
38 {
39     return m_writeCodec->fromUnicode(str);
40 }
41
42 // encoding_offset:
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)
48 {
49   const char *encode_string = "/@<>#\"&?={}|^~[]\'`\\:+%";
50   encode_string += encoding_offset;
51
52   QByteArray local;
53   if (encoding_hint==0)
54     local = segment.toLocal8Bit();
55   else
56   {
57       QTextCodec * textCodec = QTextCodec::codecForMib(encoding_hint);
58       if (!textCodec)
59           local = segment.toLocal8Bit();
60       else
61           local = textCodec->fromUnicode(segment);
62   }
63
64   int old_length = isRawURI ? local.size() - 1 : local.length();
65
66   if (!old_length)
67     return segment.isEmpty() ? QString() : QString(""); // differentiate null and empty
68
69   // a worst case approximation
70   QChar *new_segment = new QChar[ old_length * 3 + 1 ];
71   int new_length = 0;
72
73   for (int i = 0; i < old_length; i++)
74   {
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))
82     {
83       new_segment[ new_length++ ] = '%';
84
85       unsigned int c = character / 16;
86       c += (c > 9) ? ('A' - 10) : '0';
87       new_segment[ new_length++ ] = c;
88
89       c = character % 16;
90       c += (c > 9) ? ('A' - 10) : '0';
91       new_segment[ new_length++ ] = c;
92
93     }
94     else {
95       unsigned char character = local[i];
96       new_segment[ new_length++ ] = character;
97     }
98   }
99
100   QString result = QString(new_segment, new_length);
101   delete [] new_segment;
102   return result;
103 }
104
105 QString Bbs::encodeString(const QString &str, int encoding_hint)
106 {
107    return encode(str, 1, encoding_hint);
108 }