OSDN Git Service

Move the directories
[kita/kita.git] / src / libkita / bbs.cpp
diff --git a/src/libkita/bbs.cpp b/src/libkita/bbs.cpp
new file mode 100644 (file)
index 0000000..7f4e78d
--- /dev/null
@@ -0,0 +1,108 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by Kita Developers                                 *
+ *   ikemo@users.sourceforge.jp                                            *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ ***************************************************************************/
+#include "bbs.h"
+
+#include <QtCore/QTextCodec>
+
+using namespace Kita;
+
+Bbs::Bbs()
+{
+    m_readCodec = QTextCodec::codecForName("Shift-JIS");
+    m_writeCodec = QTextCodec::codecForName("Shift-JIS");
+}
+
+QString Bbs::datToUnicode(const QByteArray& ba) const
+{
+    return m_readCodec->toUnicode(ba);
+}
+
+QByteArray Bbs::datFromUnicode(const QString& str) const
+{
+    return m_readCodec->fromUnicode(str);
+}
+
+QString Bbs::postToUnicode(const QByteArray& ba) const
+{
+    return m_writeCodec->toUnicode(ba);
+}
+
+QByteArray Bbs::postFromUnicode(const QString& str) const
+{
+    return m_writeCodec->fromUnicode(str);
+}
+
+// encoding_offset:
+// 0 encode both @ and /
+// 1 encode @ but not /
+// 2 encode neither @ or /
+QString Bbs::encode(const QString& segment, int encoding_offset,
+        int encoding_hint, bool isRawURI)
+{
+  const char *encode_string = "/@<>#\"&?={}|^~[]\'`\\:+%";
+  encode_string += encoding_offset;
+
+  QByteArray local;
+  if (encoding_hint==0)
+    local = segment.toLocal8Bit();
+  else
+  {
+      QTextCodec * textCodec = QTextCodec::codecForMib(encoding_hint);
+      if (!textCodec)
+          local = segment.toLocal8Bit();
+      else
+          local = textCodec->fromUnicode(segment);
+  }
+
+  int old_length = isRawURI ? local.size() - 1 : local.length();
+
+  if (!old_length)
+    return segment.isEmpty() ? QString() : QString(""); // differentiate null and empty
+
+  // a worst case approximation
+  QChar *new_segment = new QChar[ old_length * 3 + 1 ];
+  int new_length = 0;
+
+  for (int i = 0; i < old_length; i++)
+  {
+    // 'unsave' and 'reserved' characters
+    // according to RFC 1738,
+    // 2.2. URL Character Encoding Issues (pp. 3-4)
+    // WABA: Added non-ascii
+    unsigned char character = local[i];
+    if ((character <= 32) || (character >= 127) ||
+         strchr(encode_string, character))
+    {
+      new_segment[ new_length++ ] = '%';
+
+      unsigned int c = character / 16;
+      c += (c > 9) ? ('A' - 10) : '0';
+      new_segment[ new_length++ ] = c;
+
+      c = character % 16;
+      c += (c > 9) ? ('A' - 10) : '0';
+      new_segment[ new_length++ ] = c;
+
+    }
+    else {
+      unsigned char character = local[i];
+      new_segment[ new_length++ ] = character;
+    }
+  }
+
+  QString result = QString(new_segment, new_length);
+  delete [] new_segment;
+  return result;
+}
+
+QString Bbs::encodeString(const QString &str, int encoding_hint)
+{
+   return encode(str, 1, encoding_hint);
+}