This commit is contained in:
Adrien VAN DAMME 2017-04-04 20:12:52 +02:00
parent e1654ee12e
commit 879ac887ce
1 changed files with 113 additions and 63 deletions

View File

@ -55,6 +55,7 @@ typedef struct{
Date anniversaire;
int fidelite;
Panier panier;
long ofsetFichierDat;
} Client;
@ -74,22 +75,26 @@ int itoaY(int i, char*);
int itoaDM(int i, char*);
Bool clean_keyboard();
int ageClient(Client *, Date *);
unsigned int rechercheClient(char *, char*, FILE *, Client *, Date *);
long int rechercheClient(char *, char*, FILE *, Client *, Date *);
Bool gestionClient(Client *, Date *);
int compNP(char *, char *,char *, char *);
void localiserFichiersClients(char *, FILE **);
int sauvegarderClientFile(FILE **, Client * client);
void localiserFichiersClients(char *, FILE *, FILE *);
int sauvegarderClientFile(FILE *, FILE *, Client *); //enregistre le client dans le fichier dat et txt
FILE * creerFichier(Date *, Client *);
Bool fichierExistant(char *);
void sauvergarderFacture(FILE *, Date *, Client *);
void sauvegarderClientTXT(Client *, FILE *, Bool,...); //EcrireDonneesClient
void sauvegarderClientTXT(Client *, FILE *, long,...); /*EcrireDonneesClient
si int = 0, n'affiche pas l'age, enregistre a la position courante
= 1, affiche l'age qui est repris sur le 4e arguments (int) (pour une facture par ex), enregistre a la position courante
> 1, ecris sans l'age mais a la position demandé (3e arg)
*/
void clientBinToTXT(FILE *, FILE*);
void modificationClient(Client *, FILE *);
void modificationClient(Client *, FILE *, FILE *);
int main()
{
//printf("%d\n", pointsFidelite(52.23));
FILE * clientF[2];//0=DAT;1=TXT
FILE * clientDatF = NULL, * clientTXTF = NULL;
Date date;
char tmp;
Bool w;
@ -110,11 +115,11 @@ int main()
}
Client * client;
unsigned int ofsetFileCurClient;
long int ofsetFileCurClient = 0;
char nomClient[__STRLEN__],prenomClient[__STRLEN__];
char fichierClientDat[__STRLEN_FILE__];
int choixMenu;
localiserFichiersClients(fichierClientDat, clientF);
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
while(w)
{
printf("1. Nouveau client\n");
@ -126,9 +131,10 @@ int main()
{
case 1:
client = malloc(sizeof(Client));
client->ofsetFichierDat = 0;
encoderClient(client);
afficherClient(client, &date);
sauvegarderClientFile(clientF, client);
sauvegarderClientFile(clientDatF, clientTXTF, client);
w = gestionClient(client, &date);
break;
case 2:
@ -136,7 +142,7 @@ int main()
clean_keyboard();scanf("%s", nomClient);
printf("\npRENOM du client (sensible a la casse) : ");
clean_keyboard();scanf("%s", prenomClient);
ofsetFileCurClient = rechercheClient(nomClient,prenomClient, clientF[__TAB_CLIENTDAT_FILE__], client, &date);
ofsetFileCurClient = rechercheClient(nomClient,prenomClient, clientDatF, client, &date);
if(client != NULL)
{
w = gestionClient(client, &date);
@ -150,10 +156,10 @@ int main()
}
}
free(client);
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientDatF);
return 0;
}
void encoderDate(Date * date, char invite[__STRLEN__])
void encoderDate(Date * date, char *invite)
{
do {
printf("%s\n", invite);
@ -272,26 +278,33 @@ void afficherClient(Client *client, Date *date)
printf("adresse email : %s\n",client->contact.email);
printf("\nPoints de fidelite aqui : %d\n", client->fidelite);
}
void sauvegarderClientTXT(Client *client, FILE * file, Bool ageAff, ...) //EcrireDonneesClient
void sauvegarderClientTXT(Client *client, FILE * file, long arg, ...) //EcrireDonneesClient
{
va_list ap;
int age = 0;
va_start(ap, ageAff);
fprintf(file, "Client : %s %s", client->nom, client->prenom);
if(ageAff)
va_start(ap, arg);
if(arg>1)
{
fseek(file, arg, SEEK_SET);
fprintf(file, "Client : %s %s", client->nom, client->prenom);
fprintf(file, "\n");
}
else if(arg == 1)
{
age = va_arg(ap,int);
fprintf(file," (%d)\n",age);
fprintf(file, "Client : %s %s (%d)\n", client->nom, client->prenom,age);
}
else if(!arg)
{
fprintf(file, "Client : %s %s \n", client->nom, client->prenom);
}
else
fprintf(file,"\n");
fprintf(file, "rue : %s\n",client->adresse.rue);
fprintf(file, "numero : %s\n",client->adresse.numero);
fprintf(file, "localité : %s\n",client->adresse.localite);
fprintf(file, "code postal : %d\n",client->adresse.codePostal);
fprintf(file, "numéro de tél. : %s\n",client->contact.telephone);
fprintf(file, "adresse email : %s\n",client->contact.email);
if(ageAff)
if(arg)
{
fprintf(file, "\nPoints de fidelite aqui : %d\n", client->fidelite);
}
@ -573,19 +586,21 @@ void gestionFacture(Client * client, Date * date)
free(client->panier.article);
}
unsigned int rechercheClient(char nom[__STRLEN__], char prenom[__STRLEN__], FILE * clientF, Client * client, Date * date)
long int rechercheClient(char nom[__STRLEN__], char prenom[__STRLEN__], FILE * clientDatF, Client * client, Date * date)
{
client = malloc(sizeof(Client));
fseek(clientF, 0, SEEK_SET);
clearerr(clientF); //Clear EOF
long int ofsetFileCurClient;
fseek(clientDatF, 0, SEEK_SET);
clearerr(clientDatF); //Clear EOF
do
{
fread(client, sizeof(Client), 1, clientF);
}while(!feof(clientF) && compNP(client->nom, client->prenom, nom, prenom));
if(feof(clientF))
return NULL;
ofsetFileCurClient = ftell(clientDatF);
fread(client, sizeof(Client), 1, clientDatF);
}while(!feof(clientDatF) && compNP(client->nom, client->prenom, nom, prenom));
if(feof(clientDatF))
return 0;
else
return client;
return ofsetFileCurClient;
}
Bool gestionClient(Client * client, Date * date)
@ -622,7 +637,7 @@ Bool gestionClient(Client * client, Date * date)
}
}
void localiserFichiersClients(char fichierClientDat[__STRLEN_FILE__], FILE ** clientF)
void localiserFichiersClients(char * fichierClientDat, FILE * clientDatF, FILE * clientTXTF)
{
char rep[__STRLEN_FILE__];
@ -632,60 +647,66 @@ void localiserFichiersClients(char fichierClientDat[__STRLEN_FILE__], FILE ** cl
scanf("%s", rep);
sprintf(repTmp[__TAB_CLIENTDAT_FILE__],"%sclients.dat", rep);
sprintf(repTmp[__TAB_CLIENTTXT_FILE__],"%sclients.txt", rep);
clientF[__TAB_CLIENTDAT_FILE__] = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r");
clientF[__TAB_CLIENTTXT_FILE__] = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r");
if(clientF[__TAB_CLIENTDAT_FILE__] == NULL || clientF[__TAB_CLIENTTXT_FILE__] == NULL)
clientDatF = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r");
clientTXTF = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r");
if(clientDatF == NULL || clientTXTF == NULL)
{
char c;
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientF[__TAB_CLIENTTXT_FILE__]);
fclose(clientDatF);
fclose(clientTXTF);
printf("\nUn fichier n'existe pas. Voulez-vous les creers (C) ou changer de repertoir (R). (C|R)?");
if(!(clientF[__TAB_CLIENTDAT_FILE__] == NULL && clientF[__TAB_CLIENTTXT_FILE__] == NULL))
if(!(clientDatF == NULL && clientTXTF == NULL))
printf("Attention un des deux fichier a été détecter ! Il seront remis a 0 si vous les creers\nChoix (C|R):" );
clean_keyboard();scanf("%c", &c);
if((c|0b00100000) == 'c')
{
clientF[__TAB_CLIENTDAT_FILE__] = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "wb");
clientF[__TAB_CLIENTTXT_FILE__] = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "w");
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientF[__TAB_CLIENTTXT_FILE__]);
clientDatF = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "wb");
clientTXTF = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "w");
fclose(clientDatF);
fclose(clientTXTF);
}
else
localiserFichiersClients(fichierClientDat, clientF);
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
}
else
{
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientF[__TAB_CLIENTTXT_FILE__]);
fclose(clientDatF);
fclose(clientTXTF);
}
clientF[__TAB_CLIENTDAT_FILE__] = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r+b");
clientF[__TAB_CLIENTTXT_FILE__] = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r+");
if(clientF[__TAB_CLIENTDAT_FILE__] == NULL || clientF[__TAB_CLIENTTXT_FILE__] == NULL)
clientDatF = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r+b");
clientTXTF = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r+");
if(clientDatF == NULL || clientTXTF == NULL)
{
printf("\nErreur ouverture fichier");
localiserFichiersClients(fichierClientDat, clientF);
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
}
strcpy(fichierClientDat, repTmp[__TAB_CLIENTDAT_FILE__]);
}
int sauvegarderClientFile(FILE ** clientF, Client * client)
int sauvegarderClientFile(FILE * clientTXTF, FILE * clientDatF, Client * client)
{
int posi;
unsigned long int nbrClient;
fseek(clientF[__TAB_CLIENTDAT_FILE__], 0, SEEK_SET);
fread(&nbrClient,sizeof(nbrClient),1,clientF[__TAB_CLIENTDAT_FILE__]);
nbrClient++;
fseek(clientF[__TAB_CLIENTDAT_FILE__], 0, SEEK_SET);
fwrite(&nbrClient,sizeof(nbrClient),1,clientF[__TAB_CLIENTDAT_FILE__]);
/* Si Nouveau client ofsetFichierDat = 0 donc fin fichier sinon on decale de l'ofset */
if(!client->ofsetFichierDat)
{
unsigned long int nbrClient;
fseek(clientDatF, 0, SEEK_SET);
fseek(clientF[__TAB_CLIENTDAT_FILE__], 0, SEEK_END);
fseek(clientF[__TAB_CLIENTTXT_FILE__], 0, SEEK_END);
posi = ftell(clientF[__TAB_CLIENTDAT_FILE__]);
fwrite(client, sizeof(Client), 1, clientF[__TAB_CLIENTDAT_FILE__]);
fflush(clientF[__TAB_CLIENTDAT_FILE__]);
sauvegarderClientTXT(client, clientF[__TAB_CLIENTTXT_FILE__], 0);
fflush(clientF[__TAB_CLIENTTXT_FILE__]);
return posi;
fread(&nbrClient,sizeof(nbrClient),1,clientDatF);
nbrClient++;
fseek(clientDatF, 0, SEEK_SET);
fwrite(&nbrClient,sizeof(nbrClient),1,clientDatF);
fseek(clientDatF, 0, SEEK_END);
fseek(clientTXTF, 0, SEEK_END);
client->ofsetFichierDat = ftell(clientDatF);
sauvegarderClientTXT(client, clientTXTF, 0);
fflush(clientTXTF);
}
else
fseek(clientDatF, client->ofsetFichierDat, SEEK_SET);
fwrite(client, sizeof(Client), 1, clientDatF);
fflush(clientDatF);
return client->ofsetFichierDat;
}
int compNP(char *nom1, char *prenom1,char *nom2, char *prenom2)
@ -720,11 +741,40 @@ void clientBinToTXT(FILE * bin, FILE* txt) //===================================
{
fread(&client, sizeof(Client), 1,bin);
sauvegarderClientTXT(&client, txt, 0);
fprintf(txt, "========================================================================");
}
// fread()
}
void modificationClient(Client * client, FILE * clientF)
void modificationClient(Client * client, FILE * clientDatF, FILE * clientTXTF)
{
int choix = 0;
char ** aModifier = {&client->adresse, &client->contact.telephne, &client->contact.email};
char ** aAfficher = {"Adresse", "Telephne", "E-mail"};
do
{
system("cls");
if(choix)
printf("Erreur choix errone !\n");
printf("MODIFICATION CLIENT %s %s \n", client->nom, client->prenom);
printf("1. Adresse\n2. Telephne\n3. E-mail\n");
printf("Votre choix :");scanf("%d", &choix);
}while(!(choix == 1 || choix == 2 || choix == 3));
printf("Modification %s :", aAfficher[choix-1]);
if(choix == 1)
{
if(!(printf("Entrez la rue :") && clean_keyboard() && fgets(client->adresse.rue,__STRLEN__,stdin) != NULL && \
printf("\nEntrez le numero :" ) && clean_keyboard() && fgets(client->adresse.numero,__STRLEN__,stdin) != NULL && \
printf("\nEntrez la localité :") && clean_keyboard() && fgets(client->adresse.localite,__STRLEN__,stdin) != NULL && \
printf("\nEntrez le code postal :") && clean_keyboard() && scanf("%d", &(client->adresse.codePostal)) && \
printf("\nNom client :") && clean_keyboard() && fgets(client->nom,__STRLEN__,stdin) != NULL &&\
printf("\nPrenom client :") && clean_keyboard() && fgets(client->prenom,__STRLEN__,stdin) != NULL))
{
printf("\nErreur lors de l'encodage de l'adresse");
exit(-1);
}
cleanFgets(client->adresse.rue);cleanFgets(client->adresse.numero);cleanFgets(client->adresse.localite);
cleanFgets(client->nom);cleanFgets(client->prenom);
}
}