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
| #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define CUID_TAB_SIZE 20
void freemem(char *_query, char *_increment, char *_pattern){
free(_query);
free(_increment);
free(_pattern);
}
void getCustomerID_String(char *_query, char **_cuID){
char *query = NULL;
char *start;
char *stop;
char * increment = NULL;
char *pattern = NULL;
int i = 0;
if( (query = (char*)malloc(strlen(_query)+1)) == NULL){
printf("L'allocation de la mémoire a échoué\n");
exit(-1);
}
else
strcpy(query, _query);
if( (pattern = (char*)malloc(5)) == NULL){
printf("L'allocation de la mémoire a échoué\n");
exit(-1);
}
strcpy(pattern, "cu");
if( (increment = (char*)malloc(5)) == NULL){
printf("L'allocation de la mémoire a échoué\n");
exit(-1);
}
for(i = 1 ; i <= CUID_TAB_SIZE; i++){
if( (snprintf(increment, 2, "%d", i)) < 0){
printf("La conversion a echouer\n");
freemem(query, increment, pattern);
exit(-1);
}
strcat(pattern, increment);
strcat(pattern, "=");
if((start = strstr(query, pattern)) == NULL){
// printf("La chaine %s n'a pas était trouvés!!!!\n", pattern);
break;
}
else{
if((stop = strstr(start, "&")) == NULL)
printf("Le caractère '&' n'a pas était trouvés!!!!\n");
else{
if( (_cuID[i] = (char*)realloc((char*)_cuID[i], i * strlen(start) + 1)) == NULL){
// if( (_cuID[i] = (char*)malloc(strlen(_query) + 1)) == NULL){
printf("L'allocation de la mémoire a échoué!!\n");
freemem(query, increment, pattern);
}
else{
strcpy(_cuID[i], "");
strncpy(_cuID[i], start + strlen(pattern), (strlen(start) - strlen(stop)) - strlen(pattern) );
strcpy(query, stop);
strcpy(start, stop);
}
}
}
strcpy(pattern, "cu");
}
freemem(query, increment, pattern);
}
int main(){
int j;
char **cuID;
char *MyFile = "ExempleLog";
char *buffer;
FILE *fd;
FILE *sortie;
char buf[1024];
time_t begin, end;
struct tm * t;
begin = time(NULL);
t = localtime(&begin);
printf("Début d'execution à %02uh %02umin %02usec\n", t->tm_hour, t->tm_min, t->tm_sec);
if( (fd = fopen(MyFile, "r")) == NULL){
printf("Impossible d'ouvrir le fichier %s\n", MyFile);
exit(-1);
}
if( (sortie = fopen("customerFile", "w+")) == NULL){
printf("Impossible d'ouvrir le fichier customerFile\n");
fclose(fd);
exit(-1);
}
if( (cuID = (char**)malloc(CUID_TAB_SIZE * sizeof(*cuID) )) == NULL){
printf("l'allocation de la mémoire a échoué!!\n");
exit(-1);
}
if( (buffer = (char*)malloc(1024)) == NULL){
printf("l'allocation de la mémoire a échoué!!\n");
exit(-1);
}
while( (fgets(buf, 1024, fd)) != NULL){
j = 1;
strncpy(buffer, buf, strlen(buf)+1);
getCustomerID_String(buffer, cuID);
while(cuID[j] != NULL){
if( (strcmp(cuID[j], "") != 0)){
fprintf(sortie, "cuID[%d] = %s\n", j, cuID[j]);
strncpy(cuID[j], "", strlen(cuID[j]));
}
j++;
}
}
if( (fclose(fd)) != 0)
printf("Fermeture du fichier fd impossible\n");
if( (fclose(sortie) != 0))
printf("Fermeture du fichier sortie impossible\n");
free(cuID);
end = time(NULL);
t = localtime(&begin);
printf("Début d'execution à %02uh %02umin %02usec\n", t->tm_hour, t->tm_min, t->tm_sec);
return 0;
} |
Partager