00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "regexptestdlg.h"
00013
00014
#include <sys/types.h>
00015
#include <string.h>
00016
#include <stdlib.h>
00017
#include <regex.h>
00018
#include <qlabel.h>
00019
#include <qlineedit.h>
00020
#include <qlistview.h>
00021
#include <qradiobutton.h>
00022
#include <qpushbutton.h>
00023
#include <qregexp.h>
00024
#include <kdeversion.h>
00025
#include <kregexp.h>
00026
#include <kdebug.h>
00027
#include <kglobalsettings.h>
00028
#include <klocale.h>
00029
#include <kparts/part.h>
00030
#include <ktexteditor/viewcursorinterface.h>
00031
#include <ktexteditor/editinterface.h>
00032
#include <kparts/componentfactory.h>
00033
#include <kregexpeditorinterface.h>
00034
00035
#include "kdevplugin.h"
00036
#include "kdevpartcontroller.h"
00037
00038
00039 RegexpTestDialog::RegexpTestDialog(
KDevPlugin *part)
00040 :
RegexpTestDialogBase(0, "regexp test dialog", false), _regexp_dialog(0)
00041 {
00042 pattern_edit->setFocus();
00043 pattern_edit->setFont(KGlobalSettings::fixedFont());
00044 teststring_edit->setFont(KGlobalSettings::fixedFont());
00045 subgroups_listview->setSorting(0);
00046
00047
m_part = part;
00048 }
00049
00050
00051 RegexpTestDialog::~RegexpTestDialog()
00052 {}
00053
00054
00055 void RegexpTestDialog::showEvent(
QShowEvent *)
00056 {
00057
KParts::ReadWritePart *rwpart = dynamic_cast<KParts::ReadWritePart*>
00058 (
m_part->
partController()->
activePart());
00059 insertbutton->setEnabled(rwpart);
00060 }
00061
00062
00063 void RegexpTestDialog::somethingChanged()
00064 {
00065 success_label->clear();
00066 subgroups_listview->clear();
00067
00068
if ( qregexp_button->isChecked() || qregexp_min_button->isChecked() )
00069
checkQRegExp();
00070
else if ( kregexp_button->isChecked() )
00071
checkKRegExp();
00072
else
00073
checkPOSIX();
00074 }
00075
00076 void RegexpTestDialog::checkQRegExp()
00077 {
00078
QRegExp rx( pattern_edit->text() );
00079 rx.setMinimal( qregexp_min_button->isChecked() );
00080
if ( !rx.isValid() ) {
00081
#if QT_VERSION >= 0x030100
00082
success_label->setText( rx.errorString() );
00083
#else
00084
success_label->setText( i18n(
"Error compiling the regular expression.") );
00085
#endif
00086
return;
00087 }
00088
if ( rx.search( teststring_edit->text() ) < 0 ) {
00089 success_label->setText( i18n(
"No match" ) );
00090
return;
00091 }
00092 success_label->setText( i18n(
"Successfully matched") );
00093
#if QT_VERSION >= 0x030100
00094
int numCaptures = rx.numCaptures() + 1;
00095
#else
00096
int numCaptures = 10;
00097
#endif
00098
for (
int i = 0; i < numCaptures; ++i ) {
00099
new QListViewItem( subgroups_listview, QString::number( i ), rx.cap( i ) );
00100 }
00101 }
00102
00103 void RegexpTestDialog::checkKRegExp()
00104 {
00105
KRegExp rx;
00106
if ( !rx.
compile( pattern_edit->text().latin1() ) ) {
00107 success_label->setText( i18n(
"Compile error, your regexp is invalid" ) );
00108
return;
00109 }
00110
if ( !rx.
match( teststring_edit->text().latin1() ) ) {
00111 success_label->setText( i18n(
"No match" ) );
00112
return;
00113 }
00114 success_label->setText( i18n(
"Successfully matched") );
00115
for (
int i = 0; i <= 9; ++i ) {
00116
const char* grp = rx.
group( i );
00117
if ( grp )
00118
new QListViewItem( subgroups_listview, QString::number( i ),
QString( grp ) );
00119 }
00120 }
00121
00122 void RegexpTestDialog::checkPOSIX()
00123 {
00124 regex_t compiledPattern;
00125 regmatch_t matches[20];
00126
int cflags = extendedposix_button->isChecked()? REG_EXTENDED : 0;
00127
QCString regexp = pattern_edit->text().local8Bit();
00128
int res = regcomp(&compiledPattern, regexp, cflags);
00129
if (res != 0) {
00130
QString regcompMessage;
00131
switch (res)
00132 {
00133
case REG_BADRPT:
00134 regcompMessage = i18n(
"Repetition operators must not appear as first character");
00135
break;
00136
case REG_BADBR:
00137 regcompMessage = i18n(
"Invalid use of back reference operator");
00138
break;
00139
case REG_EBRACE:
00140 regcompMessage = i18n(
"Unmatched brace interval operators");
00141
break;
00142
case REG_EBRACK:
00143 regcompMessage = i18n(
"Unmatched bracket list operators");
00144
break;
00145
case REG_ERANGE:
00146 regcompMessage = i18n(
"Invalid use of range operator");
00147
break;
00148
case REG_ECTYPE:
00149 regcompMessage = i18n(
"Unknown character class");
00150
break;
00151
case REG_ECOLLATE:
00152 regcompMessage = i18n(
"Invalid collating element");
00153
break;
00154
case REG_EPAREN:
00155 regcompMessage = i18n(
"Unmatched parenthesis group operators");
00156
break;
00157
case REG_ESUBREG:
00158 regcompMessage = i18n(
"Invalid back reference to subexpression");
00159
break;
00160
case REG_EESCAPE:
00161 regcompMessage = i18n(
"Trailing backslash");
00162
break;
00163
case REG_BADPAT:
00164 regcompMessage = i18n(
"Invalid use of pattern operators");
00165
break;
00166
#ifdef REG_ESIZE
00167
case REG_ESIZE:
00168 regcompMessage = i18n(
"Regular expression too large");
00169
break;
00170
#endif
00171
default:
00172 regcompMessage = i18n(
"Unknown error");
00173
break;
00174 }
00175 success_label->setText(regcompMessage);
00176
return;
00177 }
00178
00179
for (
int i = 0; i < 20; ++i) {
00180 matches[i].rm_so = -1;
00181 matches[i].rm_eo = -1;
00182 }
00183
00184
QCString testString = teststring_edit->text().local8Bit();
00185 res = regexec(&compiledPattern, testString, 20, matches, 0);
00186
if (res != 0) {
00187 success_label->setText(i18n(
"No match"));
00188
return;
00189 }
00190
00191 success_label->setText(i18n(
"Successfully matched"));
00192
int len = testString.length();
00193
for (
int i = 0; i < 20; ++i) {
00194
if (matches[i].rm_so >= 0 && matches[i].rm_so <=
len &&
00195 matches[i].rm_eo >= 0 && matches[i].rm_eo <=
len &&
00196 matches[i].rm_so <= matches[i].rm_eo) {
00197
QCString subGroup = testString.mid(matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
00198
new QListViewItem(subgroups_listview, QString::number(i), subGroup);
00199 }
00200 }
00201 regfree(&compiledPattern);
00202 }
00203
00204
00205 void RegexpTestDialog::insertQuoted()
00206 {
00207
QString rawstr = pattern_edit->text();
00208
00209
QString str;
00210
00211
int len = rawstr.length();
00212
for (
int i=0; i <
len; ++i) {
00213
QChar ch = rawstr[i];
00214
if (ch ==
'"')
00215 str +=
"\\\"";
00216
else if (ch ==
'\\')
00217 str +=
"\\\\";
00218
else
00219 str += ch;
00220 }
00221
00222
KParts::ReadWritePart *rwpart
00223 = dynamic_cast<KParts::ReadWritePart*>(
m_part->
partController()->
activePart());
00224
QWidget *view =
m_part->
partController()->
activeWidget();
00225
00226
KTextEditor::EditInterface *editiface
00227 = dynamic_cast<KTextEditor::EditInterface*>(rwpart);
00228
if (!editiface) {
00229
kdDebug() <<
"no edit" <<
endl;
00230
return;
00231 }
00232
KTextEditor::ViewCursorInterface *cursoriface
00233 = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
00234
if (!cursoriface) {
00235
kdDebug() <<
"no viewcursor" <<
endl;
00236
return;
00237 }
00238
00239 uint line, col;
00240 cursoriface->
cursorPositionReal(&line, &col);
00241 editiface->
insertText(line, col, str);
00242 reject();
00243 }
00244
00245 void RegexpTestDialog::showRegExpEditor( )
00246 {
00247
_regexp_dialog = KParts::ComponentFactory::createInstanceFromQuery<QDialog>(
"KRegExpEditor/KRegExpEditor" );
00248
00249
if (
_regexp_dialog )
00250 {
00251
KRegExpEditorInterface *editor =
00252 static_cast<KRegExpEditorInterface *>(
_regexp_dialog->qt_cast(
"KRegExpEditorInterface" ) );
00253
00254 editor->
setRegExp( pattern_edit->text() );
00255
00256
if (
_regexp_dialog->exec() == QDialog::Accepted )
00257 {
00258 pattern_edit->setText( editor->
regExp() );
00259 }
00260 }
00261 }
00262
00263
#include "regexptestdlg.moc"