kexi
identifier.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
00111 if (i<input.length() && input.at(i)>='0' && input.at(i)<='9')
00112 pos++;
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 & )
00125 {
00126 if (isIdentifier(v.toString()))
00127 return Validator::Ok;
00128 message = identifierExpectedMessage(valueName, v);
00129 return Validator::Error;
00130 }
00131
|