Edition des codes comptas

// édition des codes comptas



#include "saisie.h"

#include "type_def.h"

#include "fct_base.h"

#include "code.h"



void Lecture_code (char *nom_fich, int num, char *code)

// 12100

//  i -> num

// cc$ -> code

{

FILE *fichier;



  code [0] = 0;

  fichier = fopen (nom_fich, "rb");

  if (fichier != NULL) {

    fseek (fichier, ((long) num) * (LONG_CODE+1), SEEK_SET);

    fgets (code, LONG_CODE, fichier);

    fclose (fichier);

  }

}



int Code_bon (char *nom_fich, int max, char *code)

// 12000

// T->valeur retournée

// T$-> code

{

int inf,sup;

int milieu;

char code_lu[LONG_CODE];

int a;



  inf = 1;

  sup = max;

  strupr (code);

  do {

    if (inf > sup) {

      return -1;

    }

    milieu = (inf + sup) / 2;

    Lecture_code (nom_fich, milieu, code_lu);

    a = strcmp (code_lu, code);

    if ( a > 0 ) {

      sup = milieu -1;

    }

    else {

      inf = milieu +1;

    }

  }

  while ( a != 0);

  return 0;

}







static char * Ajout_code (pctxt contexte, char *code)

{

char tampon [LONG_CODE];



  eff_esp_trav ();

  if (contexte->nb_code == 500) {

    gotoxy (1,16);

    printf ("Plus de place disponible");

    return NULL;

  }

  gotoxy (1,16);

  printf ("Entrez le nouveau code compta");

  Saisie_clavier (NON_VIDE, 9, tampon);

  eff_esp_trav ();

  if (tampon[0] == ESCAPE) {

    return NULL;

  }

  sprintf (code, "%-9.9s", tampon);

  strupr (code);

  contexte->nb_code ++;

  eff_esp_fich ();

  return code;

}



static void Modification_code (pctxt contexte, char tab_code[][LONG_CODE])

{

char tampon[LONG_CODE];

char tampon2[LONG_CODE];

int i;



  eff_esp_trav ();

  gotoxy (1,16);

  printf ("Code compta à modifier ");

  Saisie_clavier (NON_VIDE, 9, tampon);

  if (tampon[0] == ESCAPE) {

    eff_esp_trav ();

    return;

  }

  sprintf (tampon2, "%-9.9s", tampon);

  strupr (tampon2);

  i=-1;

  while ( (++i < contexte->nb_code) && (strcmp (tampon2, tab_code[i])));

  if (i >= contexte->nb_code) {

    eff_esp_trav ();

    gotoxy (1,15);

    printf ("Code compta inexistant");

    return;

  }

  gotoxy (1,18);

  printf ("Nouveau code ");

  Saisie_clavier (NON_VIDE, 9, tampon, tab_code[i]);

  if (tampon[0] == ESCAPE) {

    eff_esp_trav ();

    return;

  }

  sprintf (tab_code[i], "%-9.9s", tampon);

  strupr (tab_code[i]);

  eff_esp_trav ();

  return;

}



static void Listing (pctxt contexte, char tab_code[][LONG_CODE])

{

int i;



  eff_esp_trav ();

  gotoxy (1,15);

  printf ("Impression en cours");

  fprintf (stdprn, "\n          ***   LISTING DES CODES COMPTA   **\n\n");

  for (i = 0; i < contexte->nb_code; i++) {

    fprintf (stdprn, "Code N°%3i : %s          ",i+1,tab_code[i]);

    if (i%2 == 1) {

      fprintf (stdprn, "\n");

    }

  }

  fprintf (stdprn, "\n\n\n\n");

  eff_esp_trav ();

  gotoxy (1,15);

  printf ("Impression terminée");

  return;

}





static void Enregistre_codes (pctxt contexte, char tab_code[][LONG_CODE])

{

FILE* flux;

int i;

char longueur[LONG_CODE+1];



  flux = fopen (contexte->nom_fich_code, "w");

  if (flux != NULL) {

    sprintf (longueur, "%-9.9i\n", contexte->nb_code);

    fputs (longueur, flux);

    for (i=0; i< contexte->nb_code; i++) {

      fputs (tab_code[i], flux);

      fputc ('\n', flux);

    }

    fclose (flux);

  }

}



static int fct_comparaison (const void *chaine1, const void *chaine2)

{

  return (strcmp ((char *) chaine1, (char *) chaine2));

}



void edite_code (pctxt contexte)

{

FILE *flx_code;

int n;

char tampon[LONG_CODE], code[500][LONG_CODE];

int mini;

int i,k;

char pm[4][13] = {"AJOUT       ", "MODIFICATION",

            "LISTING     ", "RETOUR MENU "};

int c=0;

int ky;

int pos;





  clrscr ();

  gotoxy (10,25);

  textattr (INVERSE);

  cprintf ("Edition des Codes Compta.");

  textattr (NORMAL);

  if ((flx_code = fopen (contexte->nom_fich_code, "rt")) == NULL) {

  //  Fichier code inexistant

    n = 0;

    gotoxy (1,19);

    printf ("Tapez ECHAPEMENT à la fin");

    do {

      gotoxy (1,16);

      clreol ();

      printf ("Code %i (9 Caracts Max)",n+1);

      Saisie_clavier (NON_VIDE, LONG_CODE-1, tampon);

      if (tampon[0] == ESCAPE) {

        if (n > 0) {

          eff_esp_trav ();

          contexte->nb_code = n;

          break;

        }

      }

      else {

        sprintf (code[n],"%-9.9s",tampon);

        strupr (code[n]);

        n++;

        if (n >= 500) {

          printf ("Fin de l'entrée des codes compta. Impossible d'en entrer plus");

          contexte->nb_code = 500;

          break;

        }

      }

    }

    while (1);

  }

  else {

    fclose (flx_code);

    Lecture_code (contexte->nom_fich_code, 0, tampon);

    contexte->nb_code = strtod (tampon, NULL);

    for (n=0; n<contexte->nb_code; n++) {

      Lecture_code (contexte->nom_fich_code, n+1, code[n]);

    }

  }

  do {

    mini = max (1, contexte->nb_code - 87);

    for (i = mini, pos = 1; i <= contexte->nb_code; i++, pos += LONG_CODE) {

      for (k = 0; (k <= 10) && (k+(i-1)*11 < contexte->nb_code); k++) {

        gotoxy (pos, k+1);

        printf ("%9.9s", code[k+(i-1)*11]);

      }

    }

    do {

      c += 4;

      c %= 4;

      for (i=0; i<4; i++) {

        gotoxy (1 + 20*(i%2), 16 + i/2);

        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 :

          c += 2;

          break;

        case ESCAPE :

          clrscr ();

          return;

        case RETURN :

          break;

        default :

          break;

      }

    }

    while (ky!=RETURN);

    switch (c) {

      case 0 :

        Ajout_code (contexte, code[contexte->nb_code]);

        break;

      case 1 :

        Modification_code (contexte, code);

        break;

      case 2 :

        Listing (contexte, code);

        break;

      case 3 :

        Enregistre_codes (contexte, code);

        clrscr();

        return;

    }

    qsort (code, contexte->nb_code, LONG_CODE, fct_comparaison);



  }

  while (1);

}