File indexing completed on 2024-03-24 15:43:45

0001 //
0002 // C++ Interface: cpattern
0003 //
0004 // Description: 
0005 //
0006 /*
0007 Copyright 2007-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef CPATTERN_H
0024 #define CPATTERN_H
0025 
0026 #include <QString>
0027 #include <QStringList>
0028 #include <QMap>
0029 #include <QVariant>
0030 #include <kmuddy_export.h>
0031 
0032 /**
0033 This class performs all types of pattern matching.
0034 
0035     @author Tomas Mecir <kmuddy@kmuddy.com>
0036 */
0037 class KMUDDY_EXPORT cPattern {
0038  public:
0039   enum PatternType {
0040     exact = 0,
0041     substring = 1,
0042     begin = 2,
0043     end = 3,
0044     regexp = 4
0045   };
0046 
0047   cPattern (const QString &pattern = QString(), PatternType pt = exact);
0048   ~cPattern ();
0049 
0050   void setPattern (const QString &pattern);
0051   QString pattern () const;
0052 
0053   void setMatching (PatternType pt);
0054   PatternType matching () const;
0055 
0056   void setCaseSensitive (bool cs);
0057   bool caseSensitive () const;
0058 
0059   void setWholeWords (bool ww);
0060   bool wholeWords () const;
0061 
0062   /** perform matching */
0063   bool match (const QString &text, int startpos = 0);
0064 
0065   /** returns last text being successfully compared */
0066   QString getLastText () const;
0067   /** returns last prefix */
0068   QString getPrefix () const;
0069   /** returns last suffix */
0070   QString getSuffix () const;
0071   int getLastPos () const;
0072   int getLastLength () const;
0073   /** returns a list of back-references (if doing regexp compare) */
0074   QStringList getBackRefList () const;
0075   int getBackRefPos (int which) const;
0076 
0077   /** Returns pseudo-variable position. */
0078   void variablePosition (const QString &varname, int *start, int *len);
0079   /** Expands a pseudo-variable; used by expandPseudoVariables() and in test areas */
0080   QString getVariable (const QString &varname, const QString &def = QString()) const;
0081 
0082   /** Expands pseudo-variables in the string. Pseudo-variables are the following:
0083   <br><b>$prefix</b> - trimmed part of string BEFORE the matched part
0084   <br><b>$suffix</b> - trimmed part of string AFTER the matched part
0085   <br><b>$prefixfull</b> - prefix including leading/trailing spaces
0086   <br><b>$suffixfull</b> - suffix including leading/trailing spaces
0087   <br><b>$matched</b> - matched part of the string
0088   <br><b>$1</b>, <b>$2</b>, ... - back-references; regexp expansion only
0089    */
0090   void expandPseudoVariables (QString &string) const;
0091 
0092   /** Return the variables in a form suitable for scripting (cScriptEval). */
0093   QMap<QString, QVariant> scriptVariables ();
0094  protected:
0095   struct Private;
0096   Private *d;
0097 };
0098 
0099 #endif