/***************************************************************************/
/* JITES                                                                   */
/* prog AL-KHANSA JAMAL                                                    */
/* r‚vis‚ … partir du 10-9-1992 comme maquette bxls                        */
/* LE 6-2-1991                                                             */
/* LANGAGE TC                                                              */
/*                                                                         */
/*                                VARIABLES                                */
/* Hcadre_candid = hauteur du cadre candidat                               */
/* Interl_candid = Interligne entre les cadres candidats                   */
/* Hsaisie_candid= hauteur de la zone de saisie dans cadre candidat        */
/* Lsaisie_candid = longueur de la zone de saisie candidat                 */
/* Lcadre_candid  = longueur du cadre candidat                             */
/* Interc_candid  = Intercolonne entre les cadres candidats                */
/* x_dep          = valeur de la coordonnee x de la  1 ere colonne         */
/* x_ent_scr      = valeur de x de l'entete du scrutin                     */
/* x_numl         = valeur de x du numero de la liste                      */
/* x_noml         = valeur de x du nom de la liste                         */
/* nbc_col        = nombre de candidats par colonne                        */
/* h    = hauteur  partir de l'entete                                     */
/* lon  = longueur d'une case liste                                        */
/* espl = espacement entre les cases listes (sur la longueur)              */
/* esph = espacement entre les cases listes (sur la hauteur)               */
/* recl = hauteur d'une case liste                                         */
/* nbc  = nombre de candidats / liste                                      */
/* recc = hauteur d'une case candidat                                      */
/* memos[3] = tableau des scrutins                        -                */
/* memol[3][40] = tableau des listes / scrutin            - MEMORISATION   */
/* memoc[3][40][100] = tableau des candidats /liste/scrutin -               */
/***************************************************************************/

#include <fcntl.h>
#include <alloc.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <graphics.h>
#include <string.h>

#include "variable.h"
#include "jamal.c"
#include "pcx.c"

union REGS rin,rout;

#define  NMAX_MSG  99
#define  LMAX_MSG  70
char Msg[1+NMAX_MSG][LMAX_MSG+1];
char gMsg[(LMAX_MSG *2) +1];

char bufc[100],caisse[30],caisse1[30],str_nbre_max[4];
int nbrl,nbr_lignes,Cl;
FILE *fpin, *fpprn;
FILE *ptc, *fpc;

char  fnin[255+1] = "listes";
char fnprn[255+1] = "prn";
FILE *fpin = NULL;
FILE *fpprn = NULL;
char bufnom[16]/*,buf[30]*/ ;
char nom_s = ' ';
char ins_s[5+1] = "     ";
char LANGUE = 'F', Choix_College = 'F';
int colonne;
char RLg = ' ';
char Choix_RLg = ' ';
int nListeF = 0, nListeN = 0;

typedef struct {
  char  college;
  char  num[2+1];
  char  nom[25+1];
} LISTINFO;
LISTINFO  listes[99];
int       iliste;

/****************************************************************************/
/**********************************************/
/**/ int getRLgType(int *p_nlF, int *p_nlN) /**/
/**********************************************/
{
  int  result;

  result = 2;

  if (p_nlF != (int *)NULL)  (*p_nlF) = nListeF;
  if (p_nlN != (int *)NULL)  (*p_nlN) = nListeN;

  return(result);
}
/*****************************************************************************/

/**************************** CORPS DU PROGRAMME ***************************/

void main(int argc, char *argv[])
{
  int ind_arg = 1;
  int iMsg;
  int val;
  int iNumX;
  char cBuf[16];

  for (iMsg = 0; (iMsg <= NMAX_MSG); iMsg++) {
    memset(Msg[iMsg], 0, (size_t)(LMAX_MSG +1));
  }

  while (ind_arg < argc) {
    if (ind_arg == 1) {
      strcpy(fnin, argv[ind_arg]);
      ind_arg++; continue;
    }
    ind_arg++; /* other parameter(s) skipped */
  }

  fpin = fopen(fnin,"r");
  if (fpin == (FILE *)NULL) {
    fprintf(stderr, "Open file (%s) ERROR.  press a key ", fnin);
    getch();
    return;
  }

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  nom_s = bufc[0];  /* CODE SCRUTIN */
  strncpy(ins_s,  bufc + 1, 5); ins_s[5]  = '\0';  /* CODE INS */
  LANGUE = bufc[6];  /* LANGUE (SIMULATION DU CHOIX DE L'UTILISATEUR) */
  Choix_College = bufc[7];

  sprintf(bufnom, "%c%s.%c%cl", nom_s, ins_s, LANGUE, Choix_College);

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(caisse,bufc + 25 ,2);
  caisse[2] = '\0';
  nbrl = atoi(caisse); /* NOMBRE TOTAL LISTES SANS VOTE BLANC */
  strncpy(caisse,bufc + 28 ,2);
  caisse[2] = '\0';
  nListeF = atoi(caisse); /* NOMBRE DE LISTES F SANS VOTE BLANC */
  strncpy(caisse,bufc + 31 ,2);
  caisse[2] = '\0';
  nListeN = atoi(caisse); /* NOMBRE DE LISTES N SANS VOTE BLANC */
  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[1],bufc,60);
  for(i = 60;((Msg[1][i] == ' ') || (Msg[1][i] == '\0')) && (i > 0);i--)
    Msg[1][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[2],bufc,70);
  for(i = 69;((Msg[2][i] == ' ') || (Msg[2][i] == '\0')) && (i > 0);i--)
    Msg[2][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[3],bufc,70);
  for(i = 69;((Msg[3][i] == ' ') || (Msg[3][i] == '\0')) && (i > 0);i--)
    Msg[3][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[4],bufc,70);
  for(i = 69;((Msg[4][i] == ' ') || (Msg[4][i] == '\0')) && (i > 0);i--)
    Msg[4][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[5],bufc,70);
  for(i = 69;((Msg[5][i] == ' ') || (Msg[5][i] == '\0')) && (i > 0);i--)
    Msg[5][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[6],bufc,70);
  for(i = 69;((Msg[6][i] == ' ') || (Msg[6][i] == '\0')) && (i > 0);i--)
    Msg[6][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[7],bufc,70);
  for(i = 69;((Msg[7][i] == ' ') || (Msg[7][i] == '\0')) && (i > 0);i--)
    Msg[7][i] = '\0';

  bufc[0] = '\0'; fgets(bufc,81,fpin);
  strncpy(Msg[8],bufc,70);
  for(i = 69;((Msg[8][i] == ' ') || (Msg[8][i] == '\0')) && (i > 0);i--)
    Msg[8][i] = '\0';

  iliste = 0;
  while(iliste < nbrl) {
    bufc[0] = '\0'; fgets(bufc,81,fpin);
    if (strlen(bufc) < 23) {
      fprintf(stderr, "Read file (%s) ERROR.  press a key ", fnin);
      getch();
      return;
    }
    listes[iliste].college = bufc[0];

    strncpy(listes[iliste].num, bufc + 1, (size_t)2);
    listes[iliste].num[2] = '\0';
    if (listes[iliste].num[0] == '0') listes[iliste].num[0] = ' ';

    strncpy(listes[iliste].nom, bufc + 3, (size_t)25);
    listes[iliste].nom[25] = '\0';
    for(i = 24; ((listes[iliste].nom[i] == ' ') ||
		 (listes[iliste].nom[i] == '\0')) && (i > 0);	i--)
		 listes[iliste].nom[i] = '\0';

    iliste++;
  }
  /* VOTE BLANC (DERNIERE LISTE) */
  bufc[0] = '\0'; fgets(bufc,81,fpin);
  if ((strlen(bufc) < 2) || (bufc[0] != ' ')) {
    fprintf(stderr, "Read file (%s) ERROR.  press a key ", fnin);
    getch();
    return;
  }
  else {
    listes[iliste].college = LANGUE;
    strcpy(listes[iliste].num, "--");
    strncpy(listes[iliste].nom, bufc + 1, (size_t)25);
    listes[iliste].nom[25] = '\0';
    for(i = 24; ((listes[iliste].nom[i] == ' ') ||
		 (listes[iliste].nom[i] == '\0')) && (i > 0);
	i--) listes[iliste].nom[i] = '\0';
  }
  nbrl++; /* Liste Vote BLANC */
  if (fpin != (FILE *)NULL) { fclose(fpin); fpin = (FILE *)NULL; }

  /* AFFICHAGE ***************************************************************/
  registerbgidriver (EGAVGA_driver);    /* Option pour linker avec la  */
  registerbgifont (small_font);         /* librairies GRAPHICS         */
  registerbgifont (sansserif_font);
  Initialize();
  Initpcx();
  setfillstyle(SOLID_FILL,BLACK);
  bar( 0,0,639,479);
  setcolor(WHITE);

  settextstyle( SMALL_FONT, HORIZ_DIR, 6);
  settextjustify( CENTER_TEXT, CENTER_TEXT );
  outtextxy(320, 10, Msg[1]);
  outtextxy(320, 11, Msg[1]);

  outtextxy(320, 35, Msg[2]);
  settextjustify( LEFT_TEXT, CENTER_TEXT );
  setfillstyle(SOLID_FILL,WHITE);
  setcolor(BLACK);

  iliste = 0;
  param();
  iliste = 0;

  settextstyle( SMALL_FONT, HORIZ_DIR, 4);


  for(Cl=0,k=0,x=4;(k < nbrl) && (Cl < colonne);x+=(espl) + lon,Cl++)
  {
    for(j = 0,y = h;(j < nbr_lignes) && (k < nbrl);k++)
    {
      if ((listes[iliste].college == Choix_College) || (k == (nbrl - 1)))
      {
        /* */
        if(Cl == 1) /* If the second column */
        {
          if(j == nbr_lignes-1) /* If the "last but one" */
          {
          if(nbrl != (nbr_lignes*3) &&
             nbrl != (nbr_lignes*3)-1)
            {
              break;
            }
          }
        }
        /* */

        bar(x,y,x + lon,y + recl);

        if(k < (nbrl - 1))
        {
          settextjustify( CENTER_TEXT, CENTER_TEXT );
          iNumX = x+11;
          outtextxy( iNumX, y + recl/2, listes[iliste].num);
          outtextxy( iNumX+1, y + recl/2, listes[iliste].num);
          settextjustify( LEFT_TEXT, CENTER_TEXT );
          outtextxy( x + 28, y + recl/2, listes[iliste].nom);
          outtextxy( x + 29, y + recl/2, listes[iliste].nom);
        }
        else
        {
/* << 9.01c */
          settextstyle(SMALL_FONT, HORIZ_DIR, 5);
/* 9.01c >> */
          outtextxy(x + 28, y + recl/2, listes[iliste].nom);
          outtextxy(x + 29, y + recl/2, listes[iliste].nom);
        }
        j++;
        y+=esph + recl;
        iliste++;
      }
    }
  }

 val = true;

  while(val)
  {
    setfillstyle(SOLID_FILL,BLACK);
    bar( 0,438,639,479);      /* Draw backgnd */
	setcolor( WHITE);
	settextjustify(CENTER_TEXT,CENTER_TEXT);
	settextstyle( SMALL_FONT, HORIZ_DIR, 6);
	outtextxy( 320,470, Msg[3]);
	c = getch();
	/*if (c == 'P' ||  c == 'p' || c =='I' || c == 'i')
	  {
		rin.h.ah = 2;
		rin.x.dx = 0;
		int86(0x17,&rin,&rout);
		if(rout.h.ah != 0x90)
		  {
			 setfillstyle(SOLID_FILL,BLACK); / Set to solid fill in color   /
			 bar( 0,438,639,479);      / Draw backgnd /
			 setcolor( WHITE);
			 settextjustify(CENTER_TEXT,CENTER_TEXT);
			 settextstyle(SMALL_FONT, HORIZ_DIR, 6);
			 outtextxy( 320,470, "Imprimante occup‚e, pressez une touche.");
			 c = getch();
		  }
		else
		 {
			int86(0x05,&rin,&rout);
			ptc = fopen("PRN","w");
			bufc[0]  = 0x0C;
			bufc[1] = 0x00;
			fputs(bufc,ptc);
			fclose(ptc);
			val = false;
		 }
	  }
	else*/
	   if (c == 'F' ||  c == 'f')
	     {
                 pcxDisplayFile(bufnom,0,0,639,479,0);
	      }
	   else
		  val = false;
  }

  closegraph();
}
/***************************  POINTAGE CRAYON *****************************/
/***************************************************************************/
param()
{
  int /*nbr_liste_ligne = 0,*/ nbre_Liste_Int = 0;
#include "lparam1.c"

}
/*****************************************************************************/
