Main Page | Data Structures | Directories | File List | Data Fields | Globals

string.c

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2006  Baris Simsek, http://www.enderunix.org/simsek
00003  *
00004  *  This library is free software; you can redistribute it and/or
00005  *  modify it under the terms of the GNU Lesser General Public
00006  *  License as published by the Free Software Foundation; either
00007  *  version 2.1 of the License, or (at your option) any later version.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public
00015  *  License along with this library; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  $Id: string_8c-source.html,v 1.4 2006/08/26 09:35:02 simsek Exp $
00019  *
00020  *  Library for data structures.
00021  *
00022  */
00023 
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 #include <regex.h>
00029 #include <string.h>
00030 #include <errno.h>
00031 
00032 #include "../include/cupl.h"
00033 #include "../include/string.h"
00034 
00035 static int regex_match_pattern(regex_t *regexp, char *buffer);
00036 
00037 /* Regular expression support. */
00038 static int regex_match_pattern(regex_t *regexp, char *buffer)
00039 {
00040     int start = 0;                      /* The offset from the beginning of the buffer */
00041     size_t no_sub = regexp->re_nsub + 1;            /* How many matches are there in a buffer? */
00042     regmatch_t *result;
00043 
00044     if ((result = (regmatch_t *) malloc(sizeof(regmatch_t) * no_sub)) == 0)
00045     {
00046         fprintf(stderr, "%s: %d: malloc(): %s.\n", __FILE__, __LINE__, strerror(errno));
00047         return -1;
00048     }
00049   
00050     while (regexec(regexp, buffer+start, no_sub, result, 0) == 0)
00051     {
00052         free(result);
00053         return 1;                   /* Found a match. */
00054     }
00055 
00056     free(result);
00057 
00058     return 0;
00059 }
00060 
00061 /* \todo Should be regex library independent.
00062    Should be written a small regex library. */
00063 extern int cupl_match_regex(char *buffer, char *pattern)
00064 {
00065     int ret = 0;
00066     regex_t *regexp; /* Regular expression pointer for the filter. */
00067 
00068     /* Allocate space for the regular expressions. */
00069     if ((regexp = (regex_t *) malloc(sizeof(regex_t))) == NULL) {
00070         fprintf(stderr, "%s: %d: malloc(): %s.\n", __FILE__, __LINE__, strerror(errno));
00071         return -1;
00072     }
00073 
00074     memset(regexp, 0, sizeof(regex_t));
00075 
00076     if ((ret = regcomp(regexp, pattern, 0)) != 0) /* Compile the regular expression. */
00077     {
00078         regfree(regexp);
00079         free(regexp);
00080         return -1;
00081     }
00082 
00083     ret = regex_match_pattern(regexp, buffer);
00084 
00085     regfree(regexp);
00086     free(regexp);
00087 
00088     return ret;
00089 }
00090 
00091 
00092 

Generated on Sat Aug 26 12:34:54 2006 for CUPL (Common UNIX Programming Library) by  doxygen 1.3.9.1