#include #include #include #include #include #define SCANSIZE 8192 typedef int (*fsearch_cb)(int pos, char *descr, char *cb_data); typedef int (*fsearch_fun)(int startstate, fsearch_cb cb, char * cb_data, char *buf, int buflen); int cb(int pos, char *descr, char *cb_data) { printf("Found [%s] at pos %i\n",descr,(*(size_t *)cb_data)+pos); return 0; } int main(int argc, char **argv) { void *handle; fsearch_fun fsearch; char *error; char *buf; char *p; size_t size; int state; size_t bufofs; struct timeval before,after; double duration; handle = dlopen ("./fsearch.so", RTLD_LAZY); if (!handle) { fprintf (stderr, "%s\n", dlerror()); exit(1); } fsearch = dlsym(handle, "fsearch"); if ((error = dlerror()) != NULL) { fprintf (stderr, "%s\n", error); exit(1); } buf=malloc(SCANSIZE); if (! buf) { perror("malloc"); exit(2); } state=0; bufofs=0; gettimeofday(&before, NULL); while ((state >= 0) && (size=read(0,buf,SCANSIZE))) { state=fsearch(state,cb,(char *)&bufofs,buf,size); bufofs+=size; } gettimeofday(&after, NULL); duration = (double) after.tv_sec-before.tv_sec + (after.tv_usec-before.tv_usec) * 1e-6; printf("\nSearched %i bytes in %.3fs. (Estimated rate : %.1f Mo/s)\n",bufofs,duration, ((double)bufofs)/(1024*1024*duration)); }