kdecore Library API Documentation

kidna.cpp

00001 /* 00002 This file is part of the KDE libraries 00003 00004 Copyright (c) 2003 Waldo Bastian <bastian@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 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; // = 0 00032 static bool KIDNA_lib_load_failed; // = false 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; // = 0 00038 static KIDNA_utf8ace_to_utf8_t KIDNA_utf8ace_to_utf8; // = 0 00039 00040 static void KIDNA_load_lib() 00041 { 00042 KIDNA_lib_load_failed = true; // Unless proven otherwise 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; // Error 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; // Error 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; // Error 00064 } 00065 KIDNA_lib_load_failed = false; // Succes 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; // Can't convert 00092 } 00093 else 00094 { 00095 // Also handle names that start with "." even though libidn 00096 // doesn't like those 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; // Can't convert 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; // Can't convert 00138 } 00139 else 00140 { 00141 // Also handle names that start with "." even though libidn 00142 // doesn't like those 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; // Can't convert 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(); // Return as is 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(); // Return as is. 00183 } 00184 } 00185 }
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 20 09:48:26 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003