Saturday, 2 May 2015

2 PASS MACRO

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct mdt_table{

int index;

char card[30];

}MDT;

typedef struct mnt_table{

int index;

char name[15];

int mdtindex;

}MNT;

typedef struct ala_table{

char index[3];

char arg[15];

}ALA;

typedef struct ala_array{

ALA item[5];

}ALA_ITEM;

MNT mnt[5];

MDT mdt[40];

ALA ala[5];

int mdtc,mntc,mdtp,alac,alap;

char word3[10],ch3[2],word[10],line[20],ch,ch1[2],ch2[2],line1[20],line2[20],k;

FILE *input,*inter,*output;

fpos_t pos;

char* num[5]={"#1","#2","#3","#4","#5"};

void get_word(){

int i=0;

//char ch;

ch=getc(input);

while(ch!=EOF){

if((ch!=' ')&&(ch!=',')&&(ch!='\n'))

word[i++]=ch;

if((ch==' ')||(ch==',')||(ch=='\n')){

word[i]='\0';

break;

}

ch=getc(input);

}

}

void display_mnt(){

int i=0;

printf("\nMNT\n");

for(i=0;i<mntc;i++){

printf("%d\t%s\t%d",mnt[i].index,mnt[i].name,mnt[i].mdtindex);

printf("\n");

}

}

void display_mdt(){

int i=0;

printf("\nMDT\n");

for(i=0;i<mdtc;i++){

printf("%d\t%s",mdt[i].index,mdt[i].card);

printf("\n");

}

}

void display_ala(){

int i=0;

printf("\nALA\n");

for(i=0;i<alac;i++){

printf("%s\t%s",ala[i].index,ala[i].arg);

printf("\n");

}

}

void display_ala_item(ALA_ITEM obj){

int i;

printf("\nALA ITEM\n");

for(i=0;i<alap;i++){

printf("%s\t%s",obj.item[i].index,obj.item[i].arg);

printf("\n");

}

}

void search_ALA(){

int i;

for(i=0;i<alac;i++){

if(strcmp(word,ala[i].arg)==0){

strcpy(word,ala[i].index);

}

}

}

void PASS1(){

int i;

input=fopen("input.txt","r");

inter=fopen("inter.txt","w+");

mdtc=mntc=alac=0;

while(!feof(input)){

fgetpos(input,&pos);

get_word();

/*printf("%s\n",word);

fprintf(inter,"%s\n",word);*/

if(strcmp(word,"MACRO")!=0){

fsetpos(input,&pos);

fgets(line,20,input);

fprintf(inter,"%s",line);

}

if(strcmp(word,"MACRO")==0){

fgetpos(input,&pos);

get_word();

//mnt

//printf("%s",word);

strcpy(mnt[mntc].name,word);

mnt[mntc].index=mntc;

mnt[mntc].mdtindex=mdtc;

mntc++;

display_mnt();

//mdt

fsetpos(input,&pos);

i=0;

//enter arg line into mdt

while(!feof(input)){

ch=getc(input);

if(ch=='\n'){

line[i]='\0';

break;

}

line[i++]=ch;

}

//enter into mdt

//printf("%s",line);

mdt[mdtc].index=mdtc;

strcpy(mdt[mdtc].card,line);

mdtc++;

display_mdt();

//setup ala

fsetpos(input,&pos);

get_word();

do{

get_word();

strcpy(ala[alac].arg,word);

strcpy(ala[alac].index,num[alac]);

alac++;

}while(ch!='\n');//this is why ch is global

display_ala();

fgetpos(input,&pos);

get_word();

ch1[0]=ch;

ch1[1]='\0';

//printf("%s%s\n",word,ch1);

while(strcmp(word,"MEND")!=0){

//clear the array

memset(line1,0,strlen(line1));

do{

search_ALA();

strcat(line1,word);

strcat(line1,ch1);

//printf("\n%s",line1);

get_word();

ch1[0]=ch;

}while(ch1[0]!='\n');

search_ALA();

strcat(line1,word);

//strcat(line1,ch1);

mdt[mdtc].index=mdtc;

strcpy(mdt[mdtc].card,line1);

mdtc++;

get_word();

ch1[0]=ch;

}

//to enter mend to mdt

mdt[mdtc].index=mdtc;

strcpy(mdt[mdtc].card,word);

mdtc++;

display_mdt();

}

}

fclose(input);

fclose(inter);

}

int search_MNT(){

int i;

for(i=0;i<mntc;i++){

if(strcmp(word,mnt[i].name)==0){

mdtp=mnt[i].mdtindex;

return mnt[i].index;

}

}

return -1;

}

void get_word_line(){

int i=0;

ch3[1]='\0';

ch3[0]=line1[k];

memset(word3,0,strlen(word));

while(ch3[0]!='\0'){

if((ch3[0]!=' ')&&(ch3[0]!=',')&&(ch3[0]!='\0'))

word3[i++]=ch3[0];

if((ch3[0]==' ')||(ch3[0]==',')||(ch3[0]=='\0')){

word3[i]='\0';

break;

}

k++;

ch3[0]=line1[k];

}

}

void search_ALA_ITEM(ALA_ITEM obj){

int i;

for(i=0;i<alap;i++){

if(strcmp(word3,obj.item[i].index)==0){

memset(word3,0,strlen(word3));

strcpy(word3,obj.item[i].arg);

}

}

}

void PASS2(){

int f,i;

input=fopen("inter.txt","r");

output=fopen("output.txt","w+");

memset(word,0,strlen(word));

memset(line,0,strlen(line));

while(!feof(input)){

get_word();

ch2[0]=ch;

ch2[1]='\0';

//printf("%s%s",word,ch2);

//search in mnt

f=search_MNT();

//if not in mnt

if(f==-1){

fprintf(output,"%s%s",word,ch2);

if(strcmp(word,"END")==0)break;

}

else{

//setup ala

alap=0;

//for every call, new ALA_ITEM object

ALA_ITEM obj;

do{

get_word();

strcpy(obj.item[alap].index,num[alap]);

strcpy(obj.item[alap].arg,word);

alap++;

}while(ch!='\n');

//display the current ALA_ITEM object

display_ala_item(obj);

mdtp++;

while(strcmp(mdt[mdtp].card,"MEND")!=0){

memset(line1,0,strlen(line1));

strcpy(line1,mdt[mdtp].card);

//fprintf(output,"%s\n",line1);

k=0;

memset(line2,0,strlen(line2));

do{

get_word_line();

//printf("%s%s\n",word3,ch3);

k++;

//replace # with arg from corresponding ala

search_ALA_ITEM(obj);

//append to line

strcat(line2,word3);

strcat(line2,ch3);

}while(ch3[0]!='\0');

fprintf(output,"%s\n",line2);

mdtp++;

}

}

}

}

void main(){

PASS1();

PASS2();

}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More