写一个程序实现以下命令行功能
foo [-h] [-pfs] [-i filename] [-o filename]
USAGE EXAMPLES:
$ ./foo -h
Usage: foo [-h] [-pfs] [-i filename] [-o filename]
-h Show the help message.
-p Only report on tests which passed.
-f Only report on tests which failed.
-s Only report summary.
-i [filename] Use the specified file as the input data.
-o [filename] Output the summary data to the specified file.
除了有-i参数将从文件获得数据,其他正常情况都需要从命令行输入参数,格式大概是:
Enter a test suite name: suite1
Enter a test name: test1
Enter a test summary for "suite1/test1": Test information for suite1/test1.
Enter a test result for "suite1/test1": PASS
Add another test: y
如此循环下去,直到你敲了n退出。
===========================
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "stdlib.h"
#define LEN sizeof(struct testcase)
struct testcase {
char sname[20];
char tname[20];
int pass;
int fail;
char summary[100];
struct testcase *next;
};
void help ();
void free1 ();
void failitem(struct testcase *);
void pasitem(struct testcase *);
void summsary(struct testcase *);
int wfile(struct testcase *);
void modifyitem(struct testcase *);
struct testcase *delitem(struct testcase *);
void finditem(struct testcase *);
void failitem(struct testcase *head ) {
struct testcase *p1;
p1=head;
while (p1!=NULL) {
if (p1->fail==1) {
printf ("fail case name:%s\n",p1->sname);
}
p1=p1->next;
}
}
void passitem(struct testcase *head ) {
struct testcase *p1;
p1=head;
while (p1!=NULL) {
if (p1->pass==1) {
printf ("pass case name:%s\n",p1->sname);
}
p1=p1->next;
}
}
void modifyitem(struct testcase *head) {
struct testcase *p1;
int flag=0;
char sname[10];
char summary[100];
printf("please enter modify item name:\n");
memset (sname,0x00,10);
scanf("%s",sname);
printf("please enter modify item summary:\n");
memset (summary,0x00,100);
scanf("%s",summary);
p1=head;
while (p1!=NULL) {
if (!strcmp(p1->sname,sname)) {
memset (p1->summary,0x00,100);
strcpy(p1->summary,summary);
flag=1;
break;
}
p1=p1->next;
}
if (flag==0)
printf ("there is no the item\n");
}
struct testcase* delitem(struct testcase *head) {
struct testcase *p1,*p2;
int flag=0;
char sname[10];
printf("please enter delete item name:\n");
memset (sname,0x00,10);
scanf("%s",sname);
p1=head;
// del head node
if (!strcmp(p1->sname,sname)) {
p1=p1->next;
free(head);
head=p1;
return head;
}
p2=p1;
p1=p1->next;
//del from the second node
while (p1!=NULL) {
if (!strcmp(p1->sname,sname)) {
p2->next=p1->next;
flag=1;
break;
}
p2=p1;
p1=p1->next;
}
if (flag==0)
printf ("there is no the item\n");
return head;
}
void finditem(struct testcase *head) {
struct testcase *p1;
char sname[10];
printf("please enter find item name:\n");
memset (sname,0x00,10);
scanf("%s",sname);
p1=head;
while (p1!=NULL) {
if (!strcmp(p1->sname,sname)) {
printf("sanme is %s---summary is %s \n",p1->sname,p1->summary);
}
p1=p1->next;
}
}
void summary1(struct testcase *head ) {
struct testcase *p1;
p1=head;
while (p1!=NULL) {
printf ("case name:%s--summary is %s\n",p1->sname,p1->summary);
p1=p1->next;
}
}
int wfile(struct testcase *head ) {
struct testcase *p1;
FILE *out;
char outfile[10]={0};
strcpy(outfile,"out.txt");
printf ("outfile=%s\n",outfile);
if ((out=fopen(outfile,"w"))==NULL) {
printf("cannot open outfile \n");
return 0;
}
p1=head;
while (p1!=NULL) {
fprintf (out,"%s\n",p1->sname);
fprintf (out,"%d\n",p1->pass);
fprintf (out,"%d\n",p1->fail);
fprintf (out,"%s\n",p1->summary);
p1=p1->next;
}
fclose (out);
return 0;
}
struct testcase* rfile() {
struct testcase *p1,*head,*p2;
FILE *in;
char infile[10]={0};
char line[100]={0};
int flag=0;
int i=0;
strcpy(infile,"out.txt");
if ((in=fopen(infile,"r"))==NULL) {
printf("cannot open outfile \n");
return 0;
}
while((fgets(line, sizeof(line), in)) != NULL) {
if (flag==0&&(i%4)==0) {
p2=p1=head=(struct testcase *)malloc(LEN);
} else if (flag==1&&(i%4)==0) {
p1=(struct testcase *)malloc(LEN);
p2->next=p1;
p2=p1;
}
p1->next=NULL;
if ((i%4)==0) {
memset (p1->sname,0x00,20);
if (line[strlen(line)-1]=='\n')
line[strlen(line)-1]='\0';
strncpy (p1->sname,line,strlen(line));
printf("sname:%s\n",p1->sname);
}
if ((i%4)==1) {
p1->pass=atoi(line);
printf ("pass:%d\n",p1->pass);
}
if ((i%4)==2) {
p1->fail=atoi(line);
printf ("fail:%d\n",p1->fail);
}
if ((i%4)==3) {
memset(p1->summary, 0, sizeof(p1->summary));
if (line[strlen(line)-1]=='\n')
line[strlen(line)-1]='\0';
strncpy (p1->summary,line,strlen(line));
printf("summary:%s\n",p1->summary);
flag=1;
}
i++;
}
fclose (in);
return (head);
}
struct testcase *additem(struct testcase *head) {
char *sname,*tname,*summary,*result,yn;
struct testcase *p1,*p2;
int first;
char temp;
if (head==NULL) {
p1=p2=head=(struct testcase *)malloc(LEN);
first=0;
} else {
p1=head;
while (p1->next!=NULL) {
p1=p1->next;
}
p2=p1;
p1=(struct testcase *)malloc(LEN);
first=1;
}
while (1) {
sname=(char *)malloc(20*sizeof(char));
tname=(char *)malloc(20*sizeof(char));
summary=(char *)malloc(100*sizeof(char));
result=(char *)malloc(100*sizeof(char));
memset (sname,0x00,20);
memset (tname,0x00,20);
memset (summary,0x00,100);
memset (result,0x00,100);
printf ("Enter a test suite name:\n");
scanf ("%s",sname);
printf ("Enter a test name:\n");
scanf ("%s",tname);
printf ("Enter a test summary for \"%s/%s\"\n",sname,tname);
scanf ("%s",summary);
printf ("Enter a test result for \"%s/%s\"\n",sname,tname);
scanf ("%s",result);
memset (p1->sname,0x00,20);
memset (p1->tname,0x00,20);
memset (p1->summary,0x00,100);
strcpy (p1->sname,sname);
strcpy (p1->tname,tname);
strcpy (p1->summary,summary);
free (sname);
free (tname);
free (summary);
free (result);
p1->pass=0;
p1->fail=0;
p1->next=NULL;
if (!strcasecmp(result,"PASS")) {
p1->pass=1;
} else if (!strcasecmp(result,"fail")){
p1->fail=1;
} else {
p1->fail=1;
}
if (first != 0) {
p2->next=p1;
p2=p1;
}
printf ("Add another test \n");
while((temp=getchar()) != '\n') {
printf ("temp=%c\n",temp);
/*scanf ("%c",&yn);*/
yn = getchar();
if (yn == 'n') break;
first=1;
p1=(struct testcase *)malloc(LEN);
}
return (head);
}
int main (int argc,char **argv) {
struct testcase *head,*p;
char ch;
int ret;
head=NULL;
//while ((ch = getopt(argc,argv,"afh:i:o:ps"))!=-1)
while (1)
{
help();
ch=getchar();
//if ch is integer ,it change to char
switch(ch)
{
case 'a':
head=additem(head);
//flush key buffer
fflush(stdin);
break;
case 'f':
failitem(head);
fflush(stdin);
break;
case 'h':
help();
fflush(stdin);
break;
case 'i':
free1(head);
head=rfile();
fflush(stdin);
break;
case 'o':
ret=wfile(head);
fflush(stdin);
break;
case 'p':
passitem(head);
fflush(stdin);
break;
case 's':
summary1(head);
fflush(stdin);
break;
case 'c':
finditem(head);
fflush(stdin);
break;
case 'm':
modifyitem(head);
fflush(stdin);
break;
case 'd':
head=delitem(head);
fflush(stdin);
break;
case 'q':
free1(head);
fflush(stdin);
exit (1);
default:
printf ("parameters are wrong\n");
break;
}
}
}
void free1 (struct testcase *head) {
struct testcase *p;
if (head!=NULL) {
p=head;
while (p!=NULL) {
p=head->next;
free (head);
head=p;
}
}
}
void help () {
printf ("Usage: foo [-h] [-pfs] [-i filename] [-o filename]\n");
printf ("-a add item.\n");
printf ("-h Show the help message.\n");
printf ("-p Only report on tests which passed.\n");
printf ("-f Only report on tests which failed.\n");
printf ("-s Only report summary.\n");
printf ("-i [filename] Use the specified file as the input data.\n");
printf ("-o [filename] Output the summary data to the specified file.\n");
printf ("-c find item\n");
printf ("-m modify item\n");
printf ("-d delete item\n");
printf ("-q quit.\n");
}