Fonctions de bases

// Fonctions de bases

#include <strstrea.h>
#include <iomanip.h>
#include "fct_base.h"

static const ctxt Contexte_const;
static const int Offset_Ctxt=((int) &(Contexte_const.nb_code)
                    - (int) &(Contexte_const));



void Ajoute_Path (char *nom)
// rajoute le directory des comptes au nom
{
char nombis[LONG_NOM_FICHIER];

  strcpy(nombis,nom);
  strcpy(nom,DIR_COMPTE);
  strcat(nom,nombis);
}

int Lecture_ligne (char* nom, int numero, Fiche &fiche)
// 10300 -> 10310
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "rb");
  if (fichier != NULL) {
    if (numero == 0) {
      printf ("Erreur programmation");
    }
    else {
      fseek (fichier, ((long) numero - 1) * sizeof(Fiche) +  Offset_Ctxt,
            SEEK_SET);
      fread (&fiche, sizeof(Fiche), 1, fichier);
    }
    fclose (fichier);
  }
  else {
    return -1;
  }
  return 0;
}

int Lecture_ligne (char* nom, int numero, ctxt &contexte)
// 10300 -> 10310
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "rb");
  if (fichier != NULL) {
    if (numero == 0) {
      fseek (fichier, 0, SEEK_SET);
      fread (&contexte,  Offset_Ctxt, 1, fichier);
    }
    else {
      printf ("Erreur programmation");
    }
    fclose (fichier);
  }
  else {
    return -1;
  }
  return 0;
}

int Ecriture_ligne (char *nom, int numero, Fiche &fiche)
// 10250->10280
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "r+b");
  if (fichier != NULL) {
    if (numero != 0) {
      fseek (fichier, ((long) numero - 1) * sizeof (Fiche) + Offset_Ctxt,
          SEEK_SET);
      fwrite (&fiche, sizeof (Fiche), 1, fichier );
    }
    else {
      printf ("Erreur programmation\n");
    }
    fclose (fichier);
  }
  else {
    printf ("Erreur ouverture fichier\n");
    return -1;
  }
  return 0;
}

int Ecriture_ligne (char *nom, int numero, ctxt &contexte)
// 10250->10280
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "r+b");
  if (fichier != NULL) {
    if (numero != 0) {
      printf ("Erreur programmation\n");
    }
    else {
      fseek (fichier, 0, SEEK_SET);
      fwrite (&contexte, Offset_Ctxt, 1, fichier );
    }
    fclose (fichier);
  }
  else {
    printf ("Erreur ouverture fichier\n");
    return -1;
  }
  return 0;
}


int Saisie_date (pctxt contexte, TDate &date, int mode)
//10100->10180
// M -> mode 0 : saisie normal
//         1 : possibilité entrée vide en appuyant ALT lors de la validation
// ARG sor : date = non modifie si pas ok
// RETOUR : 0 OK
//        1 ESCAPE appuyÚ (RET_ESCAPE)
//        2 ALT-ENTREE (RET_VIDE)
{
int a,m,j;
int l;
TDate cejour;
char defaut[5];
char date_char[5];
int Encore;

  do {
    do {
      gotoxy (1,17);
      printf ("Jour (2 caract. max.) ");
      if (contexte == NULL) {
        j = cejour.DayOfMonth();
      }
      else {
        j = contexte->date_sys.DayOfMonth();
      }
      sprintf (defaut,"%0.2i",j);
      l = Saisie_clavier (NON_VIDE, 2, date_char, defaut, mode);
      if (l == -1) {
        return RET_VIDE;
      }
      if (date_char[0] == ESCAPE) {
        return RET_ESCAPE;
      }
      j = atoi (date_char);
    }
    while ((j < 1) || (j > 31));
    do {
      gotoxy (1,18);
      printf ("Mois (2 caract. max.) ");
      if (contexte == NULL) {
        m = cejour.Month();
      }
      else {
        m = contexte->date_sys.Month();
      }
      sprintf (defaut, "%0.2i", m);
      l = Saisie_clavier (NON_VIDE, 2, date_char, defaut);
      if ( date_char[0] == ESCAPE ) {
        return RET_ESCAPE;
      }
      m = atoi (date_char);
    }
    while ((m < 1) || (m > 12));
    do {
      gotoxy (1,19);
      printf ("Année (4 caract. max.) ");
      if (contexte == NULL) {
        a = cejour.Year();
      }
      else {
        a = contexte->date_sys.Year();
      }
      sprintf (defaut, "%0.4i", a);
      l = Saisie_clavier (NON_VIDE, 4, date_char, defaut);
      if ( date_char[0] == ESCAPE ) {
        return RET_ESCAPE;
      }
      a = atoi (date_char);
    }
    while ((a < 1900) || (a > 2100));

    TDate New_Date(j, m, a);
    if (New_Date.IsValid()) {
      contexte->date_sys = New_Date;
      date = New_Date;
      Encore = 0;
    }
    else {
      Encore = 1;
    }

  }
  while ( Encore );

  return 0;
}

void eff_esp_trav ()
// 10040
{
int i;

  for (i = 16; i <= 21; i++) {
    gotoxy (1, i);
    clreol ();
  }
}

void eff_lign_etat ()
// 10030
{
  gotoxy (1, 15);
  clreol ();
}


void eff_esp_fich ()
// 10020
{
int i;

  for (i = 2; i <= 14; i++) {
    gotoxy (1, i);
    clreol ();
  }
}


void eff_esp_menu ()
// 10010
{
int i;

  for (i = 22; i <= 24; i++) {
    gotoxy (1,i);
    clreol ();
  }
}

inline int date_inferieur (Fiche &fiche1, Fiche &fiche2)
// teste si la date de la ligne1 est inferieure stricte
// a celle de la ligne2
{
  return (fiche1.Date_Op < fiche2.Date_Op);
}



void tri_fichier (pctxt contexte, int Indice_Ajout)
// tri le fichier par la date d'opération
// Indice_Ajout donne l'endroit ou la ligne a ÚtÚ modifiÚe ou ajoutÚe
{
Fiche fiche_prim, fiche_sec;
int index_prim,index_sec;

  index_prim = Indice_Ajout;
  Lecture_ligne (contexte->nom_fichier, index_prim, fiche_prim);
  // Decale les ligne vers le bas si nÚcessaire
  index_sec = index_prim;
  while ((index_sec != 1)
      && (Lecture_ligne(contexte->nom_fichier, index_sec-1, fiche_sec),
          date_inferieur (fiche_prim, fiche_sec))) {
    Ecriture_ligne (contexte->nom_fichier, index_sec, fiche_sec);
    index_sec--;
  }
  if (index_sec==index_prim) {
    // Pas de changement effectuÚ
    // il faut voir vers le bas si c'est bon
    index_sec = index_prim;
    while ((index_sec != contexte->suivant-1)
      && (Lecture_ligne(contexte->nom_fichier, index_sec+1, fiche_sec),
          date_inferieur (fiche_sec, fiche_prim))) {
      Ecriture_ligne (contexte->nom_fichier, index_sec, fiche_sec);
      index_sec++;
    }
  }
  // Verification si des changement ont ÚtÚ effectuÚs
  if (index_sec!=index_prim) { // il faut rÚÚcrire la donnÚe
    Ecriture_ligne (contexte->nom_fichier, index_sec, fiche_prim);
  }
}

void formatte_fiche (Fiche &fiche, char *chaine, int Num)
{
char libelle[LONG_LIB_AFF+1];
  ostrstream out(chaine, LONG_VISU+1);

  out << setw(4) << Num << " "
    << setfill ('0') << setw(2) << fiche.Date_Op.DayOfMonth()
    << "/"
    << setfill ('0') << setw(2) << fiche.Date_Op.Month()
    << setfill (' ') << " "
    << mode2str (fiche.mode_paiement)
    << " ";
  out.setf(ios::left, ios::adjustfield);
  if (strlen (fiche.libelle) > LONG_LIB_AFF) {
    strncpy (libelle, fiche.libelle, LONG_LIB_AFF);
    libelle[LONG_LIB_AFF] = 0;
    out << setw(LONG_LIB_AFF) << libelle;
  }
  else {
    out << setw(LONG_LIB_AFF) << fiche.libelle;
  }
  out << " ";
  out.setf(ios::right, ios::adjustfield);
  if (fiche.montant > 0) {
    // c'est un crÚdit
    out << setw(LONG_CREDIT) << fiche.montant << " " << setw(LONG_DEBIT) << " ";
  }
  else {
    // DÚbit
    out << setw(LONG_CREDIT) << " " << " " << setw(LONG_DEBIT) << fiche.montant;
  }
  out << " " << fiche.code << " ";
  if (fiche.Date_Marquage.Year() == ANNEE_NON_MARQ) {
    out << setw(5) << " ";
  }
  else {
    out << setfill ('0') << setw(2) << fiche.Date_Marquage.DayOfMonth()
      << "/"
      << setfill ('0') << setw(2) << fiche.Date_Marquage.Month();
  }
  out << ends;
}

char* mode2str(Mode_Paiement &mode_paiement)
{
static char Chaine[LONG_TYPPAI];
static char Type_chaine[][LONG_TYPPAI] = { "CHEQUE  ", "CB.     ", "PREL.   ", "VIR.    ", "TIP     ", "AUTRE   " };

  if ((mode_paiement.type == TYPE_CHEQUE)
      && (mode_paiement.numero_chq != 0)) {
    // valeur
    ostrstream out(Chaine, sizeof(Chaine));

    out << setfill ('0') << setw (LONG_TYPPAI-1) << mode_paiement.numero_chq;
  }
  else {
    strcpy (Chaine, Type_chaine[mode_paiement.type - TYPE_CHEQUE]);
  }

  return Chaine;
}