From 304d1c42e070c64567a1633e701fd737fde289a9 Mon Sep 17 00:00:00 2001 From: Kromi <33200410+4weKromi@users.noreply.github.com> Date: Sun, 8 Jul 2018 14:17:34 +0530 Subject: [PATCH] Update fileop.c --- pianoTC/main/fileop.c | 136 ++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 90 deletions(-) diff --git a/pianoTC/main/fileop.c b/pianoTC/main/fileop.c index c0cda69..7a235df 100644 --- a/pianoTC/main/fileop.c +++ b/pianoTC/main/fileop.c @@ -16,25 +16,20 @@ extern char mgList[]; extern char buff[]; /* MG data structure */ -struct keyData -{ +struct keyData{ float keyF; float keyD; }mk; /* setting files to open */ -int setFiles() -{ +int setFiles(){ int x; int flag,i,j=0; char ch; FILE *ptr = fopen(mgList, "r"); if (ptr == NULL) - { ptr=fopen(mgList,"w"); - } - for(i=0;i<32;i++) - { + for(i=0;i<32;i++){ buff[i]='\0'; fnameC[i]='\0'; fnameD[i]='\0'; @@ -44,19 +39,16 @@ int setFiles() mgArr[2][i]='\0'; } i=0; - while (!feof(ptr)) - { + while (!feof(ptr)){ ch = getc(ptr); if (ch == '>') flag = 1; - else if (ch == ';') - { + else if (ch == ';'){ flag = 0; i=0; j++; } - if (flag == 1 && ch != '>') - { + if (flag == 1 && ch != '>'){ mgArr[j][i] = ch; i++; } @@ -70,47 +62,39 @@ int setFiles() buff[x]=mgArr[0][x]; sprintf(fnameC,"%s",buff); outtextxy(40,262,fnameC); - for(x=0;buff[x]!='\0';x++) buff[x]='\0'; for(x=0;mgArr[1][x]!='\0';x++) buff[x]=mgArr[1][x]; sprintf(fnameD,"%s",buff); outtextxy(40,312,fnameD); - for(x=0;buff[x]!='\0';x++) buff[x]='\0'; for(x=0;mgArr[2][x]!='\0';x++) buff[x]=mgArr[2][x]; sprintf(fnameE,"%s",buff); outtextxy(40,362,fnameE); - fclose(ptr); return 0; } /* loading .mg file to memory */ -struct node -{ +struct node{ struct keyData sf; - struct node *next; + struct node *next; struct node *prev; }*top; -int pushToMem(float k,float d) -{ +int pushToMem(float k,float d){ struct node *tmpNode; tmpNode=(struct node*)malloc(sizeof(struct node)); tmpNode->sf.keyF=k; tmpNode->sf.keyD=d; tmpNode->prev=NULL; if(top==NULL) - { tmpNode->next=NULL; - } - else - { + else{ tmpNode->next=top; top->prev=tmpNode; } @@ -118,51 +102,43 @@ int pushToMem(float k,float d) return 0; } -int loadFile(char filename[]) -{ +int loadFile(char filename[]){ int index=0; - FILE *ptr=fopen(filename,"rb"); - if(ptr==NULL) - { - dialogBox(); + FILE *ptr=fopen(filename,"rb"); + if(ptr==NULL){ + dialogBox(); setcolor(BLUE); outtextxy(240,170,filename); outtextxy(240,190,"Not Found"); outtextxy(220,220,"Press any key.."); getch(); - } - else - { + } + else{ setcolor(WHITE); dialogBox(); setcolor(BLUE); outtextxy(240,190,"Loading . ."); top=NULL; - while(!feof(ptr)) - { - fseek(ptr,index*sizeof(mk),SEEK_SET); - fread(&mk,sizeof(mk),1,ptr); - if(feof(ptr)) - break; - pushToMem(mk.keyF,mk.keyD); - index++; - } - } + while(!feof(ptr)){ + fseek(ptr,index*sizeof(mk),SEEK_SET); + fread(&mk,sizeof(mk),1,ptr); + if(feof(ptr)) + break; + pushToMem(mk.keyF,mk.keyD); + index++; + } + } fclose(ptr); return 0; } -int playSong() -{ +int playSong(){ int stopKey; struct node *tmp; tmp=top; while(tmp->next!=NULL) - { tmp=tmp->next; - } - while(tmp!=NULL&&stopKey!=27) - { + while(tmp!=NULL&&stopKey!=27){ stopKey=0; if(kbhit()) stopKey=getch(); @@ -173,31 +149,25 @@ int playSong() } /* delete third song */ -int deleteC() -{ +int deleteC(){ char ch; int req,i,count=0; FILE *ptr = fopen(mgList, "r"); FILE *fp=fopen(tmpList,"w"); - if(ptr==NULL) - { + if(ptr==NULL){ outtextxy(240,180,"Error"); outtextxy(220,220,mgList); getch(); exit(1); } for(i=0;mgBuff[i]!='\0';i++) - mgBuff[i]='\0'; + mgBuff[i]='\0'; i=0; - while(!feof(ptr)) - { + while(!feof(ptr)){ ch=getc(ptr); if(ch=='>') - { count++; - } - if(count>1) - { + if(count>1){ mgBuff[i]=ch; putc(mgBuff[i],fp); } @@ -209,8 +179,7 @@ int deleteC() remove(mgList); rename(tmpList,mgList); dialogBox(); - if(req==0) - { + if(req==0){ dialogBox(); outtextxy(240,190,"Deleted"); outtextxy(220,220,"Press any key.."); @@ -218,32 +187,27 @@ int deleteC() } return 0; } + /* delete fourth song */ -int deleteD() -{ +int deleteD(){ char ch; int req,i=0,count=0; FILE *ptr = fopen(mgList, "r"); FILE *fp=fopen(tmpList,"w"); - if(ptr==NULL) - { + if(ptr==NULL){ outtextxy(240,180,"Error"); outtextxy(240,220,mgList); getch(); exit(1); } for(i=0;mgBuff[i]!='\0';i++) - mgBuff[i]='\0'; + mgBuff[i]='\0'; i=0; - while(!feof(ptr)) - { + while(!feof(ptr)){ ch=getc(ptr); if(ch=='>') - { count++; - } - if(count==1||count==3) - { + if(count==1||count==3){ mgBuff[i]=ch; putc(mgBuff[i],fp); } @@ -254,8 +218,7 @@ int deleteD() req=remove(fnameD); remove(mgList); rename(tmpList,mgList); - if(req==0) - { + if(req==0){ dialogBox(); outtextxy(240,190,"Deleted"); outtextxy(220,220,"Press any key.."); @@ -265,31 +228,25 @@ int deleteD() } /* delete fifth song */ -int deleteE() -{ +int deleteE(){ char ch; int req,i,count=0; FILE *ptr = fopen(mgList, "r"); FILE *fp=fopen(tmpList,"w"); - if(ptr==NULL) - { + if(ptr==NULL){ outtextxy(240,180,"Error"); outtextxy(240,220,mgList); getch(); exit(1); } for(i=0;mgBuff[i]!='\0';i++) - mgBuff[i]='\0'; + mgBuff[i]='\0'; i=0; - while(!feof(ptr)) - { + while(!feof(ptr)){ ch=getc(ptr); if(ch=='>') - { count++; - } - if(count==1||count==2) - { + if(count==1||count==2){ mgBuff[i]=ch; putc(mgBuff[i],fp); } @@ -300,8 +257,7 @@ int deleteE() req=remove(fnameE); remove(mgList); rename(tmpList,mgList); - if(req==0) - { + if(req==0){ dialogBox(); outtextxy(240,190,"Deleted"); outtextxy(220,220,"Press any key..");