You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
koffice/kexi/kexiutils/identifier.cpp

132 lines
3.7 KiB

/* This file is part of the KDE project
Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
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<s.length(); i++) {
TQChar c = s.tqat(i).lower();
if (!((int)c=='_' || (int)c>='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.tqunicode() >= TRANSLITERATION_TABLE_SIZE)
return TQString(TQChar('_'));
const char *const s = transliteration_table[c.tqunicode()];
return s ? TQString::tqfromLatin1(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<id.length(); i++) {
add = char2Identifier(id.at(i));
if (wasUnderscore && add == "_")
continue;
wasUnderscore = add == "_";
r+=add;
}
return r;
}
//--------------------------------------------------------------------------------
TQString KexiUtils::identifierExpectedMessage(const TQString &valueName, const TQVariant& v)
{
return "<p>"+i18n("Value of \"%1\" column must be an identifier.").tqarg(valueName)
+"</p><p>"+i18n("\"%1\" is not a valid identifier.").tqarg(v.toString())+"</p>";
}
//--------------------------------------------------------------------------------
IdentifierValidator::IdentifierValidator(TQObject * tqparent, const char * name)
: Validator(tqparent,name)
{
}
IdentifierValidator::~IdentifierValidator()
{
}
TQValidator::State IdentifierValidator::validate( TQString& input, int& pos ) const
{
uint i;
for (i=0; i<input.length() && input.at(i)==' '; i++)
;
pos -= i; //i chars will be removed from beginning
if (i<input.length() && input.at(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;
}