Conversion des codes


// é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 (num != 0)

    fseek (fichier, ((long) num - 1) * LONG_CODE + 3, 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;

}







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);

  if (tampon[0] == ESCAPE) {

    eff_esp_trav ();

    return NULL;

  }

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

  strupr (code);

  contexte->nb_code ++;

  eff_esp_fich ();

  return code;

}



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);

  Saisie_clavier (NON_VIDE, 9, tampon);

  if (tampon[0] == ESCAPE) {

    eff_esp_trav ();

    return;

  }

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

  strupr (tab_code[i]);

  eff_esp_trav ();

  return;

}



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 = 1; i <= contexte->nb_code; i++) {

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

    if (i%2 == 0)

      fprintf (stdprn, "\n");

  }

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

  eff_esp_trav ();

  gotoxy (1,15);

  printf ("Impression terminée");

  return;

}





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

{

FILE* flux;

int i;



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

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

    fputs (tab_code[i], flux);

  fclose (flux);

}



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 ();

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

  //  Fichier code inexistant

    n = 0;

    gotoxy (10,24);

    textattr (INVERSE);

    cprintf ("Edition des Codes Compta.");

    textattr (NORMAL);

    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, 9, code[n]);

      if (code[n][0] == ESCAPE)

        if (n > 0) {

          eff_esp_trav ();

          contexte->nb_code = n+1;

          break;

        }

      else {

        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=1; n<contexte->nb_code; n++) {

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

    }

  }

  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 <= contexte->nb_code); k++) {

      gotoxy (pos, k+1);

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

    }

  do {

    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 :

          eff_esp_trav ();

          eff_esp_fich ();

          return;

        case RETURN :

          break;

        default :

          break;

      }

    }

    while (ky!=RETURN);

    switch (c) {

      case 0 :

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

        break;

      case 1 :

        Modification_code (contexte, code);

        break;

      case 2 :

        Listing (contexte, code);

        break;

      case 3 :

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

        sprintf (code[0], "%i", contexte->nb_code);

        Enregistre_codes (contexte, code);

        eff_esp_trav ();

        eff_esp_fich ();

        return;

    }

  }

  while (1);

}





main ()

{ctxt ctx = {0,0,0,"bid","code.gec",0,0,0,0,0,0,0};

 edite_code (&ctx);

 return 0;

}