// C로 짠 도서관리 프로그램.
malloc과 sorting 그리고 file open 말고는 그다지 중요한건 없는듯?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
typedef struct _booktype
{
char book[30];
char author[15];
char publisher[15];
char price[15];
char year[15];
}booktype;
typedef struct _node
{
booktype Data;
struct _node *next;
}node;
int fileopen();
int checkfile(int iDs);
void menulist();
void Datainput(booktype *Data);
void data_write(node *Head,int icnt,int iDs);
void datalist(node * Head,int icnt);
void release(node * Head);
node * read_data(int iDs,int icnt);
node * data_sort(node *Temp, node * Head);
int main()
{
node Temp;
node * Head;
int Val;
int iDs;
int icnt;
iDs = fileopen();
icnt = checkfile(iDs);
while(1)
{
Head = read_data(iDs,icnt);
menulist();
fprintf(stdout, "Menu Select = ");
fscanf(stdin,"%d", &Val);
getchar();
switch(Val)
{
case 1:
{
while(1)
{
Datainput(&Temp.Data);
Head = data_sort(&Temp,Head);
icnt++;
data_write(Head,icnt,iDs);
break;
}
break;
}
case 2:
{
datalist(Head,icnt);
break;
}
case 3:
{
release(Head);
close(iDs);
printf("Program Exit!!\n");
return 0;
}
default:
return 0;
}
}
return 0;
}
void menulist()
{
fprintf(stdout,"\n");
fprintf(stdout,"\t\t\t-------------\n");
fprintf(stdout,"\t\t\t| 1. INSERT |\n");
fprintf(stdout,"\t\t\t| 2. LIST |\n");
fprintf(stdout,"\t\t\t| 3. QUIT |\n");
fprintf(stdout,"\t\t\t-------------\n");
fprintf(stdout,"\n");
}
int fileopen()
{
int iDs;
int iRen;
char FHeader[30] = "******************************";
iDs = open("Book.pdf",O_RDWR);
if(-1 == iDs)
{
printf("\tNo Data File..!! Create DataFile? (choice: y or n) = ");
iRen = fgetc(stdin);
if(iRen == 'y')
{
iDs = open("Book.pdf", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IXUSR);
iRen = 0;
write(iDs,&iRen,4);
write(iDs,FHeader,30);
lseek(iDs,34,SEEK_SET);
return iDs;
}
else
{
printf("\tProgram Exit!!\n");
exit(1);
}
}
lseek(iDs,34,SEEK_SET);
return iDs;
}
int checkfile(int iDs)
{
int icnt;
lseek(iDs,0,SEEK_SET);
read(iDs,&icnt,4);
printf("\n\n");
printf("\tTotal Books = [%d] book\n", icnt);
return icnt;
}
void Datainput(booktype *Data)
{
int iRet;
memset(Data,'0',sizeof(booktype));
fprintf(stdout,"--------------------\n");
fprintf(stdout,"BookName Input\n");
iRet = read(0,Data->book,30);
Data->book[iRet-1] = 0;
fprintf(stdout,"Author input\n");
iRet = read(0,Data->author, 15);
Data->author[iRet-1] = 0;
fprintf(stdout,"Publisher input\n");
iRet = read(0,Data->publisher, 15);
Data->publisher[iRet-1] = 0;
fprintf(stdout,"Price input\n");
iRet = read(0,Data->price, 15);
Data->price[iRet-1] = 0;
fprintf(stdout,"Year input\n");
iRet = read(0,Data->year, 15);
Data->year[iRet-1] = 0;
fprintf(stdout,"--------------------\n");
}
node * read_data(int iDs,int icnt)
{
node *Head=0;
node *Temp;
node *Back;
lseek(iDs,34,SEEK_SET);
for(;icnt!=0;--icnt)
{
Temp = malloc(sizeof(node));
read(iDs,&Temp->Data,sizeof(booktype));
if(Head == 0)
{
Temp->next = 0;
Head = Back = Temp;
}
else
{
Back->next = Temp;
Back=Temp;
Temp->next = 0;
}
lseek(iDs,1,SEEK_CUR);
}
return Head;
}
node * data_sort(node *Temp, node * Head)
{
node *Back;
node *Pre;
Back = Pre = Head;
while(1)
{
if(Pre==0)
{
break;
}
if((strcmp(Pre->Data.book,Temp->Data.book)) > 0)
{
break;
}
Back = Pre;
Pre = Pre->next;
}
Temp->next = Pre;
if(Pre == Head)
{
Head = Temp;
}
else
{
Back->next = Temp;
}
return Head;
}
void data_write(node *Head,int icnt,int iDs)
{
node *Temp;
lseek(iDs,0,SEEK_SET);
write(iDs,&icnt,4);
lseek(iDs,34,SEEK_SET);
while(1)
{
Temp = Head->next;
write(iDs,&Head->Data,sizeof(booktype));
write(iDs,"|",1);
Head = Temp;
if(Head==0)
{
break;
}
}
}
void datalist(node * Head,int icnt)
{
node *Temp;
node *Back;
int iCnt1;
Temp = Head;
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "------------\n");
fprintf(stdout, " %s ","Number");
fprintf(stdout, "%11s","Book name");
fprintf(stdout, "%15s","Author");
fprintf(stdout, "%15s","Publisher");
fprintf(stdout, "%10s","Price");
fprintf(stdout, "%10s\n","Year");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "------------\n");
for(iCnt1=1;iCnt1<=icnt;iCnt1++)
{
Back = Temp->next;
fprintf(stdout, " %d ",iCnt1);
fprintf(stdout, "%15s",Temp->Data.book);
fprintf(stdout, "%15s",Temp->Data.author);
fprintf(stdout, "%15s",Temp->Data.publisher);
fprintf(stdout, "%10s",Temp->Data.price);
fprintf(stdout, "%10s\n",Temp->Data.year);
Temp = Back;
}
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "------------\n");
}
void release(node * Head)
{
node * Temp;
Temp = Head;
while(1)
{
if(Temp == 0)
{
break;
}
Temp = Head->next;
free(Head);
Head = Temp;
}
}