#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 60058
typedef struct nodeData{
char ID[MAX];
char Author[MAX];
char Title[MAX];
char Journal[MAX];
char Publisher[MAX];
char Address[MAX];
char Type[MAX];
char Language[MAX];
char ISSN[MAX];
char Number_Cited[MAX];
}data;
typedef struct nodeTree{
data d;
struct nodeTree *left,*right;
}tree;
typedef struct nodeList{
data d;
struct nodeList *nxt;
}list;
/* FUNCTIONS */
/* */
void loadFile(list **r);
/* */
void search();
/* */
data loadData(char a[], char b[], char c[], char d[], char e[], char f[], char g[], char h[], char i[], char j[]);
/* */
void insertTree(tree **r, data d);
/* */
void insertList(list **h, data d);
/* */
void mostrarLista(list **h);
/* */
char *delete (char string[],int type, char stringaux[]);
/* */
void freestring (char string[]);
int main(){
int option=0;
//tree *root=NULL;
list *head=NULL;
loadFile(&head);
printf("[[ DATA LOADED SUCCEFULY. PRESS ANY KEY TO CONTINUE...]]"); getchar();getchar();
do{
system("clear"),
printf("\t::MENU::\n1.Search\n2.Modify\n3.Delete\n4.Exit\nOPTION: ");
scanf("%i",&option);
switch(option){
case 1:
search();
break;
case 2:
mostrarLista(&head);
break;
case 3:
break;
case 4:
printf("\nGoodbye n_n"); exit(0);
}
}while(option!=4);
return 0;
}
void loadFile(list **head){
FILE *pf=fopen("db.bib","r");
data dataNode;
int cont=1;
if(!pf) printf("\nFile doesn't exist");
char string[MAX],saux[MAX];
char a[MAX],b[MAX],c[MAX],d[MAX],e[MAX],f[MAX],g[MAX],h[MAX],i[MAX],j[MAX];
char end[]="}},";
while(fgets(string,MAX,pf)){
if(strstr(string,"@article{ ISI:")){
strcpy(a,strchr(string,':')+1);
strcpy(a,delete(a,2,saux));
cont++;
}
else if(strstr(string,"Author = {")){
strcpy(b,strchr(string,'{')+1);
while(!strstr(string,"},"))
strcat(b,fgets(string,MAX,pf));
strcpy(b,delete(b,2,saux));
cont++;
}
else if(strstr(string,"Title = {{")){
strcpy(c,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(c,fgets(string,MAX,pf));
strcpy(c,delete(c,3,saux));
cont++;
}
else if(strstr(string,"Journal = {{")){
strcpy(d,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(d,fgets(string,MAX,pf));
strcpy(d,delete(d,3,saux));
cont++;
}
else if(strstr(string,"Publisher = {{")){
strcpy(e,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(e,fgets(string,MAX,pf));
strcpy(e,delete(e,3,saux));
cont++;
}
else if(strstr(string,"Address = {{")){
strcpy(f,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(f,fgets(string,MAX,pf));
strcpy(f,delete(f,3,saux));
cont++;
}
else if(strstr(string,"Type = {{")){
strcpy(g,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(g,fgets(string,MAX,pf));
strcpy(g,delete(g,3,saux));
cont++;
}
else if(strstr(string,"Language = {{")){
strcpy(h,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(h,fgets(string,MAX,pf));
strcpy(h,delete(h,3,saux));
cont++;
}
else if(strstr(string,"ISSN = {{")){
strcpy(i,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(i,fgets(string,MAX,pf));
strcpy(i,delete(i,3,saux));
cont++;
}
else if(strstr(string,"Number-of-Cited-References = {{")){
strcpy(j,strrchr(string,'{')+1);
while(!strstr(string,end))
strcat(j,fgets(string,MAX,pf));
strcpy(j,delete(j,3,saux));
cont++;
}
if(cont==10){
dataNode=loadData(a,b,c,d,e,f,g,h,i,j);
insertList(&(*head),dataNode);
cont=0;
}
freestring(saux);
}
}
void search(){
int option=0;
char search[100];
printf("\n\t.:Searching Atribute:.\n1.Author\n2.Title\n3.Year\n4.Journal\n5.Publisher\nATRIBUTE: ");
scanf("%i",&option);
switch(option){
case 1:
printf("\nAUTHOR: ");
scanf(" %[^\n]",search);
break;
case 2:
printf("\nTITLE: ");
scanf(" %[^\n]",search);
break;
case 3:
printf("\nYEAR: ");
scanf(" %[^\n]",search);
break;
case 4:
printf("\nJOURNAL: ");
scanf(" %[^\n]",search);
break;
case 5:
printf("\nPUBLISHER: ");
scanf(" %[^\n]",search);
break;
}
}
data loadData(char a[], char b[], char c[], char d[], char e[], char f[], char g[], char h[], char i[], char j[]){
data node;
strcpy(node.ID,a);
strcpy(node.Author,b);
strcpy(node.Title,c);
strcpy(node.Journal,d);
strcpy(node.Publisher,e);
strcpy(node.Address,f);
strcpy(node.Type,g);
strcpy(node.Language,h);
strcpy(node.ISSN,i);
strcpy(node.Number_Cited,j);
return node;
}
void insertList(list **h, data d){
list *newNode,*aux=*h;
newNode=(list *)malloc(sizeof(list));
newNode->nxt=NULL;
newNode->d=d;
if(!*h)
*h=newNode;
else{
while(aux->nxt)
aux=aux->nxt;
aux->nxt=newNode;
}
}
void mostrarLista(list **h){
list *aux=*h;
int i=1;
while(aux){
printf("%i) ID: %s\nAUTHOR: %s\nTITLE: %s\nJOURNAL: %s\nPUBLISHER: %s\nADRESS: %s\nTYPE: %s\nLANGUAGE: %s\nISSN: %s\nNUMBER CITED: %s\n\n",i,aux->d.ID,aux->d.Author,aux->d.Title,aux->d.Journal,aux->d.Publisher,aux->d.Address,aux->d.Type,aux->d.Language,aux->d.ISSN,aux->d.Number_Cited);
aux=aux->nxt;
i++;
}
getchar();getchar();
}
char *delete (char string[],int type,char stringaux[]){
int i=0;
i=strlen(string);
switch(type){
case 1:
strncpy(stringaux,string,i-2);
break;
case 2:
strncpy(stringaux,string,i-3);
break;
case 3:
strncpy(stringaux,string,i-4);
break;
}
return stringaux;
}
void freestring (char string[]){
int i=0;
for(i=0;i<MAX;i++)
string[i]='\0';
}