laboProgra/VANDAMME28.c

767 lines
23 KiB
C
Raw Normal View History

2017-04-05 23:06:44 +02:00
2017-04-02 17:00:23 +02:00
/*
2017-04-05 23:06:44 +02:00
* Fichier client :
------------------------------------------------------------
|UINTL NBClients|Bool Tri| Client 0 | Client 1 | .......ETc|
------------------------------------------------------------
2017-04-02 17:00:23 +02:00
*/
2017-04-02 16:44:34 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <conio.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
2017-04-02 17:02:15 +02:00
#include <stdarg.h>
2017-04-02 16:44:34 +02:00
#define __TVA__ 1.21
#define __REDUC__ 0.9
#define __MAXNBRART__ 3
#define __STRLEN__ 34
#define __STRLEN_FILE__ 500
2017-04-02 17:02:15 +02:00
#define __TAB_CLIENTDAT_FILE__ 0
#define __TAB_CLIENTTXT_FILE__ 1
2017-04-02 16:44:34 +02:00
typedef enum{FALSE,TRUE} Bool;
typedef struct {
char deisgnation[__STRLEN__];
float prixHTVA;
int quant;
} Article;
typedef struct {
Article * article;
unsigned int nbrArt;
} Panier;
typedef struct{
int jour,moi,annee;
} Date;
typedef struct{
char rue[__STRLEN__],numero[__STRLEN__],localite[__STRLEN__];
int codePostal;
} Adresse;
typedef struct{
char telephone[13],email[__STRLEN__];
} Contact;
typedef struct{
char nom[__STRLEN__];
char prenom[__STRLEN__];
Adresse adresse;
Contact contact;
Date anniversaire;
int fidelite;
Panier panier;
2017-04-04 20:12:52 +02:00
long ofsetFichierDat;
2017-04-07 10:32:52 +02:00
long ofsetFichierTXT;
2017-04-02 16:44:34 +02:00
} Client;
void encoderDate(Date *, char *);
Bool dateValide (Date *);
void encoderArticle(Article *);
void encoderClient(Client *);
void afficherClient(Client *, Date *);
void encoderFacture(Panier *);
void afficherFacture(Date *, Client *);
2017-04-02 17:02:15 +02:00
int pointsFidelite(float);
void gestionFacture(Client *, Date *);
2017-04-02 16:44:34 +02:00
void cleanFgets(char*);
int itoaY(int i, char*);
int itoaDM(int i, char*);
Bool clean_keyboard();
int ageClient(Client *, Date *);
2017-04-04 20:12:52 +02:00
long int rechercheClient(char *, char*, FILE *, Client *, Date *);
2017-04-07 10:32:52 +02:00
Bool gestionClient(Client *, Date *, FILE *, FILE *);
2017-04-02 16:44:34 +02:00
int compNP(char *, char *,char *, char *);
2017-04-05 23:06:44 +02:00
void localiserFichiersClients(char *, FILE **, FILE **);
2017-04-04 20:12:52 +02:00
int sauvegarderClientFile(FILE *, FILE *, Client *); //enregistre le client dans le fichier dat et txt
2017-04-02 17:02:15 +02:00
FILE * creerFichier(Date *, Client *);
Bool fichierExistant(char *);
void sauvergarderFacture(FILE *, Date *, Client *);
2017-04-04 20:12:52 +02:00
void sauvegarderClientTXT(Client *, FILE *, long,...); /*EcrireDonneesClient
2017-04-07 10:32:52 +02:00
si long = 0, n'affiche pas l'age, enregistre a la position courante
2017-04-05 23:06:44 +02:00
= 1, affiche l'age qui est repris sur le 4e arguments (int) (pour une facture par ex), enregistre a la position courante
2017-04-07 10:32:52 +02:00
> 1, ecris sans l'age mais a la position demandé (3e arg, long)
2017-04-04 20:12:52 +02:00
*/
2017-04-07 10:32:52 +02:00
void clientBinToTXT(FILE *, FILE*);//En cours de dev
2017-04-04 20:12:52 +02:00
void modificationClient(Client *, FILE *, FILE *);
2017-04-02 16:44:34 +02:00
int main()
{
2017-04-02 17:02:15 +02:00
//printf("%d\n", pointsFidelite(52.23));
2017-04-07 10:32:52 +02:00
FILE * clientDatF = NULL;
FILE * clientTXTF = NULL;
2017-04-05 23:06:44 +02:00
Client * client;
long int ofsetFileCurClient = 0;
char nomClient[__STRLEN__],prenomClient[__STRLEN__];
char fichierClientDat[__STRLEN_FILE__];
int choixMenu;
Date date;
char tmp;
Bool w;
time_t curTimeStamp = time(NULL);
struct tm *locTime = localtime(&curTimeStamp);
locTime->tm_mon += 1;
locTime->tm_year += 1900;
printf("Voulez-vous utilisez la date système %d/%d/%d (Y|n)?\n", locTime->tm_mday,locTime->tm_mon,locTime->tm_year);
scanf("%c", &tmp);
if((tmp|0b00100000) != 'y')
encoderDate(&date, "Date facturation");
else
{
date.jour = locTime->tm_mday;
date.moi = locTime->tm_mon;
date.annee = locTime->tm_year;
2017-04-02 17:02:15 +02:00
// printf("%d/%d/%d\n", date->jour, date->moi, date->annee );
2017-04-05 23:06:44 +02:00
}
2017-04-07 10:32:52 +02:00
localiserFichiersClients(fichierClientDat, &clientDatF, &clientTXTF);
2017-04-05 23:06:44 +02:00
while(w)
{
printf("1. Nouveau client\n");
printf("2. Client enregistré\n");
do{
printf("Choix (1|2):");clean_keyboard();scanf("%d", &choixMenu);
}while(!(choixMenu<=2 && choixMenu>=1));
switch (choixMenu)
2017-04-02 16:44:34 +02:00
{
2017-04-05 23:06:44 +02:00
case 1:
client = malloc(sizeof(Client));
client->ofsetFichierDat = 0;
2017-04-07 10:32:52 +02:00
client->ofsetFichierTXT = 0;
2017-04-05 23:06:44 +02:00
encoderClient(client);
afficherClient(client, &date);
2017-04-07 10:32:52 +02:00
fseek(clientTXTF, 0, SEEK_END);
sauvegarderClientFile(clientTXTF, clientDatF, client);
w = gestionClient(client, &date, clientDatF, clientTXTF);
2017-04-05 23:06:44 +02:00
break;
case 2:
2017-04-07 10:32:52 +02:00
client = malloc(sizeof(Client));
printf("\nNom du client (sensible a la casse) : ");
2017-04-05 23:06:44 +02:00
clean_keyboard();scanf("%s", nomClient);
2017-04-07 10:32:52 +02:00
printf("\nPrenom du client (sensible a la casse) : ");
2017-04-05 23:06:44 +02:00
clean_keyboard();scanf("%s", prenomClient);
2017-04-07 10:32:52 +02:00
ofsetFileCurClient = rechercheClient(nomClient,prenomClient, clientDatF, client, &date);
2017-04-05 23:06:44 +02:00
if(client != NULL)
{
2017-04-07 10:32:52 +02:00
w = gestionClient(client, &date, clientDatF, clientTXTF);
2017-04-05 23:06:44 +02:00
}
else
2017-04-02 16:44:34 +02:00
{
2017-04-07 10:32:52 +02:00
printf("\nClient non trouvé !\n");
2017-04-05 23:06:44 +02:00
printf("%d",(int)sizeof(Client));
2017-04-02 16:44:34 +02:00
}
2017-04-05 23:06:44 +02:00
break;
2017-04-02 16:44:34 +02:00
}
2017-04-05 23:06:44 +02:00
}
free(client);
2017-04-07 10:32:52 +02:00
fclose(clientDatF);
fclose(clientTXTF);
2017-04-05 23:06:44 +02:00
return 0;
2017-04-02 16:44:34 +02:00
}
2017-04-04 20:12:52 +02:00
void encoderDate(Date * date, char *invite)
2017-04-02 16:44:34 +02:00
{
do {
2017-04-07 10:32:52 +02:00
printf("\n%s\n", invite);
2017-04-02 16:44:34 +02:00
printf("Entrez la date sous cette forme : jj/mm/aaaa\n");
scanf("%d/%d/%d",&(date->jour),&(date->moi),&(date->annee));
} while(!dateValide(date));
}
Bool dateValide(Date * date)
{
2017-04-07 10:32:52 +02:00
int j = date->jour;
int m = date->moi;
int a = date->annee;
int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))//Si bis février = 29
daysinmonth[1]=29;
if (m<13)
2017-04-02 16:44:34 +02:00
{
2017-04-07 10:32:52 +02:00
if( d <= daysinmonth[m-1] )
return 1;
2017-04-05 23:06:44 +02:00
else
2017-04-07 10:32:52 +02:00
return 0;
2017-04-05 23:06:44 +02:00
}
else
2017-04-07 10:32:52 +02:00
return 0;
2017-04-02 16:44:34 +02:00
}
void encoderArticle(Article * article)
{
printf("\nVeuillez entrer le nom : ");
clean_keyboard();
scanf("%34s", article->deisgnation);
printf("\nVeuillez entrer le prix a l'unite hors TVA : ");
clean_keyboard();
scanf("%f", &article->prixHTVA);
printf("\nVeuillez entrer la quantite desiree : ");
clean_keyboard();
scanf("%d", &article->quant);
}
void encoderClient(Client * client)
{
2017-04-07 10:32:52 +02:00
printf("\nEncoage Client\n");
2017-04-02 16:44:34 +02:00
client->fidelite = 0;
if(!(printf("Entrez la rue :") && clean_keyboard() && fgets(client->adresse.rue,__STRLEN__,stdin) != NULL && \
2017-04-05 23:06:44 +02:00
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))
2017-04-02 16:44:34 +02:00
{
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);
if(!(printf("\n Entrez le numéro de tél. :") && clean_keyboard() &&fgets(client->contact.telephone,13,stdin) && \
2017-04-05 23:06:44 +02:00
printf("\nEntrez l'adresse email :") && clean_keyboard() &&fgets(client->contact.email,__STRLEN__,stdin)))
2017-04-02 16:44:34 +02:00
{
printf("\nErreur lors de l'encodage des coo de contact");
exit(-1);
}
cleanFgets(client->contact.telephone);cleanFgets(client->contact.email);
encoderDate(&(client->anniversaire), "\nAnniversaire client:");
}
void afficherClient(Client *client, Date *date)
{
2017-04-07 10:32:52 +02:00
printf("\nClient : %s %s (%d)\n", client->nom, client->prenom, ageClient(client, date));
2017-04-02 16:44:34 +02:00
printf("rue : %s\n",client->adresse.rue);
printf("numero : %s\n",client->adresse.numero);
printf("localité : %s\n",client->adresse.localite);
printf("code postal : %d\n",client->adresse.codePostal);
printf("numéro de tél. : %s\n",client->contact.telephone);
printf("adresse email : %s\n",client->contact.email);
printf("\nPoints de fidelite aqui : %d\n", client->fidelite);
}
2017-04-04 20:12:52 +02:00
void sauvegarderClientTXT(Client *client, FILE * file, long arg, ...) //EcrireDonneesClient
2017-04-02 16:44:34 +02:00
{
2017-04-02 17:02:15 +02:00
va_list ap;
int age = 0;
2017-04-04 20:12:52 +02:00
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)
2017-04-02 17:02:15 +02:00
{
age = va_arg(ap,int);
2017-04-04 20:12:52 +02:00
fprintf(file, "Client : %s %s (%d)\n", client->nom, client->prenom,age);
}
2017-04-05 23:06:44 +02:00
else
2017-04-04 20:12:52 +02:00
{
fprintf(file, "Client : %s %s \n", client->nom, client->prenom);
2017-04-02 17:02:15 +02:00
}
2017-04-02 16:44:34 +02:00
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);
2017-04-05 23:06:44 +02:00
if(arg == 1)
2017-04-03 10:57:10 +02:00
{
2017-04-02 17:02:15 +02:00
fprintf(file, "\nPoints de fidelite aqui : %d\n", client->fidelite);
2017-04-03 10:57:10 +02:00
}
else
{
fprintf(file, "\n===========================================================================\n");
}
2017-04-05 23:06:44 +02:00
va_end(ap);
2017-04-02 16:44:34 +02:00
}
void encoderFacture(Panier * panier)
{
char continuer = 'y';
do{
if(panier->nbrArt <= __MAXNBRART__)
{
encoderArticle(&(panier->article[panier->nbrArt-1]));
printf("Y-a-t-il encore un article ?(Y|n)");
clean_keyboard();
scanf("%c", &continuer);
if(panier->nbrArt < __MAXNBRART__ && (continuer|0b00100000) == 'y')
{
panier->nbrArt++;
Article * tempArticle;
tempArticle = (Article*)realloc(panier->article, sizeof(Article)*(panier->nbrArt));
if(panier->article == NULL)
{
2017-04-07 10:32:52 +02:00
printf("ERREUR ALLOCATION panier->article !");
2017-04-02 16:44:34 +02:00
free(tempArticle);
free(panier->article);
exit(-1);
}
else
{
panier->article = tempArticle;
}
}
else if((continuer|0b00100000) == 'y')
{
system("cls");
printf("Vous avez ateint le nombre d'article maximum !\n\n");
break;
}
}
}while((continuer|0b00100000) == 'y' && panier->nbrArt <= __MAXNBRART__);
}
FILE * creerFichier(Date * date, Client * client)
{
char fileName[__STRLEN__+16];
fileName[0] = '\0';
char dir[__STRLEN__];
int i;
char buf[5];
printf("Dossier d'enregistrement : ");
clean_keyboard();
scanf("%s", dir);
//Toute cette partie aurais pus être remplacé par un sprintf
2017-04-07 10:32:52 +02:00
sprintf(fileName, "%s%d%d%d%d_", client->nom, date->anne, date->moi, date->jour);
2017-04-02 16:44:34 +02:00
strcat(fileName,client->nom);
itoaY(date->annee, buf);
strcat(fileName,buf);
itoaDM(date->moi, buf);
strcat(fileName, buf);
itoaDM(date->jour, buf);
strcat(fileName, buf);
strcat(fileName,"_");
for(i=1; i<= 99; i++)
{
char fileNameTmp[__STRLEN__+16+255];
sprintf(fileNameTmp,"%s%s%d.txt",dir,fileName,i);
2017-04-05 23:06:44 +02:00
if(!fichierExistant(fileNameTmp))
{
FILE * f = fopen(fileNameTmp,"w");
if(f==NULL)
2017-04-02 16:44:34 +02:00
{
2017-04-05 23:06:44 +02:00
printf("Erreur lors de l'ouverture du fichier");
return creerFichier(date, client);
2017-04-02 16:44:34 +02:00
}
2017-04-05 23:06:44 +02:00
else
return f;
}
2017-04-02 16:44:34 +02:00
}
return NULL;
}
Bool fichierExistant(char * filename)
{
FILE * f = fopen(filename, "r");
fclose(f);
if(f==NULL)
2017-04-05 23:06:44 +02:00
return FALSE;
2017-04-02 16:44:34 +02:00
else
2017-04-05 23:06:44 +02:00
return TRUE;
2017-04-02 16:44:34 +02:00
}
void sauvergarderFacture(FILE * file, Date * date, Client * client)
{
int i;
float prixTot = 0;
fprintf(file,"La Bulle Informatique\nRoute du Condroz 78, 4123 Neupre\n");
fprintf(file,"\nFacture a payer\n");
fprintf(file,"Date de facturation :");
fprintf(file,"%02d/%02d/%02d", date->jour, date->moi, date->annee);
fprintf(file, "\n Info client :");
2017-04-02 17:02:15 +02:00
sauvegarderClientTXT(client, file, 1,ageClient(client,date));
2017-04-02 16:44:34 +02:00
fprintf(file,"\n|---------------------------------------------------------------------------------------------|\
2017-04-05 23:06:44 +02:00
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
\n|---------------------------------------------------------------------------------------------|");
2017-04-02 16:44:34 +02:00
for(i=0;client->panier.nbrArt>i;i++)
{
fprintf(file,"\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
2017-04-05 23:06:44 +02:00
client->panier.article[i].deisgnation,client->panier.article[i].prixHTVA,client->panier.article[i].quant\
,(client->panier.article[i].quant>=20)?'x':' ',\
(client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant);
2017-04-02 16:44:34 +02:00
prixTot += (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant;
}
fprintf(file,"\n|---------------------------------------------------------------------------------------------|");
fprintf(file,"\nPrix totale :%f", prixTot);
fprintf(file, "\nPoints de fidelite aqui : %d\n", client->fidelite);
}
void afficherFacture(Date * date, Client * client)
{
int i;
float prixTot = 0;
printf("La Bulle Informatique\nRoute du Condroz 78, 4123 Neupre\n");
printf("\nFacture a payer\n");
printf("Date de facturation :");
printf("%02d/%02d/%02d", date->jour, date->moi, date->annee);
printf("\n Info client :");
afficherClient(client, date);
printf("\n|---------------------------------------------------------------------------------------------|\
2017-04-05 23:06:44 +02:00
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
\n|---------------------------------------------------------------------------------------------|");
2017-04-02 16:44:34 +02:00
for(i=0;client->panier.nbrArt>i;i++)
{
printf("\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
2017-04-05 23:06:44 +02:00
client->panier.article[i].deisgnation,client->panier.article[i].prixHTVA,client->panier.article[i].quant\
,(client->panier.article[i].quant>=20)?'x':' ',\
(client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant);
2017-04-02 16:44:34 +02:00
prixTot += (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant;
}
printf("\n|---------------------------------------------------------------------------------------------|");
client->fidelite = pointsFidelite(prixTot);
printf("\nPrix totale :%f", prixTot);
printf("\nPoints de fidelite aqui : %d\n", client->fidelite);
}
void cleanFgets(char * str)
{
char *p = strchr(str, '\n');
if(p!=NULL)
2017-04-05 23:06:44 +02:00
*p = '\0';
2017-04-02 16:44:34 +02:00
else
{
printf("Une erreur est arrivé lors du nettoygae \\0");
exit(-1);
}
}
int itoaY(int i, char * buf)
{
buf [0] = '0';
buf [1] = '0';
buf [2] = '0';
buf [3] = '0';
buf [4] = '\0';
int j;
if(!i)
2017-04-05 23:06:44 +02:00
return 1;
2017-04-02 16:44:34 +02:00
buf [3] = '1';
for(j=1; j<=i && j<=9999 ; j++)
{
if(!(j%1000))
{
buf[0]++;
buf[1]='0';
buf[2]='0';
buf[3]='0';
}
else if(!(j%100))
{
buf[1]++;
buf[2]='0';
buf[3]='0';
}
else if(!(j%10))
{
buf[2]++;
buf[3]='0';
}
else
{
buf[3]++;
}
}
if(j>9999)
2017-04-05 23:06:44 +02:00
return 0;
2017-04-02 16:44:34 +02:00
else
2017-04-05 23:06:44 +02:00
return 1;
2017-04-02 16:44:34 +02:00
}
int itoaDM(int i, char * buf)
{
buf[0] = '0';
buf[1] = '0';
buf[2] = '\0';
int j;
if(!i)
2017-04-05 23:06:44 +02:00
return 1;
2017-04-02 16:44:34 +02:00
buf[1]++;
for(j=1; j<=i && j<=99 ; j++)
{
if(!(j%10))
{
(buf[0])++;
(buf[1])='0';
}
else
{
(buf[1])++;
}
}
if(j>99)
2017-04-05 23:06:44 +02:00
return 0;
2017-04-02 16:44:34 +02:00
else
2017-04-05 23:06:44 +02:00
return 1;
2017-04-02 16:44:34 +02:00
}
Bool clean_keyboard()
{
fflush(stdin);
// char c;
2017-04-05 23:06:44 +02:00
//while ((c = getchar()) != '\n' && c != EOF) { }
2017-04-02 16:44:34 +02:00
/* do {
2017-04-05 23:06:44 +02:00
c = getc(stdin);
printf("%c\n", c);
} while (c != '\n' && c != EOF);*/
return 1;
2017-04-02 16:44:34 +02:00
}
int ageClient(Client * client, Date * date)
{
if((date->moi == client->anniversaire.moi && date->jour >= client->anniversaire.jour) || (date->moi > client->anniversaire.moi))//anniversaire passé
{
return (date->annee - client->anniversaire.annee);
}
else
2017-04-05 23:06:44 +02:00
return (date->annee - client->anniversaire.annee - 1);
2017-04-02 16:44:34 +02:00
}
int pointsFidelite(float prix)
{
return (int)(prix/10);
}
void gestionFacture(Client * client, Date * date)
{
FILE * file;
printf("%d/%d/%d\n", date->jour, date->moi, date->annee );
file = creerFichier(date, client);
client->panier.article = (Article *)malloc(sizeof(Article));
client->panier.nbrArt = 1;
if(client->panier.article == NULL)
{
printf("ERREUR ALLOCATION ARTICLE!");
free(client->panier.article);
exit(-1);
}
encoderFacture(&client->panier);
afficherFacture(date, client);
sauvergarderFacture(file, date, client);
// getch();
fclose(file);
free(client->panier.article);
}
2017-04-04 20:12:52 +02:00
long int rechercheClient(char nom[__STRLEN__], char prenom[__STRLEN__], FILE * clientDatF, Client * client, Date * date)
2017-04-02 16:44:34 +02:00
{
2017-04-04 20:12:52 +02:00
long int ofsetFileCurClient;
2017-04-04 22:52:21 +02:00
fseek(clientDatF, sizeof(unsigned long int)+sizeof(Bool), SEEK_SET);
2017-04-04 20:12:52 +02:00
clearerr(clientDatF); //Clear EOF
2017-04-02 16:44:34 +02:00
do
{
2017-04-04 20:12:52 +02:00
ofsetFileCurClient = ftell(clientDatF);
fread(client, sizeof(Client), 1, clientDatF);
2017-04-05 23:06:44 +02:00
afficherClient(client, date);
2017-04-04 20:12:52 +02:00
}while(!feof(clientDatF) && compNP(client->nom, client->prenom, nom, prenom));
if(feof(clientDatF))
2017-04-05 23:06:44 +02:00
return 0;
2017-04-02 16:44:34 +02:00
else
2017-04-05 23:06:44 +02:00
return ofsetFileCurClient;
2017-04-02 16:44:34 +02:00
}
2017-04-07 10:32:52 +02:00
Bool gestionClient(Client * client, Date * date, FILE * clientDatF, FILE * clientTXTF)
2017-04-02 16:44:34 +02:00
{
char choix;
while(1)
{
do
{
printf("A. Donnees client\n");
2017-04-05 23:06:44 +02:00
printf("B. Modification client\n");//acce au file *
2017-04-02 17:02:15 +02:00
printf("C. Facture\n");
printf("D. Retour\n");
printf("E. Quitter");
2017-04-02 16:44:34 +02:00
clean_keyboard();scanf("%c", &choix);
choix |= 0b00100000;
2017-04-02 19:10:59 +02:00
}while(!(choix == 'a' || choix == 'b' || choix == 'c' || choix == 'd' || choix == 'e'));
2017-04-02 16:44:34 +02:00
switch (choix) {
2017-04-05 23:06:44 +02:00
case 'a':
2017-04-02 16:44:34 +02:00
afficherClient(client, date);
break;
2017-04-05 23:06:44 +02:00
case 'b':
2017-04-07 10:32:52 +02:00
modificationClient(client, clientDatF, clientTXTF);
2017-04-02 16:44:34 +02:00
break;
2017-04-05 23:06:44 +02:00
case 'c':
2017-04-02 17:02:15 +02:00
gestionFacture(client, date);
2017-04-02 16:44:34 +02:00
break;
2017-04-05 23:06:44 +02:00
case 'd':
2017-04-02 17:02:15 +02:00
return 1;
break;
2017-04-05 23:06:44 +02:00
case 'e':
2017-04-02 16:44:34 +02:00
return 0;
break;
}
}
}
2017-04-05 23:06:44 +02:00
void localiserFichiersClients(char * fichierClientDat, FILE ** clientDatF, FILE ** clientTXTF)
2017-04-02 16:44:34 +02:00
{
2017-04-02 17:02:15 +02:00
2017-04-02 16:44:34 +02:00
char rep[__STRLEN_FILE__];
2017-04-02 17:02:15 +02:00
char repTmp[2][__STRLEN_FILE__];
2017-04-05 23:06:44 +02:00
unsigned long int nbClients = 0;
Bool tri = 0;
2017-04-02 17:02:15 +02:00
printf("Repertoir ou se trouve le fichier client.dat et client.txt");
2017-04-02 16:44:34 +02:00
clean_keyboard();
scanf("%s", rep);
2017-04-02 17:02:15 +02:00
sprintf(repTmp[__TAB_CLIENTDAT_FILE__],"%sclients.dat", rep);
sprintf(repTmp[__TAB_CLIENTTXT_FILE__],"%sclients.txt", rep);
2017-04-05 23:06:44 +02:00
*clientTXTF = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r"); //A faire Une verification correcte se ferait avec access (voir GNU libc P418)
*clientDatF = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r"); //A faire Une verification correcte se ferait avec access (voir GNU libc P418)
if(*clientDatF == NULL || *clientTXTF == NULL) //Si un des fichier n'existe pas
2017-04-02 16:44:34 +02:00
{
char c;
2017-04-02 17:02:15 +02:00
printf("\nUn fichier n'existe pas. Voulez-vous les creers (C) ou changer de repertoir (R). (C|R)?");
2017-04-05 23:06:44 +02:00
if(!(*clientDatF == NULL && *clientTXTF == NULL))
2017-04-02 17:02:15 +02:00
printf("Attention un des deux fichier a été détecter ! Il seront remis a 0 si vous les creers\nChoix (C|R):" );
2017-04-02 16:44:34 +02:00
clean_keyboard();scanf("%c", &c);
2017-04-02 17:02:15 +02:00
if((c|0b00100000) == 'c')
2017-04-02 16:44:34 +02:00
{
2017-04-05 23:06:44 +02:00
fclose(*clientDatF);
fclose(*clientTXTF);
*clientDatF = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "wb");
*clientTXTF = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "w");
fwrite(&nbClients, sizeof(nbClients), 1, *clientDatF);
fwrite(&tri, sizeof(tri), 1, *clientDatF);
fclose(*clientDatF);
fclose(*clientTXTF);
2017-04-02 16:44:34 +02:00
}
else
2017-04-05 23:06:44 +02:00
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
2017-04-02 16:44:34 +02:00
}
else
2017-04-02 17:02:15 +02:00
{
2017-04-05 23:06:44 +02:00
fclose(*clientDatF);
fclose(*clientTXTF);
2017-04-02 17:02:15 +02:00
}
2017-04-05 23:06:44 +02:00
clearerr(*clientDatF);clearerr(*clientTXTF);
*clientDatF = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r+b");
*clientTXTF = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r+");
strcpy(fichierClientDat, repTmp[__TAB_CLIENTDAT_FILE__]);
if(*clientDatF == NULL || *clientTXTF == NULL)
2017-04-02 16:44:34 +02:00
{
printf("\nErreur ouverture fichier");
2017-04-05 23:06:44 +02:00
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
2017-04-02 16:44:34 +02:00
}
}
2017-04-04 20:12:52 +02:00
int sauvegarderClientFile(FILE * clientTXTF, FILE * clientDatF, Client * client)
2017-04-02 16:44:34 +02:00
{
2017-04-04 20:12:52 +02:00
/* 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);
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
2017-04-05 23:06:44 +02:00
fseek(clientDatF, client->ofsetFichierDat, SEEK_SET);
2017-04-04 20:12:52 +02:00
fwrite(client, sizeof(Client), 1, clientDatF);
fflush(clientDatF);
return client->ofsetFichierDat;
2017-04-02 16:44:34 +02:00
}
2017-04-02 17:02:15 +02:00
2017-04-03 10:57:10 +02:00
int compNP(char *nom1, char *prenom1,char *nom2, char *prenom2)
2017-04-02 16:44:34 +02:00
{
2017-04-03 10:57:10 +02:00
unsigned int strLen1, strLen2;
char * tmp;
for(strLen1 = 0, tmp = nom1;*tmp++;strLen1++);
for(tmp = prenom1;*tmp++;strLen1++);
2017-04-05 23:06:44 +02:00
for(strLen2 = 0, tmp = nom2;*tmp++;strLen2++);
2017-04-03 10:57:10 +02:00
for(tmp = prenom2;*tmp++;strLen2++);
char * np1 = malloc(strLen1+1);
char * np2 = malloc(strLen2+1);
2017-04-02 17:02:15 +02:00
int renvois;
sprintf(np1,"%s%s", nom1, prenom1);
sprintf(np2,"%s%s", nom2, prenom2);
renvois = strcmp(np1,np2);
if(renvois>0)
2017-04-05 23:06:44 +02:00
renvois = 1;
2017-04-02 17:02:15 +02:00
else if(renvois<0)
2017-04-05 23:06:44 +02:00
renvois = -1;
2017-04-02 17:02:15 +02:00
return renvois;
2017-04-02 16:44:34 +02:00
}
2017-04-02 17:02:15 +02:00
2017-04-05 23:06:44 +02:00
void clientBinToTXT(FILE * bin, FILE* txt)/*Si je veux sup des client, il fau que j'aie le nom du fichier
le standard c n'autorise que l'ajout et la modif */
2017-04-02 17:00:23 +02:00
{
2017-04-03 10:57:10 +02:00
Client client;
2017-04-02 17:00:23 +02:00
fseek(bin, 0, SEEK_SET);fseek(txt, 0, SEEK_SET);
2017-04-03 10:57:10 +02:00
clearerr(bin);clearerr(txt);
2017-04-02 19:10:59 +02:00
fseek(bin, sizeof(unsigned long int), SEEK_CUR);
fseek(bin, sizeof(Bool), SEEK_CUR);
2017-04-03 10:57:10 +02:00
while(!feof(bin))
{
fread(&client, sizeof(Client), 1,bin);
sauvegarderClientTXT(&client, txt, 0);
2017-04-04 20:12:52 +02:00
fprintf(txt, "========================================================================");
2017-04-03 10:57:10 +02:00
}
2017-04-05 23:06:44 +02:00
// fread()
2017-04-02 17:00:23 +02:00
}
2017-04-03 13:35:51 +02:00
2017-04-04 20:12:52 +02:00
void modificationClient(Client * client, FILE * clientDatF, FILE * clientTXTF)
2017-04-03 13:35:51 +02:00
{
2017-04-07 10:32:52 +02:00
char choix = 0;
void * aModifier[] = {(Adresse *)&client->adresse, (char *)&client->contact.telephone, (char *)&client->contact.email};
char * aAfficher[] = {"Adresse", "Telephne", "E-mail"};
char w;
2017-04-04 20:12:52 +02:00
do
{
2017-04-07 10:32:52 +02:00
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 :");clean_keyboard();scanf("%d", (int *)&choix);
}while(!(choix == 1 || choix == 2 || choix == 3));
printf("Modification %s :", aAfficher[choix-1]);
if(choix == 1)
2017-04-04 20:12:52 +02:00
{
2017-04-07 10:32:52 +02:00
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);
2017-04-04 20:12:52 +02:00
}
2017-04-07 10:32:52 +02:00
else
{
printf("Modification de %s :", aAfficher[choix-1]);
clean_keyboard();
scanf("%s", (char *)aModifier[choix-1]);
}
printf("Y-a-til encore des Modification (y|n) ?");
clean_keyboard();scanf("%c", &choix);
}while((choix&0b00100000) == 'y');
sauvegarderClientFile(clientTXTF, clientDatF, client);
2017-04-03 13:35:51 +02:00
}