1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/* 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.at(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.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<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.").arg(valueName)
+"</p><p>"+i18n("\"%1\" is not a valid identifier.").arg(v.toString())+"</p>";
}
//--------------------------------------------------------------------------------
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<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;
}
|