summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3bvalidators.cpp
blob: b8a6e65af6da2c3a0395f5abfcb667229839766f (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 
 *
 * $Id: k3bvalidators.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */


#include "k3bvalidators.h"

#include <ctype.h>


K3bCharValidator::K3bCharValidator( TQObject* tqparent, const char* name )
  : TQValidator( tqparent, name ),
    m_replaceChar( '_' )
{
}


TQValidator::State K3bCharValidator::validate( TQString& s, int& pos ) const
{
  Q_UNUSED(pos);

  for( unsigned int i = 0; i < s.length(); ++i ) {
    State r = validateChar( s[i] );
    if( r != Acceptable )
      return r;
  }

  return Acceptable;
}


void K3bCharValidator::fixup( TQString& s ) const
{
  for( unsigned int i = 0; i < s.length(); ++i ) {
    if( validateChar( s[i] ) != Acceptable )
      s[i] = m_replaceChar;
  }
}


K3bLatin1Validator::K3bLatin1Validator( TQObject* tqparent, const char* name )
  : K3bCharValidator( tqparent, name )
{
}


TQValidator::State K3bLatin1Validator::validateChar( const TQChar& c ) const
{
  if( !c.latin1() )
    return Invalid;
  else
    return Acceptable;
}


K3bAsciiValidator::K3bAsciiValidator( TQObject* tqparent, const char* name )
  : K3bLatin1Validator( tqparent, name )
{
}


TQValidator::State K3bAsciiValidator::validateChar( const TQChar& c ) const
{
  if( K3bLatin1Validator::validateChar( c ) == Invalid )
    return Invalid;
  else if( !isascii( c.latin1() ) )
    return Invalid;
  else
    return Acceptable;
}



K3bValidator::K3bValidator( TQObject* tqparent, const char* name )
  : TQRegExpValidator( tqparent, name ),
    m_replaceChar('_')
{
}


K3bValidator::K3bValidator( const TQRegExp& rx, TQObject* tqparent, const char* name )
  : TQRegExpValidator( rx, tqparent, name ),
    m_replaceChar('_')
{
}


void K3bValidator::fixup( TQString& input ) const
{
  for( unsigned int i = 0; i < input.length(); ++i )
    if( !regExp().exactMatch( input.mid(i, 1) ) )
      input[i] = m_replaceChar;
}


TQString K3bValidators::fixup( const TQString& input, const TQRegExp& rx, const TQChar& replaceChar )
{
  TQString s;
  for( unsigned int i = 0; i < input.length(); ++i )
    if( rx.exactMatch( input.mid(i, 1) ) )
      s += input[i];
    else
      s += replaceChar;
  return s;
}


K3bValidator* K3bValidators::isrcValidator( TQObject* tqparent, const char* name )
{
  return new K3bValidator( TQRegExp("^[A-Z\\d]{2,2}-[A-Z\\d]{3,3}-\\d{2,2}-\\d{5,5}$"), tqparent, name );
}


K3bValidator* K3bValidators::iso9660Validator( bool allowEmpty, TQObject* tqparent, const char* name )
{
  if( allowEmpty )
    return new K3bValidator( TQRegExp( "[^/]*" ), tqparent, name );
  else
    return new K3bValidator( TQRegExp( "[^/]+" ), tqparent, name );
}


K3bValidator* K3bValidators::iso646Validator( int type, bool AllowLowerCase, TQObject* tqparent, const char* name )
{
  TQRegExp rx;
  switch ( type ) {
  case Iso646_d:
    if ( AllowLowerCase )
      rx = TQRegExp( "[a-zA-Z0-9_]*" );
    else
      rx = TQRegExp( "[A-Z0-9_]*" );
    break;
  case Iso646_a:
  default: 
    if ( AllowLowerCase )
      rx = TQRegExp( "[a-zA-Z0-9!\"\\s%&'\\(\\)\\*\\+,\\-\\./:;<=>\\?_]*" );
    else
      rx = TQRegExp( "[A-Z0-9!\"\\s%&'\\(\\)\\*\\+,\\-\\./:;<=>\\?_]*" );
    break;
  }

  return new K3bValidator( rx, tqparent, name );
}