00001 /***************************************************************************
00002 * This file is part of the MagiC++ library. *
00003 * *
00004 * Copyright (C) 1998-2001 Marko Grönroos <magi@iki.fi> *
00005 * *
00006 ***************************************************************************
00007 * *
00008 * This library is free software; you can redistribute it and/or *
00009 * modify it under the terms of the GNU Library General Public *
00010 * License as published by the Free Software Foundation; either *
00011 * version 2 of the License, or (at your option) any later version. *
00012 * *
00013 * This library is distributed in the hope that it will be useful, *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
00016 * Library General Public License for more details. *
00017 * *
00018 * You should have received a copy of the GNU Library General Public *
00019 * License along with this library; see the file COPYING.LIB. If *
00020 * not, write to the Free Software Foundation, Inc., 59 Temple Place *
00021 * - Suite 330, Boston, MA 02111-1307, USA. *
00022 * *
00023 ***************************************************************************/
00024
00025 #include <magic/mobject.h>
00026 #include <magic/mstring.h>
00027 #include <magic/mmagisupp.h>
00028 #include <magic/mpararr.h>
00029 #include <magic/mregexp.h>
00030
00031 // impl_dynamic (RegExp, {Object});
00032
00033
00037
00038 BEGIN_NAMESPACE (MagiC);
00039
00040 RegExp::~RegExp () {
00041 if (regt) {
00042 regfree (regt);
00043 delete regt;
00044 regt=NULL;
00045 }
00046 }
00047
00048 int RegExp::make (const char* expr) {
00049 this->~RegExp ();
00050 regt = new regex_t;
00051
00052 if ((errcode = regcomp (regt, expr, REG_EXTENDED)))
00053 throw invalid_format (format ("%%%%%%RegExp Error:%s\n", (CONSTR) geterror ()));
00054
00055 return errcode;
00056 }
00057
00058 int RegExp::match (const char* str) {
00059 return !(errcode = regexec (regt, str, 0, NULL, REG_NOSUB));
00060 }
00061
00062 int RegExp::match (const String& str, Array<String>& results) {
00063 regmatch_t matches [20];
00064 errcode = regexec (regt, str, 20, matches, 0);
00065 if (errcode) {
00066 if (errcode == REG_NOMATCH)
00067 return 0;
00068 else
00069 throw invalid_format (geterror());
00070 }
00071
00072 int rescnt=0;
00073 for (; rescnt<19; rescnt++)
00074 if (matches[rescnt].rm_so>str.length() || matches[rescnt].rm_eo>str.length() ||
00075 matches[rescnt].rm_so<0 || matches[rescnt].rm_eo<0 ||
00076 matches[rescnt].rm_so > matches[rescnt].rm_eo)
00077 break;
00078
00079 results.resize (rescnt);
00080
00081 for (int i=0; i<rescnt; i++)
00082 results[i] = str.mid (matches[i].rm_so, matches[i].rm_eo-matches[i].rm_so);
00083
00084 return 1;
00085 }
00086
00087 String RegExp::geterror () const {
00088 char errbuf [256];
00089 regerror (errcode, regt, errbuf, 256);
00090 return errbuf;
00091 }
00092
00093 END_NAMESPACE;
1.2.6 written by Dimitri van Heesch,
© 1997-2001