File indexing completed on 2024-04-28 04:49:53

0001 /*
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 
0007 #include "k3bvalidators.h"
0008 
0009 #include <QRegExp>
0010 
0011 #include <ctype.h>
0012 
0013 
0014 K3b::CharValidator::CharValidator( QObject* parent )
0015   : QValidator( parent ),
0016     m_replaceChar( '_' )
0017 {
0018 }
0019 
0020 
0021 QValidator::State K3b::CharValidator::validate( QString& s, int& pos ) const
0022 {
0023   Q_UNUSED(pos);
0024 
0025   for( int i = 0; i < s.length(); ++i ) {
0026     State r = validateChar( s[i] );
0027     if( r != Acceptable )
0028       return r;
0029   }
0030 
0031   return Acceptable;
0032 }
0033 
0034 
0035 void K3b::CharValidator::fixup( QString& s ) const
0036 {
0037   for( int i = 0; i < s.length(); ++i ) {
0038     if( validateChar( s[i] ) != Acceptable )
0039       s[i] = m_replaceChar;
0040   }
0041 }
0042 
0043 
0044 K3b::Latin1Validator::Latin1Validator( QObject* parent )
0045   : K3b::CharValidator( parent )
0046 {
0047 }
0048 
0049 
0050 QValidator::State K3b::Latin1Validator::validateChar( const QChar& c ) const
0051 {
0052   if( !c.toLatin1() )
0053     return Invalid;
0054   else
0055     return Acceptable;
0056 }
0057 
0058 
0059 K3b::AsciiValidator::AsciiValidator( QObject* parent )
0060   : K3b::Latin1Validator( parent )
0061 {
0062 }
0063 
0064 
0065 QValidator::State K3b::AsciiValidator::validateChar( const QChar& c ) const
0066 {
0067   if( K3b::Latin1Validator::validateChar( c ) == Invalid )
0068     return Invalid;
0069   else if( !isascii( c.toLatin1() ) )
0070     return Invalid;
0071   else
0072     return Acceptable;
0073 }
0074 
0075 
0076 
0077 K3b::Validator::Validator( QObject* parent )
0078   : QRegularExpressionValidator( parent ),
0079     m_replaceChar('_')
0080 {
0081 }
0082 
0083 
0084 K3b::Validator::Validator( const QRegularExpression& rx, QObject* parent )
0085   : QRegularExpressionValidator( rx, parent ),
0086     m_replaceChar('_')
0087 {
0088 }
0089 
0090 
0091 void K3b::Validator::fixup( QString& input ) const
0092 {
0093   for( int i = 0; i < input.length(); ++i )
0094     if( !regularExpression().match( input.mid(i, 1) ).hasMatch() )
0095       input[i] = m_replaceChar;
0096 }
0097 
0098 
0099 QString K3b::Validators::fixup(const QString& input, const QRegularExpression &rx, const QChar& replaceChar )
0100 {
0101   QString s;
0102   for( int i = 0; i < input.length(); ++i )
0103     if( rx.match( input.mid(i, 1) ).hasMatch() )
0104       s += input[i];
0105     else
0106       s += replaceChar;
0107   return s;
0108 }
0109 
0110 
0111 K3b::Validator* K3b::Validators::isrcValidator( QObject* parent )
0112 {
0113   static const QRegularExpression rx( "^[A-Z\\d]{2,2}-[A-Z\\d]{3,3}-\\d{2,2}-\\d{5,5}$" );
0114   return new K3b::Validator( rx, parent );
0115 }
0116 
0117 
0118 K3b::Validator* K3b::Validators::iso9660Validator( bool allowEmpty, QObject* parent )
0119 {
0120   if( allowEmpty ) {
0121     static const QRegularExpression rx( "[^/]*" );
0122     return new K3b::Validator( rx, parent );
0123   }
0124 
0125   static const QRegularExpression rx( "[^/]+" );
0126   return new K3b::Validator( rx, parent );
0127 }
0128 
0129 
0130 K3b::Validator* K3b::Validators::iso646Validator( int type, bool AllowLowerCase, QObject* parent )
0131 {
0132   QRegularExpression rx;
0133   switch ( type ) {
0134   case Iso646_d:
0135     if ( AllowLowerCase )
0136       rx = QRegularExpression( "[a-zA-Z0-9_]*" );
0137     else
0138       rx = QRegularExpression( "[A-Z0-9_]*" );
0139     break;
0140   case Iso646_a:
0141   default:
0142     if ( AllowLowerCase )
0143       rx = QRegularExpression( "[a-zA-Z0-9!\"\\s%&'\\(\\)\\*\\+,\\-\\./:;<=>\\?_]*" );
0144     else
0145       rx = QRegularExpression( "[A-Z0-9!\"\\s%&'\\(\\)\\*\\+,\\-\\./:;<=>\\?_]*" );
0146     break;
0147   }
0148 
0149   return new K3b::Validator( rx, parent );
0150 }