00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <time.h>
00016 #include "regex.h"
00017
00018 static FILE *infile = NULL;
00019
00020 static int prompt(char *prompt,char *data,size_t len,char *help)
00021 {char *p;
00022 while(1)
00023 {if (!infile) printf("%s: ",prompt);
00024 if (!fgets(data,len,(infile) ? infile : stdin)) return(0);
00025 if ((p = strchr(data,'\n'))) *p = 0;
00026 if ((*data == 0) || (*data == '#')) continue;
00027 if (infile) printf("%s: %s\n",prompt,data);
00028 if (strcmp(data,"quit") == 0) return(0);
00029 if (strcmp(data,"?") == 0)
00030 printf("%s\n",help);
00031 else
00032 return(1);}}
00033
00034 static double myclock(void)
00035 {return(((double)clock()) / ((double) CLOCKS_PER_SEC));}
00036
00037 int main(int argc,char **argv)
00038 {char pattern[128],line[128],errbuff[128];
00039 int error,n,j,k,nloop,acc_loop = 0;
00040 double before_t,after_t,diff_t,acc_t = 0;
00041 regex_t *r; regmatch_t *m;
00042 printf("Copyright 1997 by George J. Carrette.\n");
00043 printf("Regex test driver. For more info see:\n");
00044 printf("http://people.delphi.com/gjc/winregex.html\n");
00045 if ((argc > 1) && (argv[1][0]))
00046 {if (!(infile = fopen(argv[1],"r")))
00047 {perror(argv[1]);
00048 return(1);}}
00049 r = (regex_t *) malloc(sizeof(regex_t));
00050 if (prompt("nloop",pattern,sizeof(pattern),"default 1"))
00051 nloop = atol(pattern);
00052 if (nloop <= 0) nloop = 1;
00053 while(prompt("Pattern",pattern,sizeof(pattern),
00054 "quit, or try ^([0-9]+)(\\-| |$)(.*)$"))
00055 {memset(r,0,sizeof(regex_t));
00056 if ((error = regcomp(r,pattern,REG_EXTENDED)))
00057 {regerror(error,r,errbuff,sizeof(errbuff));
00058 printf("regcomp: %s\n",errbuff);}
00059 else
00060 {printf("Compiled with %d nsub\n",r->re_nsub);
00061 n = r->re_nsub + 1;
00062 m = (regmatch_t *) malloc(sizeof(regmatch_t) * n);
00063 while(prompt("Data",line,sizeof(line),pattern))
00064 {before_t = myclock();
00065 for(k=0;k<nloop;++k) error = regexec(r,line,n,m,0);
00066 after_t = myclock();
00067 diff_t = after_t - before_t;
00068 acc_loop += nloop;
00069 acc_t += diff_t;
00070 printf("%d loops, %.3f seconds, %.1f micro-seconds per loop\n",
00071 nloop,diff_t,
00072 diff_t * 1000000 / nloop);
00073 if (error)
00074 {regerror(error,r,errbuff,sizeof(errbuff));
00075 printf("regexec: %s\n",errbuff);}
00076 else
00077 for(j=0;j<n;++j)
00078 printf("%d[%d,%d] = %.*s\n",
00079 j,m[j].rm_so,m[j].rm_eo,
00080 (m[j].rm_so >= 0)
00081 ? (m[j].rm_eo - m[j].rm_so) : 0,
00082 (m[j].rm_so >= 0)
00083 ? &line[m[j].rm_so] : "");}
00084 free(m);
00085 regfree(r);}}
00086 free(r);
00087 if (infile) fclose(infile);
00088 if (acc_loop)
00089 printf("%d total loops. %.1f seconds, %.1f micro-seconds per loop\n",
00090 acc_loop,acc_t, acc_t * 100000 / acc_loop);
00091 return(0);}
00092
00093