~ubuntu-branches/ubuntu/maverick/datakiosk/maverick

« back to all changes in this revision

Viewing changes to src/datakiosk/src/asciivalidator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-06-27 22:48:06 UTC
  • Revision ID: james.westby@ubuntu.com-20050627224806-8farkci1dc2onhbs
Tags: upstream-0.7
ImportĀ upstreamĀ versionĀ 0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Adam Treat                                      *
 
3
 *   treat@kde.org                                                         *
 
4
 *                                                                         *
 
5
 *   Copyright (C) 2004 by Scott Wheeler                                   *
 
6
 *   wheeler@kde.org                                                       *
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of the GNU General Public License as published by  *
 
10
 *   the Free Software Foundation; either version 2 of the License, or     *
 
11
 *   (at your option) any later version.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
 
 
15
#include "asciivalidator.h"
 
16
 
 
17
#include <qstring.h>
 
18
 
 
19
AsciiValidator::AsciiValidator( QObject * parent, const char *name )
 
20
        : QValidator( parent, name ), functionName( false )
 
21
{}
 
22
 
 
23
AsciiValidator::AsciiValidator( bool funcName, QObject * parent, const char *name )
 
24
        : QValidator( parent, name ), functionName( funcName )
 
25
{}
 
26
 
 
27
AsciiValidator::AsciiValidator( const QString &allow, QObject * parent, const char *name )
 
28
        : QValidator( parent, name ), functionName( false ), allowedChars( allow )
 
29
{}
 
30
 
 
31
AsciiValidator::~AsciiValidator()
 
32
{}
 
33
 
 
34
QValidator::State AsciiValidator::validate( QString &s, int & ) const
 
35
{
 
36
    bool inParen = false;
 
37
    bool outParen = false;
 
38
    if ( !s.isEmpty() && s[ 0 ].row() == 0 && s[ 0 ].cell() >= '0' && s[ 0 ].cell() <= '9' )
 
39
        s[ 0 ] = '_';
 
40
    for ( int i = 0, j = 0; i < ( int ) s.length(); i++ )
 
41
    {
 
42
        uchar r = s[ i ].row();
 
43
        uchar c = s[ i ].cell();
 
44
 
 
45
        if ( outParen )
 
46
        { // check if we have 'const' or 'volatile'
 
47
            static const QString con = " const";
 
48
            static const QString vol = " volatile";
 
49
            QString mid = s.mid( j );
 
50
            if ( !( con.startsWith( mid ) || vol.startsWith( mid ) ) )
 
51
                return QValidator::Invalid;
 
52
        }
 
53
 
 
54
        if ( inParen && c != ')' )
 
55
            continue;
 
56
 
 
57
        if ( r == 0 && ( ( c >= '0' && c <= '9' ) ||
 
58
                         ( c >= 'a' && c <= 'z' ) ||
 
59
                         ( c >= 'A' && c <= 'Z' ) ) )
 
60
            continue;
 
61
 
 
62
        if ( functionName )
 
63
        {
 
64
            if ( c == '(' )
 
65
            {
 
66
                inParen = true;
 
67
                continue;
 
68
            }
 
69
            if ( c == ')' )
 
70
            {
 
71
                outParen = true;
 
72
                j = i + 1;
 
73
                continue;
 
74
            }
 
75
        }
 
76
 
 
77
        if ( allowedChars.find( s[ i ] ) != -1 )
 
78
            continue;
 
79
 
 
80
        s[ i ] = '_';
 
81
    }
 
82
    return QValidator::Acceptable;
 
83
}
 
84
 
 
85
#include "asciivalidator.moc"