Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <getopt.h>
- char *readIntoBuffer(FILE *fp);
- FILE * openfile(char *file, int n);
- void printFile(char *f,int n);
- void seekWord(char *w,char *sword,
- FILE *storage);
- void usage(char *argv[]);
- void feedback(int c,int flag1,int flag2,
- char * file, char * searchword,
- int optopt,char * optarg);
- void chopToChars(char *source,char *searchwd,
- FILE *resultptr);
- int main(int argc, char **argv){
- int c,index,flag1,flag2,flag3,flag4,flag5;
- char *file,*searchword;
- opterr=0;
- /*
- getopt options
- placing the colon : to the right
- of the letter in string_options
- indecates required argument,
- using two :: is optional argument
- */
- static char string_options[]=
- "xpc::f:s:";
- /* longname
- 0 no_argument
- 1 required_argument
- 2 optional_argument
- */
- static struct option long_options[]=
- {
- {"filename",required_argument,NULL,'f'},
- {"searchword",required_argument,NULL,'s'},
- {"print",no_argument,NULL,'p'},/*result file*/
- {"printsource",no_argument,0,'x'}, /* print source file */
- {"chop",0,0,'c'},/* search char by char */
- {0,0,0,0}
- // has proper formatting
- // {"f",1,0,100}, /* filename */
- // {"s",1,0,101}, /*search word */
- // {"p",0,0,102}, /*print result file */
- // {0,0,0,0}
- };
- while((c=getopt_long(argc,argv,string_options,
- long_options,&index))!=-1)
- {
- switch(c)
- {
- case 'p':
- flag3=1;
- break;
- case 'f': /* requiers argument so
- if its missing or
- another option - */
- if( (optarg[0] == '-' ) ||
- optarg == NULL){
- usage(argv);
- }
- file=strdup(optarg);
- flag1=1;
- break;
- case 's':
- if( (optarg[0] == '-') ||
- optarg == NULL){
- usage(argv);
- }
- searchword=strdup(optarg);
- flag2=1;
- break;
- case 'x':
- flag4=1;
- break;
- case 'c':
- flag5=1;
- break;
- case ':':
- printf("optopt= %c optarg= %s\n",
- optopt,optarg);
- usage(argv);
- break;
- case '?':
- if(optopt == 'f' ||
- optopt == 's'){
- fprintf(stderr, "Option -%c requires an "
- "argument.\n",optopt);
- usage(argv);
- exit(1);
- }
- else if (isprint(optopt))
- fprintf(stderr,"Unknown option -%c"
- "\n",optopt);
- else
- fprintf(stderr,"Unknown option "
- "character `\\x%x`.\n",
- optopt);
- break;
- default:
- break;
- } // end switch
- }//end while
- //feedback(c,flag1,flag2,file,searchword,optopt,optarg);
- if( !flag1 || !flag2 )
- usage(argv);
- /* source file */
- FILE *fptr = openfile(file,1);
- /* file for, if search word match
- write search word to file
- */
- FILE *foundptr = openfile("found.txt",2);
- /* source file */
- char *s=readIntoBuffer(fptr);
- if(flag4)
- /* prints source file -x */
- printFile(s,1);
- chopToChars(s,searchword,foundptr);
- //seekWord(s,searchword,foundptr);
- /*check found words read from file */
- char *fw=readIntoBuffer(foundptr);
- if(flag3)
- /* prints results file -p */
- printFile(fw,2);
- /* search char by char*/
- //if(flag5)
- //chopToChars(s,searchword,foundptr);
- free(s);
- free(fw);
- fclose(fptr);
- fclose(foundptr);
- return 0;
- }
- FILE * openfile(char *file, int n) {
- FILE *fptr;
- if(n==2)
- /* wipes the file clean w+ */
- fptr = fopen(file,"w+");
- else
- fptr = fopen(file,"r");
- if(fptr==NULL){
- printf("FILE %d B NULL\n",n);
- exit(1);
- }
- return fptr;
- }
- char *readIntoBuffer(FILE *fp){
- // unknown file size
- char *source = NULL;
- /* make sure its at the start */
- fseek(fp,0,SEEK_SET);
- /* Go to the end of the file. */
- if (fseek(fp, 0L, SEEK_END) == 0) {
- /* Get the size of the file. */
- long bufsize = ftell(fp);
- if (bufsize == -1) { /* Error */ }
- /* Allocate our buffer to that size. */
- source =
- malloc(sizeof(char) * (bufsize + 1));
- /* Go back to the start of the file. */
- if (fseek(fp, 0L, SEEK_SET) != 0)
- { /* Error */ }
- /* Read the entire file into memory. */
- size_t newLen =
- fread(source, sizeof(char),
- bufsize, fp);
- if ( ferror( fp ) != 0 ) {
- fputs("Error reading file",
- stderr);
- } else {
- source[newLen++] = '\0';
- /* Just to be safe add EOF */
- }
- }
- return source;
- }
- void seekWord(char *w,char *sword,FILE *storage){
- char *token;
- token=strtok(w, " ");
- while(token != NULL){
- if(strcmp(sword,token)==0){
- /* write to file*/
- fprintf(storage, "word found: %s:: "
- "search word is: %s\n",token,sword);
- }
- token=strtok(NULL, " ");
- }
- }
- void printFile(char *f,int n)
- {
- if(n==1)
- printf("Source file\n");
- else
- printf("Results file:\n");
- size_t sz=strlen(f);
- for(int i=0;i<sz;i++){
- printf("%c",f[i]);
- }
- printf("\n");
- }
- void chopToChars(char *source,char *searchwd,
- FILE *resultptr){
- char *t1,*ret;
- int hits=0,nohits=0,total=0;
- while((t1=strtok_r(source," ",&source))){
- ret=strstr(t1,searchwd);
- if(!ret){
- //if( (ret=strstr(t1,searchwd) == '\0' ) ){
- nohits++;
- }else{
- hits++;
- fprintf(resultptr, "\nhit num:[ %d ]:"
- "word found::[[ %s ]]:: "
- "search word is: %s\n\n\n",hits,t1,searchwd);
- }
- total++;
- }
- fprintf(resultptr, "total checks is [[ %d ]]\n"
- "total misses is [[ %d ]]\n"
- "total hits is [[ %d ]]\n",
- total,nohits,hits);
- }
- void usage(char *argv[]){
- printf("\nUsage:\n%s "
- " -filename <argument> | -f <argument> \n"
- " -searchword <argument> | -s <argument> \n"
- " -print | -p [prints results]\n"
- " -printsource | -x [prints source file]\n",argv[0]);
- exit(0);
- }
- void feedback(int c,int flag1,int flag2,
- char * file, char * searchword,
- int optopt,char * optarg){
- printf("c = %d, flag1= %d, flag2= %d\n"
- " filename= %s sword= %s\n"
- "optopt= %c ,optarg= %s\n",
- c,flag1,flag2,file,searchword,optopt,optarg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement