#define _GNU_SOURCE //forstrcasestr()
#include <stdio.h>
#include <string.h>
#define V_MAX_LINE_LEN 1024
#define V_MAX_SUBSTR_LEN 80
#define V_ARGC_CNT 9
int Parse_Option(int opt_num, char *opt_list[]);
int Find_SubString(char *opt_list[]);
int main(int argc, char *argv[])
{
int ret = 0;
ret = Parse_Option(argc,argv);
if(ret == -1)
{
return -1;
}
ret = Find_SubString(argv);
return ret;
}
/********************************************************************************
* Name Parse_Option
* Function judge of functionsparameter
* Prototype int Parse_Option(int opt_num, char*opt_list[])
* Parameter argv(i)
* Return Value 0: normal end
* -1: abnormal end
* [Create] caojian Date: 2018/01/12(init)
********************************************************************************/
int Parse_Option(int opt_num, char *opt_list[])
{
//judge the parameter number
if(opt_num != V_ARGC_CNT)
{
goto lb_error;
}
if(strcmp(opt_list[1],"-infile") != 0)
{
printf("error:-infile*.txt!\n");
goto lb_error;
}
if(strcmp(opt_list[3],"-outfile") != 0)
{
printf("error:-outfile*.txt!\n");
goto lb_error;
}
if(strcmp(opt_list[5],"-findstr") != 0)
{
printf("error:-findstrstring!\n");
goto lb_error;
}
//judge the sizeof string
if(strlen(opt_list[6]) >V_MAX_SUBSTR_LEN)
{
printf("the string is toolong(size<= 80)!");
goto lb_error;
}
if(strcmp(opt_list[7],"-case") != 0)
{
printf("error:-caseY/N!\n");
goto lb_error;
}
if((strcmp(opt_list[8],"Y") != 0) && (strcmp(opt_list[8], "N") !=0))
{
printf("error:inputN/Y!\n");
goto lb_error;
}
return 0;
lb_error:
printf("Usage: %s -infile*.txt -outfile *.txt -findstr *** -case Y/N\n", opt_list[0]);
return -1;
}
/********************************************************************************
* Name Find_SubString
* Function search the string in theinfile,and count the sum number,in the line,in the row,and result in theoutfile.
* Prototype int Find_SubString(char*opt_list)
* Parameter argv(i)
* Return Value 0: normal end
* -1: abnormal end
* [Create] caojian Date: 2018/01/12(init)
********************************************************************************/
int Find_SubString(char *opt_list[])
{
int line_idx = 0;
int row_idx = 0;
int total_find_cnt = 0;
int case_flg = 0;
int substr_len = 0;
charline_buf[V_MAX_LINE_LEN];
char *arr1_ptr = NULL;
char *arr2_ptr = NULL;
FILE *infile_ptr = NULL;
FILE *outfile_ptr = NULL;
memset(line_buf, 0,sizeof(line_buf));
if((infile_ptr =fopen(opt_list[2], "r")) == 0)
{
perror("open infilefailed");
return -1;
}
if((outfile_ptr =fopen(opt_list[4], "w")) == 0)
{
perror("open outfilefailed");
return -1;
}
substr_len =strlen(opt_list[6]);
while(fgets(line_buf,V_MAX_LINE_LEN, infile_ptr))
{
//get one line from inputfile
line_idx++;
arr1_ptr = line_buf;
arr2_ptr = line_buf;
while(1)
{
if((strcmp(opt_list[8],"N")) == 0)
{
arr1_ptr =strcasestr(arr1_ptr, opt_list[6]);
}
if((strcmp(opt_list[8],"Y")) == 0)
{
arr1_ptr =strstr(arr1_ptr, opt_list[6]);
}
row_idx = arr1_ptr- arr2_ptr +1;
total_find_cnt++;
fprintf(outfile_ptr,"%d / %d :%s\n", line_idx, row_idx, line_buf);
arr1_ptr +=substr_len;
}
}
fprintf(outfile_ptr, "thetotal number: %d\n", total_find_cnt);
//close all file
fclose(infile_ptr);
fclose(outfile_ptr);
}