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
| #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
char *ltrim(char *s) ;
char *rtrim(char *s) ;
char *trim(char *s) ;
int tabledehash(char * tab, FILE* fl) ;
int cpteblocks(void) ;
/***************************Structure applications*****************************************/
typedef struct
{
char id [80];
char descr [80];
char owner [80];
char preid [80];
} applications ;
int ID = 0;
FILE * fileLog = NULL ;
/***************************Structure table de hachage************************************/
/*struct HashTable
{
int key;
Liste *PREAID; // Pointeur sur la table allouée
};*/
/***************************Fonction Hachage => codage des ID en clé**********************/
char *ltrim(char *s)
{
while(isspace(*s)) s++;
return s;
}
char *rtrim(char *s)
{
char* back = s + strlen(s);
while(isspace(*--back));
*(back+1) = '\0';
return s;
}
char *trim(char *s)
{
return rtrim(ltrim(s));
}
/*int tabledehash(char * tab, FILE* fl)
{
int i = 0;
int nombrehache = 0;
fprintf(fl, "### Compute HashKey with %d for %d ###", ID, strlen(tab));
for (i = 0 ; i<strlen(tab) ; i++)
{
nombrehache += tab[i];
}
//nombrehache %= ID;
fprintf(fl, "### HashKey brut = %d ###", nombrehache);
//fprintf(fl, "### HashKey net = %d ###", (nombrehache%ID));
//return nombrehache % ID;
return 1 ;
}*/
/************************Nombre de block pour allocation dynamique**********************/
int cpteblocks(void)
{
char lignelue [256];
int nb_blocks=0;
FILE* batchFile;
batchFile=fopen("D:\\xxx\\xxx\\xxx\\batch_loader_turc.txt","r");
while(fgets(lignelue, 80, batchFile) != NULL) {
if(strstr(lignelue, "ADD") != NULL) {
nb_blocks ++;
}
}
fclose(batchFile) ;
batchFile=NULL;
return nb_blocks;
}
/**************************Fonction main**************************************************/
int main()
{
char * ligne_lue ;
FILE * fileLog;
/**************************Tests & Ecriture dans les structures******************************/
applications * * tabApp = NULL ;
applications * app = NULL ;
char * * tab_id ;
int bloc=0 ;
int j=0 ;
int blocks = 0 ;
int nbApp = 0 ;
char * p;
blocks = cpteblocks() ;
fileLog = fopen ("D:\\xxx\\xxx\\xxx\\batch_loader_LOG.txt","w+");
//printf("### Nombre d'ADD dans le fichier : %d\n\n",blocks);
fprintf(fileLog, "### Nombre d'ADD dans le fichier : %d\n\n",blocks);
tabApp = malloc((blocks)*sizeof(applications)) ;//Allocation dynamique pour tableau de structure
FILE* batch;
batch=fopen("D:\\xxx\\xxx\\xxx\\batch_loader_turc.txt","r");
/****************************Test de lecture fichier*****************************************/
if (batch==NULL)
{
fputs("erreur a l'ouverture du fichier\n",stderr);
return EXIT_FAILURE;
}
ligne_lue = malloc(80*sizeof(char)) ;
tab_id = malloc((blocks) * sizeof(char*));
while((fgets(ligne_lue, 80, batch) != NULL)) {
p = strchr(ligne_lue,'\n');
if(p == NULL){
// Identification d'une nouvelle Application
if(strstr(ligne_lue, "ADD") != NULL) {//Rentrer dans la boucle si la ligne lue est ADD
// Si c'est la première application de l'extract, alors il n'y a encore rien a stocker
// Sinon on stocke l'application qui vient d'être récupérée
if((bloc != 0) && (app != NULL)) {
tabApp[nbApp] = malloc(sizeof(applications)) ;
tabApp[nbApp++] = app ;
}
bloc=0 ;
//nbApp++ ;
// Nouvelle Application a lire
app = malloc (sizeof(applications)) ;
// Agrandissement de la table du tableau de stockage des applications
//tabApp = realloc(tabApp, nbApp*(sizeof(applications))) ;
}else{
switch(bloc++){
case 0:
strcpy(app->id, trim(ligne_lue));
//printf("ID >> %s\n\n",app->id);
fprintf(fileLog, "ID >> $%s$\n\n",app->id);
tab_id[nbApp] = malloc(strlen(trim(ligne_lue+1))) ;
strcpy (tab_id[nbApp], trim(ligne_lue));
break;
case 1:
strcpy(app->descr, trim(ligne_lue));
//printf("DESC >> %s\n\n",app->descr);
fprintf(fileLog, "DESC >> $%s$\n\n",app->descr);
break;
case 2:
strcpy(app->owner, trim(ligne_lue));
//printf("OWNER >> %s\n\n",app->owner);
fprintf(fileLog, "OWNER >> $%s$\n\n",app->owner);
break;
default:
//printf("FIRSTCAR >> %c\n\n",ligne_lue[0]);
if(ligne_lue[0]!='\'') {
strcpy(app->preid, trim(ligne_lue));
//printf("PREID >> %s\n\n",app->preid);
fprintf(fileLog, "PREID >> $%s$\n\n",app->preid);
}
break;
}
}
}
}
tabApp[nbApp] = malloc(sizeof(applications)) ;
tabApp[nbApp] = app ;
//tab_id = malloc ((ID+1)* sizeof(int));
//tab_id = calloc ((nbApp+1), sizeof(int));
//printf("%d",ID);
//fprintf(fileLog, "##ID=%d##\n",ID);
for(j=1;j<=nbApp;j++) {
//if(strcmp((tabApp[j])->id, "XXXXNURADPREB01J") ==0) {
if(strcmp(tab_id[j], "XXXXNURADPREB01J") ==0) {
fprintf(fileLog, "Trouvé %s!!", (tabApp[j])->id);
}
//printf("New APP :\n##\nID=%s\n##DESC=%s\n##OWNER=%s\n##PREID=%s\n",(tabApp[j])->id,(tabApp[j])->descr,(tabApp[j])->owner,(tabApp[j])->preid);
}
fclose(batch) ;
fclose(fileLog) ;
batch=NULL;
char trash [10] ;
printf("Fin de Traitement.\n") ;
scanf("%s", trash) ;
free(app) ;
free(tabApp) ;
free (tab_id) ;
return 0 ;
} |
Partager