kexi

identifier.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
00003    Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this program; see the file COPYING.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "identifier.h"
00022 #include "transliteration_table.h"
00023 
00024 using namespace KexiUtils;
00025 
00026 bool KexiUtils::isIdentifier(const QString& s)
00027 {
00028     uint i;
00029     for (i=0; i<s.length(); i++) {
00030         QChar c = s.at(i).lower();
00031         if (!(c=='_' || c>='a' && c<='z' || i>0 && c>='0' && c<='9'))
00032             break;
00033     }
00034     return i>0 && i==s.length();
00035 }
00036 
00037 QString KexiUtils::string2FileName(const QString &s)
00038 {
00039     QString fn( s.simplifyWhiteSpace() );
00040     fn.replace(' ',"_"); fn.replace('$',"_");
00041     fn.replace('\\',"-"); fn.replace('/',"-"); 
00042     fn.replace(':',"-"); fn.replace('*',"-"); 
00043     return fn;
00044 }
00045 
00046 inline QString char2Identifier(const QChar& c)
00047 {
00048     if (c.unicode() >= TRANSLITERATION_TABLE_SIZE)
00049         return QString(QChar('_'));
00050     const char *const s = transliteration_table[c.unicode()];
00051     return s ? QString::fromLatin1(s) : QString(QChar('_'));
00052 }
00053 
00054 QString KexiUtils::string2Identifier(const QString &s)
00055 {
00056     if (s.isEmpty())
00057         return QString::null;
00058     QString r, id = s.simplifyWhiteSpace();
00059     if (id.isEmpty())
00060         return QString::null;
00061     r.reserve(id.length());
00062     id.replace(' ',"_");
00063     QChar c = id[0];
00064     QString add;
00065     bool wasUnderscore = false;
00066 
00067     if (c>='0' && c<='9') {
00068         r+='_';
00069         r+=c;
00070     } else {
00071         add = char2Identifier(c);
00072         r+=add;
00073         wasUnderscore = add == "_";
00074     }
00075 
00076     for (uint i=1; i<id.length(); i++) {
00077         add = char2Identifier(id.at(i));
00078         if (wasUnderscore && add == "_")
00079             continue;
00080         wasUnderscore = add == "_";
00081         r+=add;
00082     }
00083     return r;
00084 }
00085 
00086 //--------------------------------------------------------------------------------
00087 
00088 QString KexiUtils::identifierExpectedMessage(const QString &valueName, const QVariant& v)
00089 {
00090     return "<p>"+i18n("Value of \"%1\" column must be an identifier.").arg(valueName)
00091         +"</p><p>"+i18n("\"%1\" is not a valid identifier.").arg(v.toString())+"</p>";
00092 }
00093 
00094 //--------------------------------------------------------------------------------
00095 
00096 IdentifierValidator::IdentifierValidator(QObject * parent, const char * name)
00097 : Validator(parent,name)
00098 {
00099 }
00100 
00101 IdentifierValidator::~IdentifierValidator()
00102 {
00103 }
00104 
00105 QValidator::State IdentifierValidator::validate( QString& input, int& pos ) const
00106 {
00107     uint i;
00108     for (i=0; i<input.length() && input.at(i)==' '; i++)
00109         ;
00110     pos -= i; //i chars will be removed from beginning
00111     if (i<input.length() && input.at(i)>='0' && input.at(i)<='9')
00112         pos++; //_ will be added at the beginning
00113     bool addspace = (input.right(1)==" ");
00114     input = string2Identifier(input);
00115     if (addspace)
00116         input += "_";
00117     if((uint)pos>input.length())
00118         pos=input.length();
00119     return input.isEmpty() ? Valid : Acceptable;
00120 }
00121 
00122 Validator::Result IdentifierValidator::internalCheck(
00123     const QString &valueName, const QVariant& v, 
00124     QString &message, QString & /*details*/)
00125 {
00126     if (isIdentifier(v.toString()))
00127         return Validator::Ok;
00128     message = identifierExpectedMessage(valueName, v);
00129     return Validator::Error;
00130 }
00131 
KDE Home | KDE Accessibility Home | Description of Access Keys