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 <idna.h>
00027 #include <stdlib.h>
00028 
00029 #define IDNA_SUCCESS 0
00030 
00031 QCString KIDNA::toAsciiCString(const QString &idna)
00032 {
00033    int l = idna.length();
00034    const QChar *u = idna.unicode();
00035    bool needConversion = false;
00036    for(;l--;)
00037    {
00038       if ((*u++).unicode() > 127)
00039       {
00040           needConversion = true;
00041           break;
00042       }
00043    }
00044    if (!needConversion)
00045       return idna.lower().latin1();
00046 
00047    // Also handle names that start with "." even though libidn
00048    // doesn't like those
00049    bool bStartsWithDot = (idna[0] == '.');
00050       char *pOutput;
00051       if (idna_to_ascii_8z(idna.utf8().data()+(bStartsWithDot ? 1: 0), &pOutput, 0) == IDNA_SUCCESS)
00052       {
00053          QCString result = pOutput;
00054          free(pOutput);
00055          if (bStartsWithDot)
00056             return "."+result;
00057          return result;
00058       }
00059       else
00060       {
00061          return 0; // Can't convert
00062       }
00063 }
00064 
00065 QString KIDNA::toAscii(const QString &idna)
00066 {
00067    int l = idna.length();
00068    const QChar *u = idna.unicode();
00069    bool needConversion = false;
00070    for(;l--;)
00071    {
00072       if ((*u++).unicode() > 127)
00073       {
00074           needConversion = true;
00075           break;
00076       }
00077    }
00078    if (!needConversion)
00079       return idna.lower();
00080 
00081       // Also handle names that start with "." even though libidn
00082       // doesn't like those
00083       bool bStartsWithDot = (idna[0] == '.');
00084       char *pOutput;
00085       if (idna_to_ascii_8z(idna.utf8().data()+(bStartsWithDot ? 1: 0), &pOutput, 0) == IDNA_SUCCESS)
00086       {
00087          QString result(pOutput);
00088          free(pOutput);
00089          if (bStartsWithDot)
00090             return "."+result;
00091          return result;
00092       }
00093       else
00094       {
00095          return QString::null; // Can't convert
00096       }
00097 }
00098 
00099 QString KIDNA::toUnicode(const QString &idna)
00100 {
00101       char *pOutput;
00102       if (idna_to_unicode_8z8z(idna.utf8(), &pOutput, 0) == IDNA_SUCCESS)
00103       {
00104          QString result = QString::fromUtf8(pOutput);
00105          free(pOutput);
00106          return result;
00107       }
00108       else
00109       {
00110          return idna.lower(); // Return as is.
00111       }
00112 }
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 Mon Aug 9 13:53:57 2004 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003