/* This file is part of the KDE project
Copyright (C) 2003-2005 Jaroslaw Staniek
Copyright (C) 2005 Martin Ellis
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "identifier.h"
#include "transliteration_table.h"
using namespace KexiUtils;
bool KexiUtils::isIdentifier(const TQString& s)
{
uint i;
for (i=0; i='a' && (int)c<='z' || i>0 && (int)c>='0' && (int)c<='9'))
break;
}
return i>0 && i==s.length();
}
TQString KexiUtils::string2FileName(const TQString &s)
{
TQString fn( s.simplifyWhiteSpace() );
fn.replace(' ',"_"); fn.replace('$',"_");
fn.replace('\\',"-"); fn.replace('/',"-");
fn.replace(':',"-"); fn.replace('*',"-");
return fn;
}
inline TQString char2Identifier(const TQChar& c)
{
if (c.unicode() >= TRANSLITERATION_TABLE_SIZE)
return TQString(TQChar('_'));
const char *const s = transliteration_table[c.unicode()];
return s ? TQString::fromLatin1(s) : TQString(TQChar('_'));
}
TQString KexiUtils::string2Identifier(const TQString &s)
{
if (s.isEmpty())
return TQString();
TQString r, id = s.simplifyWhiteSpace();
if (id.isEmpty())
return TQString();
r.reserve(id.length());
id.replace(' ',"_");
TQChar c = id[0];
TQString add;
bool wasUnderscore = false;
if ((int)c>='0' && (int)c<='9') {
r+='_';
r+=c;
} else {
add = char2Identifier(c);
r+=add;
wasUnderscore = add == "_";
}
for (uint i=1; i"+i18n("Value of \"%1\" column must be an identifier.").arg(valueName)
+"
"+i18n("\"%1\" is not a valid identifier.").arg(v.toString())+"
";
}
//--------------------------------------------------------------------------------
IdentifierValidator::IdentifierValidator(TQObject * parent, const char * name)
: Validator(parent,name)
{
}
IdentifierValidator::~IdentifierValidator()
{
}
TQValidator::State IdentifierValidator::validate( TQString& input, int& pos ) const
{
uint i;
for (i=0; i='0' && input.at(i)<='9')
pos++; //_ will be added at the beginning
bool addspace = (input.right(1)==" ");
input = string2Identifier(input);
if (addspace)
input += "_";
if((uint)pos>input.length())
pos=input.length();
return input.isEmpty() ? Valid : Acceptable;
}
Validator::Result IdentifierValidator::internalCheck(
const TQString &valueName, const TQVariant& v,
TQString &message, TQString & /*details*/)
{
if (isIdentifier(v.toString()))
return Validator::Ok;
message = identifierExpectedMessage(valueName, v);
return Validator::Error;
}