changeset 3:fe4b316b4386

rcg.c: working multiple regexec matches; couple of error handling functions
author paulo@twcdns.fastsearch.net
date Mon, 05 Apr 2010 22:37:37 -0700
parents 4efb50ce4c9c
children 99770a6b967d
files rcg.c
diffstat 1 files changed, 37 insertions(+), 18 deletions(-) [+]
line diff
     1.1 --- a/rcg.c	Mon Apr 05 21:02:51 2010 -0700
     1.2 +++ b/rcg.c	Mon Apr 05 22:37:37 2010 -0700
     1.3 @@ -10,52 +10,71 @@
     1.4  const char *CLR_START = "-CLR_START-";
     1.5  const char *CLR_END = "-CLR_END-";
     1.6  
     1.7 +int re_error(int errcode, const regex_t *re)
     1.8 +{
     1.9 +	char *err_string = calloc(BUFSIZE, sizeof(char));
    1.10 +	regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
    1.11 +	fprintf(stderr, "%s \n", err_string);
    1.12 +	free(err_string);
    1.13 +	return errcode;
    1.14 +}
    1.15 +
    1.16 +int realloc_error()
    1.17 +{
    1.18 +	fprintf(stderr, "realloc() failure \n");
    1.19 +	return 1;
    1.20 +}
    1.21 +
    1.22  int main(int argc, char **argv)
    1.23  {
    1.24  	char *buf = calloc(BUFSIZE, sizeof(char));
    1.25  
    1.26 +	int re_err;
    1.27  	regex_t *re = calloc(1, sizeof(regex_t));
    1.28 -	regcomp(re, "Hello", REG_EXTENDED);
    1.29 +	re_err = regcomp(re, "Hello", REG_EXTENDED);
    1.30 +	if (re_err != 0)
    1.31 +		exit(re_error(re_err, re));
    1.32  
    1.33  	regmatch_t *rem = calloc(1, sizeof(regmatch_t));
    1.34  
    1.35  	size_t CLR_START_len = strlen(CLR_START);
    1.36  	size_t CLR_END_len = strlen(CLR_END);
    1.37  
    1.38 -	char *out = NULL;
    1.39 -	unsigned int out_pos = 0;
    1.40 -	unsigned int buf_pos = 0;
    1.41  
    1.42  	while (fgets(buf, BUFSIZE, stdin))
    1.43  	{
    1.44 -		int i = 0;
    1.45  		int so = -1;
    1.46  		int eo = -1;
    1.47  
    1.48 -		do
    1.49 +		size_t out_len = strlen(buf);
    1.50 +		char *out = calloc(out_len, sizeof(char));
    1.51 +		unsigned int out_pos = 0;
    1.52 +		unsigned int buf_pos = 0;
    1.53 +
    1.54 +		while (re_err != REG_NOMATCH)
    1.55  		{
    1.56 -			regexec(re, &buf[buf_pos], 1, rem, 0);
    1.57 +			re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
    1.58  
    1.59 -			size_t out_len = strlen(&buf[buf_pos]);
    1.60 +			//fprintf(stderr, "%s", &buf[buf_pos]);		//d// 20100405 PBA
    1.61 +
    1.62  			if (out_len >= (BUFSIZE - buf_pos))
    1.63  				out_len = (BUFSIZE - buf_pos) - 1;
    1.64  
    1.65  			so = rem[0].rm_so;
    1.66  			eo = rem[0].rm_eo;
    1.67  
    1.68 -			fprintf(stderr, "%d %d \n", so, eo);		//d// 20100327 PBA
    1.69 +			//fprintf(stderr, "%d %d \n", so, eo);		//d// 20100327 PBA
    1.70  
    1.71 -			if (so >= 0 && eo >= 0)
    1.72 +			if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
    1.73  			{
    1.74  				int match_len = eo - so;
    1.75  				out_len += CLR_START_len + CLR_END_len;
    1.76  				out = (char *)realloc(out, out_len*sizeof(char));
    1.77 -				if (i++ == 0)
    1.78 -				{
    1.79 -					strncpy(out, buf, so);
    1.80 -					out_pos += so;
    1.81 -					buf_pos += so;
    1.82 -				}
    1.83 +				if (!out)
    1.84 +					exit(realloc_error());
    1.85 +				strncpy(&out[out_pos], &buf[buf_pos], so);
    1.86 +				out_pos += so;
    1.87 +				buf_pos += so;
    1.88  				strncpy(&out[out_pos], CLR_START, CLR_START_len);
    1.89  				out_pos += CLR_START_len;
    1.90  				strncpy(&out[out_pos], &buf[buf_pos], match_len);
    1.91 @@ -66,11 +85,11 @@
    1.92  				buf_pos += match_len;
    1.93  			}
    1.94  			else
    1.95 -				strncpy(&out[out_pos], &buf[eo], out_len - out_pos);
    1.96 +				strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
    1.97  		}
    1.98 -		while (so >= 0 && eo >= 0);
    1.99  
   1.100  		fputs(out, stdout);
   1.101 +		free(out);
   1.102  	}
   1.103  
   1.104  	return 0;