identifier.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
3 Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "identifier.h"
22#include "transliteration_table.h"
23
24using namespace KexiUtils;
25
26bool KexiUtils::isIdentifier(const TQString& s)
27{
28 uint i;
29 for (i=0; i<s.length(); i++) {
30 TQChar c = s.at(i).lower();
31 if (!((int)c=='_' || (int)c>='a' && (int)c<='z' || i>0 && (int)c>='0' && (int)c<='9'))
32 break;
33 }
34 return i>0 && i==s.length();
35}
36
37TQString KexiUtils::string2FileName(const TQString &s)
38{
39 TQString fn( s.simplifyWhiteSpace() );
40 fn.replace(' ',"_"); fn.replace('$',"_");
41 fn.replace('\\',"-"); fn.replace('/',"-");
42 fn.replace(':',"-"); fn.replace('*',"-");
43 return fn;
44}
45
46inline TQString char2Identifier(const TQChar& c)
47{
48 if (c.unicode() >= TRANSLITERATION_TABLE_SIZE)
49 return TQString(TQChar('_'));
50 const char *const s = transliteration_table[c.unicode()];
51 return s ? TQString::fromLatin1(s) : TQString(TQChar('_'));
52}
53
54TQString KexiUtils::string2Identifier(const TQString &s)
55{
56 if (s.isEmpty())
57 return TQString();
58 TQString r, id = s.simplifyWhiteSpace();
59 if (id.isEmpty())
60 return TQString();
61 r.reserve(id.length());
62 id.replace(' ',"_");
63 TQChar c = id[0];
64 TQString add;
65 bool wasUnderscore = false;
66
67 if ((int)c>='0' && (int)c<='9') {
68 r+='_';
69 r+=c;
70 } else {
71 add = char2Identifier(c);
72 r+=add;
73 wasUnderscore = add == "_";
74 }
75
76 for (uint i=1; i<id.length(); i++) {
77 add = char2Identifier(id.at(i));
78 if (wasUnderscore && add == "_")
79 continue;
80 wasUnderscore = add == "_";
81 r+=add;
82 }
83 return r;
84}
85
86//--------------------------------------------------------------------------------
87
88TQString KexiUtils::identifierExpectedMessage(const TQString &valueName, const TQVariant& v)
89{
90 return "<p>"+i18n("Value of \"%1\" column must be an identifier.").arg(valueName)
91 +"</p><p>"+i18n("\"%1\" is not a valid identifier.").arg(v.toString())+"</p>";
92}
93
94//--------------------------------------------------------------------------------
95
96IdentifierValidator::IdentifierValidator(TQObject * parent, const char * name)
97: Validator(parent,name)
98{
99}
100
101IdentifierValidator::~IdentifierValidator()
102{
103}
104
105TQValidator::State IdentifierValidator::validate( TQString& input, int& pos ) const
106{
107 uint i;
108 for (i=0; i<input.length() && input.at(i)==' '; i++)
109 ;
110 pos -= i; //i chars will be removed from beginning
111 if (i<input.length() && input.at(i)>='0' && input.at(i)<='9')
112 pos++; //_ will be added at the beginning
113 bool addspace = (input.right(1)==" ");
115 if (addspace)
116 input += "_";
117 if((uint)pos>input.length())
118 pos=input.length();
119 return input.isEmpty() ? Valid : Acceptable;
120}
121
122Validator::Result IdentifierValidator::internalCheck(
123 const TQString &valueName, const TQVariant& v,
124 TQString &message, TQString & /*details*/)
125{
126 if (isIdentifier(v.toString()))
127 return Validator::Ok;
129 return Validator::Error;
130}
131
virtual State validate(TQString &input, int &pos) const
A validator extending TQValidator with offline-checking for value's validity.
Definition validator.h:46
General Utils.
Definition identifier.h:27
KEXIUTILS_EXPORT TQString string2FileName(const TQString &s)
type * findParent(TQObject *o, const char *className)
KEXIUTILS_EXPORT TQString identifierExpectedMessage(const TQString &valueName, const TQVariant &v)
KEXIUTILS_EXPORT TQString string2Identifier(const TQString &s)
KEXIUTILS_EXPORT bool isIdentifier(const TQString &s)