Programme principal


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <math.h>
#include <strstrea.h>
#include <iomanip.h>
#include "saisie.h"
#include "type_def.h"
#include "code.h"
#include "fct_base.h"
#include "auto_op.h"




void Affiche_compte (char *nom_fich, int debut, int fin, int ligne_1)
// 11090
// d -> debut
// f -> fin
// vb -> ligne_1
{
int i;
char chaine[LONG_VISU+1];
Fiche fiche;

  for (i = debut; i <= fin; i++) {
    Lecture_ligne (nom_fich, i, fiche);
    formatte_fiche (fiche, chaine, i);
    gotoxy (1, i - debut + ligne_1);
    printf ("%s",chaine);
    clreol ();
  }
}


int Mode_paiement (pctxt contexte, Mode_Paiement &mode_paiement, int again,
              int saisie_cheque = 1)
// 10980
// c retourné
// pm$(c) dans le libelle
// ca -> again
// saisie_cheque : 1 pour demander la saisie du numéro de chèque
{
int ky;
char pm[6][9] = { "CHEQUE  ", "CB.     ", "PREL.   ", "VIR.    ", "TIP     ", "AUTRE   " };
static int c=0;
int i;
char tampon[9];

  if (!again) {
    gotoxy (1,17);
    printf ("Type de paiement");
    do {
      c += 6;
      c %= 6;
      for (i=0; i < 6; i++) {
        gotoxy (10*i+1,18);
        if (i == c) textattr (INVERSE);
        cprintf ("%s", pm[i]);
        textattr (NORMAL);
      }
      ky = getch ();
      if ( ky == 0) {
        ky = 1000 + getch ();
      }
      switch (ky) {
        case FL_DR :
          c++;
          break;
        case FL_GA :
          c--;
          break;
        case ESCAPE :
          return -1;
        case RETURN :
          break;
        default :
          break;
      }
    }
    while (ky != RETURN);
  }
  if ((c == 0) && (saisie_cheque == 1) && (again <= 1)) {
  char defaut[9];
    gotoxy (1,18);
    clreol ();
    gotoxy (1,17);
    clreol ();
    printf ("N° de chèque ");
    if (contexte->num_chq != 0) {
      sprintf (defaut,"%08li",contexte->num_chq +1);
      Saisie_clavier (NON_VIDE, 8, tampon, defaut);
    }
    else {
      Saisie_clavier (NON_VIDE, 8, tampon);
    }
    if (tampon[0] == ESCAPE) {
      return -1;
    }
    mode_paiement.numero_chq = atol (tampon);
  }
  else {
    mode_paiement.numero_chq = 0;
  }
  mode_paiement.type = TYPE_CHEQUE + c;
  gotoxy (1,17);
  clreol ();
  gotoxy (1,18);
  clreol ();
  return c;
}

int Recherche_ligne (pctxt contexte, Fiche &fiche, int marquee_ok = 0)
// retourne -1 si escape
//        le numéro de l'enregistrement sinon
// 11440
// xe -> marquée_ok
// a$(1) -> chaine
{
char pm[5][20] = {"N. D'ENREGISTREMENT", "DATE               ",
            "TYPE DE PAIEMENT   ", "CREDIT/DEBIT       ",
            "CODE COMPTA.       "};
static int c=0;
int i, l;
int ky;
int encore;
Fiche fiche_ex;
char chaine_aff[LONG_VISU+1];
char chaine_cherchee[30];
char tampon[30];
char reponse[2];
int debut, longueur;
TMontant val;
TDate date;

  if (!contexte->again) {
    gotoxy (1, 16);
    printf ("Choisissez le moyen d'identification");
    do {
      c +=5;
      c %= 5;
      for (i = 0; i < 5; i++) {
        gotoxy (20 * (i - 3 * (i > 2))+1, 17 + i / 3);
        if (i == c) {
          textattr (INVERSE);
        }
        cprintf ("%s", pm[i]);
        textattr (NORMAL);
      }
      ky = getch ();
      if (ky == 0) {
        ky = 1000 + getch ();
      }
      switch (ky) {
        case FL_DR :
          c++;
          break;
        case FL_GA :
          c--;
          break;
        case FL_HA :
        case FL_BA :
        case ESPACE :
          if (c < 2)
            c += 3;
          else
            if (c > 2)
              c += 2;
          break;
        case ESCAPE :
          eff_esp_trav ();
          return -1;
        case RETURN :
          break;
        default :
          break;
      }
    }
    while (ky != RETURN);
  } // fin du if
  gotoxy (1, 17);
  clreol ();
  gotoxy (1, 18);
  clreol ();
  switch (c) {
    case 2 : // Type de paiement
      debut = (int) ((int) &(fiche_ex.mode_paiement) - (int) &(fiche_ex));
      longueur = sizeof(fiche_ex.mode_paiement);
      l = Mode_paiement (contexte, fiche_ex.mode_paiement, contexte->again, 1);
      if (l == -1) {
        eff_esp_trav();
        return -1;
      }
      break;
    case 1 : // Date
      debut = (int) ((int) &(fiche_ex.Date_Op) - (int) &(fiche_ex));
      longueur = sizeof (fiche_ex.Date_Op);
      eff_esp_trav ();
      l = Saisie_date (contexte, fiche_ex.Date_Op);
      if (l != 0) {
        eff_esp_trav();
        return -1;
      }
      break;
    case 0 : // Numero d'enregistrement
      eff_esp_trav ();
      do {
      char defaut[5];
        gotoxy (1,16);
        printf ("%s ", pm[0]);
        sprintf (defaut, "%-4i", contexte->suivant -1);  // par defaut le dernier
        Saisie_clavier (NON_VIDE, 4, chaine_cherchee, defaut);
        if (chaine_cherchee[0] == ESCAPE) {
          eff_esp_trav();
          return -1;
        }
        l = atoi (chaine_cherchee);
      }
      while ((l < 1) || (l >= contexte->suivant));
      Lecture_ligne (contexte->nom_fichier, l, fiche);
      return l;
    case 3 : // Credit Debit
      eff_esp_trav ();
      gotoxy (1,16);
      printf ("(C)rédit ou (D)ébit ?");
      do {
        encore = 0;
        debut = (int) ((int) &(fiche_ex.montant) - (int) &(fiche_ex));
        longueur = sizeof(fiche_ex.montant);
        switch (getch ()) {
          case ESCAPE :
            eff_esp_trav();
            return -1;
          case 'C' :
          case 'c' :
            do {
              eff_esp_trav ();
              gotoxy (1,16);
              printf ("Crédit ");
              Saisie_clavier (NON_VIDE, LONG_MAX_MONTANT, tampon);
              if (tampon[0] == ESCAPE) {
                eff_esp_trav();
                return -1;
              }
              fiche_ex.montant = TMontant (tampon, LONG_MAX_MONTANT);
              fiche_ex.montant = abs(fiche_ex.montant);
            }
            while (!(fiche_ex.montant.IsValid()));
            break;
          case 'D' :
          case 'd' :
            do {
              eff_esp_trav ();
              gotoxy (1,16);
              printf ("Débit ");
              Saisie_clavier (NON_VIDE, LONG_MAX_MONTANT, tampon);
              if (tampon[0] == ESCAPE) {
                eff_esp_trav();
                return -1;
              }
              fiche_ex.montant = TMontant (tampon, LONG_MAX_MONTANT);
              fiche_ex.montant = -abs(fiche_ex.montant);
            }
            while (!(val.IsValid()));
            break;
          default :
            encore = 1;
        }
      }
      while (encore);
      break;
    case 4 : // Code compta
      debut = (int) ((int) &(fiche_ex.code) - (int) &(fiche_ex));
      longueur = sizeof(fiche_ex.code);
      eff_esp_trav();
      gotoxy (1,16);
      printf ("Code compta. ");
      l = Saisie_clavier (NON_VIDE, longueur-1, fiche_ex.code);
      if (chaine_cherchee[0] == ESCAPE) {
        eff_esp_trav();
        return -1;
      }
      strupr (fiche_ex.code);
      for (i=l; i<longueur-1; i++) {
        fiche_ex.code[i] = ' ';
      }
      fiche_ex.code[longueur-1] = 0;
      break;
  }
  // recherche de fiche_ex
  i = contexte->suivant;
  while ( --i >= contexte->rang_max ) {
    eff_esp_trav ();
    Lecture_ligne (contexte->nom_fichier, i, fiche);
    if ( ((memcmp (((char*)&fiche)+debut, ((char*)&fiche_ex)+debut, longueur)) == 0)
      && ((fiche.Date_Marquage.Year() == ANNEE_NON_MARQ)
        || (marquee_ok == 0)) ) {
      gotoxy (1,16);
      formatte_fiche (fiche, chaine_aff, i);
      printf ("%s",chaine_aff);
      do {
        encore = 0;
        gotoxy (1,17);
        printf ("Est-ce la bonne fiche (O/N) ");
        Saisie_clavier (NON_VIDE, 1, reponse, "N");
        switch (reponse[0]) {
          case ESCAPE :
            return -1;
          case 'N' :
          case 'n' :
            break;
          case 'O' :
          case 'o' :
            return i;
          default :
            encore = 1;
        }
      }
      while (encore);
    }
  } // fin du while
  printf ("Donnée non trouvée");
  return -1;
}


int creation_fichier (char *nom)
// cree le fichier de nom
// retourne -1 si annulation
// sinon 0
{
char solde[LONG_MAX_MONTANT+1], solde2[LONG_MAX_MONTANT+1];
int i;
TDate date;
ctxt Contexte;

  eff_esp_trav ();
  gotoxy (1,16);
  printf ("Numéro de compte (%i caract. max.)", LONG_NUM_COMPTE);
  Saisie_clavier (NON_VIDE, LONG_NUM_COMPTE, Contexte.numero_compte);
  if ( Contexte.numero_compte [0] == ESCAPE ) {
    eff_esp_trav ();
    return -1;
  }
  i = Saisie_date (NULL, Contexte.date_creation);
  if (i != 0 ) {
    eff_esp_trav ();
    return -1;
  }
  gotoxy (1, 20);
  cout << "Solde banque du compte au " << Contexte.date_creation;
  Saisie_clavier (NON_VIDE, LONG_MAX_MONTANT, solde);
  if (solde[0] == ESCAPE ) {
    eff_esp_trav ();
    return -1;
  }
  Contexte.solde_banque = TMontant (solde, LONG_MAX_MONTANT);
  Contexte.solde_banque_init = TMontant (solde, LONG_MAX_MONTANT);
  gotoxy (1, 21);
  cout << "Solde réel du compte au " << Contexte.date_creation;
  Saisie_clavier (NON_VIDE, LONG_MAX_MONTANT, solde2, solde);
  if (solde2[0] == ESCAPE) {
    eff_esp_trav ();
    return -1;
  }
  Contexte.solde_reel = TMontant (solde2, LONG_MAX_MONTANT);
  Contexte.solde_reel_init = TMontant (solde2, LONG_MAX_MONTANT);
  // Valeurs de debut constantes
  Contexte.suivant = 1;
  Contexte.rang_max = 0;
  Contexte.date_sys = Contexte.date_creation;
  Contexte.num_chq = 0;
  // enregistrement
  close ( creat (nom, S_IREAD | S_IWRITE)); // cree le fichier
  Ecriture_ligne (nom, 0, Contexte);
  return 0;                 // tout est OK
}


void nouveau_fichier ()
{
char nom[LONG_NOM_FICHIER];
char Ext[5];
int i;
FILE *fichier;

  eff_esp_trav ();
  gotoxy (1, 16);
  printf ("Nom du fichier ");
  Saisie_clavier (NON_VIDE, LONG_NOM_FICHIER - 1 - strlen(DIR_COMPTE), nom);
  if ( nom [0] == ESCAPE ) {
    eff_esp_trav ();
    return;
  }
  // Recherche l'extension
  Ext[0] = 0;
  for (i=strlen (nom); i>0; i--) {
    if (nom[i] == '.') {
      strncpy (Ext, nom+i, 4);
      Ext[4] = 0;
      break;
    }
    else if (nom[i] == '\\') {
      break;
    }
  }
  if (Ext[0] == 0) {
    // Ajoute l'extension
    strcat (nom, EXT_COMPTE);
  }
  if (strchr(nom,'\\') == NULL) {
    // pas de path, on rajoute en defaut
    Ajoute_Path (nom);
  }
  if ((fichier = fopen ( (char *) nom, "r")) == NULL ) {
    //  Nouveau compte
    if (creation_fichier (nom) == -1) {
      // si pas création
      return;
    }
  }
  else {
    fclose (fichier);
  }
  fichier = fopen (NOM_FICHIER , "wt");
  if (fichier != NULL) {
    fprintf (fichier, "%s\n", nom);
    fclose (fichier);
  }
  eff_esp_trav ();
  gotoxy (1,16);
  printf ("INITIALISATION EFFECTUE");
}


int quitter (pctxt contexte)
{
char ky;

  eff_esp_trav ();
  gotoxy (1, 17);
  printf ("Voulez-vous vraiment quitter (O/N) ?");
  do {
    ky = getch ();
    if (ky == 0) {
      getch ();
    }
    ky = toupper (ky);
    if ( (ky == 'N') || (ky == ESCAPE)) {
      eff_esp_trav ();
      return 0;
    }
  }
  while ( ky != 'O');
  eff_esp_trav ();
  gotoxy (1,17);
  printf ("Voulez-vous imprimer l'état (O/N) ?");
  do {
    ky = getch ();
    if (ky == 0) {
      getch ();
    }
    if (ky == ESCAPE) {
      eff_esp_trav ();
      return 0;
    }
    ky = toupper (ky);
  }
  while (( ky != 'O') && ( ky != 'N'));
  if ( ky == 'O') {
  char reel[LONG_MAX_MONTANT+1],banque[LONG_MAX_MONTANT+1];

    fprintf (stdprn, "Etat au %0.2i/%0.2i/%0.2i\n",
            contexte->date_sys.Day(),
            contexte->date_sys.Month(),
            contexte->date_sys.Year());
    ostrstream (banque, LONG_MAX_MONTANT+1) << setw(LONG_MAX_MONTANT)
                              << contexte->solde_banque;
    ostrstream (reel, LONG_MAX_MONTANT+1) << setw(LONG_MAX_MONTANT)
                              << contexte->solde_reel;
    fprintf (stdprn, "FICHIER: %s\n SOLDE BANQUE: %s SOLDE REEL: %s\n",
          contexte->nom_fichier,
          banque, reel);
  }
  return 1;
}



void part1 ()
{
int c = 0;
int ky;
char pm[2][34] = { "ACCES AU COMPTE                  ",
           "INITIALISATION DU FICHIER DONNEES"};


  eff_esp_menu ();
  eff_esp_fich ();
  gotoxy (10,25);
  textattr (INVERSE);
  cprintf (" MENU PRINCIPAL ");
  textattr (NORMAL);
  clreol ();
  gotoxy (1,22);
  printf ("%s\n%s", pm[0], pm[1]);
  do {
    do {
      gotoxy (1,22+c);
      textattr (INVERSE);
      cprintf ("%s", pm[c]);
      textattr (NORMAL);
      ky = getch ();
      if (ky == 0) {
        ky = 1000 + getch ();
      }
      switch (ky) {
        case FL_GA :
        case FL_DR :
        case ' ' :
        case FL_HA :
        case FL_BA :
          gotoxy(1,22+c);
          printf ("%s",pm[c]);
          c++;
          c %= 2;
          break;
        default :
          break;
      }
    }
    while (ky != RETURN);
    eff_esp_trav ();
    if ( c == 1 ) {
      nouveau_fichier ();
    }
  }
  while ( c == 1);
  eff_esp_trav ();
  eff_esp_menu ();
  gotoxy (1, 25);
  clreol ();
  gotoxy (1,16);
  printf ("Accès au compte en cours...");
}


void marquage (pctxt contexte)
{
int c, cr;
Fiche fiche;
TDate date;
char ligne[LONG_VISU+1];

  eff_esp_trav ();
  c = Recherche_ligne (contexte, fiche, 1);
  if (c == -1) {
    return;
  }
  eff_esp_trav ();
  if (fiche.Date_Marquage.Year() != ANNEE_NON_MARQ) {
    gotoxy (1, 17);
    printf ("Le marquage a déjà été opéré sur cette donnée");
    return;
  }
  gotoxy (1,20);
  textattr (INVERSE);
  formatte_fiche (fiche, ligne, c);
  cprintf ("%s",ligne);
  textattr (NORMAL);
  gotoxy (1,16);
  printf ("Entrez la date du marquage");
  cr = Saisie_date (contexte, fiche.Date_Marquage);
  if (cr != 0) {
    eff_esp_trav ();
    return;
  }
  contexte->solde_banque += fiche.montant;
  eff_esp_trav ();
  Ecriture_ligne (contexte->nom_fichier, c, fiche);
  gotoxy (1,16);
  printf ("Marquage effectué");
  return;
}

void debit (pctxt contexte)
{
Fiche fiche;
char tampon[LONG_MAX_MONTANT+1];
int c,i;
long val=0;
TMontant montant;
int pasbon;

  eff_esp_trav ();
  i = Saisie_date (contexte, fiche.Date_Op);
  eff_esp_trav ();
  if (i != 0) {
    return;
  }
  gotoxy (1,16);
  cout << "DATE : " << fiche.Date_Op;
  c = Mode_paiement (contexte, fiche.mode_paiement, contexte->again, (contexte->again != 2));
  if (c == -1) {
    eff_esp_trav ();
    return;
  }
  gotoxy (1,17);
  clreol ();
  if (c == 0) {
    if (contexte->again == 2) {
      val = contexte->num_chq + 1;
      fiche.mode_paiement.numero_chq = val;
    }
    else {
      val = fiche.mode_paiement.numero_chq;
    }
    printf ("Cheque : ");
  }
  printf ("%s", mode2str(fiche.mode_paiement));
  gotoxy (1,18);
  printf ("Objet (%i Caracts. max.)", LONG_LIB-1);
  c = Saisie_clavier (VIDE_OK, LONG_LIB-1, fiche.libelle);
  if (fiche.libelle[0] == ESCAPE) {
    eff_esp_trav ();
    return;
  }
  if (stricmp (fiche.libelle, "annule") == 0) {
    strupr (fiche.libelle);
    fiche.montant = 0.0;
    strcpy (fiche.code, "ANNULE   ");
    fiche.Date_Marquage = fiche.Date_Op;
  }
  else {
    gotoxy (1,19);
    printf ("Débit ");
    Saisie_clavier (NON_VIDE, LONG_MAX_MONTANT, tampon);
    if (tampon[0] == ESCAPE ) {
      eff_esp_trav ();
      return;
    }
    fiche.montant = TMontant (tampon, LONG_MAX_MONTANT);
    if (fiche.montant > 0) {
      fiche.montant *= -1;
    }
    do {
      pasbon =0;
      gotoxy (1,20);
      printf ("Code compta. (%i Caracts. max.)", LONG_CODE-1);
      c = Saisie_clavier (NON_VIDE, LONG_CODE-1, fiche.code);
      if (fiche.code[0] == ESCAPE) {
        eff_esp_trav ();
        return;
      }
      for (i=c; i<LONG_CODE-1; i++) {
        fiche.code[i] = ' ';
      }
      fiche.code[LONG_CODE-1]=0;
      if (Code_bon (contexte->nom_fich_code, contexte->nb_code, fiche.code) == -1) {
        gotoxy (1,21);
        printf ("Code compta incorrect");
        pasbon = 1;
      }
    }
    while (pasbon==1);
  }
  fiche.Date_Marquage = TDate (1, 1, ANNEE_NON_MARQ);
  Ecriture_ligne (contexte->nom_fichier, contexte->suivant++, fiche);
  tri_fichier (contexte, contexte->suivant-1);
  eff_esp_trav ();
  contexte->solde_reel += fiche.montant;
  if ( val !=0 ) {
    contexte->num_chq = val;
  }
}

void credit (pctxt contexte)
{
Fiche fiche;
int c,i;
int pasbon;
char tampon[LONG_MAX_MONTANT+1];

  eff_esp_trav ();
  i = Saisie_date (contexte, fiche.Date_Op);
  eff_esp_trav ();
  if (i != 0) {
    return;
  }
  gotoxy (1,16);
  cout << "DATE : " << fiche.Date_Op;
  c = Mode_paiement (contexte, fiche.mode_paiement, contexte->again, 0);
  if (c == -1) {
    eff_esp_trav ();
    return;
  }
  fiche.mode_paiement.numero_chq = 0;
  gotoxy (1,17);
  printf ("%s", mode2str (fiche.mode_paiement));
  gotoxy (1,18);
  printf ("Objet (%i Caracts. max.)", LONG_LIB-1);
  c = Saisie_clavier (VIDE_OK, LONG_LIB-1, fiche.libelle);
  if (fiche.libelle[0] == ESCAPE) {
    eff_esp_trav ();
    return;
  }
  gotoxy (1,19);
  printf ("Crédit ");
  Saisie_clavier (NON_VIDE, LONG_MAX_MONTANT, tampon);
  if (tampon[0] == ESCAPE ) {
    eff_esp_trav ();
    return;
  }
  fiche.montant = TMontant (tampon, LONG_MAX_MONTANT);
  if (fiche.montant < 0) {
    fiche.montant *= -1;
  }
  do {
    pasbon =0;
    gotoxy (1,20);
    printf ("Code compta. (%i Caracts. max.)", LONG_CODE-1);
    c = Saisie_clavier (NON_VIDE, LONG_CODE-1, fiche.code);
    if (fiche.code[0] == ESCAPE) {
      eff_esp_trav ();
      return;
    }
    for (i=c; i<LONG_CODE-1; i++) {
      fiche.code[i] = ' ';
    }
    fiche.code[LONG_CODE-1]=0;
    if (Code_bon (contexte->nom_fich_code, contexte->nb_code, fiche.code) == -1) {
      gotoxy (1,21);
      printf ("Code compta incorrect");
      pasbon = 1;
    }
  }
  while (pasbon==1);
  fiche.Date_Marquage = TDate (1, 1, ANNEE_NON_MARQ);
  Ecriture_ligne (contexte->nom_fichier, contexte->suivant++, fiche);
  tri_fichier (contexte, contexte->suivant-1);
  eff_esp_trav ();
  contexte->solde_reel += fiche.montant;
}


void modification_ligne (pctxt contexte)
{
Fiche fiche;
char ligne[LONG_VISU+1];
char tampon[LONG_VISU+1], tampon2[LONG_VISU+1];
int c,i;
int num_enr;
char pm[8][17] = {"DATE            ", "TYPE DE PAIEMENT", "OBJET           ",
            "CREDIT          ", "DEBIT           ", "CODE COMPTA.    ",
            "MARQUAGE        ", "RETOUR MENU     "};
int ky;
int encore_une_fois;
TMontant modif_solde_banque;
TMontant modif_solde_reel;

  eff_esp_trav ();
  num_enr = Recherche_ligne (contexte, fiche);
  if ( num_enr == -1) {
    return;
  }
  contexte->again = 0;
  c = 0;
  do {
    encore_une_fois = 1;
    eff_esp_trav ();
    do {
      c += 8;
      c %= 8;
      gotoxy (1,20);
      textattr (INVERSE);
      formatte_fiche (fiche, ligne, num_enr);
      cprintf ("%s",ligne);
      textattr (NORMAL);
      clreol ();
      gotoxy (1,16);
      printf ("Que voulez-vous modifier ?");
      for (i=0; i<8; i++) {
        gotoxy (20*(i%4)+1, 17 + i/4);
        if (i==c) {
          textattr (INVERSE);
        }
        cprintf ("%s", pm[i]);
        textattr (NORMAL);
      }
      gotoxy (27,25);
      printf ("DATE SYSTEME : %0.2i/%0.2i/%0.4i",
            contexte->date_sys.DayOfMonth(),
            contexte->date_sys.Month(),
            contexte->date_sys.Year());
      ky = getch ();

      if (ky == 0) {
        ky = 1000+getch();
      }
      switch (ky) {
        case FL_DR :
          c++;
          break;
        case FL_GA :
          c--;
          break;
        case ESPACE :
        case FL_BA :
        case FL_HA :
          c += 4;
          break;
        case ESCAPE :
          return;
        case RETURN :
          encore_une_fois = 0;
          break;
      } // fin switch
    }
    while (encore_une_fois); // fin selection menu
    switch (c) {
      case 0 : // date op
        eff_esp_trav ();
        gotoxy (1,20);
        textattr(INVERSE);
        cprintf ("%s",ligne);
        textattr(NORMAL);
        gotoxy(1,16);
        printf ("Modification de ");
        textattr (INVERSE);
        cout << fiche.Date_Op;
        textattr (NORMAL);
        // La date n'est pas modifiee si escape
        Saisie_date (contexte, fiche.Date_Op);
        break;
      case 1 :  // mode de paiement
        eff_esp_trav ();
        gotoxy (1,20);
        textattr(INVERSE);
        cprintf ("%s", ligne);
        textattr(NORMAL);
        gotoxy (1,16);
        printf ("Modification de ");
        textattr(INVERSE);
        cprintf ("%.8s", mode2str (fiche.mode_paiement));
        textattr (NORMAL);
        Mode_paiement (contexte, fiche.mode_paiement, contexte->again,
                  (fiche.montant <= 0));
        break;
      case 2 : { // objet
        char defaut[LONG_LIB];
          eff_esp_trav ();
          gotoxy (1,20);
          textattr(INVERSE);
          cprintf ("%s", ligne);
          textattr(NORMAL);
          gotoxy (1,16);
          printf ("Modification de ");
          textattr(INVERSE);
          cprintf ("%.*s", LONG_LIB-1, fiche.libelle);
          textattr (NORMAL);
          gotoxy (1,18);
          printf ("%s ", pm[2]);
          sprintf (defaut, "%.*s", LONG_LIB-1, fiche.libelle);
          Saisie_clavier (VIDE_OK, LONG_LIB-1, tampon, defaut);
          if ((tampon[0] != 27) && (tampon[0] != 0)) {
            strcpy (fiche.libelle, tampon);
          }
          break;
        } // case objet
      case 3 : { // credit
        char defaut[LONG_MAX_MONTANT+1];
          eff_esp_trav ();
          gotoxy (1,20);
          textattr(INVERSE);
          cprintf ("%s", ligne);
          textattr(NORMAL);
          gotoxy (1,16);
          printf ("Modification de ");
          textattr(INVERSE);
          cout << setw (LONG_MAX_MONTANT) << fiche.montant << ends;
          textattr (NORMAL);
          gotoxy (1,18);
          printf ("%s ", pm[c]);
          ostrstream def (defaut, LONG_MAX_MONTANT+1);
          def << setw (LONG_MAX_MONTANT) << fiche.montant << ends;
          Saisie_clavier (VIDE_OK, LONG_MAX_MONTANT, tampon, defaut);
          if ((tampon[0] != 27) && (tampon[0] != 0)) {
          TMontant montant(tampon, LONG_MAX_MONTANT);
            if (montant < 0) {
              montant = abs(montant);
            }
            modif_solde_reel -= fiche.montant - montant;
            if (fiche.Date_Marquage.Year() != ANNEE_NON_MARQ) {
              modif_solde_banque -= fiche.montant - montant;
            }
            fiche.montant = montant;
          }
          break;
        }
      case 4 : { // debit
        char defaut[LONG_MAX_MONTANT+1];
          eff_esp_trav ();
          gotoxy (1,20);
          textattr(INVERSE);
          cprintf ("%s", ligne);
          textattr(NORMAL);
          gotoxy (1,16);
          printf ("Modification de ");
          textattr(INVERSE);
          cout << setw (LONG_MAX_MONTANT) << fiche.montant << ends;
          textattr (NORMAL);
          gotoxy (1,18);
          printf ("%s ", pm[c]);
          ostrstream def (defaut, LONG_MAX_MONTANT+1);
          def << setw (LONG_MAX_MONTANT) << fiche.montant << ends;
          Saisie_clavier (VIDE_OK, LONG_MAX_MONTANT, tampon, defaut);
          if ((tampon[0] != 27) && (tampon[0] != 0)) {
          TMontant montant(tampon, LONG_MAX_MONTANT);
            if (montant > 0) {
              montant = -abs(montant);
            }
            modif_solde_reel -= fiche.montant - montant;
            if (fiche.Date_Marquage.Year() != ANNEE_NON_MARQ) {
              modif_solde_banque -= fiche.montant - montant;
            }
            fiche.montant = montant;
          }
          break;
        }
      case 5 : { // code compta
        char defaut[10];
          eff_esp_trav ();
          gotoxy (1,20);
          textattr(INVERSE);
          cprintf ("%s", ligne);
          textattr(NORMAL);
          gotoxy (1,16);
          printf ("Modification de ");
          textattr(INVERSE);
          cprintf ("%.*s", LONG_CODE-1, fiche.code);
          textattr (NORMAL);
          sprintf (defaut, "%.*s", LONG_CODE-1, fiche.code);
          do {
            gotoxy (1,18);
            printf ("%s ", pm[5]);
            Saisie_clavier (NON_VIDE, LONG_CODE-1, tampon, defaut);
            if (tampon[0] == 27) {
              break;
            }
            sprintf (tampon2, "%-*s", LONG_CODE-1, tampon);
            strupr (tampon2);
            i = Code_bon (contexte->nom_fich_code, contexte->nb_code, tampon2);
            if (i == -1) {
              gotoxy (1,19);
              printf ("Code compta Incorrect !");
            }
            else {
              strcpy (fiche.code, tampon2);
            }
          }
          while (i==-1);
          break;
        }
      case 6 : // date marquage
        if (fiche.Date_Marquage.Year() == ANNEE_NON_MARQ) {
          break;
        }
        eff_esp_trav ();
        gotoxy (1,20);
        textattr(INVERSE);
        cprintf ("%s", ligne);
        textattr(NORMAL);
        gotoxy (1,16);
        printf ("Modification de ");
        textattr(INVERSE);
        cout << fiche.Date_Marquage;
        textattr (NORMAL);
        printf (" (Alt-Entree pour annuler le marquage)");
        i = Saisie_date (contexte, fiche.Date_Marquage, 1);
        if (i == RET_VIDE) {
          fiche.Date_Marquage = TDate (1, 1, ANNEE_NON_MARQ);
          modif_solde_banque -= fiche.montant;
          break;
        }
        break;
      case 7 :
        Ecriture_ligne (contexte->nom_fichier, num_enr, fiche);
        tri_fichier (contexte, num_enr);
        contexte->solde_banque += modif_solde_banque;
        contexte->solde_reel += modif_solde_reel;
        return;
    }
    /* Place le curseur sur la retour menu */
    c = 7;
  }
  while (1);
}


void  modification (pctxt contexte)
{
char pm[3][15] ={ "DONNEE GESBANQ", "CODES COMPTA. ", "OP. AUTO.     "};
int i;
static int c=0;
int ky;
int encore_une_fois=1;

  eff_esp_trav ();
  gotoxy (1,17);
  printf ("Que voulez-vous modifier ?");
  do {
    c %= 3;
    for (i=0; i<3; i++) {
      gotoxy (i*19 +1, 19);
      if ( i == c ) {
        textattr (INVERSE);
      }
      cprintf ("%s", pm[i]);
      textattr (NORMAL);
    }
    ky = getch ();
    if (ky == 0) {
      ky = 1000 + getch ();
    }
    switch (ky) {
      case FL_DR :
        c++;
        break;
      case FL_GA :
        c--;
        break;
      case ESCAPE :
        eff_esp_trav ();
        return;
      case RETURN :
        encore_une_fois = 0;
        break;
    }
  }
  while (encore_une_fois);
  switch (c) {
    case 0 :
      modification_ligne (contexte);
      break;
    case 1 :
      edite_code (contexte);
      break;
    case 2 :
//      edite_op_auto (contexte);
      break;
  }
  eff_esp_trav ();
}


void recherche (pctxt contexte)
{
int num_enr;
int deb, ligne1;
int ky;
Fiche fiche;

  eff_esp_trav ();
  num_enr = Recherche_ligne (contexte, fiche);
  if (num_enr == -1)
    return;
  eff_esp_trav ();
  do {
    if (num_enr < 12) {
      ligne1 = 14 - num_enr;
      deb = 1;
    }
    else {
      ligne1 = 2;
      deb = num_enr - 11;
    }
    Affiche_compte (contexte->nom_fichier, deb, num_enr, ligne1);
    ky = getch ();
    if (ky == 0) {
      ky = 1000 + getch ();
    }
    switch (ky) {
      case SCR_HA :
        num_enr -= 12;
        if (num_enr <= 1) {
          num_enr = 1;
        }
        break;
      case SCR_BA :
      case ' ' :
        num_enr += 12;
        if (num_enr >= contexte->suivant) {
          num_enr = contexte->suivant-1;
        }
        break;
      case FL_BA :
        if (num_enr != contexte->suivant -1) {
          num_enr++;
        }
        break;
      case FL_HA :
        if (num_enr > 1) {
          num_enr--;
        }
        break;
      case ESCAPE :
        return;
      case HOME :
        if (contexte->suivant < 13) {
          num_enr = contexte->suivant - 1;
        }
        else {
          num_enr = 12;
        }
        break;
      case FIN :
        num_enr = contexte->suivant -1;
        break;
    }
  }
  while (1);
}

void listing (pctxt contexte)
{
char pm[2][12] = { "IMPRIMANTE ", "ECRAN      " };
static int c=0;
int c2 = 0;
int encore = 0, encore2 = 0;
int i,j;
int ky;
char tampon[2],tampon2[20], tampon3[10];
int inf,sup;
FILE *sortie;
int nligne;
Fiche fiche;
char ligne[LONG_VISU+1];
char pm2[4][13] = { "N. DE CHEQUE", "CB.         ", "CODE COMPTA.", "NON DEBITE  " };
int co;
TMontant cr,de;
int encore_une_fois;


  eff_esp_trav ();
  if (!contexte->again) {
    do {
      c %= 2;
      for (i=0; i<2; i++) {
        gotoxy (1+ 19*i,16);
        if (i==c) {
          textattr (INVERSE);
        }
        cprintf ("%s", pm[i]);
        textattr (NORMAL);
      }
      ky = getch();
      if (ky == 0) {
        ky = 1000 + getch();
      }
      switch (ky) {
        case FL_DR :
        case FL_GA :
          c ++;
          break;
        case ESCAPE :
          eff_esp_trav ();
          return;
        case RETURN :
          encore = 1;
          break;
      }
    }
    while (encore == 0);
  }
  if (c == 0) {
    sortie = stdprn;
  }
  else {
    sortie = stdout;
  }
  gotoxy (1,16);
  clreol ();
  printf ("Edition sur l'%s", pm[c]);
  clreol();
  do {
    encore = 1;
    gotoxy (1,17);
    printf ("Voulez-vous un tri (O/N) ");
    Saisie_clavier (NON_VIDE, 1, tampon, "O");
    strupr (tampon);
    switch (tampon[0]) {
      case 'O' :
        gotoxy (1,17);
        clreol ();
        printf ("Tri sur quoi ?");
        do {
          c2 += 4;
          c2 %= 4;
          for (i=0 ; i<4; i++) {
            gotoxy (20*i +1,18);
            if (i == c2) {
              textattr (INVERSE);
            }
            cprintf ("%s", pm2[i]);
            textattr (NORMAL);
          }
          ky = getch ();
          if (ky == 0) {
            ky = 1000+getch ();
          }
          switch (ky) {
            case FL_DR :
              c2++;
              break;
            case FL_GA :
              c2--;
              break;
            case ESCAPE :
              eff_esp_trav ();
              return;
            case RETURN :
              encore2 = 1;
              break;
          }
        }
        while (encore2 == 0);
        eff_esp_trav();
        switch (c2) {
          case 0 : {
            Mode_Paiement cle;

            // Numero de cheque
            cle.type = TYPE_CHEQUE;
            gotoxy (1,16);
            printf ("%s", pm2[c2]);
            do {
              Saisie_clavier (NON_VIDE, 8, tampon2);
              if (tampon2[0] == ESCAPE) {
                eff_esp_trav ();
                return;
              }
            }
            while ((cle.numero_chq = atol (tampon2)) == 0);
            if (c==0) {
              fprintf (sortie, "\rTRI SUR %s\rLE : %0.2i/%0.2i/%0.2i\r"
                    "\rNo   DATE    TYPE       OBJET                 CREDIT   DEBIT     CODE      MARQ.\r"
                      "---- ----- -------- ------------------------- -------- --------- --------- -----\r"
                      ,pm2[c2], contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year() );
            }
            cr = 0;
            de = 0;
            co = 0;
            nligne = 2;
            if (c == 1) {
              eff_esp_fich ();
            }
            for (i=1; i<contexte->suivant; i++) {
              if (kbhit () && (getch () == ESCAPE)) {
                return;
              }
              Lecture_ligne (contexte->nom_fichier, i, fiche);
              if (fiche.mode_paiement.numero_chq == cle.numero_chq) {
                if (c==1) {
                  if (nligne == 14) {
                    getch ();
                    nligne = 2;
                    eff_esp_fich ();
                  }
                  gotoxy (1,nligne);
                }
                formatte_fiche (fiche, ligne, i);
                fprintf (sortie, "%s\r", ligne);
                nligne ++;
                co++;
                if (fiche.montant >= 0) {
                  cr += fiche.montant;
                }
                else {
                  de += fiche.montant;
                }
              }
            }
            if (co == 0) {
              gotoxy (1,17);
              fprintf (sortie, "Aucune donnée trouvée\r");
            }
            if (c == 0) {
            char credit[LONG_MAX_MONTANT+1], debit[LONG_MAX_MONTANT+1];
            ostrstream out_cr(credit, LONG_MAX_MONTANT+1);
            ostrstream out_de(debit, LONG_MAX_MONTANT+1);

              out_cr << setw(LONG_MAX_MONTANT) << cr << ends;
              out_de << setw(LONG_MAX_MONTANT) << de << ends;
              fprintf (sortie, "\rCrédit : %s\rDébit : %s\r", credit, debit);
            }
            eff_esp_trav ();
            getch ();
          }
          break;

          case 1 :
            // CB
            if (c==0) {
              fprintf (sortie, "\rTRI SUR %s\rLE : %0.2i/%0.2i/%0.2i\r"
                    "\rNo   DATE    TYPE       OBJET                 CREDIT   DEBIT     CODE      MARQ.\r"
                      "---- ----- -------- ------------------------- -------- --------- --------- -----\r"
                      ,pm2[c2], contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year() );
            }
            cr = 0;
            de = 0;
            nligne = 2;
            if (c == 1) {
              eff_esp_fich ();
            }
            for (i=1; i<contexte->suivant; i++) {
              if (kbhit () && (getch () == ESCAPE)) {
                return;
              }
              Lecture_ligne (contexte->nom_fichier, i, fiche);
              if (fiche.mode_paiement.type == TYPE_CB) {
                if (c==1) {
                  if (nligne == 14) {
                    getch ();
                    nligne = 2;
                    eff_esp_fich ();
                  }
                  gotoxy (1,nligne);
                }
                formatte_fiche (fiche, ligne, i);
                fprintf (sortie, "%s\r", ligne);
                nligne ++;
                co++;
                if (fiche.montant >= 0) {
                  cr += fiche.montant;
                }
                else {
                  de += fiche.montant;
                }
              }
            }
            if (co == 0) {
              gotoxy (1,17);
              fprintf (sortie, "Aucune donnée trouvée\r");
            }
            if (c == 0) {
            char credit[LONG_MAX_MONTANT+1], debit[LONG_MAX_MONTANT+1];
            ostrstream out_cr(credit, LONG_MAX_MONTANT+1);
            ostrstream out_de(debit, LONG_MAX_MONTANT+1);

              out_cr << setw(LONG_MAX_MONTANT) << cr << ends;
              out_de << setw(LONG_MAX_MONTANT) << de << ends;
              fprintf (sortie, "\rCrédit : %s\rDébit : %s\r", credit, debit);
            }
            eff_esp_trav ();
            getch ();
            break;
          case 3 : // non debité
            if (c==0) {
              fprintf (sortie, "\rTRI SUR %s\rLE : %0.2i/%0.2i/%0.2i\r"
                    "\rNo   DATE    TYPE       OBJET                 CREDIT   DEBIT     CODE      MARQ.\r"
                      "---- ----- -------- ------------------------- -------- --------- --------- -----\r"
                      ,pm2[c2], contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year() );
            }
            cr = 0;
            de = 0;
            nligne = 2;
            if (c == 1) {
              eff_esp_fich ();
            }
            for (i=1; i<contexte->suivant; i++) {
              if (kbhit () && (getch () == ESCAPE)) {
                return;
              }
              Lecture_ligne (contexte->nom_fichier, i, fiche);
              if (fiche.Date_Marquage.Year() == ANNEE_NON_MARQ) {
                if (c==1) {
                  if (nligne == 14) {
                    getch ();
                    nligne = 2;
                    eff_esp_fich ();
                  }
                  gotoxy (1,nligne);
                }
                formatte_fiche (fiche, ligne, i);
                fprintf (sortie, "%s\r", ligne);
                nligne ++;
                co++;
                if (fiche.montant >= 0) {
                  cr += fiche.montant;
                }
                else {
                  de += fiche.montant;
                }
              }
            }
            if (co == 0) {
              gotoxy (1,17);
              fprintf (sortie, "Aucune donnée trouvée\r");
            }
            if (c == 0) {
            char credit[LONG_MAX_MONTANT+1], debit[LONG_MAX_MONTANT+1];
            ostrstream out_cr(credit, LONG_MAX_MONTANT+1);
            ostrstream out_de(debit, LONG_MAX_MONTANT+1);

              out_cr << setw(LONG_MAX_MONTANT) << cr << ends;
              out_de << setw(LONG_MAX_MONTANT) << de << ends;
              fprintf (sortie, "\rCrédit : %s\rDébit : %s\r", credit, debit);
            }
            eff_esp_trav ();
            getch ();
            break;
          case 2 : // code compta
            do {
              gotoxy (1,16);
              printf ("Tous les codes compta (O/N) ? ");
              Saisie_clavier (NON_VIDE, 1, tampon2, "N");
              if (tampon2[0] == ESCAPE) {
                eff_esp_trav ();
                return;
              }
              strupr (tampon2);
            }
            while ((tampon2[0] != 'O') && (tampon2[0] != 'N'));
            switch (tampon2[0]) {
              case 'O' :
                encore = 1;
                if ( c == 0) {
                  fprintf (sortie, "\rLISTING AVEC TRI SUR TOUS LES CODES COMPTA.\rLE : %0.2i/%0.2i/%0.2i\r\r"
                              "No   DATE    TYPE       OBJET                 CREDIT   DEBIT     CODE      MARQ.\r"
                              "---- ----- -------- ------------------------- -------- --------- --------- -----\r"
                              ,contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year() );
                }
                cr = 0;
                de = 0;
                for ( i=1; i<=contexte->nb_code; i++) {
                  co = 0;
                  Lecture_code (contexte->nom_fich_code, i, tampon3);
                  if (c == 1) {
                    gotoxy (1,17);
                    fprintf (sortie, "Code compta : %s", tampon3);
                    clreol ();
                    eff_esp_fich ();
                  }
                  nligne = 2;
                  for (j=1; j<contexte->suivant; j++) {
                    if (kbhit () && (getch () == ESCAPE)) {
                      return;
                    }
                    Lecture_ligne (contexte->nom_fichier, j, fiche);
                    if (!strncmp (tampon3, fiche.code, 9)) {
                      if (c==1) {
                        if (nligne == 14) {
                          getch();
                          nligne = 2;
                          eff_esp_fich ();
                        }
                        gotoxy (1,nligne);
                      }
                      formatte_fiche (fiche, ligne, j);
                      fprintf (sortie, "%s\r", ligne);
                      nligne ++;
                      co++;
                      if (fiche.montant >= 0) {
                        cr += fiche.montant;
                      }
                      else {
                        de += fiche.montant;
                      }
                    }
                  }
                  if (c == 1) {
                    if (co == 0) {
                      gotoxy (1,18);
                      printf ("Aucune donnée trouvée\r");
                    }
                    getch();
                    gotoxy (1,18);
                    clreol ();
                  }
                }
                if (c == 0) {
                char credit[LONG_MAX_MONTANT+1], debit[LONG_MAX_MONTANT+1];
                ostrstream out_cr(credit, LONG_MAX_MONTANT+1);
                ostrstream out_de(debit, LONG_MAX_MONTANT+1);

                  out_cr << setw(LONG_MAX_MONTANT) << cr << ends;
                  out_de << setw(LONG_MAX_MONTANT) << de << ends;
                  fprintf (sortie, "\rCrédit : %s\rDébit : %s\r", credit, debit);
                }
                eff_esp_trav ();
                break;
              case 'N' :
                encore = 1;
                encore_une_fois = 0;
                do {
                int l,i;

                  gotoxy (1,16);
                  printf ("Code compta. ");
                  l = Saisie_clavier (NON_VIDE, 9, tampon3);
                  if (tampon3[0] == ESCAPE) {
                    eff_esp_trav ();
                    return;
                  }
                  for (i=l; i<9; i++) tampon3[i]=' ';
                  tampon3[9]=0;
                  if  (Code_bon (contexte->nom_fich_code, contexte->nb_code, tampon3)) {
                    encore_une_fois = 1;
                    gotoxy (1,17);
                    printf ("Code compta Incorrect");
                  }
                }
                while (encore_une_fois);
                if (c==0) {
                  fprintf (sortie, "\rTRI SUR %s\rLE : %0.2i/%0.2i/%0.2i\r"
                        "\rNo   DATE    TYPE       OBJET                 CREDIT   DEBIT     CODE      MARQ.\r"
                          "---- ----- -------- ------------------------- -------- --------- --------- -----\r"
                        ,pm2[c2], contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year() );
                }
                cr = 0;
                de = 0;
                nligne = 2;
                if (c == 1) {
                  eff_esp_fich ();
                }
                for (i=1; i<contexte->suivant; i++) {
                  if (kbhit () && (getch () == ESCAPE)) {
                    return;
                  }
                  Lecture_ligne (contexte->nom_fichier, i, fiche);
                  if (!strncmp (tampon3, fiche.code, 9)) {
                    if (c==1) {
                      if (nligne == 14) {
                        while (!kbhit ());
                        nligne = 2;
                        eff_esp_fich ();
                      }
                      gotoxy (1,nligne);
                    }
                    formatte_fiche (fiche, ligne, i);
                    fprintf (sortie, "%s\r", ligne);
                    nligne ++;
                    co++;
                    if (fiche.montant >= 0) {
                      cr += fiche.montant;
                    }
                    else {
                      de += fiche.montant;
                    }
                  }
                }
                if (co == 0) {
                  gotoxy (1,16);
                  fprintf (sortie, "Aucune donnée trouvée\r");
                  clreol ();
                }
                if (c == 0) {
                char credit[LONG_MAX_MONTANT+1], debit[LONG_MAX_MONTANT+1];
                ostrstream out_cr(credit, LONG_MAX_MONTANT+1);
                ostrstream out_de(debit, LONG_MAX_MONTANT+1);

                  out_cr << setw(LONG_MAX_MONTANT) << cr << ends;
                  out_de << setw(LONG_MAX_MONTANT) << de << ends;
                  fprintf (sortie, "\rCrédit : %s\rDébit : %s\r", credit, debit);
                }
                eff_esp_trav ();
                getch ();
                break;
            }
            break;
        }
        break;
      case 'N' : // pas de tri
        gotoxy (1,17);
        printf ("Entrez les bornes de l'intervalle");
        do {
          gotoxy (1,18);
          printf ("Borne Inférieure ");
          Saisie_clavier (NON_VIDE, 6, tampon2, "1");
          if (tampon2[0] == ESCAPE) {
            eff_esp_trav ();
            return;
          }
          inf = atoi (tampon2);
        }
        while ((inf < 1) || (inf >= contexte->suivant));
        do {
        char defaut[5];
          gotoxy (1,19);
          printf ("Borne Supérieure ");
          sprintf (defaut, "%i", contexte->suivant-1);
          Saisie_clavier (NON_VIDE, 6, tampon2, defaut);
          if (tampon2[0] == ESCAPE) {
            eff_esp_trav ();
            return;
          }
          sup = atoi (tampon2);
        }
        while (((sup < 1) || (sup >= contexte->suivant)) && (sup < inf));
        eff_esp_trav ();
        gotoxy (1,16);
        printf ("Edition sur l'%s...",pm[c]);
        if (c == 0) {
          fprintf (sortie, "\rLISTING DES LIGNES %i A %i LE :%0.2i/%0.2i/%0.2i\r"
                    "\r\rNo   DATE    TYPE       OBJET                 CREDIT   DEBIT     CODE      MARQ.\r"
                       "---- ----- -------- ------------------------- -------- --------- --------- -----\r"
                    , inf, sup, contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year() );
        }
        else {
          eff_esp_fich ();
        }
        nligne = 2;
        for (i= inf ; i <= sup; i++) {
          if (kbhit () && (getch () == ESCAPE)) {
            return;
          }
          if ( (nligne == 14) && (c == 1) ) {
            nligne = 2;
            while (!kbhit ());
            eff_esp_fich ();
          }
          Lecture_ligne  (contexte->nom_fichier, i, fiche);
          if (c==1) {
            gotoxy (1,nligne);
          }
          formatte_fiche (fiche, ligne, i);
          fprintf (sortie, "%s\r", ligne);
          nligne++;
        }
        if (c==0) {
        char banque[LONG_MAX_MONTANT+1], reel[LONG_MAX_MONTANT+1];
        ostrstream out_ba(banque, LONG_MAX_MONTANT+1);
        ostrstream out_re(reel, LONG_MAX_MONTANT+1);

          out_ba << setw(LONG_MAX_MONTANT) << contexte->solde_banque << ends;
          out_re << setw(LONG_MAX_MONTANT) << contexte->solde_reel << ends;
          printf ("\rSolde Banque : %s\rSolde Réel : %s\r\r\r",
                banque, reel);
        }
        else {
          getch ();
          eff_esp_fich ();
          eff_esp_trav ();
        }
        break;
      case ESCAPE :
        eff_esp_trav ();
        return;
      default :
        encore = 0;
        break;
    }
  }
  while (encore == 0);
  eff_esp_fich ();
}


void change_fichier (pctxt contexte)
{
char nom[LONG_NOM_FICHIER], Ext[5];
int encore, i;
FILE *fichier, *fichier2;

  eff_esp_trav ();
  do {
    gotoxy (1,17);
    printf ("Nom du fichier ");
    Saisie_clavier (NON_VIDE, LONG_NOM_FICHIER - 1 , nom, contexte->nom_fichier);
    if (nom[0] == ESCAPE) {
      eff_esp_trav ();
      return;
    }
    // Recherche l'extension
    Ext[0] = 0;
    for (i=strlen (nom); i>0; i--) {
      if (nom[i] == '.') {
        strncpy (Ext, nom+i, 4);
        Ext[4] = 0;
        break;
      }
      else if (nom[i] == '\\') {
        break;
      }
    }
    if (Ext[0] == 0) {
      // Ajoute l'extension
      strcat (nom, EXT_COMPTE);
    }
    if (strchr(nom,'\\') == NULL) {
      // pas de path, on rajoute en defaut
      Ajoute_Path (nom);
    }
    if ((fichier2 = fopen ( (char *) nom, "r")) == NULL ) {
    char Reponse[1];

      encore = 1;
      gotoxy (1,19);
      printf ("Fichier non existant");
      gotoxy (1,20);
      printf ("Voulez-vous le creer ? ");
      Saisie_clavier (NON_VIDE, 1, Reponse, "N");
      if ((Reponse[0] == 'o') || (Reponse[0] == 'O')) {
        encore = creation_fichier (nom);
      }
    }
    else {
      fclose (fichier2);
      encore = 0;
    }
  }
  while (encore);
  fichier = fopen (NOM_FICHIER , "wt");
  if (fichier != NULL) {
    fprintf (fichier, "%s\n", nom);
    fclose (fichier);
  }
  strcpy (contexte->nom_fichier, nom);
  eff_esp_trav ();
  eff_esp_fich ();
}

int Mise_A_Jour_Auto (pctxt contexte)
{
FILE* fich_auto;
int cr=0;
Op_Auto op_auto;

  eff_esp_trav ();
  fich_auto = fopen (contexte->Nom_auto, "rb");
  if (fich_auto != NULL) {
    // mise a jour
    gotoxy (1,17);
    printf ("Mise a jour des operations automatiques");

    contexte->nb_op_auto = 0;
    while ( fread (&op_auto, sizeof (op_auto), 1, fich_auto) == 1) {
      op_auto.Mise_A_Jour (contexte);
      contexte->nb_op_auto++;
      cr = 1;
    }

    // Aujourd'hui on a mis a jour
    contexte->date_utilisation = TDate();
    fclose (fich_auto);
  }
  return cr;
}

void part2 (char *fich_code)
{
Fiche fiche;
FILE *fichier;
TDate date_sys;
int i, cr;
char m[8][13] = {"DEBIT       ", "CREDIT      ", "MARQUAGE    ",
            "MODIFICATION", "RECHERCHE   ", "LISTING     ",
            "FICHIER     ", "FIN         "};
static int c=0;
int encore;
int quitte=0;
ctxt Contexte;
pctxt contexte=&Contexte;
int ch;
int Ligne_Aff;
char chaine[LONG_CODE];
int mise_a_jour=1;

  clrscr ();
  strcpy (contexte->nom_fich_code, fich_code);
  do {
    Lecture_code (fich_code, 0, chaine);
    if (chaine[0] == 0) {
      // fichier vide création automatique
      edite_code (contexte);
    }
  }
  while ((contexte->nb_code = atoi (chaine)) == 0);
  // Lecture du compte a ouvrir
  fichier = fopen(NOM_FICHIER, "rt");
  if (fichier == NULL) {
    nouveau_fichier();
    fichier = fopen(NOM_FICHIER, "rt");
    if (fichier == NULL) {
      cerr << "Ouverture impossible du fichier " << NOM_FICHIER << "\n";
      return;
    }
  }
  fscanf (fichier, "%s", contexte->nom_fichier);
  fclose (fichier);
  do {
    // boucle jusqu'a lecture reussie
    cr = Lecture_ligne (contexte->nom_fichier, 0, *contexte);
    if (cr != 0) {
      // erreur de lecture
      change_fichier (contexte);
    }
  } while (cr != 0);
  do {
    Lecture_ligne (contexte->nom_fichier, 0, *contexte);
    if (mise_a_jour) {
      int i;
      char Ext[5];

      strcpy (contexte->Nom_auto, contexte->nom_fichier);
      // Recherche l'extension
      Ext[0] = 0;
      for (i=strlen (contexte->Nom_auto); i>0; i--) {
        if (contexte->Nom_auto[i] == '.') {
          strncpy (Ext, contexte->Nom_auto+i, 4);
          Ext[4] = 0;
          break;
        }
        else if (contexte->Nom_auto[i] == '\\') {
          break;
        }
      }
      if (Ext[0] == 0) {
        // Ajoute l'extension
        strcat (contexte->Nom_auto, EXT_AUTO);
      }
      else {
        // remplace l'extension
        strcpy (contexte->Nom_auto+i, EXT_AUTO);
      }
      if (Mise_A_Jour_Auto (contexte)) {
        // Des modifs ont ete faite
        // enrgistrement du contexte
        Ecriture_ligne (contexte->nom_fichier, 0, *contexte);
      }
      mise_a_jour = 0;
    }
    gotoxy (1,25);
    printf ("COMPTE No %.*s", LONG_NUM_COMPTE, contexte->numero_compte);
    gotoxy (55,25);
    printf ("FICHIER %*.*s", LONG_AFF_NOM_FICH, LONG_AFF_NOM_FICH, contexte->nom_fichier);
    gotoxy (1,15);
    printf ("GESBANQ "VERSION);
    gotoxy (1,1);
    textattr (INVERSE);
    cprintf (" No  DATE    TYPE       OBJET                 CREDIT    DEBIT    CODE      MARQ.");
    textattr (NORMAL);
    gotoxy (27,25);
    printf ("DATE SYSTEME : %0.2i/%0.2i/%0.4i", contexte->date_sys.DayOfMonth(), contexte->date_sys.Month(), contexte->date_sys.Year());
    Ligne_Aff = contexte->suivant;
    Affiche_compte (contexte->nom_fichier, max(1,(Ligne_Aff-12)), Ligne_Aff-1, max(2,(15-Ligne_Aff)));
    gotoxy (24,15);
    textattr (INVERSE);
    cout << "SOLDE BANQUE: " << setw(LONG_SOLDE) << contexte->solde_banque;
    gotoxy (55,15);
    cout << "SOLDE REEL: " << setw(LONG_SOLDE) << contexte->solde_reel;
    textattr (NORMAL);
    contexte->again = 0;
    do {
      encore = 0;
      c += 8;
      c %= 8;
      for (i=0; i<8; i++) {
        gotoxy (1 + 20 * (i - 4*( (int) i/4)), 22 + i/4);
        if (i == c) {
          textattr (INVERSE);
        }
        cprintf ("%s",m[i]);
        textattr (NORMAL);
      }
      ch = getch ();
      if (ch == 0) {
        ch = 1000 + getch ();
      }
      switch (ch) {
        case FL_DR :
          c++;
          break;
        case CTRL_R :
          contexte->again = 2;
          encore = 1;
          break;
        case CTRL_A :
          contexte->again = 1;
          encore = 1;
          break;
        case FL_GA :
          c--;
          break;
        case ESPACE :
        case FL_HA :
        case FL_BA :
          c += 4;
          break;
        case RETURN :
          encore = 1;
          break;
        case SCR_HA :
          eff_esp_fich ();
          if (contexte->suivant != 1) {
            // Le fichier n'est pas vide
            Ligne_Aff -= 12;
            if (Ligne_Aff <= 2) {
              Ligne_Aff = 2;
            }
          }
          Affiche_compte (contexte->nom_fichier, max(1,(Ligne_Aff-12)),
                    Ligne_Aff-1, max(2,(15-Ligne_Aff)));
          break;

        case HOME :
          eff_esp_fich ();
          if (contexte->suivant != 1) {
            // Le fichier n'est pas vide
            Ligne_Aff = 2;
          }
          Affiche_compte (contexte->nom_fichier, max(1,(Ligne_Aff-12)),
                    Ligne_Aff-1, max(2,(15-Ligne_Aff)));
          break;

        case FIN :
          eff_esp_fich ();
          Ligne_Aff = contexte->suivant;
          Affiche_compte (contexte->nom_fichier, max(1,(Ligne_Aff-12)),
                    Ligne_Aff-1, max(2,(15-Ligne_Aff)));
          break;

        case SCR_BA :
          eff_esp_fich ();
          Ligne_Aff += 12;
          if (Ligne_Aff > contexte->suivant) {
            Ligne_Aff = contexte->suivant;
          }
          Affiche_compte (contexte->nom_fichier, max(1,(Ligne_Aff-12)),
                    Ligne_Aff-1, max(2,(15-Ligne_Aff)));
          break;
      }
    }
    while (encore == 0);
    switch (c) {
      case 0 :
        debit (contexte);
        Ecriture_ligne (contexte->nom_fichier, 0, *contexte);
        break;
      case 1 :
        credit (contexte);
        Ecriture_ligne (contexte->nom_fichier, 0, *contexte);
        break;
      case 2 :
        marquage (contexte);
        Ecriture_ligne (contexte->nom_fichier, 0, *contexte);
        break;
      case 3 :
        modification (contexte);
        Ecriture_ligne (contexte->nom_fichier, 0, *contexte);
        break;
      case 4 :
        recherche (contexte);
        break;
      case 5 :
        listing (contexte);
        break;
      case 6 :
        change_fichier (contexte);
        mise_a_jour=1;
        break;
      case 7 :
        quitte = quitter (contexte);
        break;
    }
  }
  while (quitte == 0);
  clrscr ();
}


void main ()
{
  TDate().SetPrintOption(TDate::EuropeanNumbers);
  textmode (C80);
  textattr (NORMAL);
  clrscr ();
  _setcursortype (_NOCURSOR);
//  part1 ();
  part2 ("code.gec");
  _setcursortype (_NORMALCURSOR);
}