Skip to content

Commit

Permalink
Update fileop.c
Browse files Browse the repository at this point in the history
  • Loading branch information
4weKromi authored Jul 8, 2018
1 parent a77f737 commit 304d1c4
Showing 1 changed file with 46 additions and 90 deletions.
136 changes: 46 additions & 90 deletions pianoTC/main/fileop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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++;
}
Expand All @@ -70,99 +62,83 @@ 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;
}
top=tmpNode;
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();
Expand All @@ -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);
}
Expand All @@ -209,41 +179,35 @@ 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..");
getch();
}
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);
}
Expand All @@ -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..");
Expand All @@ -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);
}
Expand All @@ -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..");
Expand Down

0 comments on commit 304d1c4

Please sign in to comment.