#include
#include
#include
#include
//
struct Used
{
int line_num;
struct Used *used_next;
};
//
struct Node
{
char id[11];
int line_num;
struct Used *used_next;
};
//
void dealFile(struct Node *, char *);
void createList(char *, char *);
void createTable(struct Node *, char *);
void createBoth(struct Node *, char *, char *);
int main(int argc, char* argv[])
{
struct Node lines[100] = {{"", 0, NULL}};;
char flag[3];
char inputfile[20];
char outputfile[20];
strcpy(flag, argv[1]);
strcpy(inputfile, argv[2]);
strcpy(outputfile, argv[3]);
printf("%s\n%s\n%s\n", argv[1], argv[2], argv[3]);
//flag
if(!strcmp(flag, "-l"))
{
createList(inputfile, outputfile);
}
else if(!strcmp(flag, "-c"))
{
dealFile(lines, inputfile);
createTable(lines, outputfile);
}
else if(!strcmp(flag, "-b"))
{
dealFile(lines, inputfile);
createBoth(lines, inputfile, outputfile);
}
}
//int main()
//{
//struct Node lines[100] = {{"", 0, NULL}};;
//char string[] = "avg: and i and 2";
//char *ps = strtok(string, ":");
//printf("%s\n", strtok(string, ":"));
//if(!strcmp(strtok(string, ":"), string)) printf("1");
//if(strcmp(strtok(string, ":"), string)) printf("2");
//dealFile(lines);
//createList();
//createTable(lines);
//createBoth(lines);
/**
//list
FILE *r = fopen("C:\\Users\\Alex\\Desktop\\prog3\\test1_out_list.txt.", "rb");
if(r==NULL)
{
printf("Cannot read file\n");
}
while(!feof(r))
{
char a[100] = {0};
fgets(a, sizeof(a), r);
printf("%s", a);
}
fclose(r);
*/
/**
//crt
FILE *s = fopen("C:\\Users\\Alex\\Desktop\\prog3\\test1_out_both.txt.", "rb");
if(s==NULL)
{
printf("Cannot read file\n");
}
while(!feof(s))
{
char a[100] = {0};
fgets(a, sizeof(a), s);
printf("%s\n", a);
}
fclose(s);
*/
/**
int i = 0;
struct Used *point = NULL;
while(lines[i].line_num=1)
{
printf("\n%s\t%d\t", lines[i].id, lines[i].line_num);
point = lines[i].used_next;
while(point)
{
printf("%d ", point->line_num);
point = point->used_next;
}
i++;
}
*/
//}
//: 、、
void dealFile(struct Node *lines, char *inputfile)
{
FILE *p = fopen(inputfile, "rb");
int count=0, length1=0, length2=0, line_pre, line, head;
struct Used *point = NULL;
struct Used *newnode;
if(p==NULL)
{
printf("Fun(DealFile) Cannot read file %s\n", inputfile);
return;
}
//int used_line_num[100] = {0};
while(!feof(p))
{
char a[100]={'\n'};
fgets(a, sizeof(a), p);
char b[100];
if(a[0]=='\n') continue;
count++;
strcpy(b, a);
if(b[0]!='\0')
{
length1 = strlen(b);
length2 = strlen(strtok(b, ":"));
}
if(length2line_num = line;
newnode->used_next = NULL;
if(head == 1)
{
head = 0;
lines[line_pre-1].used_next = newnode;
point = newnode;
}
else
{
head = 0;
point->used_next = newnode;
point = newnode;
}
}
}
fclose(q);
}
else
{
strcpy(lines[count-1].id, "");
//lines[count-1].line_num = count;
//lines[count-1].used=NULL:
}
lines[count-1].line_num = count;
}
fclose(p);
}
void createList(char *inputfile, char *outputfile)
{
FILE *p = fopen(inputfile, "rb");
FILE *q = fopen(outputfile, "wb");
int count=0;
if(p==NULL)
{
printf("Fun(createList) Cannot read file %s\n", outputfile);
return;
}
while(!feof(p))
{
char a[100]={'\n'};
fgets(a, sizeof(a), p);
if(a[0]=='\n') continue;
count++;
fprintf(q, "%d\t", count);
fputs(a, q);
}
fclose(p);
fclose(q);
}
void createTable(struct Node *lines, char *outputfile)
{
FILE *q = fopen(outputfile, "wb");
int i;
struct Used *point = NULL;
if(q==NULL)
{
printf("Fun(createTale) Cannot read file %s\n", outputfile);
return;
}
fputs("Cross Reference Table\n\n", q);
fputs("Identifier\tDefinition\tUse\n\n", q);
for (i=0; iline_num);
point = point->used_next;
}
fputs("\n", q);
}
}
fclose(q);
}
void createBoth(struct Node *lines, char *inputfile, char *outputfile)
{
FILE *p = fopen(inputfile, "rb");
FILE *q = fopen(outputfile, "wb");
int i;
struct Used *point = NULL;
int count=0;
if(p==NULL)
{
printf("Fun(createBoth) Cannot read file %s\n", inputfile);
return;
}
if(q==NULL)
{
printf("Fun(createBoth) Cannot read file %s\n", outputfile);
return;
}
while(!feof(p))
{
char a[100]={'\n'};
fgets(a, sizeof(a), p);
if(a[0]=='\n') continue;
count++;
fprintf(q, "%d\t", count);
fputs(a, q);
}
fputs("\n\nCross Reference Table\n\n", q);
fputs("Identifier\tDefinition\tUse\n\n", q);
for (i=0; iline_num);
point = point->used_next;
}
fputs("\n", q);
}
}
fclose(p);
fclose(q);
}