-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxtsi.c
More file actions
422 lines (255 loc) · 6.04 KB
/
xtsi.c
File metadata and controls
422 lines (255 loc) · 6.04 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#define TAILLE_MAX 262144
#define TAILLE_MAX2 524288
//rend la ligne : INSERT INTO "tatable"
void get_table(FILE* source, char* chaine, int ligne){
rewind(source);
for(int i=0;i<=ligne && fgets(chaine, TAILLE_MAX, source) != NULL;i++){}
rewind(source);
int i = 0;
while(chaine[i] != '>'){
if (chaine[i] == '<'){
chaine[i] = '"';
}
i++;
}
chaine[i] = '"';
chaine[i+1] = '\0';
}
void rm_space(char * chaine){
int i = 0;
while((chaine[i] != '\0') && (chaine[i] != ' ')){
i++;
}
if(chaine[i] == ' '){
while(chaine[i] != '\0'){
chaine[i] = chaine[i+1];
i++;
}
chaine[i-1] = '\0';
}
}
int ispace(char * chaine){
int i = 0;
while((chaine[i] != '\0') && (chaine[i] != ' ')){
i++;
}
return (chaine[i] == ' ');
}
//retourne le noms des colonnes de la premiere entree <row>
void get_colname(char* chaine, FILE* sql){
//on parcourt la ligne jusqu'a tomber sur '/>'
int i = 0;
while(i < 5){
chaine[i] = ' ';
i++;
}
chaine[i] = '"';
i++;
while(chaine[i] != '>' && chaine[i-1] != '/'){
if (chaine[i] == '"'){
i++;
while(chaine[i] != '"'){
chaine[i] = ' ';
i++;
}
}
i++;
}
chaine[i-3] = '\0';
//on reparcourt la ligne pour enlever les '='
i= 0;
while(chaine[i] != '\0'){
if((chaine[i] == '"') && (chaine[i-1] == '=') ){
chaine[i-1] = ' ';
}
i++;
}
//on rajoute les virgules
i= 0;
int second = 0;
while(chaine[i] != '\0'){
if((chaine[i] == '"')){
if(second){
if(chaine[i+1] == '"'){
chaine[i+1] = ',';
chaine[i+2] = '"';
}else{
chaine[i+1] = ',';
}
second = 0;
}else{
second = 1;
}
}
i++;
}
//on reparcourt la chaine pour enlever les ' '
while(ispace(chaine)){
rm_space(chaine);
}
//on retire la virgule en trop
i=0;
while(chaine[i] != '\0'){
i++;
}
chaine[i-1] = ' ';
rm_space(chaine);
fprintf(sql, " (%s)\n", chaine );
}
//remplace tous les caractères par un espace jusqu'a un '"' et retourne sa position
int rmtogui (char* chaine, int e){
int i =e;
while(chaine[i] != '"'){
chaine[i] = ' ';
i++;
}
return i;
}
// donne la position du prochain guillemet
int nextgui (char* chaine, int e){
int i = e;
while((chaine[i] != '"') ){
i++;
}
return i;
}
//indique si il reste des guillemets dans la chaine
int gui (char* chaine, int e){
int i = e;
while((chaine[i] != '\0') && (chaine[i] != '"') ){
i++;
}
return (chaine[i] == '"');
}
//indique si il y a d'autres apostrophes à partir de la position e
int ifquote(char* chaine, int e){
int i = e;
while((chaine[i] != '\0') && (chaine[i] != '\'') ){
i++;
}
return (chaine[i] == '\'');
}
// donne la position du prochain apostrophe
int nextquote (char* chaine, int e){
int i = e;
while((chaine[i] != '\'') ){
i++;
}
return i;
}
//affiche les valeurs
void get_values (char* chaine, int ligne, FILE* sql){
//on supprime tout ce qui n'est pas entre guillemet
int e = 0;
int a = 0;
while(gui(chaine, e)){
a = rmtogui(chaine, e);
e = a + 1;
if(gui(chaine, e)){
e = nextgui(chaine, a+1);
e++;
chaine[e] = ',';
e++;
}
}
//on supprime le bout de la fin
chaine[e-1] = '\0';
/*//on retire les espaces (probl : les espaces entre les guillemets sont aussi retires)
while(ispace(chaine)){
rm_space(chaine);
}*/
//on double quote les simples quote pour faire comprendre au sql que c est des simple quote
int start = 0;
int posQuote = 0;
char chaineres[TAILLE_MAX2] ="";
if(ifquote(chaine, start)){
int quoteadd = 0;
while(ifquote(chaine, start)){
posQuote = nextquote(chaine, start); //on prend la position de la quote
int a = 0;
for (int i = start; i <= posQuote; ++i){
chaineres[i+quoteadd] = chaine[i];
a = i;
}
quoteadd++;
chaineres[a+quoteadd] = chaine[a];
for(int i = a+1; chaine[i] != '\0';i++){
chaineres[i+quoteadd] = chaine[i];
}
start = posQuote +1;
}
}else{
for (int i = 0; chaine[i] != '\0'; ++i)
{
chaineres[i] = chaine[i];
}
}
//on remplace les guillemets pas des '
for(int i = 0; chaineres[i] != '\0'; i++){
if(chaineres[i] == '"'){
chaineres[i] = '\'';
}
}
fprintf(sql,"VALUES \n" );
fprintf(sql, "(%s);\n", chaineres );
}
//retourne le nombre de lignes que contient un fichier
int nbr_lignes_fichier(FILE* fichier){
rewind(fichier);
int res = 0;
char chaine[TAILLE_MAX] ="";
while(fgets(chaine, TAILLE_MAX, fichier) != NULL){
res = res + 1;
}
rewind(fichier);
return res;
}
//chaine2 devient une copie de chaine1
void cpchaine(char* chaine1, char* chaine2){
for (int i = 0; chaine1[i] != '\0'; ++i)
{
chaine2[i] = chaine1[i];
}
}
void get_allvalues (FILE* fichier,char* intable, int e, FILE* sql){
int ligne = nbr_lignes_fichier(fichier);
char chaine[TAILLE_MAX] ="";
char chainecol[TAILLE_MAX] ="";
double poucent = 0;
double a = 0;
double b = ligne;
for(int i=1;i<ligne && fgets(chaine, TAILLE_MAX, fichier) != NULL;i++){
if(i>e){
fprintf(sql, "INSERT INTO %s ", intable);
cpchaine(chaine,chainecol);
get_colname(chainecol, sql);
get_values(chaine, i, sql);
}
a = i;
poucent = ((a+1)/b)*100;
printf("%d / %d || %.2f %s \r", i+1 ,ligne, poucent, "%");
}
}
int main(int argc, char** argv){
FILE* xml = NULL;
xml = fopen(argv[1], "r");
if(xml == NULL){perror ("error : fopen xml : please enter an xml file ! ");return -1;}
FILE* sql = NULL;
sql = fopen(argv[2], "w+");
if(sql == NULL){perror ("error : fopen sql : please enter a sql file name ! ");return -1;}
fprintf(sql,"BEGIN; \n" );
//on recupere le nom de la table et on stock la chaine INSERT INTO latable dans 'intable'
char intable[TAILLE_MAX] ="";
get_table(xml, intable, 1);
get_allvalues(xml, intable, 2, sql);
fprintf(sql,"COMMIT; \n" );
if(fclose(xml) != 0){perror ("error : fclose file.xml");return -1;}
if(fclose(sql) != 0){perror ("error : fclose file.sql");return -1;}
printf("%s\n", "Conversion completed successfully!" );
return 0;
}