kidna.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kidna.h"
00023
00024 #include <kdebug.h>
00025
00026 #include "ltdl.h"
00027 #include <stdlib.h>
00028
00029 #define IDNA_SUCCESS 0
00030
00031 static lt_dlhandle KIDNA_lib;
00032 static bool KIDNA_lib_load_failed;
00033
00034 typedef int (*KIDNA_utf8_to_ace_t)(const char *, char **, int);
00035 typedef int (*KIDNA_utf8ace_to_utf8_t)(const char *, char **, int);
00036
00037 static KIDNA_utf8_to_ace_t KIDNA_utf8_to_ace;
00038 static KIDNA_utf8ace_to_utf8_t KIDNA_utf8ace_to_utf8;
00039
00040 static void KIDNA_load_lib()
00041 {
00042 KIDNA_lib_load_failed = true;
00043 KIDNA_lib = lt_dlopen("/usr/local/lib/libidn.la");
00044 if (!KIDNA_lib)
00045 {
00046 KIDNA_lib = lt_dlopen("/usr/lib/libidn.la");
00047 }
00048
00049 if (!KIDNA_lib)
00050 return;
00051
00052 KIDNA_utf8_to_ace = (KIDNA_utf8_to_ace_t) lt_dlsym(KIDNA_lib, "idna_to_ascii_8z");
00053 if (!KIDNA_utf8_to_ace)
00054 {
00055 kdWarning() << "Symbol idna_utf8_to_ace not found." << endl;
00056 return;
00057 }
00058
00059 KIDNA_utf8ace_to_utf8 = (KIDNA_utf8ace_to_utf8_t) lt_dlsym(KIDNA_lib, "idna_to_unicode_8z8z");
00060 if (!KIDNA_utf8ace_to_utf8)
00061 {
00062 kdWarning() << "Symbol idna_utf8ace_to_utf8 not found." << endl;
00063 return;
00064 }
00065 KIDNA_lib_load_failed = false;
00066 }
00067
00068 QCString KIDNA::toAsciiCString(const QString &idna)
00069 {
00070 int l = idna.length();
00071 const QChar *u = idna.unicode();
00072 bool needConversion = false;
00073 for(;l--;)
00074 {
00075 if ((*u++).unicode() > 127)
00076 {
00077 needConversion = true;
00078 break;
00079 }
00080 }
00081 if (!needConversion)
00082 return idna.lower().latin1();
00083
00084 if (!KIDNA_lib && !KIDNA_lib_load_failed)
00085 {
00086 KIDNA_load_lib();
00087 }
00088
00089 if (KIDNA_lib_load_failed)
00090 {
00091 return 0;
00092 }
00093 else
00094 {
00095
00096
00097 bool bStartsWithDot = (idna[0] == '.');
00098 char *pOutput;
00099 if ((*KIDNA_utf8_to_ace)(idna.utf8().data()+(bStartsWithDot ? 1: 0), &pOutput, 0) == IDNA_SUCCESS)
00100 {
00101 QCString result = pOutput;
00102 free(pOutput);
00103 if (bStartsWithDot)
00104 return "."+result;
00105 return result;
00106 }
00107 else
00108 {
00109 return 0;
00110 }
00111 }
00112 }
00113
00114 QString KIDNA::toAscii(const QString &idna)
00115 {
00116 int l = idna.length();
00117 const QChar *u = idna.unicode();
00118 bool needConversion = false;
00119 for(;l--;)
00120 {
00121 if ((*u++).unicode() > 127)
00122 {
00123 needConversion = true;
00124 break;
00125 }
00126 }
00127 if (!needConversion)
00128 return idna.lower();
00129
00130 if (!KIDNA_lib && !KIDNA_lib_load_failed)
00131 {
00132 KIDNA_load_lib();
00133 }
00134
00135 if (KIDNA_lib_load_failed)
00136 {
00137 return QString::null;
00138 }
00139 else
00140 {
00141
00142
00143 bool bStartsWithDot = (idna[0] == '.');
00144 char *pOutput;
00145 if ((*KIDNA_utf8_to_ace)(idna.utf8().data()+(bStartsWithDot ? 1: 0), &pOutput, 0) == IDNA_SUCCESS)
00146 {
00147 QString result(pOutput);
00148 free(pOutput);
00149 if (bStartsWithDot)
00150 return "."+result;
00151 return result;
00152 }
00153 else
00154 {
00155 return QString::null;
00156 }
00157 }
00158 }
00159
00160 QString KIDNA::toUnicode(const QString &idna)
00161 {
00162 if (!KIDNA_lib && !KIDNA_lib_load_failed)
00163 {
00164 KIDNA_load_lib();
00165 }
00166
00167 if (KIDNA_lib_load_failed)
00168 {
00169 return idna.lower();
00170 }
00171 else
00172 {
00173 char *pOutput;
00174 if ((*KIDNA_utf8ace_to_utf8)(idna.utf8(), &pOutput, 0) == IDNA_SUCCESS)
00175 {
00176 QString result = QString::fromUtf8(pOutput);
00177 free(pOutput);
00178 return result;
00179 }
00180 else
00181 {
00182 return idna.lower();
00183 }
00184 }
00185 }
This file is part of the documentation for kdecore Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:43:30 2004 by
doxygen 1.3.6-20040222 written by
Dimitri van Heesch, © 1997-2003