add 29
This commit is contained in:
parent
aa9900460a
commit
da768d089c
|
@ -586,7 +586,7 @@ void localiserFichiersClients(char * fichierClientDat, FILE ** clientDatF, FILE
|
|||
|
||||
int sauvegarderClientFile(FILE * clientTXTF, FILE * clientDatF, Client * client)
|
||||
{
|
||||
/* Si Nouveau client ofsetFichierDat = 0 donc fin fichier sinon on decale de l'ofset */
|
||||
/* Si Nouveau client ofsetFichierDat = 0 donc nouveau client sinon on decale de l'ofset */
|
||||
if(!client->ofsetFichierDat)
|
||||
{
|
||||
unsigned long int nbrClient;
|
||||
|
|
|
@ -0,0 +1,692 @@
|
|||
|
||||
/*
|
||||
* Fichier client :
|
||||
*------------------------------------------------------------
|
||||
*|UINTL NBClients|Bool Tri| Client 0 | Client 1 | .......ETc|
|
||||
*------------------------------------------------------------
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <conio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define __TVA__ 1.21
|
||||
#define __REDUC__ 0.9
|
||||
#define __MAXNBRART__ 3
|
||||
|
||||
#define __STRLEN__ 34
|
||||
#define __STRLEN_FILE__ 500
|
||||
|
||||
#define __TAB_CLIENTDAT_FILE__ 0
|
||||
#define __TAB_CLIENTTXT_FILE__ 1
|
||||
|
||||
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;
|
||||
long ofsetFichierDat;
|
||||
long ofsetFichierTXT;
|
||||
} 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 *);
|
||||
int pointsFidelite(float);
|
||||
void gestionFacture(Client *, Date *);
|
||||
void cleanFgets(char*);
|
||||
int itoaY(int i, char*);
|
||||
int itoaDM(int i, char*);
|
||||
Bool clean_keyboard();
|
||||
int ageClient(Client *, Date *);
|
||||
long int rechercheClient(char *, char*, FILE *, Client *, Date *);
|
||||
Bool gestionClient(Client *, Date *, FILE *, FILE *);
|
||||
int compNP(char *, char *,char *, char *);
|
||||
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 *, long,...); /*EcrireDonneesClient
|
||||
si long = 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, long)
|
||||
*/
|
||||
void clientBinToTXT(FILE *, FILE*);//En cours de dev
|
||||
void modificationClient(Client *, FILE *, FILE *);
|
||||
|
||||
int main()
|
||||
{
|
||||
//printf("%d\n", pointsFidelite(52.23));
|
||||
FILE * clientDatF = NULL;
|
||||
FILE * clientTXTF = NULL;
|
||||
Client * client;
|
||||
long int ofsetFileCurClient = 0;
|
||||
if(ofsetFileCurClient);//Pour ne pas avoir de warn
|
||||
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;
|
||||
// printf("%d/%d/%d\n", date->jour, date->moi, date->annee );
|
||||
}
|
||||
localiserFichiersClients(fichierClientDat, &clientDatF, &clientTXTF);
|
||||
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)
|
||||
{
|
||||
case 1:
|
||||
client = malloc(sizeof(Client));
|
||||
client->ofsetFichierDat = 0;
|
||||
client->ofsetFichierTXT = 0;
|
||||
encoderClient(client);
|
||||
afficherClient(client, &date);
|
||||
fseek(clientTXTF, 0, SEEK_END);
|
||||
sauvegarderClientFile(clientTXTF, clientDatF, client);
|
||||
w = gestionClient(client, &date, clientDatF, clientTXTF);
|
||||
break;
|
||||
case 2:
|
||||
client = malloc(sizeof(Client));
|
||||
printf("\nNom du client (sensible a la casse) : ");
|
||||
clean_keyboard();scanf("%s", nomClient);
|
||||
printf("\nPrenom du client (sensible a la casse) : ");
|
||||
clean_keyboard();scanf("%s", prenomClient);
|
||||
ofsetFileCurClient = rechercheClient(nomClient,prenomClient, clientDatF, client, &date);
|
||||
if(ofsetFileCurClient)
|
||||
{
|
||||
w = gestionClient(client, &date, clientDatF, clientTXTF);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\nClient non trouvé !\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(client);
|
||||
fclose(clientDatF);
|
||||
fclose(clientTXTF);
|
||||
return 0;
|
||||
}
|
||||
void encoderDate(Date * date, char *invite)
|
||||
{
|
||||
do {
|
||||
printf("\n%s\n", invite);
|
||||
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)
|
||||
{
|
||||
int m = date->jour;
|
||||
int d = date->moi;
|
||||
int y = 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)
|
||||
{
|
||||
if( d <= daysinmonth[m-1] )
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
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)
|
||||
{
|
||||
printf("\nEncoage Client\n");
|
||||
client->fidelite = 0;
|
||||
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);
|
||||
if(!(printf("\n Entrez le numéro de tél. :") && clean_keyboard() &&fgets(client->contact.telephone,13,stdin) && \
|
||||
printf("\nEntrez l'adresse email :") && clean_keyboard() &&fgets(client->contact.email,__STRLEN__,stdin)))
|
||||
{
|
||||
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)
|
||||
{
|
||||
printf("\nClient : %s %s (%d)\n", client->nom, client->prenom, ageClient(client, date));
|
||||
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);
|
||||
}
|
||||
void sauvegarderClientTXT(Client *client, FILE * file, long arg, ...) //EcrireDonneesClient
|
||||
{
|
||||
va_list ap;
|
||||
int age = 0;
|
||||
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, "Client : %s %s (%d)\n", client->nom, client->prenom,age);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(file, "Client : %s %s \n", client->nom, client->prenom);
|
||||
}
|
||||
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(arg == 1)
|
||||
{
|
||||
fprintf(file, "\nPoints de fidelite aqui : %d\n", client->fidelite);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(file, "\n===========================================================================\n");
|
||||
}
|
||||
va_end(ap);
|
||||
}
|
||||
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)
|
||||
{
|
||||
printf("ERREUR ALLOCATION panier->article !");
|
||||
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;
|
||||
printf("Dossier d'enregistrement : ");
|
||||
clean_keyboard();
|
||||
scanf("%s", dir);
|
||||
|
||||
sprintf(fileName, "%s%d%d%d_", client->nom, date->annee, date->moi, date->jour);
|
||||
|
||||
for(i=1; i<= 99; i++)
|
||||
{
|
||||
char fileNameTmp[__STRLEN__+16+255];
|
||||
sprintf(fileNameTmp,"%s%s%d.txt",dir,fileName,i);
|
||||
if(!fichierExistant(fileNameTmp))
|
||||
{
|
||||
FILE * f = fopen(fileNameTmp,"w");
|
||||
if(f==NULL)
|
||||
{
|
||||
printf("Erreur lors de l'ouverture du fichier");
|
||||
return creerFichier(date, client);
|
||||
}
|
||||
else
|
||||
return f;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
Bool fichierExistant(char * filename)
|
||||
{
|
||||
FILE * f = fopen(filename, "r");
|
||||
fclose(f);
|
||||
if(f==NULL)
|
||||
return FALSE;
|
||||
else
|
||||
return TRUE;
|
||||
}
|
||||
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 :");
|
||||
sauvegarderClientTXT(client, file, 1,ageClient(client,date));
|
||||
fprintf(file,"\n|---------------------------------------------------------------------------------------------|\
|
||||
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
|
||||
\n|---------------------------------------------------------------------------------------------|");
|
||||
|
||||
for(i=0;client->panier.nbrArt>i;i++)
|
||||
{
|
||||
fprintf(file,"\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
|
||||
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);
|
||||
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|---------------------------------------------------------------------------------------------|\
|
||||
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
|
||||
\n|---------------------------------------------------------------------------------------------|");
|
||||
|
||||
for(i=0;client->panier.nbrArt>i;i++)
|
||||
{
|
||||
printf("\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
|
||||
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);
|
||||
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)
|
||||
*p = '\0';
|
||||
else
|
||||
{
|
||||
printf("Une erreur est arrivé lors du nettoygae \\0");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
Bool clean_keyboard()
|
||||
{
|
||||
fflush(stdin);
|
||||
// char c;
|
||||
//while ((c = getchar()) != '\n' && c != EOF) { }
|
||||
/* do {
|
||||
c = getc(stdin);
|
||||
printf("%c\n", c);
|
||||
} while (c != '\n' && c != EOF);*/
|
||||
return 1;
|
||||
}
|
||||
|
||||
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
|
||||
return (date->annee - client->anniversaire.annee - 1);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
long int rechercheClient(char *nom, char *prenom, FILE * clientDatF, Client * client, Date * date)
|
||||
{
|
||||
long int ofsetFileCurClient;
|
||||
int t;
|
||||
fseek(clientDatF, sizeof(unsigned long int)+sizeof(Bool), SEEK_SET);
|
||||
clearerr(clientDatF); //Clear EOF
|
||||
do
|
||||
{
|
||||
ofsetFileCurClient = ftell(clientDatF);
|
||||
fread(client, sizeof(Client), 1, clientDatF);
|
||||
t = compNP(client->nom, client->prenom, nom, prenom);
|
||||
}while(!feof(clientDatF) && t);
|
||||
if(feof(clientDatF))
|
||||
{
|
||||
client = NULL;
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
return ofsetFileCurClient;
|
||||
}
|
||||
|
||||
Bool gestionClient(Client * client, Date * date, FILE * clientDatF, FILE * clientTXTF)
|
||||
{
|
||||
char choix;
|
||||
while(1)
|
||||
{
|
||||
do
|
||||
{
|
||||
printf("A. Donnees client\n");
|
||||
printf("B. Modification client\n");//acce au file *
|
||||
printf("C. Facture\n");
|
||||
printf("D. Retour\n");
|
||||
printf("E. Quitter");
|
||||
clean_keyboard();scanf("%c", &choix);
|
||||
choix |= 0b00100000;
|
||||
}while(!(choix == 'a' || choix == 'b' || choix == 'c' || choix == 'd' || choix == 'e'));
|
||||
switch (choix) {
|
||||
case 'a':
|
||||
afficherClient(client, date);
|
||||
break;
|
||||
case 'b':
|
||||
modificationClient(client, clientDatF, clientTXTF);
|
||||
break;
|
||||
case 'c':
|
||||
gestionFacture(client, date);
|
||||
break;
|
||||
case 'd':
|
||||
sauvegarderClientFile(clientTXTF, clientDatF, client);
|
||||
return 1;
|
||||
break;
|
||||
case 'e':
|
||||
sauvegarderClientFile(clientTXTF, clientDatF, client);
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void localiserFichiersClients(char * fichierClientDat, FILE ** clientDatF, FILE ** clientTXTF)
|
||||
{
|
||||
|
||||
char rep[__STRLEN_FILE__];
|
||||
char repTmp[2][__STRLEN_FILE__];
|
||||
unsigned long int nbClients = 0;
|
||||
Bool tri = 0;
|
||||
printf("Repertoir ou se trouve le fichier client.dat et client.txt");
|
||||
clean_keyboard();
|
||||
scanf("%s", rep);
|
||||
sprintf(repTmp[__TAB_CLIENTDAT_FILE__],"%sclients.dat", rep);
|
||||
sprintf(repTmp[__TAB_CLIENTTXT_FILE__],"%sclients.txt", rep);
|
||||
*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
|
||||
{
|
||||
char c;
|
||||
printf("\nUn fichier n'existe pas. Voulez-vous les creers (C) ou changer de repertoir (R). (C|R)?");
|
||||
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')
|
||||
{
|
||||
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);
|
||||
}
|
||||
else
|
||||
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
fclose(*clientDatF);
|
||||
fclose(*clientTXTF);
|
||||
}
|
||||
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)
|
||||
{
|
||||
printf("\nErreur ouverture fichier");
|
||||
localiserFichiersClients(fichierClientDat, clientDatF, clientTXTF);
|
||||
}
|
||||
}
|
||||
|
||||
int sauvegarderClientFile(FILE * clientTXTF, FILE * clientDatF, Client * client)
|
||||
{
|
||||
/* Si Nouveau client ofsetFichierDat = 0 donc nouveau client 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);
|
||||
client->ofsetFichierTXT = ftell(clientTXTF);
|
||||
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)
|
||||
{
|
||||
unsigned int strLen1, strLen2;
|
||||
char * tmp;
|
||||
for(strLen1 = 0, tmp = nom1;*tmp++;strLen1++);
|
||||
for(tmp = prenom1;*tmp++;strLen1++);
|
||||
for(strLen2 = 0, tmp = nom2;*tmp++;strLen2++);
|
||||
for(tmp = prenom2;*tmp++;strLen2++);
|
||||
char * np1 = malloc(strLen1+1);
|
||||
char * np2 = malloc(strLen2+1);
|
||||
int renvois=0;
|
||||
sprintf(np1,"%s%s", nom1, prenom1);
|
||||
sprintf(np2,"%s%s", nom2, prenom2);
|
||||
renvois = strcmp(np1,np2);
|
||||
if(renvois>0)
|
||||
renvois = 1;
|
||||
else if(renvois<0)
|
||||
renvois = -1;
|
||||
free(np1);free(np2);
|
||||
return renvois;
|
||||
}
|
||||
|
||||
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 EN COURS*/
|
||||
{
|
||||
Client client;
|
||||
fseek(bin, 0, SEEK_SET);fseek(txt, 0, SEEK_SET);
|
||||
clearerr(bin);clearerr(txt);
|
||||
fseek(bin, sizeof(unsigned long int), SEEK_CUR);
|
||||
fseek(bin, sizeof(Bool), SEEK_CUR);
|
||||
while(!feof(bin))
|
||||
{
|
||||
if(fread(&client, sizeof(Client), 1,bin))
|
||||
sauvegarderClientTXT(&client, txt, 0);
|
||||
}
|
||||
fflush(txt);
|
||||
// fread()
|
||||
}
|
||||
|
||||
void modificationClient(Client * client, FILE * clientDatF, FILE * clientTXTF)
|
||||
{
|
||||
char choix = 0;
|
||||
void * aModifier[] = {(Adresse *)&client->adresse, (char *)&client->contact.telephone, (char *)&client->contact.email};
|
||||
char * aAfficher[] = {"Adresse", "Telephne", "E-mail"};
|
||||
do
|
||||
{
|
||||
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)
|
||||
{
|
||||
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("\nErreur lors de l'encodage de l'adresse");
|
||||
exit(-1);
|
||||
}
|
||||
cleanFgets(client->adresse.rue);cleanFgets(client->adresse.numero);cleanFgets(client->adresse.localite);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
clientBinToTXT(clientDatF, clientTXTF);
|
||||
}
|
Loading…
Reference in New Issue