-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2bib.c
208 lines (175 loc) · 5.38 KB
/
csv2bib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//Evelin Soares
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 350
typedef struct article {
char label[MAX/2];
char author[MAX];
char title[MAX];
char journal[MAX];
char year[5];
} Article;
typedef struct {
char nome[MAX];
int label;
} Autor;
int GerarBibTex (char *Arq, Article ar); // Retorna 1 se houve problema pra criar ou abrir o arquivo Bib
char *primeiroAutor(char *autores);
void limparMemoria(FILE *arq, Autor *autor, char *aux);
void Liberar(void *ptr);
int main (int argc, char *argv[]) {
char c, delim, nome_bib[256], string[MAX];
char aux[MAX] = "", label[MAX/2], *p, *p_autor = NULL;
int cont = 0, tab_cont = 0, numArtigos = 0, i;
Article ar;
Autor *autores = NULL;
FILE *Arq;
if ( argc >= 4 ) {
if ( ( Arq = fopen(argv[1], "r") ) == NULL ) {
printf("\nO arquivo .csv nao foi encontrado!\nEncerrando o programa...\n");
return 1;
}
//Conta o número de linhas antes para saber quantos artigos serão e assim alocar posições suficientes para autores[]
while ( ! feof(Arq) ) {
c = fgetc(Arq);
if ( c == '\n' ) {
numArtigos++;
}
}
rewind(Arq);
if ( ( autores = calloc(numArtigos, sizeof(Autor) ) ) == NULL ) {
printf("\nMemoria insuficiente para continuar a execucao do programa!\n");
limparMemoria(Arq, autores, aux);
exit(4);
}
numArtigos = 0;
strcpy(nome_bib, argv[2]);
if ( ! ( strcmp(argv[3], "\t") ) )
delim = '\t';
else
delim = argv[3][0];
} else {
printf("\nErro!\nUso do programa: %s \"arq_origem.csv\" \"arq_destino.bib\" delimitador(\";\" \"\\t\")\n", argv[0]);
return 1;
}
while ( ! feof(Arq) ) {
c = fgetc( Arq );
if ( c != delim && c != '\n' ) {
string[cont++] = c;
} else {
string[cont] = '\0';
tab_cont++;
cont = 0;
switch (tab_cont) {
case 1:
strcpy(ar.title, string);
break;
case 2:
strcpy(ar.author, string);
break;
case 3:
strcpy(ar.year, string);
break;
case 4:
strcpy(ar.journal, string);
tab_cont = 0;
p = NULL;
numArtigos++;
if ( strlen(ar.author) > 0 ) {
p_autor = primeiroAutor(ar.author);
int achou = 0;
for ( i = 0; i < numArtigos - 1; i++ ) {
if ( strstr(autores[i].nome, p_autor) != NULL ) {
achou = 1;
break;
}
}
if ( achou ) {
autores[i].label++;
} else {
i = numArtigos - 1;
strcpy(autores[i].nome, p_autor);
autores[i].label = 1;
}
strcpy(aux, autores[i].nome);
p = strtok(aux, ", "); // p = NULL antes de começar o novo artigo, evita problemas com endereço para onde aponta
sprintf(label, "%s%d", p, autores[i].label); // printf em string
strcpy(ar.label, label);
printf("\nTitulo: %s\nAutores: %s\nAno: %s\nPublicacao: %s", ar.title, ar.author, ar.year, ar.journal);
//GerarBibTex(nome do arquivo bib, struct article com dados do artigo processado);
if ( GerarBibTex(nome_bib, ar) ) { // retorna 1 se houve erro!
printf("\nNao foi possivel criar o arquivo .bib! Encerrando o programa...\n");
limparMemoria(Arq, autores, aux);
Liberar(p_autor);
return 1;
}
Liberar(p_autor); // p_autor é alocado dinamicamente em char *primeiroAutor
}
// Depois de identificar todos os campos do artigo, busca ler um ponto-e-vírgula ou um espaço ( formato .csv )
// e sai do loop quando acha um caractere diferente.
// O primeiro caracter diferente encontrado será o "\n", então sai do while e na próxima leitura
//na 1ª linha depois do while principal será um caracter para gravar no título do próximo artigo.
while ( ! feof(Arq) && ( ( c = fgetc(Arq) ) == delim || c == ' ') );
break;
}
}
}
printf("\nForam analisados %d artigos\n", numArtigos);
system ("pause");
limparMemoria(Arq, autores, aux);
return 0;
}
int GerarBibTex (char *Arq, Article ar ) {
FILE *a;
char inproceedings = 0;
/* Sera INPROCEEDINGS quando encontrar na leitura da string: proceedings,
acta, conference, symposium. Caso contrário, sera ARTICLE.
*/
if ( ( strstr(ar.journal, "Inproceedings") != NULL ) || ( strstr(ar.journal, "Conference") != NULL ) || ( strstr(ar.journal, "Symposium") != NULL ) || ( strstr(ar.journal, " Acta ") != NULL ) ) {
inproceedings = 1;
}
if ( ( a = fopen(Arq, "a+") ) != NULL ) {
if ( inproceedings )
fprintf(a, "@inproceedings {%s\n", ar.label);
else
fprintf(a, "@article {%s\n", ar.label);
fprintf(a, "author = {\"%s\"},\n", ar.author);
fprintf(a, "title = {\"%s\"},\n", ar.title);
fprintf(a, "journal = {\"%s\"},\n", ar.journal);
fprintf(a, "year = {\"%s\"}\n}\n\n", ar.year);
} else {
fclose(a);
return 1;
}
fclose(a);
return 0;
}
char *primeiroAutor(char *autores) {
char *s, *nome;
int tam;
if ( ( s = strstr(autores, " and") ) != NULL ) {
tam = (strlen(autores) - strlen(s)) + 1;
nome = (char *) malloc(tam * sizeof(char));
strncpy(nome, autores, s - autores);
nome[tam-1] = '\0';
return nome;
} else {
if ( ( tam = strlen(autores) ) > 0 ) {
tam++;
nome = (char *) malloc(tam * sizeof(char) );
strcpy(nome, autores);
return nome;
}
}
return NULL;
}
void limparMemoria(FILE *arq, Autor *autor, char *aux) {
fclose(arq);
Liberar(autor);
Liberar(aux);
}
void Liberar(void *ptr) {
free(ptr);
ptr = NULL; // evitar "dangling pointers"
}